103b716c4STim J. Robbins /*- 203b716c4STim J. Robbins * Copyright (c) 2002 Tim J. Robbins 303b716c4STim J. Robbins * All rights reserved. 403b716c4STim J. Robbins * 53c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 63c87aa1dSDavid Chisnall * All rights reserved. 73c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 83c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 93c87aa1dSDavid Chisnall * 1003b716c4STim J. Robbins * Redistribution and use in source and binary forms, with or without 1103b716c4STim J. Robbins * modification, are permitted provided that the following conditions 1203b716c4STim J. Robbins * are met: 1303b716c4STim J. Robbins * 1. Redistributions of source code must retain the above copyright 1403b716c4STim J. Robbins * notice, this list of conditions and the following disclaimer. 1503b716c4STim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright 1603b716c4STim J. Robbins * notice, this list of conditions and the following disclaimer in the 1703b716c4STim J. Robbins * documentation and/or other materials provided with the distribution. 1803b716c4STim J. Robbins * 1903b716c4STim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2003b716c4STim J. Robbins * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2103b716c4STim J. Robbins * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2203b716c4STim J. Robbins * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2303b716c4STim J. Robbins * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2403b716c4STim J. Robbins * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2503b716c4STim J. Robbins * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2603b716c4STim J. Robbins * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2703b716c4STim J. Robbins * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2803b716c4STim J. Robbins * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2903b716c4STim J. Robbins * SUCH DAMAGE. 3003b716c4STim J. Robbins */ 3103b716c4STim J. Robbins 3203b716c4STim J. Robbins #include <sys/cdefs.h> 3303b716c4STim J. Robbins __FBSDID("$FreeBSD$"); 3403b716c4STim J. Robbins 3503b716c4STim J. Robbins #include <stdlib.h> 3603b716c4STim J. Robbins #include <wchar.h> 3703b716c4STim J. Robbins #include <wctype.h> 383c87aa1dSDavid Chisnall #include "xlocale_private.h" 3903b716c4STim J. Robbins 4003b716c4STim J. Robbins /* 4103b716c4STim J. Robbins * Convert a string to a double-precision number. 4203b716c4STim J. Robbins * 4303b716c4STim J. Robbins * This is the wide-character counterpart of strtod(). So that we do not 4403b716c4STim J. Robbins * have to duplicate the code of strtod() here, we convert the supplied 4503b716c4STim J. Robbins * wide character string to multibyte and call strtod() on the result. 4603b716c4STim J. Robbins * This assumes that the multibyte encoding is compatible with ASCII 4703b716c4STim J. Robbins * for at least the digits, radix character and letters. 4803b716c4STim J. Robbins */ 4903b716c4STim J. Robbins double 503c87aa1dSDavid Chisnall wcstod_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, 513c87aa1dSDavid Chisnall locale_t locale) 5203b716c4STim J. Robbins { 53dc763237STim J. Robbins static const mbstate_t initial; 54dc763237STim J. Robbins mbstate_t mbs; 5503b716c4STim J. Robbins double val; 561e8742e9STim J. Robbins char *buf, *end; 57*81f91de9SEric van Gyzen const wchar_t *wcp; 581e8742e9STim J. Robbins size_t len; 59*81f91de9SEric van Gyzen size_t spaces; 603c87aa1dSDavid Chisnall FIX_LOCALE(locale); 6103b716c4STim J. Robbins 62*81f91de9SEric van Gyzen wcp = nptr; 63*81f91de9SEric van Gyzen spaces = 0; 643c87aa1dSDavid Chisnall while (iswspace_l(*wcp, locale)) { 653c87aa1dSDavid Chisnall wcp++; 663c87aa1dSDavid Chisnall spaces++; 673c87aa1dSDavid Chisnall } 6803b716c4STim J. Robbins 6903b716c4STim J. Robbins /* 7003b716c4STim J. Robbins * Convert the supplied numeric wide char. string to multibyte. 7103b716c4STim J. Robbins * 7203b716c4STim J. Robbins * We could attempt to find the end of the numeric portion of the 7303b716c4STim J. Robbins * wide char. string to avoid converting unneeded characters but 7403b716c4STim J. Robbins * choose not to bother; optimising the uncommon case where 7503b716c4STim J. Robbins * the input string contains a lot of text after the number 7603b716c4STim J. Robbins * duplicates a lot of strtod()'s functionality and slows down the 7703b716c4STim J. Robbins * most common cases. 7803b716c4STim J. Robbins */ 79dc763237STim J. Robbins mbs = initial; 803c87aa1dSDavid Chisnall if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) { 8103b716c4STim J. Robbins if (endptr != NULL) 8203b716c4STim J. Robbins *endptr = (wchar_t *)nptr; 8303b716c4STim J. Robbins return (0.0); 8403b716c4STim J. Robbins } 85*81f91de9SEric van Gyzen if ((buf = malloc(len + 1)) == NULL) { 86*81f91de9SEric van Gyzen if (endptr != NULL) 87*81f91de9SEric van Gyzen *endptr = (wchar_t *)nptr; 8803b716c4STim J. Robbins return (0.0); 89*81f91de9SEric van Gyzen } 90dc763237STim J. Robbins mbs = initial; 913c87aa1dSDavid Chisnall wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale); 9203b716c4STim J. Robbins 9303b716c4STim J. Robbins /* Let strtod() do most of the work for us. */ 943c87aa1dSDavid Chisnall val = strtod_l(buf, &end, locale); 9503b716c4STim J. Robbins 9603b716c4STim J. Robbins /* 9703b716c4STim J. Robbins * We only know where the number ended in the _multibyte_ 9803b716c4STim J. Robbins * representation of the string. If the caller wants to know 9903b716c4STim J. Robbins * where it ended, count multibyte characters to find the 10003b716c4STim J. Robbins * corresponding position in the wide char string. 10103b716c4STim J. Robbins */ 1023c87aa1dSDavid Chisnall if (endptr != NULL) { 10303b716c4STim J. Robbins *endptr = (wchar_t *)nptr + (end - buf); 1043c87aa1dSDavid Chisnall if (buf != end) 1053c87aa1dSDavid Chisnall *endptr += spaces; 1063c87aa1dSDavid Chisnall } 1073c87aa1dSDavid Chisnall 10803b716c4STim J. Robbins free(buf); 10903b716c4STim J. Robbins 11003b716c4STim J. Robbins return (val); 11103b716c4STim J. Robbins } 1123c87aa1dSDavid Chisnall double 1133c87aa1dSDavid Chisnall wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr) 1143c87aa1dSDavid Chisnall { 1153c87aa1dSDavid Chisnall return wcstod_l(nptr, endptr, __get_locale()); 1163c87aa1dSDavid Chisnall } 117