wcstod.c revision 1.8 1 /* $NetBSD: wcstod.c,v 1.8 2006/04/13 01:25:13 tnozaki Exp $ */
2
3 /*-
4 * Copyright (c) 2002 Tim J. Robbins
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * Original version ID:
29 * FreeBSD: /repoman/r/ncvs/src/lib/libc/locale/wcstod.c,v 1.4 2004/04/07 09:47:56 tjr Exp
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: wcstod.c,v 1.8 2006/04/13 01:25:13 tnozaki Exp $");
35 #endif /* LIBC_SCCS and not lint */
36
37 #include <assert.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <wchar.h>
42 #include <wctype.h>
43
44 /*
45 * Convert a string to a double-precision number.
46 *
47 * This is the wide-character counterpart of strtod(). So that we do not
48 * have to duplicate the code of strtod() here, we convert the supplied
49 * wide character string to multibyte and call strtod() on the result.
50 * This assumes that the multibyte encoding is compatible with ASCII
51 * for at least the digits, radix character and letters.
52 */
53 double
54 wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
55 {
56 const wchar_t *src, *start;
57 double val;
58 char *buf, *end;
59 size_t bufsiz, len;
60
61 _DIAGASSERT(nptr != NULL);
62 /* endptr may be null */
63
64 src = nptr;
65 while (iswspace((wint_t)*src) != 0)
66 ++src;
67 if (*src == L'\0')
68 goto no_convert;
69
70 /*
71 * Convert the supplied numeric wide char. string to multibyte.
72 *
73 * We could attempt to find the end of the numeric portion of the
74 * wide char. string to avoid converting unneeded characters but
75 * choose not to bother; optimising the uncommon case where
76 * the input string contains a lot of text after the number
77 * duplicates a lot of strtod()'s functionality and slows down the
78 * most common cases.
79 */
80 start = src;
81 len = wcstombs(NULL, src, 0);
82 if (len == (size_t)-1)
83 /* errno = EILSEQ */
84 goto no_convert;
85
86 _DIAGASSERT(len > 0);
87
88 bufsiz = len;
89 buf = (void *)malloc(bufsiz + 1);
90 if (buf == NULL)
91 /* errno = ENOMEM */
92 goto no_convert;
93
94 len = wcstombs(buf, src, bufsiz + 1);
95
96 _DIAGASSERT(len == bufsiz);
97 _DIAGASSERT(buf[len] == '\0');
98
99 /* Let strtod() do most of the work for us. */
100 val = strtod(buf, &end);
101 if (buf == end) {
102 free(buf);
103 goto no_convert;
104 }
105
106 /*
107 * We only know where the number ended in the _multibyte_
108 * representation of the string. If the caller wants to know
109 * where it ended, count multibyte characters to find the
110 * corresponding position in the wide char string.
111 */
112 if (endptr != NULL)
113 /* XXX Assume each wide char is one byte. */
114 *endptr = __UNCONST(start + (size_t)(end - buf));
115
116 free(buf);
117
118 return val;
119
120 no_convert:
121 if (endptr != NULL)
122 *endptr = __UNCONST(nptr);
123 return 0;
124 }
125