]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/wcstold.c.patch
Libc-498.1.5.tar.gz
[apple/libc.git] / locale / FreeBSD / wcstold.c.patch
1 --- wcstold.c.orig 2007-03-16 01:15:20.000000000 -0700
2 +++ wcstold.c 2007-03-16 03:04:39.000000000 -0700
3 @@ -27,44 +27,64 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/locale/wcstold.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>
12 +#include <_simple.h>
13
14 /*
15 * See wcstod() for comments as to the logic used.
16 */
17 long double
18 -wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
19 +wcstold_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
20 + locale_t loc)
21 {
22 static const mbstate_t initial;
23 mbstate_t mbs;
24 long double val;
25 char *buf, *end;
26 - const wchar_t *wcp;
27 size_t len;
28 + locale_t ctype;
29 + _SIMPLE_STRING b;
30 + char mb[MB_CUR_MAX + 1];
31 + const wchar_t *nptr0 = nptr;
32 + const wchar_t *first;
33
34 - while (iswspace(*nptr))
35 + NORMALIZE_LOCALE(loc);
36 + ctype = __numeric_ctype(loc);
37 +
38 + while (iswspace_l(*nptr, ctype))
39 nptr++;
40
41 - wcp = nptr;
42 - mbs = initial;
43 - if ((len = wcsrtombs(NULL, &wcp, 0, &mbs)) == (size_t)-1) {
44 - if (endptr != NULL)
45 - *endptr = (wchar_t *)nptr;
46 - return (0.0);
47 - }
48 - if ((buf = malloc(len + 1)) == NULL)
49 + if ((b = _simple_salloc()) == NULL)
50 return (0.0);
51 +
52 + first = nptr;
53 mbs = initial;
54 - wcsrtombs(buf, &wcp, len + 1, &mbs);
55 + while (*nptr && (len = wcrtomb_l(mb, *nptr, &mbs, ctype)) != (size_t)-1) {
56 + mb[len] = 0;
57 + if (_simple_sappend(b, mb) < 0) { /* no memory */
58 + _simple_sfree(b);
59 + return (0.0);
60 + }
61 + nptr++;
62 + }
63
64 - val = strtold(buf, &end);
65 + buf = _simple_string(b);
66 + val = strtold_l(buf, &end, loc);
67
68 if (endptr != NULL)
69 - *endptr = (wchar_t *)nptr + (end - buf);
70 + *endptr = (end == buf) ? (wchar_t *)nptr0 : ((wchar_t *)first + (end - buf));
71
72 - free(buf);
73 + _simple_sfree(b);
74
75 return (val);
76 }
77 +
78 +long double
79 +wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
80 +{
81 + return wcstold_l(nptr, endptr, __current_locale());
82 +}