1112177Stjr/*-
2112177Stjr * Copyright (c) 2002, 2003 Tim J. Robbins
3112177Stjr * All rights reserved.
4112177Stjr *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
10112177Stjr * Redistribution and use in source and binary forms, with or without
11112177Stjr * modification, are permitted provided that the following conditions
12112177Stjr * are met:
13112177Stjr * 1. Redistributions of source code must retain the above copyright
14112177Stjr *    notice, this list of conditions and the following disclaimer.
15112177Stjr * 2. Redistributions in binary form must reproduce the above copyright
16112177Stjr *    notice, this list of conditions and the following disclaimer in the
17112177Stjr *    documentation and/or other materials provided with the distribution.
18112177Stjr *
19112177Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20112177Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21112177Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22112177Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23112177Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24112177Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25112177Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26112177Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27112177Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28112177Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29112177Stjr * SUCH DAMAGE.
30112177Stjr */
31112177Stjr
32112177Stjr#include <sys/cdefs.h>
33112177Stjr__FBSDID("$FreeBSD: stable/10/lib/libc/locale/wcstold.c 309334 2016-11-30 20:48:44Z vangyzen $");
34112177Stjr
35112177Stjr#include <stdlib.h>
36112177Stjr#include <wchar.h>
37112177Stjr#include <wctype.h>
38227753Stheraven#include "xlocale_private.h"
39112177Stjr
40112177Stjr/*
41112177Stjr * See wcstod() for comments as to the logic used.
42112177Stjr */
43112177Stjrlong double
44227753Stheravenwcstold_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
45227753Stheraven		locale_t locale)
46112177Stjr{
47127998Stjr	static const mbstate_t initial;
48127998Stjr	mbstate_t mbs;
49112177Stjr	long double val;
50124174Snectar	char *buf, *end;
51309334Svangyzen	const wchar_t *wcp;
52124174Snectar	size_t len;
53309334Svangyzen	size_t spaces;
54227753Stheraven	FIX_LOCALE(locale);
55112177Stjr
56309334Svangyzen	wcp = nptr;
57309334Svangyzen	spaces = 0;
58227753Stheraven	while (iswspace_l(*wcp, locale)) {
59227753Stheraven		wcp++;
60227753Stheraven		spaces++;
61227753Stheraven	}
62112177Stjr
63127998Stjr	mbs = initial;
64227753Stheraven	if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
65112177Stjr		if (endptr != NULL)
66112177Stjr			*endptr = (wchar_t *)nptr;
67112177Stjr		return (0.0);
68112177Stjr	}
69309334Svangyzen	if ((buf = malloc(len + 1)) == NULL) {
70309334Svangyzen		if (endptr != NULL)
71309334Svangyzen			*endptr = (wchar_t *)nptr;
72112177Stjr		return (0.0);
73309334Svangyzen	}
74127998Stjr	mbs = initial;
75227753Stheraven	wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
76112177Stjr
77227753Stheraven	val = strtold_l(buf, &end, locale);
78112177Stjr
79227753Stheraven	if (endptr != NULL) {
80112177Stjr		*endptr = (wchar_t *)nptr + (end - buf);
81227753Stheraven		if (buf != end)
82227753Stheraven			*endptr += spaces;
83227753Stheraven	}
84112177Stjr
85112177Stjr	free(buf);
86112177Stjr
87112177Stjr	return (val);
88112177Stjr}
89227753Stheravenlong double
90227753Stheravenwcstold(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
91227753Stheraven{
92227753Stheraven	return wcstold_l(nptr, endptr, __get_locale());
93227753Stheraven}
94