]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/wcstof.c.patch
Libc-594.9.5.tar.gz
[apple/libc.git] / locale / FreeBSD / wcstof.c.patch
1 --- wcstof.c.orig 2008-10-09 11:50:52.000000000 -0700
2 +++ wcstof.c 2008-10-29 00:51:43.000000000 -0700
3 @@ -27,44 +27,67 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/locale/wcstof.c,v 1.3 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 +
18 +extern size_t __wcs_end_offset(const char * __restrict buf, const char * __restrict end, locale_t loc);
19 +
20 float
21 -wcstof(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
22 +wcstof_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
23 + locale_t loc)
24 {
25 static const mbstate_t initial;
26 mbstate_t mbs;
27 float val;
28 char *buf, *end;
29 - const wchar_t *wcp;
30 size_t len;
31 + locale_t ctype;
32 + _SIMPLE_STRING b;
33 + char mb[MB_CUR_MAX + 1];
34 + const wchar_t *nptr0 = nptr;
35 + const wchar_t *first;
36
37 - while (iswspace(*nptr))
38 + NORMALIZE_LOCALE(loc);
39 + ctype = __numeric_ctype(loc);
40 +
41 + while (iswspace_l(*nptr, ctype))
42 nptr++;
43
44 - wcp = nptr;
45 - mbs = initial;
46 - if ((len = wcsrtombs(NULL, &wcp, 0, &mbs)) == (size_t)-1) {
47 - if (endptr != NULL)
48 - *endptr = (wchar_t *)nptr;
49 - return (0.0);
50 - }
51 - if ((buf = malloc(len + 1)) == NULL)
52 + if ((b = _simple_salloc()) == NULL)
53 return (0.0);
54 +
55 + first = nptr;
56 mbs = initial;
57 - wcsrtombs(buf, &wcp, len + 1, &mbs);
58 + while (*nptr && (len = wcrtomb_l(mb, *nptr, &mbs, ctype)) != (size_t)-1) {
59 + mb[len] = 0;
60 + if (_simple_sappend(b, mb) < 0) { /* no memory */
61 + _simple_sfree(b);
62 + return (0.0);
63 + }
64 + nptr++;
65 + }
66
67 - val = strtof(buf, &end);
68 + buf = _simple_string(b);
69 + val = strtof_l(buf, &end, loc);
70
71 if (endptr != NULL)
72 - *endptr = (wchar_t *)nptr + (end - buf);
73 + *endptr = (end == buf) ? (wchar_t *)nptr0 : ((wchar_t *)first + __wcs_end_offset(buf, end, loc));
74
75 - free(buf);
76 + _simple_sfree(b);
77
78 return (val);
79 }
80 +
81 +float
82 +wcstof(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
83 +{
84 + return wcstof_l(nptr, endptr, __current_locale());
85 +}