1 --- wcstod.c.orig 2009-11-09 15:05:25.000000000 -0800
2 +++ wcstod.c 2009-11-09 15:05:26.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/locale/wcstod.c,v 1.4 2004/04/07 09:47:56 tjr Exp $");
7 +#include "xlocale_private.h"
15 + * __wcs_end_offset calculates the offset to the end within the wide character
16 + * string, assuming numbers and letters are single bytes in multibyte
17 + * representation, get the actual decimal string for localeconv_l. If the
18 + * decimal point was within the string, compensate for the fact that the
19 + * (possible more than one byte) decimal point one takes one wide character.
21 +__private_extern__ size_t
22 +__wcs_end_offset(const char * __restrict buf, const char * __restrict end, locale_t loc)
24 + const char *decimalpoint = localeconv_l(loc)->decimal_point;
25 + size_t n = end - buf;
28 + if ((p = strnstr(buf, decimalpoint, n)) != NULL)
29 + n -= strlen(decimalpoint) - 1;
34 * Convert a string to a double-precision number.
35 @@ -38,45 +60,48 @@ __FBSDID("$FreeBSD: src/lib/libc/locale/
36 * have to duplicate the code of strtod() here, we convert the supplied
37 * wide character string to multibyte and call strtod() on the result.
38 * This assumes that the multibyte encoding is compatible with ASCII
39 - * for at least the digits, radix character and letters.
40 + * for at least the digits and letters. The radix character can be more
45 -wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
46 +wcstod_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
49 static const mbstate_t initial;
57 + char mb[MB_CUR_MAX + 1];
58 + const wchar_t *nptr0 = nptr;
59 + const wchar_t *first;
61 + NORMALIZE_LOCALE(loc);
62 + ctype = __numeric_ctype(loc);
64 - while (iswspace(*nptr))
65 + while (iswspace_l(*nptr, ctype))
69 - * Convert the supplied numeric wide char. string to multibyte.
71 - * We could attempt to find the end of the numeric portion of the
72 - * wide char. string to avoid converting unneeded characters but
73 - * choose not to bother; optimising the uncommon case where
74 - * the input string contains a lot of text after the number
75 - * duplicates a lot of strtod()'s functionality and slows down the
76 - * most common cases.
80 - if ((len = wcsrtombs(NULL, &wcp, 0, &mbs)) == (size_t)-1) {
82 - *endptr = (wchar_t *)nptr;
85 - if ((buf = malloc(len + 1)) == NULL)
86 + if ((b = _simple_salloc()) == NULL)
91 - wcsrtombs(buf, &wcp, len + 1, &mbs);
92 + while (*nptr && (len = wcrtomb_l(mb, *nptr, &mbs, ctype)) != (size_t)-1) {
94 + if (_simple_sappend(b, mb) < 0) { /* no memory */
101 /* Let strtod() do most of the work for us. */
102 - val = strtod(buf, &end);
103 + buf = _simple_string(b);
104 + val = strtod_l(buf, &end, loc);
107 * We only know where the number ended in the _multibyte_
108 @@ -86,9 +111,15 @@ wcstod(const wchar_t * __restrict nptr,
111 /* XXX Assume each wide char is one byte. */
112 - *endptr = (wchar_t *)nptr + (end - buf);
113 + *endptr = (end == buf) ? (wchar_t *)nptr0 : ((wchar_t *)first + __wcs_end_offset(buf, end, loc));
122 +wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
124 + return wcstod_l(nptr, endptr, __current_locale());