135e0c06aSPeter Avalos /*-
20d5acd74SJohn Marino * Copyright (c) 2002, 2003 Tim J. Robbins
335e0c06aSPeter Avalos * All rights reserved.
435e0c06aSPeter Avalos *
50d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
60d5acd74SJohn Marino * All rights reserved.
70d5acd74SJohn Marino * Portions of this software were developed by David Chisnall
80d5acd74SJohn Marino * under sponsorship from the FreeBSD Foundation.
90d5acd74SJohn Marino *
1035e0c06aSPeter Avalos * Redistribution and use in source and binary forms, with or without
1135e0c06aSPeter Avalos * modification, are permitted provided that the following conditions
1235e0c06aSPeter Avalos * are met:
1335e0c06aSPeter Avalos * 1. Redistributions of source code must retain the above copyright
1435e0c06aSPeter Avalos * notice, this list of conditions and the following disclaimer.
1535e0c06aSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1635e0c06aSPeter Avalos * notice, this list of conditions and the following disclaimer in the
1735e0c06aSPeter Avalos * documentation and/or other materials provided with the distribution.
1835e0c06aSPeter Avalos *
1935e0c06aSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2035e0c06aSPeter Avalos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2135e0c06aSPeter Avalos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2235e0c06aSPeter Avalos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2335e0c06aSPeter Avalos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2435e0c06aSPeter Avalos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2535e0c06aSPeter Avalos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2635e0c06aSPeter Avalos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2735e0c06aSPeter Avalos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2835e0c06aSPeter Avalos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2935e0c06aSPeter Avalos * SUCH DAMAGE.
300d5acd74SJohn Marino *
31*bc18712bSzrj * $FreeBSD: head/lib/libc/locale/wcstof.c 326193 2017-11-25 17:12:48Z pfg $
3235e0c06aSPeter Avalos */
3335e0c06aSPeter Avalos
340d5acd74SJohn Marino
3535e0c06aSPeter Avalos #include <stdlib.h>
3635e0c06aSPeter Avalos #include <wchar.h>
3735e0c06aSPeter Avalos #include <wctype.h>
380d5acd74SJohn Marino #include "xlocale_private.h"
3935e0c06aSPeter Avalos
400d5acd74SJohn Marino /*
410d5acd74SJohn Marino * See wcstod() for comments as to the logic used.
420d5acd74SJohn Marino */
430d5acd74SJohn Marino float
wcstof_l(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr,locale_t locale)440d5acd74SJohn Marino wcstof_l(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr,
450d5acd74SJohn Marino locale_t locale)
460d5acd74SJohn Marino {
470d5acd74SJohn Marino static const mbstate_t initial;
480d5acd74SJohn Marino mbstate_t mbs;
490d5acd74SJohn Marino float val;
500d5acd74SJohn Marino char *buf, *end;
510d5acd74SJohn Marino const wchar_t *wcp;
520d5acd74SJohn Marino size_t len;
53*bc18712bSzrj size_t spaces;
540d5acd74SJohn Marino FIX_LOCALE(locale);
5535e0c06aSPeter Avalos
560d5acd74SJohn Marino wcp = nptr;
57*bc18712bSzrj spaces = 0;
58*bc18712bSzrj while (iswspace_l(*wcp, locale)) {
59*bc18712bSzrj wcp++;
60*bc18712bSzrj spaces++;
61*bc18712bSzrj }
62*bc18712bSzrj
630d5acd74SJohn Marino mbs = initial;
640d5acd74SJohn Marino if ((len = wcsrtombs_l(NULL, &wcp, 0, &mbs, locale)) == (size_t)-1) {
650d5acd74SJohn Marino if (endptr != NULL)
660d5acd74SJohn Marino *endptr = (wchar_t *)nptr;
670d5acd74SJohn Marino return (0.0);
680d5acd74SJohn Marino }
69*bc18712bSzrj if ((buf = malloc(len + 1)) == NULL) {
70*bc18712bSzrj if (endptr != NULL)
71*bc18712bSzrj *endptr = (wchar_t *)nptr;
720d5acd74SJohn Marino return (0.0);
73*bc18712bSzrj }
740d5acd74SJohn Marino mbs = initial;
750d5acd74SJohn Marino wcsrtombs_l(buf, &wcp, len + 1, &mbs, locale);
760d5acd74SJohn Marino
770d5acd74SJohn Marino val = strtof_l(buf, &end, locale);
780d5acd74SJohn Marino
79*bc18712bSzrj if (endptr != NULL) {
800d5acd74SJohn Marino *endptr = (wchar_t *)nptr + (end - buf);
81*bc18712bSzrj if (buf != end)
82*bc18712bSzrj *endptr += spaces;
83*bc18712bSzrj }
840d5acd74SJohn Marino
850d5acd74SJohn Marino free(buf);
860d5acd74SJohn Marino
870d5acd74SJohn Marino return (val);
880d5acd74SJohn Marino }
890d5acd74SJohn Marino float
wcstof(const wchar_t * __restrict nptr,wchar_t ** __restrict endptr)900d5acd74SJohn Marino wcstof(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr)
910d5acd74SJohn Marino {
920d5acd74SJohn Marino return wcstof_l(nptr, endptr, __get_locale());
930d5acd74SJohn Marino }
94