103b716c4STim J. Robbins /*- 2d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3d915a14eSPedro F. Giffuni * 403b716c4STim J. Robbins * Copyright (c) 2002 Tim J. Robbins 503b716c4STim J. Robbins * All rights reserved. 603b716c4STim J. Robbins * 73c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 8*5b5fa75aSEd Maste * 93c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 103c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 113c87aa1dSDavid Chisnall * 1203b716c4STim J. Robbins * Redistribution and use in source and binary forms, with or without 1303b716c4STim J. Robbins * modification, are permitted provided that the following conditions 1403b716c4STim J. Robbins * are met: 1503b716c4STim J. Robbins * 1. Redistributions of source code must retain the above copyright 1603b716c4STim J. Robbins * notice, this list of conditions and the following disclaimer. 1703b716c4STim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright 1803b716c4STim J. Robbins * notice, this list of conditions and the following disclaimer in the 1903b716c4STim J. Robbins * documentation and/or other materials provided with the distribution. 2003b716c4STim J. Robbins * 2103b716c4STim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2203b716c4STim J. Robbins * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2303b716c4STim J. Robbins * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2403b716c4STim J. Robbins * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2503b716c4STim J. Robbins * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2603b716c4STim J. Robbins * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2703b716c4STim J. Robbins * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2803b716c4STim J. Robbins * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2903b716c4STim J. Robbins * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3003b716c4STim J. Robbins * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3103b716c4STim J. Robbins * SUCH DAMAGE. 3203b716c4STim J. Robbins */ 3303b716c4STim J. Robbins 3403b716c4STim J. Robbins #include <sys/cdefs.h> 3503b716c4STim J. Robbins __FBSDID("$FreeBSD$"); 3603b716c4STim J. Robbins 3703b716c4STim J. Robbins #include <stdlib.h> 3803b716c4STim J. Robbins #include <wchar.h> 3903b716c4STim J. Robbins #include <wctype.h> 403c87aa1dSDavid Chisnall #include "xlocale_private.h" 4103b716c4STim J. Robbins 4203b716c4STim J. Robbins /* 4303b716c4STim J. Robbins * Convert a string to a double-precision number. 4403b716c4STim J. Robbins * 4503b716c4STim J. Robbins * This is the wide-character counterpart of strtod(). So that we do not 4603b716c4STim J. Robbins * have to duplicate the code of strtod() here, we convert the supplied 4703b716c4STim J. Robbins * wide character string to multibyte and call strtod() on the result. 4803b716c4STim J. Robbins * This assumes that the multibyte encoding is compatible with ASCII 4903b716c4STim J. Robbins * for at least the digits, radix character and letters. 5003b716c4STim J. Robbins */ 5103b716c4STim J. Robbins double 523c87aa1dSDavid Chisnall wcstod_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, 533c87aa1dSDavid Chisnall locale_t locale) 5403b716c4STim J. Robbins { 55dc763237STim J. Robbins static const mbstate_t initial; 56dc763237STim J. Robbins mbstate_t mbs; 5703b716c4STim J. Robbins double val; 581e8742e9STim J. Robbins char *buf, *end; 5981f91de9SEric van Gyzen const wchar_t *wcp; 601e8742e9STim J. Robbins size_t len; 6181f91de9SEric van Gyzen size_t spaces; 623c87aa1dSDavid Chisnall FIX_LOCALE(locale); 6303b716c4STim J. Robbins 6481f91de9SEric van Gyzen wcp = nptr; 6581f91de9SEric van Gyzen spaces = 0; 663c87aa1dSDavid Chisnall while (iswspace_l(*wcp, locale)) { 673c87aa1dSDavid Chisnall wcp++; 683c87aa1dSDavid Chisnall spaces++; 693c87aa1dSDavid Chisnall } 7003b716c4STim J. Robbins 7103b716c4STim J. Robbins /* 7203b716c4STim J. Robbins * Convert the supplied numeric wide char. string to multibyte. 7303b716c4STim J. Robbins * 7403b716c4STim J. Robbins * We could attempt to find the end of the numeric portion of the 7503b716c4STim J. Robbins * wide char. string to avoid converting unneeded characters but 7603b716c4STim J. Robbins * choose not to bother; optimising the uncommon case where 7703b716c4STim J. Robbins * the input string contains a lot of text after the number 7803b716c4STim J. Robbins * duplicates a lot of strtod()'s functionality and slows down the 7903b716c4STim J. Robbins * most common cases. 8003b716c4STim J. Robbins */ 81dc763237STim J. Robbins mbs = initial; 823c87aa1dSDavid Chisnall if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) { 8303b716c4STim J. Robbins if (endptr != NULL) 8403b716c4STim J. Robbins *endptr = (wchar_t *)nptr; 8503b716c4STim J. Robbins return (0.0); 8603b716c4STim J. Robbins } 8781f91de9SEric van Gyzen if ((buf = malloc(len + 1)) == NULL) { 8881f91de9SEric van Gyzen if (endptr != NULL) 8981f91de9SEric van Gyzen *endptr = (wchar_t *)nptr; 9003b716c4STim J. Robbins return (0.0); 9181f91de9SEric van Gyzen } 92dc763237STim J. Robbins mbs = initial; 933c87aa1dSDavid Chisnall wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale); 9403b716c4STim J. Robbins 9503b716c4STim J. Robbins /* Let strtod() do most of the work for us. */ 963c87aa1dSDavid Chisnall val = strtod_l(buf, &end, locale); 9703b716c4STim J. Robbins 9803b716c4STim J. Robbins /* 9903b716c4STim J. Robbins * We only know where the number ended in the _multibyte_ 10003b716c4STim J. Robbins * representation of the string. If the caller wants to know 10103b716c4STim J. Robbins * where it ended, count multibyte characters to find the 10203b716c4STim J. Robbins * corresponding position in the wide char string. 10303b716c4STim J. Robbins */ 1043c87aa1dSDavid Chisnall if (endptr != NULL) { 10503b716c4STim J. Robbins *endptr = (wchar_t *)nptr + (end - buf); 1063c87aa1dSDavid Chisnall if (buf != end) 1073c87aa1dSDavid Chisnall *endptr += spaces; 1083c87aa1dSDavid Chisnall } 1093c87aa1dSDavid Chisnall 11003b716c4STim J. Robbins free(buf); 11103b716c4STim J. Robbins 11203b716c4STim J. Robbins return (val); 11303b716c4STim J. Robbins } 1143c87aa1dSDavid Chisnall double 1153c87aa1dSDavid Chisnall wcstod(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr) 1163c87aa1dSDavid Chisnall { 1173c87aa1dSDavid Chisnall return wcstod_l(nptr, endptr, __get_locale()); 1183c87aa1dSDavid Chisnall } 119