xref: /freebsd/lib/libc/locale/wcstold.c (revision 5b5fa75acff11d871d0c90045f8c1a58fed85365)
1542bd65fSTim J. Robbins /*-
2d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
8*5b5fa75aSEd 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 <sys/cdefs.h>
35542bd65fSTim J. Robbins __FBSDID("$FreeBSD$");
36542bd65fSTim J. Robbins 
37542bd65fSTim J. Robbins #include <stdlib.h>
38542bd65fSTim J. Robbins #include <wchar.h>
39542bd65fSTim J. Robbins #include <wctype.h>
403c87aa1dSDavid Chisnall #include "xlocale_private.h"
41542bd65fSTim J. Robbins 
42542bd65fSTim J. Robbins /*
43542bd65fSTim J. Robbins  * See wcstod() for comments as to the logic used.
44542bd65fSTim J. Robbins  */
45542bd65fSTim J. Robbins long double
463c87aa1dSDavid Chisnall wcstold_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
473c87aa1dSDavid Chisnall 		locale_t locale)
48542bd65fSTim J. Robbins {
49dc763237STim J. Robbins 	static const mbstate_t initial;
50dc763237STim J. Robbins 	mbstate_t mbs;
51542bd65fSTim J. Robbins 	long double val;
5284d9142fSJacques Vidrine 	char *buf, *end;
5381f91de9SEric van Gyzen 	const wchar_t *wcp;
5484d9142fSJacques Vidrine 	size_t len;
5581f91de9SEric van Gyzen 	size_t spaces;
563c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
57542bd65fSTim J. Robbins 
5881f91de9SEric van Gyzen 	wcp = nptr;
5981f91de9SEric van Gyzen 	spaces = 0;
603c87aa1dSDavid Chisnall 	while (iswspace_l(*wcp, locale)) {
613c87aa1dSDavid Chisnall 		wcp++;
623c87aa1dSDavid Chisnall 		spaces++;
633c87aa1dSDavid Chisnall 	}
64542bd65fSTim J. Robbins 
65dc763237STim J. Robbins 	mbs = initial;
663c87aa1dSDavid Chisnall 	if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
67542bd65fSTim J. Robbins 		if (endptr != NULL)
68542bd65fSTim J. Robbins 			*endptr = (wchar_t *)nptr;
69542bd65fSTim J. Robbins 		return (0.0);
70542bd65fSTim J. Robbins 	}
7181f91de9SEric van Gyzen 	if ((buf = malloc(len + 1)) == NULL) {
7281f91de9SEric van Gyzen 		if (endptr != NULL)
7381f91de9SEric van Gyzen 			*endptr = (wchar_t *)nptr;
74542bd65fSTim J. Robbins 		return (0.0);
7581f91de9SEric van Gyzen 	}
76dc763237STim J. Robbins 	mbs = initial;
773c87aa1dSDavid Chisnall 	wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
78542bd65fSTim J. Robbins 
793c87aa1dSDavid Chisnall 	val = strtold_l(buf, &end, locale);
80542bd65fSTim J. Robbins 
813c87aa1dSDavid Chisnall 	if (endptr != NULL) {
82542bd65fSTim J. Robbins 		*endptr = (wchar_t *)nptr + (end - buf);
833c87aa1dSDavid Chisnall 		if (buf != end)
843c87aa1dSDavid Chisnall 			*endptr += spaces;
853c87aa1dSDavid Chisnall 	}
86542bd65fSTim J. Robbins 
87542bd65fSTim J. Robbins 	free(buf);
88542bd65fSTim J. Robbins 
89542bd65fSTim J. Robbins 	return (val);
90542bd65fSTim J. Robbins }
913c87aa1dSDavid Chisnall long double
923c87aa1dSDavid Chisnall wcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
933c87aa1dSDavid Chisnall {
943c87aa1dSDavid Chisnall 	return wcstold_l(nptr, endptr, __get_locale());
953c87aa1dSDavid Chisnall }
96