struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / wcstoull.c
blobc8ffbdf79acf0d16f58f145aaa6e24f634c0f60e
1 /*---------------------------------------------------------------------
2 wcstoull() - convert a wide string to an unsigned long long int and return it
4 Copyright (C) 2018-2023, Philipp Klaus Krause . krauseph@informatik.uni-freiburg.de
5 2023, Benedikt Freisen . b.freisen@gmx.net
7 This library is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA.
22 As a special exception, if you link this library with other files,
23 some of which are compiled with SDCC, to produce an executable,
24 this library does not by itself cause the resulting executable to
25 be covered by the GNU General Public License. This exception does
26 not however invalidate any other reasons why the executable file
27 might be covered by the GNU General Public License.
28 -------------------------------------------------------------------------*/
30 #include <stdlib.h>
32 #include <stdbool.h>
33 #include <ctype.h>
34 #include <string.h>
35 #ifdef __SDCC_LONGLONG
36 #include <stdckdint.h>
37 #endif
38 #include <limits.h>
39 #include <errno.h>
40 #include <wchar.h>
42 #if 0 // Bug 3798
43 static signed char _isdigit(const wchar_t c, unsigned char base)
45 unsigned char v;
47 if (c >= L'0' && c <= L'9')
48 v = c - L'0';
49 else if (c >= L'a' && c <= L'z')
50 v = c - L'a' + 10;
51 else if (c >= L'A' && c <= L'Z')
52 v = c - L'A' + 10;
53 else
54 return (-1);
56 if (v >= base)
57 return (-1);
59 return (v);
62 // NOTE for maintenance: strtoull, wcstoul and wcstoull have been derived from strtoul
64 unsigned long long int wcstoull(const wchar_t *nptr, wchar_t **endptr, int base)
66 const wchar_t *ptr = nptr;
67 unsigned long long int ret;
68 bool range_error = false;
69 bool neg = false;
70 unsigned char b = base;
72 while (iswblank (*ptr))
73 ptr++;
75 // Handle sign.
76 switch(*ptr)
78 case L'-':
79 neg = true;
80 case L'+':
81 ptr++;
84 // base not specified.
85 if (!b)
87 if (!wcsncmp (ptr, L"0x", 2) || !wcsncmp (ptr, L"0X", 2))
89 b = 16;
90 ptr += 2;
92 else if (!wcsncmp (ptr, L"0b", 2) || !wcsncmp (ptr, L"0B", 2))
94 b = 2;
95 ptr += 2;
97 else if (*ptr == L'0')
99 b = 8;
100 ptr++;
102 else
103 b = 10;
105 // Handle optional hex prefix.
106 else if (b == 16 && (!wcsncmp (ptr, L"0x", 2) || !wcsncmp (ptr, L"0X", 2)))
107 ptr += 2;
108 else if (b == 2 && (!wcsncmp (ptr, L"0b", 2) || !wcsncmp (ptr, L"0B", 2)))
109 ptr += 2;
111 // Empty sequence conversion error
112 if (_isdigit (*ptr, b) < 0)
114 if (endptr)
115 *endptr = (wchar_t*)nptr;
116 return (0);
119 for (ret = 0;; ptr++)
121 signed char digit = _isdigit (*ptr, b);
123 if (digit < 0)
124 break;
126 range_error |= ckd_mul(&ret, ret, b);
127 range_error |= ckd_add (&ret, ret, digit);
129 ret += (unsigned char)digit;
132 if (endptr)
133 *endptr = (wchar_t*)ptr;
135 if (range_error)
137 errno = ERANGE;
138 return (ULLONG_MAX);
141 return (neg ? -ret : ret);
143 #endif