Don't bother passing a freshly-zeroed mbstate to mbsrtowcs() etc.

when the current implementation won't use it, anyway. Just pass NULL.
This will need to be changed when state-dependent encodings are
supported, but there's no need to take the performance hit
in the meantime.
This commit is contained in:
Tim J. Robbins 2003-10-31 13:29:00 +00:00
parent 573e036e31
commit 1e8742e9cd
4 changed files with 25 additions and 75 deletions

View file

@ -50,8 +50,6 @@ size_t
wcsftime(wchar_t * __restrict wcs, size_t maxsize,
const wchar_t * __restrict format, const struct tm * __restrict timeptr)
{
static const mbstate_t initial;
mbstate_t state;
char *dst, *dstp, *sformat;
size_t n, sflen;
int sverrno;
@ -61,15 +59,17 @@ wcsftime(wchar_t * __restrict wcs, size_t maxsize,
/*
* Convert the supplied format string to a multibyte representation
* for strftime(), which only handles single-byte characters.
*
* We pass NULL as the state pointer to wcrtomb() because we don't
* support state-dependent encodings and don't want to waste time
* creating a zeroed mbstate_t that will not be used.
*/
state = initial;
sflen = wcsrtombs(NULL, &format, 0, &state);
sflen = wcsrtombs(NULL, &format, 0, NULL);
if (sflen == (size_t)-1)
goto error;
if ((sformat = malloc(sflen + 1)) == NULL)
goto error;
state = initial;
wcsrtombs(sformat, &format, sflen + 1, &state);
wcsrtombs(sformat, &format, sflen + 1, NULL);
/*
* Allocate memory for longest multibyte sequence that will fit
@ -86,9 +86,8 @@ wcsftime(wchar_t * __restrict wcs, size_t maxsize,
goto error;
if (strftime(dst, maxsize, sformat, timeptr) == 0)
goto error;
state = initial;
dstp = dst;
n = mbsrtowcs(wcs, (const char **)&dstp, maxsize, &state);
n = mbsrtowcs(wcs, (const char **)&dstp, maxsize, NULL);
if (n == (size_t)-2 || n == (size_t)-1 || dstp != NULL)
goto error;

View file

@ -43,12 +43,10 @@ __FBSDID("$FreeBSD$");
double
wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
{
static const mbstate_t initial;
mbstate_t state;
double val;
char *buf, *end, *p;
char *buf, *end;
const wchar_t *wcp;
size_t clen, len;
size_t len;
while (iswspace(*nptr))
nptr++;
@ -62,18 +60,20 @@ wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
* the input string contains a lot of text after the number
* duplicates a lot of strtod()'s functionality and slows down the
* most common cases.
*
* We pass NULL as the state pointer to wcrtomb() because we don't
* support state-dependent encodings and don't want to waste time
* creating a zeroed mbstate_t that will not be used.
*/
state = initial;
wcp = nptr;
if ((len = wcsrtombs(NULL, &wcp, 0, &state)) == (size_t)-1) {
if ((len = wcsrtombs(NULL, &wcp, 0, NULL)) == (size_t)-1) {
if (endptr != NULL)
*endptr = (wchar_t *)nptr;
return (0.0);
}
if ((buf = malloc(len + 1)) == NULL)
return (0.0);
state = initial;
wcsrtombs(buf, &wcp, len + 1, &state);
wcsrtombs(buf, &wcp, len + 1, NULL);
/* Let strtod() do most of the work for us. */
val = strtod(buf, &end);
@ -84,22 +84,9 @@ wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
* where it ended, count multibyte characters to find the
* corresponding position in the wide char string.
*/
if (endptr != NULL) {
#if 1 /* Fast, assume 1:1 WC:MBS mapping. */
if (endptr != NULL)
/* XXX Assume each wide char is one byte. */
*endptr = (wchar_t *)nptr + (end - buf);
(void)clen;
(void)p;
#else /* Slow, conservative approach. */
state = initial;
*endptr = (wchar_t *)nptr;
p = buf;
while (p < end &&
(clen = mbrlen(p, end - p, &state)) > 0) {
p += clen;
(*endptr)++;
}
#endif
}
free(buf);

View file

@ -37,46 +37,28 @@ __FBSDID("$FreeBSD$");
float
wcstof(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
{
static const mbstate_t initial;
mbstate_t state;
float val;
char *buf, *end, *p;
char *buf, *end;
const wchar_t *wcp;
size_t clen, len;
size_t len;
while (iswspace(*nptr))
nptr++;
state = initial;
wcp = nptr;
if ((len = wcsrtombs(NULL, &wcp, 0, &state)) == (size_t)-1) {
if ((len = wcsrtombs(NULL, &wcp, 0, NULL)) == (size_t)-1) {
if (endptr != NULL)
*endptr = (wchar_t *)nptr;
return (0.0);
}
if ((buf = malloc(len + 1)) == NULL)
return (0.0);
state = initial;
wcsrtombs(buf, &wcp, len + 1, &state);
wcsrtombs(buf, &wcp, len + 1, NULL);
val = strtof(buf, &end);
if (endptr != NULL) {
#if 1 /* Fast, assume 1:1 WC:MBS mapping. */
if (endptr != NULL)
*endptr = (wchar_t *)nptr + (end - buf);
(void)clen;
(void)p;
#else /* Slow, conservative approach. */
state = initial;
*endptr = (wchar_t *)nptr;
p = buf;
while (p < end &&
(clen = mbrlen(p, end - p, &state)) > 0) {
p += clen;
(*endptr)++;
}
#endif
}
free(buf);

View file

@ -37,8 +37,6 @@ __FBSDID("$FreeBSD$");
long double
wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
{
static const mbstate_t initial;
mbstate_t state;
long double val;
char *buf, *end, *p;
const wchar_t *wcp;
@ -47,36 +45,20 @@ wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
while (iswspace(*nptr))
nptr++;
state = initial;
wcp = nptr;
if ((len = wcsrtombs(NULL, &wcp, 0, &state)) == (size_t)-1) {
if ((len = wcsrtombs(NULL, &wcp, 0, NULL)) == (size_t)-1) {
if (endptr != NULL)
*endptr = (wchar_t *)nptr;
return (0.0);
}
if ((buf = malloc(len + 1)) == NULL)
return (0.0);
state = initial;
wcsrtombs(buf, &wcp, len + 1, &state);
wcsrtombs(buf, &wcp, len + 1, NULL);
val = strtold(buf, &end);
if (endptr != NULL) {
#if 1 /* Fast, assume 1:1 WC:MBS mapping. */
if (endptr != NULL)
*endptr = (wchar_t *)nptr + (end - buf);
(void)clen;
(void)p;
#else /* Slow, conservative approach. */
state = initial;
*endptr = (wchar_t *)nptr;
p = buf;
while (p < end &&
(clen = mbrlen(p, end - p, &state)) > 0) {
p += clen;
(*endptr)++;
}
#endif
}
free(buf);