3 <<wcstod>>, <<wcstof>>, <<wcstold>>, <<wcstod_l>>, <<wcstof_l>>, <<wcstold_l>>---wide char string to double or float
31 double wcstod(const wchar_t *__restrict <[str]>,
32 wchar_t **__restrict <[tail]>);
33 float wcstof(const wchar_t *__restrict <[str]>,
34 wchar_t **__restrict <[tail]>);
35 long double wcstold(const wchar_t *__restrict <[str]>,
36 wchar_t **__restrict <[tail]>);
39 double wcstod_l(const wchar_t *__restrict <[str]>,
40 wchar_t **__restrict <[tail]>, locale_t <[locale]>);
41 float wcstof_l(const wchar_t *__restrict <[str]>,
42 wchar_t **__restrict <[tail]>, locale_t <[locale]>);
43 long double wcstold_l(const wchar_t *__restrict <[str]>,
44 wchar_t **__restrict <[tail]>,
47 double _wcstod_r(void *<[reent]>,
48 const wchar_t *<[str]>, wchar_t **<[tail]>);
49 float _wcstof_r(void *<[reent]>,
50 const wchar_t *<[str]>, wchar_t **<[tail]>);
53 <<wcstod>>, <<wcstof>>, <<wcstold>> parse the wide-character string
54 <[str]>, producing a substring which can be converted to a double,
55 float, or long double value. The substring converted is the longest
56 initial subsequence of <[str]>, beginning with the first non-whitespace
57 character, that has one of these formats:
58 .[+|-]<[digits]>[.[<[digits]>]][(e|E)[+|-]<[digits]>]
59 .[+|-].<[digits]>[(e|E)[+|-]<[digits]>]
60 .[+|-](i|I)(n|N)(f|F)[(i|I)(n|N)(i|I)(t|T)(y|Y)]
61 .[+|-](n|N)(a|A)(n|N)[<(>[<[hexdigits]>]<)>]
62 .[+|-]0(x|X)<[hexdigits]>[.[<[hexdigits]>]][(p|P)[+|-]<[digits]>]
63 .[+|-]0(x|X).<[hexdigits]>[(p|P)[+|-]<[digits]>]
64 The substring contains no characters if <[str]> is empty, consists
65 entirely of whitespace, or if the first non-whitespace
66 character is something other than <<+>>, <<->>, <<.>>, or a
67 digit, and cannot be parsed as infinity or NaN. If the platform
68 does not support NaN, then NaN is treated as an empty substring.
69 If the substring is empty, no conversion is done, and
70 the value of <[str]> is stored in <<*<[tail]>>>. Otherwise,
71 the substring is converted, and a pointer to the final string
72 (which will contain at least the terminating null character of
73 <[str]>) is stored in <<*<[tail]>>>. If you want no
74 assignment to <<*<[tail]>>>, pass a null pointer as <[tail]>.
76 This implementation returns the nearest machine number to the
77 input decimal string. Ties are broken by using the IEEE
78 round-even rule. However, <<wcstof>> is currently subject to
79 double rounding errors.
81 <<wcstod_l>>, <<wcstof_l>>, <<wcstold_l>> are like <<wcstod>>,
82 <<wcstof>>, <<wcstold>> but perform the conversion based on the
83 locale specified by the locale object locale. If <[locale]> is
84 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is
87 The alternate functions <<_wcstod_r>> and <<_wcstof_r>> are
88 reentrant versions of <<wcstod>> and <<wcstof>>, respectively.
89 The extra argument <[reent]> is a pointer to a reentrancy structure.
92 Return the converted substring value, if any. If
93 no conversion could be performed, 0 is returned. If the
94 correct value is out of the range of representable values,
95 plus or minus <<HUGE_VAL>> is returned, and <<ERANGE>> is
96 stored in errno. If the correct value would cause underflow, 0
97 is returned and <<ERANGE>> is stored in errno.
101 <<wcstof>>, <<wcstold>> are C99.
102 <<wcstod_l>>, <<wcstof_l>>, <<wcstold_l>> are GNU extensions.
104 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
105 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
109 * Copyright (c) 2002 Tim J. Robbins
110 * All rights reserved.
112 * Redistribution and use in source and binary forms, with or without
113 * modification, are permitted provided that the following conditions
115 * 1. Redistributions of source code must retain the above copyright
116 * notice, this list of conditions and the following disclaimer.
117 * 2. Redistributions in binary form must reproduce the above copyright
118 * notice, this list of conditions and the following disclaimer in the
119 * documentation and/or other materials provided with the distribution.
121 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
122 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
124 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
125 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
126 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
127 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
128 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
129 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
130 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
145 _wcstod_l (struct _reent
*ptr
, const wchar_t *nptr
, wchar_t **endptr
,
148 static const mbstate_t initial
;
155 while (iswspace_l(*nptr
, loc
))
159 * Convert the supplied numeric wide char. string to multibyte.
161 * We could attempt to find the end of the numeric portion of the
162 * wide char. string to avoid converting unneeded characters but
163 * choose not to bother; optimising the uncommon case where
164 * the input string contains a lot of text after the number
165 * duplicates a lot of strtod()'s functionality and slows down the
170 if ((len
= _wcsnrtombs_l(ptr
, NULL
, &wcp
, (size_t) -1, 0, &mbs
, loc
))
173 *endptr
= (wchar_t *)nptr
;
176 if ((buf
= _malloc_r(ptr
, len
+ 1)) == NULL
)
179 _wcsnrtombs_l(ptr
, buf
, &wcp
, (size_t) -1, len
+ 1, &mbs
, loc
);
181 /* Let strtod() do most of the work for us. */
182 val
= _strtod_l(ptr
, buf
, &end
, loc
);
185 * We only know where the number ended in the _multibyte_
186 * representation of the string. If the caller wants to know
187 * where it ended, count multibyte characters to find the
188 * corresponding position in the wide char string.
190 if (endptr
!= NULL
) {
191 const char *decimal_point
= __get_numeric_locale(loc
)->decimal_point
;
192 /* The only valid multibyte char in a float converted by
193 strtod/wcstod is the radix char. What we do here is,
194 figure out if the radix char was in the valid leading
195 float sequence in the incoming string. If so, the
196 multibyte float string is strlen(radix char) - 1 bytes
197 longer than the incoming wide char string has characters.
198 To fix endptr, reposition end as if the radix char was
199 just one byte long. The resulting difference (end - buf)
200 is then equivalent to the number of valid wide characters
201 in the input string. */
202 len
= strlen (decimal_point
);
204 char *d
= strstr (buf
, decimal_point
);
208 *endptr
= (wchar_t *)nptr
+ (end
- buf
);
217 _wcstod_r (struct _reent
*ptr
,
221 return _wcstod_l (ptr
, nptr
, endptr
, __get_current_locale ());
225 _wcstof_r (struct _reent
*ptr
,
229 double retval
= _wcstod_l (ptr
, nptr
, endptr
, __get_current_locale ());
232 return (float)retval
;
238 wcstod_l (const wchar_t *__restrict nptr
, wchar_t **__restrict endptr
,
241 return _wcstod_l (_REENT
, nptr
, endptr
, loc
);
245 wcstod (const wchar_t *__restrict nptr
, wchar_t **__restrict endptr
)
247 return _wcstod_l (_REENT
, nptr
, endptr
, __get_current_locale ());
251 wcstof_l (const wchar_t *__restrict nptr
, wchar_t **__restrict endptr
,
254 double val
= _wcstod_l (_REENT
, nptr
, endptr
, loc
);
257 float retval
= (float) val
;
259 if (isinf (retval
) && !isinf (val
))
260 _REENT_ERRNO(_REENT
) = ERANGE
;
266 wcstof (const wchar_t *__restrict nptr
,
267 wchar_t **__restrict endptr
)
269 double val
= _wcstod_l (_REENT
, nptr
, endptr
, __get_current_locale ());
272 float retval
= (float) val
;
274 if (isinf (retval
) && !isinf (val
))
275 _REENT_ERRNO(_REENT
) = ERANGE
;