xref: /freebsd/lib/libc/locale/wcstold.c (revision 559a218c)
1542bd65fSTim J. Robbins /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4542bd65fSTim J. Robbins  * Copyright (c) 2002, 2003 Tim J. Robbins
5542bd65fSTim J. Robbins  * All rights reserved.
6542bd65fSTim J. Robbins  *
73c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
85b5fa75aSEd Maste  *
93c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall  *
12542bd65fSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
13542bd65fSTim J. Robbins  * modification, are permitted provided that the following conditions
14542bd65fSTim J. Robbins  * are met:
15542bd65fSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
16542bd65fSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
17542bd65fSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
18542bd65fSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
19542bd65fSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
20542bd65fSTim J. Robbins  *
21542bd65fSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22542bd65fSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23542bd65fSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24542bd65fSTim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25542bd65fSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26542bd65fSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27542bd65fSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28542bd65fSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29542bd65fSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30542bd65fSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31542bd65fSTim J. Robbins  * SUCH DAMAGE.
32542bd65fSTim J. Robbins  */
33542bd65fSTim J. Robbins 
34542bd65fSTim J. Robbins #include <stdlib.h>
35542bd65fSTim J. Robbins #include <wchar.h>
36542bd65fSTim J. Robbins #include <wctype.h>
373c87aa1dSDavid Chisnall #include "xlocale_private.h"
38542bd65fSTim J. Robbins 
39542bd65fSTim J. Robbins /*
40542bd65fSTim J. Robbins  * See wcstod() for comments as to the logic used.
41542bd65fSTim J. Robbins  */
42542bd65fSTim J. Robbins long double
wcstold_l(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr,locale_t locale)433c87aa1dSDavid Chisnall wcstold_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
443c87aa1dSDavid Chisnall 		locale_t locale)
45542bd65fSTim J. Robbins {
46dc763237STim J. Robbins 	static const mbstate_t initial;
47dc763237STim J. Robbins 	mbstate_t mbs;
48542bd65fSTim J. Robbins 	long double val;
4984d9142fSJacques Vidrine 	char *buf, *end;
5081f91de9SEric van Gyzen 	const wchar_t *wcp;
5184d9142fSJacques Vidrine 	size_t len;
5281f91de9SEric van Gyzen 	size_t spaces;
533c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
54542bd65fSTim J. Robbins 
5581f91de9SEric van Gyzen 	wcp = nptr;
5681f91de9SEric van Gyzen 	spaces = 0;
573c87aa1dSDavid Chisnall 	while (iswspace_l(*wcp, locale)) {
583c87aa1dSDavid Chisnall 		wcp++;
593c87aa1dSDavid Chisnall 		spaces++;
603c87aa1dSDavid Chisnall 	}
61542bd65fSTim J. Robbins 
62dc763237STim J. Robbins 	mbs = initial;
633c87aa1dSDavid Chisnall 	if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
64542bd65fSTim J. Robbins 		if (endptr != NULL)
65542bd65fSTim J. Robbins 			*endptr = (wchar_t *)nptr;
66542bd65fSTim J. Robbins 		return (0.0);
67542bd65fSTim J. Robbins 	}
6881f91de9SEric van Gyzen 	if ((buf = malloc(len + 1)) == NULL) {
6981f91de9SEric van Gyzen 		if (endptr != NULL)
7081f91de9SEric van Gyzen 			*endptr = (wchar_t *)nptr;
71542bd65fSTim J. Robbins 		return (0.0);
7281f91de9SEric van Gyzen 	}
73dc763237STim J. Robbins 	mbs = initial;
743c87aa1dSDavid Chisnall 	wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
75542bd65fSTim J. Robbins 
763c87aa1dSDavid Chisnall 	val = strtold_l(buf, &end, locale);
77542bd65fSTim J. Robbins 
783c87aa1dSDavid Chisnall 	if (endptr != NULL) {
79542bd65fSTim J. Robbins 		*endptr = (wchar_t *)nptr + (end - buf);
803c87aa1dSDavid Chisnall 		if (buf != end)
813c87aa1dSDavid Chisnall 			*endptr += spaces;
823c87aa1dSDavid Chisnall 	}
83542bd65fSTim J. Robbins 
84542bd65fSTim J. Robbins 	free(buf);
85542bd65fSTim J. Robbins 
86542bd65fSTim J. Robbins 	return (val);
87542bd65fSTim J. Robbins }
883c87aa1dSDavid Chisnall long double
wcstold(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr)893c87aa1dSDavid Chisnall wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
903c87aa1dSDavid Chisnall {
913c87aa1dSDavid Chisnall 	return wcstold_l(nptr, endptr, __get_locale());
923c87aa1dSDavid Chisnall }
93