]> git.saurik.com Git - apple/libc.git/blame - locale/FreeBSD/wcstod.c.patch
Libc-498.1.7.tar.gz
[apple/libc.git] / locale / FreeBSD / wcstod.c.patch
CommitLineData
224c7076
A
1--- wcstod.c.orig 2007-03-16 01:15:20.000000000 -0700
2+++ wcstod.c 2007-03-16 03:03:41.000000000 -0700
3@@ -27,9 +27,12 @@
3d9156a7
A
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/locale/wcstod.c,v 1.4 2004/04/07 09:47:56 tjr Exp $");
6
7+#include "xlocale_private.h"
8+
9 #include <stdlib.h>
10 #include <wchar.h>
11 #include <wctype.h>
224c7076
A
12+#include <_simple.h>
13
14 /*
15 * Convert a string to a double-precision number.
16@@ -41,42 +44,43 @@
3d9156a7
A
17 * for at least the digits, radix character and letters.
18 */
19 double
20-wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
21+wcstod_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
22+ locale_t loc)
23 {
24 static const mbstate_t initial;
25 mbstate_t mbs;
224c7076
A
26 double val;
27 char *buf, *end;
28- const wchar_t *wcp;
3d9156a7 29 size_t len;
224c7076
A
30+ locale_t ctype;
31+ _SIMPLE_STRING b;
32+ char mb[MB_CUR_MAX + 1];
33+ const wchar_t *nptr0 = nptr;
34+ const wchar_t *first;
3d9156a7
A
35
36- while (iswspace(*nptr))
37+ NORMALIZE_LOCALE(loc);
224c7076
A
38+ ctype = __numeric_ctype(loc);
39+
40+ while (iswspace_l(*nptr, ctype))
3d9156a7
A
41 nptr++;
42
224c7076
A
43- /*
44- * Convert the supplied numeric wide char. string to multibyte.
45- *
46- * We could attempt to find the end of the numeric portion of the
47- * wide char. string to avoid converting unneeded characters but
48- * choose not to bother; optimising the uncommon case where
49- * the input string contains a lot of text after the number
50- * duplicates a lot of strtod()'s functionality and slows down the
51- * most common cases.
52- */
53- wcp = nptr;
54- mbs = initial;
3d9156a7 55- if ((len = wcsrtombs(NULL, &wcp, 0, &mbs)) == (size_t)-1) {
224c7076
A
56- if (endptr != NULL)
57- *endptr = (wchar_t *)nptr;
58- return (0.0);
59- }
60- if ((buf = malloc(len + 1)) == NULL)
61+ if ((b = _simple_salloc()) == NULL)
3d9156a7 62 return (0.0);
224c7076
A
63+
64+ first = nptr;
3d9156a7
A
65 mbs = initial;
66- wcsrtombs(buf, &wcp, len + 1, &mbs);
224c7076
A
67+ while (*nptr && (len = wcrtomb_l(mb, *nptr, &mbs, ctype)) != (size_t)-1) {
68+ mb[len] = 0;
69+ if (_simple_sappend(b, mb) < 0) { /* no memory */
70+ _simple_sfree(b);
71+ return (0.0);
72+ }
73+ nptr++;
74+ }
3d9156a7
A
75
76 /* Let strtod() do most of the work for us. */
77- val = strtod(buf, &end);
224c7076 78+ buf = _simple_string(b);
3d9156a7
A
79+ val = strtod_l(buf, &end, loc);
80
81 /*
82 * We only know where the number ended in the _multibyte_
224c7076
A
83@@ -86,9 +90,15 @@
84 */
85 if (endptr != NULL)
86 /* XXX Assume each wide char is one byte. */
87- *endptr = (wchar_t *)nptr + (end - buf);
88+ *endptr = (end == buf) ? (wchar_t *)nptr0 : ((wchar_t *)first + (end - buf));
89
90- free(buf);
91+ _simple_sfree(b);
3d9156a7
A
92
93 return (val);
224c7076 94 }
3d9156a7
A
95+
96+double
97+wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
98+{
99+ return wcstod_l(nptr, endptr, __current_locale());
224c7076 100+}