1 /*
2 (C) Copyright IBM Corp. 2009
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 * Neither the name of IBM nor the names of its contributors may be
15 used to endorse or promote products derived from this software without
16 specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #define _GNU_SOURCE
32 #include <stdlib.h>
33 #include <string.h>
34 #include <wchar.h>
35 #include <wctype.h>
36 #include <locale.h>
37 #include "local.h"
38 #include "../locale/setlocale.h"
39
40 #if defined(_HAVE_LONG_DOUBLE) && __LDBL_MANT_DIG == 64
41
42 long double
wcstold_l(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr,locale_t loc)43 wcstold_l (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,
44 locale_t loc)
45 {
46 #ifdef _LDBL_EQ_DBL
47 /* On platforms where long double is as wide as double. */
48 return wcstod_l(nptr, endptr, loc);
49
50 #else /* This is a duplicate of the code in wcstod.c, but converted to long double. */
51
52 static const mbstate_t initial;
53 mbstate_t mbs;
54 long double val;
55 char *buf, *end;
56 const wchar_t *wcp;
57 size_t len;
58
59 while (iswspace (*nptr))
60 nptr++;
61
62 /* Convert the supplied numeric wide char string to multibyte. */
63 wcp = nptr;
64 mbs = initial;
65 if ((len = _wcsnrtombs_l (NULL, &wcp, (size_t) -1, 0, &mbs, loc))
66 == (size_t) -1)
67 {
68 if (endptr != NULL)
69 *endptr = (wchar_t *) nptr;
70 return 0.0L;
71 }
72
73 if ((buf = malloc (len + 1)) == NULL)
74 return 0.0L;
75
76 mbs = initial;
77 _wcsnrtombs_l (buf, &wcp, (size_t) -1, len + 1, &mbs, loc);
78
79 val = strtold_l (buf, &end, loc);
80
81 /* We only know where the number ended in the _multibyte_
82 representation of the string. If the caller wants to know
83 where it ended, count multibyte characters to find the
84 corresponding position in the wide char string. */
85
86 if (endptr != NULL)
87 {
88 const char *decimal_point = __get_numeric_locale(loc)->decimal_point;
89 /* The only valid multibyte char in a float converted by
90 strtold/wcstold is the radix char. What we do here is,
91 figure out if the radix char was in the valid leading
92 float sequence in the incoming string. If so, the
93 multibyte float string is strlen (radix char) - 1 bytes
94 longer than the incoming wide char string has characters.
95 To fix endptr, reposition end as if the radix char was
96 just one byte long. The resulting difference (end - buf)
97 is then equivalent to the number of valid wide characters
98 in the input string. */
99 len = strlen (decimal_point);
100 if (len > 1)
101 {
102 char *d = strstr (buf, decimal_point);
103
104 if (d && d < end)
105 end -= len - 1;
106 }
107
108 *endptr = (wchar_t *) nptr + (end - buf);
109 }
110
111 free (buf);
112
113 return val;
114 #endif /* _LDBL_EQ_DBL */
115 }
116
117 long double
wcstold(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr)118 wcstold (const wchar_t *__restrict nptr, wchar_t **__restrict endptr)
119 {
120 #ifdef _LDBL_EQ_DBL
121 /* On platforms where long double is as wide as double. */
122 return wcstod_l(nptr, endptr, __get_current_locale ());
123 #else
124 return wcstold_l(nptr, endptr, __get_current_locale ());
125 #endif
126 }
127
128 #endif /* _HAVE_LONG_DOUBLE && __LDBL_MANT_DIG == 64 */
129