Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / wcstoll.c
blob34a7e376d9b327641d43f4811d007fd23922c071
1 /*
2 FUNCTION
3 <<wcstoll>>, <<wcstoll_l>>---wide string to long long
5 INDEX
6 wcstoll
8 INDEX
9 wcstoll_l
11 INDEX
12 _wcstoll_r
14 SYNOPSIS
15 #include <wchar.h>
16 long long wcstoll(const wchar_t *__restrict <[s]>,
17 wchar_t **__restrict <[ptr]>,int <[base]>);
19 #include <wchar.h>
20 long long wcstoll_l(const wchar_t *__restrict <[s]>,
21 wchar_t **__restrict <[ptr]>, int <[base]>,
22 locale_t <[locale]>);
24 long long _wcstoll_r(void *<[reent]>, const wchar_t *<[s]>,
25 wchar_t **<[ptr]>, int <[base]>);
27 DESCRIPTION
28 The function <<wcstoll>> converts the wide string <<*<[s]>>> to
29 a <<long long>>. First, it breaks down the string into three parts:
30 leading whitespace, which is ignored; a subject string consisting
31 of characters resembling an integer in the radix specified by <[base]>;
32 and a trailing portion consisting of zero or more unparseable characters,
33 and always including the terminating null character. Then, it attempts
34 to convert the subject string into a <<long long>> and returns the
35 result.
37 If the value of <[base]> is 0, the subject string is expected to look
38 like a normal C integer constant: an optional sign, a possible `<<0x>>'
39 indicating a hexadecimal base, and a number. If <[base]> is between
40 2 and 36, the expected form of the subject is a sequence of letters
41 and digits representing an integer in the radix specified by <[base]>,
42 with an optional plus or minus sign. The letters <<a>>--<<z>> (or,
43 equivalently, <<A>>--<<Z>>) are used to signify values from 10 to 35;
44 only letters whose ascribed values are less than <[base]> are
45 permitted. If <[base]> is 16, a leading <<0x>> is permitted.
47 The subject sequence is the longest initial sequence of the input
48 string that has the expected form, starting with the first
49 non-whitespace character. If the string is empty or consists entirely
50 of whitespace, or if the first non-whitespace character is not a
51 permissible letter or digit, the subject string is empty.
53 If the subject string is acceptable, and the value of <[base]> is zero,
54 <<wcstoll>> attempts to determine the radix from the input string. A
55 string with a leading <<0x>> is treated as a hexadecimal value; a string with
56 a leading 0 and no <<x>> is treated as octal; all other strings are
57 treated as decimal. If <[base]> is between 2 and 36, it is used as the
58 conversion radix, as described above. If the subject string begins with
59 a minus sign, the value is negated. Finally, a pointer to the first
60 character past the converted subject string is stored in <[ptr]>, if
61 <[ptr]> is not <<NULL>>.
63 If the subject string is empty (or not in acceptable form), no conversion
64 is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
65 not <<NULL>>).
67 The alternate function <<_wcstoll_r>> is a reentrant version. The
68 extra argument <[reent]> is a pointer to a reentrancy structure.
70 <<wcstoll_l>> is like <<wcstoll>> but performs the conversion based on the
71 locale specified by the locale object locale. If <[locale]> is
72 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
74 RETURNS
75 <<wcstoll>>, <<wcstoll_l>> return the converted value, if any. If no
76 conversion was made, 0 is returned.
78 <<wcstoll>>, <<wcstoll_l>> return <<LONG_LONG_MAX>> or <<LONG_LONG_MIN>>
79 if the magnitude of the converted value is too large, and sets <<errno>>
80 to <<ERANGE>>.
82 PORTABILITY
83 <<wcstoll>> is ANSI.
84 <<wcstoll_l>> is a GNU extension.
86 No supporting OS subroutines are required.
89 /*-
90 * Copyright (c) 1990 The Regents of the University of California.
91 * All rights reserved.
93 * Redistribution and use in source and binary forms, with or without
94 * modification, are permitted provided that the following conditions
95 * are met:
96 * 1. Redistributions of source code must retain the above copyright
97 * notice, this list of conditions and the following disclaimer.
98 * 2. Redistributions in binary form must reproduce the above copyright
99 * notice, this list of conditions and the following disclaimer in the
100 * documentation and/or other materials provided with the distribution.
101 * 3. Neither the name of the University nor the names of its contributors
102 * may be used to endorse or promote products derived from this software
103 * without specific prior written permission.
105 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
106 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
107 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
108 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
109 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
110 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
111 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
112 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
113 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
114 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
115 * SUCH DAMAGE.
118 #define _GNU_SOURCE
119 #include <_ansi.h>
120 #include <limits.h>
121 #include <wctype.h>
122 #include <errno.h>
123 #include <wchar.h>
124 #include <reent.h>
125 #include "../locale/setlocale.h"
128 * Convert a wide string to a long long integer.
130 long long
131 _wcstoll_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
132 int base, locale_t loc)
134 register const wchar_t *s = nptr;
135 register unsigned long long acc;
136 register int c;
137 register unsigned long long cutoff;
138 register int neg = 0, any, cutlim;
141 * Skip white space and pick up leading +/- sign if any.
142 * If base is 0, allow 0x for hex and 0 for octal, else
143 * assume decimal; if base is already 16, allow 0x.
145 do {
146 c = *s++;
147 } while (iswspace_l(c, loc));
148 if (c == L'-') {
149 neg = 1;
150 c = *s++;
151 } else if (c == L'+')
152 c = *s++;
153 if ((base == 0 || base == 16) &&
154 c == L'0' && (*s == L'x' || *s == L'X')) {
155 c = s[1];
156 s += 2;
157 base = 16;
159 if (base == 0)
160 base = c == L'0' ? 8 : 10;
163 * Compute the cutoff value between legal numbers and illegal
164 * numbers. That is the largest legal value, divided by the
165 * base. An input number that is greater than this value, if
166 * followed by a legal input character, is too big. One that
167 * is equal to this value may be valid or not; the limit
168 * between valid and invalid numbers is then based on the last
169 * digit. For instance, if the range for longs is
170 * [-2147483648..2147483647] and the input base is 10,
171 * cutoff will be set to 214748364 and cutlim to either
172 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
173 * a value > 214748364, or equal but the next digit is > 7 (or 8),
174 * the number is too big, and we will return a range error.
176 * Set any if any `digits' consumed; make it negative to indicate
177 * overflow.
179 cutoff = neg ? -(unsigned long long)LONG_LONG_MIN : LONG_LONG_MAX;
180 cutlim = cutoff % (unsigned long long)base;
181 cutoff /= (unsigned long long)base;
182 for (acc = 0, any = 0;; c = *s++) {
183 if (c >= L'0' && c <= L'9')
184 c -= L'0';
185 else if (c >= L'A' && c <= L'Z')
186 c -= L'A' - 10;
187 else if (c >= L'a' && c <= L'z')
188 c -= L'a' - 10;
189 else
190 break;
191 if (c >= base)
192 break;
193 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
194 any = -1;
195 else {
196 any = 1;
197 acc *= base;
198 acc += c;
201 if (any < 0) {
202 acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
203 _REENT_ERRNO(rptr) = ERANGE;
204 } else if (neg)
205 acc = -acc;
206 if (endptr != 0)
207 *endptr = (wchar_t *) (any ? s - 1 : nptr);
208 return (acc);
211 long long
212 _wcstoll_r (struct _reent *rptr,
213 const wchar_t *nptr,
214 wchar_t **endptr,
215 int base)
217 return _wcstoll_l (rptr, nptr, endptr, base, __get_current_locale ());
220 #ifndef _REENT_ONLY
222 long long
223 wcstoll_l (const wchar_t *__restrict s, wchar_t **__restrict ptr, int base,
224 locale_t loc)
226 return _wcstoll_l (_REENT, s, ptr, base, loc);
229 long long
230 wcstoll (const wchar_t *__restrict s,
231 wchar_t **__restrict ptr,
232 int base)
234 return _wcstoll_l (_REENT, s, ptr, base, __get_current_locale ());
237 #endif