xref: /dflybsd-src/lib/libc/locale/wcstod.c (revision bc18712b47db31b43d8deca33b3540dc5f20d1fe)
19cb03a6aSJoerg Sonnenberger /*-
20d5acd74SJohn Marino  * Copyright (c) 2002 Tim J. Robbins
39cb03a6aSJoerg Sonnenberger  * All rights reserved.
49cb03a6aSJoerg Sonnenberger  *
50d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
60d5acd74SJohn Marino  * All rights reserved.
70d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
80d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
90d5acd74SJohn Marino  *
109cb03a6aSJoerg Sonnenberger  * Redistribution and use in source and binary forms, with or without
119cb03a6aSJoerg Sonnenberger  * modification, are permitted provided that the following conditions
129cb03a6aSJoerg Sonnenberger  * are met:
139cb03a6aSJoerg Sonnenberger  * 1. Redistributions of source code must retain the above copyright
149cb03a6aSJoerg Sonnenberger  *    notice, this list of conditions and the following disclaimer.
159cb03a6aSJoerg Sonnenberger  * 2. Redistributions in binary form must reproduce the above copyright
169cb03a6aSJoerg Sonnenberger  *    notice, this list of conditions and the following disclaimer in the
179cb03a6aSJoerg Sonnenberger  *    documentation and/or other materials provided with the distribution.
189cb03a6aSJoerg Sonnenberger  *
199cb03a6aSJoerg Sonnenberger  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
209cb03a6aSJoerg Sonnenberger  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219cb03a6aSJoerg Sonnenberger  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229cb03a6aSJoerg Sonnenberger  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
239cb03a6aSJoerg Sonnenberger  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249cb03a6aSJoerg Sonnenberger  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259cb03a6aSJoerg Sonnenberger  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269cb03a6aSJoerg Sonnenberger  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279cb03a6aSJoerg Sonnenberger  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289cb03a6aSJoerg Sonnenberger  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299cb03a6aSJoerg Sonnenberger  * SUCH DAMAGE.
300d5acd74SJohn Marino  *
31*bc18712bSzrj  * $FreeBSD: head/lib/libc/locale/wcstod.c 326193 2017-11-25 17:12:48Z pfg $
329cb03a6aSJoerg Sonnenberger  */
339cb03a6aSJoerg Sonnenberger 
340d5acd74SJohn Marino 
359cb03a6aSJoerg Sonnenberger #include <stdlib.h>
369cb03a6aSJoerg Sonnenberger #include <wchar.h>
379cb03a6aSJoerg Sonnenberger #include <wctype.h>
380d5acd74SJohn Marino #include "xlocale_private.h"
399cb03a6aSJoerg Sonnenberger 
400d5acd74SJohn Marino /*
410d5acd74SJohn Marino  * Convert a string to a double-precision number.
420d5acd74SJohn Marino  *
430d5acd74SJohn Marino  * This is the wide-character counterpart of strtod(). So that we do not
440d5acd74SJohn Marino  * have to duplicate the code of strtod() here, we convert the supplied
450d5acd74SJohn Marino  * wide character string to multibyte and call strtod() on the result.
460d5acd74SJohn Marino  * This assumes that the multibyte encoding is compatible with ASCII
470d5acd74SJohn Marino  * for at least the digits, radix character and letters.
480d5acd74SJohn Marino  */
490d5acd74SJohn Marino double
wcstod_l(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr,locale_t locale)500d5acd74SJohn Marino wcstod_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
510d5acd74SJohn Marino 		locale_t locale)
520d5acd74SJohn Marino {
530d5acd74SJohn Marino 	static const mbstate_t initial;
540d5acd74SJohn Marino 	mbstate_t mbs;
550d5acd74SJohn Marino 	double val;
560d5acd74SJohn Marino 	char *buf, *end;
57*bc18712bSzrj 	const wchar_t *wcp;
580d5acd74SJohn Marino 	size_t len;
59*bc18712bSzrj 	size_t spaces;
600d5acd74SJohn Marino 	FIX_LOCALE(locale);
619cb03a6aSJoerg Sonnenberger 
62*bc18712bSzrj 	wcp = nptr;
63*bc18712bSzrj 	spaces = 0;
640d5acd74SJohn Marino 	while (iswspace_l(*wcp, locale)) {
650d5acd74SJohn Marino 		wcp++;
660d5acd74SJohn Marino 		spaces++;
670d5acd74SJohn Marino 	}
680d5acd74SJohn Marino 
690d5acd74SJohn Marino 	/*
700d5acd74SJohn Marino 	 * Convert the supplied numeric wide char. string to multibyte.
710d5acd74SJohn Marino 	 *
720d5acd74SJohn Marino 	 * We could attempt to find the end of the numeric portion of the
730d5acd74SJohn Marino 	 * wide char. string to avoid converting unneeded characters but
740d5acd74SJohn Marino 	 * choose not to bother; optimising the uncommon case where
750d5acd74SJohn Marino 	 * the input string contains a lot of text after the number
760d5acd74SJohn Marino 	 * duplicates a lot of strtod()'s functionality and slows down the
770d5acd74SJohn Marino 	 * most common cases.
780d5acd74SJohn Marino 	 */
790d5acd74SJohn Marino 	mbs = initial;
800d5acd74SJohn Marino 	if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
810d5acd74SJohn Marino 		if (endptr != NULL)
820d5acd74SJohn Marino 			*endptr = (wchar_t *)nptr;
830d5acd74SJohn Marino 		return (0.0);
840d5acd74SJohn Marino 	}
85*bc18712bSzrj 	if ((buf = malloc(len + 1)) == NULL) {
86*bc18712bSzrj 		if (endptr != NULL)
87*bc18712bSzrj 			*endptr = (wchar_t *)nptr;
880d5acd74SJohn Marino 		return (0.0);
89*bc18712bSzrj 	}
900d5acd74SJohn Marino 	mbs = initial;
910d5acd74SJohn Marino 	wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
920d5acd74SJohn Marino 
930d5acd74SJohn Marino 	/* Let strtod() do most of the work for us. */
940d5acd74SJohn Marino 	val = strtod_l(buf, &end, locale);
950d5acd74SJohn Marino 
960d5acd74SJohn Marino 	/*
970d5acd74SJohn Marino 	 * We only know where the number ended in the _multibyte_
980d5acd74SJohn Marino 	 * representation of the string. If the caller wants to know
990d5acd74SJohn Marino 	 * where it ended, count multibyte characters to find the
1000d5acd74SJohn Marino 	 * corresponding position in the wide char string.
1010d5acd74SJohn Marino 	 */
1020d5acd74SJohn Marino 	if (endptr != NULL) {
1030d5acd74SJohn Marino 		*endptr = (wchar_t *)nptr + (end - buf);
1040d5acd74SJohn Marino 		if (buf != end)
1050d5acd74SJohn Marino 			*endptr += spaces;
1060d5acd74SJohn Marino 	}
1070d5acd74SJohn Marino 
1080d5acd74SJohn Marino 	free(buf);
1090d5acd74SJohn Marino 
1100d5acd74SJohn Marino 	return (val);
1110d5acd74SJohn Marino }
1120d5acd74SJohn Marino double
wcstod(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr)1130d5acd74SJohn Marino wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
1140d5acd74SJohn Marino {
1150d5acd74SJohn Marino 	return wcstod_l(nptr, endptr, __get_locale());
1160d5acd74SJohn Marino }
117