struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / wcstoul.c
blobf51255e5a67e6fa486f0db74ac2e66b645bdfbe8
1 /*---------------------------------------------------------------------
2 wcstoul() - convert a wide string to a unsigned 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 #if !defined(__SDCC_pic14) && !defined(__SDCC_pic16)
36 #include <stdckdint.h>
37 #endif
38 #include <limits.h>
39 #include <errno.h>
40 #include <wchar.h>
42 static signed char _isdigit(const wchar_t c, unsigned char base)
44 unsigned char v;
46 if (c >= L'0' && c <= L'9')
47 v = c - L'0';
48 else if (c >= L'a' && c <= L'z')
49 v = c - L'a' + 10;
50 else if (c >= L'A' && c <= L'Z')
51 v = c - L'A' + 10;
52 else
53 return (-1);
55 if (v >= base)
56 return (-1);
58 return (v);
61 // NOTE for maintenance: strtoull, wcstoul and wcstoull have been derived from strtoul
63 unsigned long int wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
65 const wchar_t *ptr = nptr;
66 unsigned long int ret;
67 bool range_error = false;
68 bool neg = false;
69 unsigned char b = base;
71 while (iswblank (*ptr))
72 ptr++;
74 // Handle sign.
75 switch(*ptr)
77 case L'-':
78 neg = true;
79 case L'+':
80 ptr++;
83 // base not specified.
84 if (!b)
86 if (!wcsncmp (ptr, L"0x", 2) || !wcsncmp (ptr, L"0X", 2))
88 b = 16;
89 ptr += 2;
91 else if (!wcsncmp (ptr, L"0b", 2) || !wcsncmp (ptr, L"0B", 2))
93 b = 2;
94 ptr += 2;
96 else if (*ptr == L'0')
98 b = 8;
99 ptr++;
101 else
102 b = 10;
104 // Handle optional hex prefix.
105 else if (b == 16 && (!wcsncmp (ptr, L"0x", 2) || !wcsncmp (ptr, L"0X", 2)))
106 ptr += 2;
107 else if (b == 2 && (!wcsncmp (ptr, L"0b", 2) || !wcsncmp (ptr, L"0B", 2)))
108 ptr += 2;
110 // Empty sequence conversion error
111 if (_isdigit (*ptr, b) < 0)
113 if (endptr)
114 *endptr = (wchar_t*)nptr;
115 return (0);
118 for (ret = 0;; ptr++)
120 signed char digit = _isdigit (*ptr, b);
122 if (digit < 0)
123 break;
125 #if !defined(__SDCC_pic14) && !defined(__SDCC_pic16)
126 range_error |= ckd_mul (&ret, ret, b);
127 range_error |= ckd_add (&ret, ret, digit);
128 #else
129 unsigned long int oldret = ret;
130 ret *= b;
131 if (ret < oldret)
132 range_error = true;
133 ret += (unsigned char)digit;
134 #warning INEXACT RANGE ERROR CHECK WILL NOT REPORT ALL OVERFLOWS (fix by implementing ckd_mul support)
135 #endif
138 if (endptr)
139 *endptr = (wchar_t*)ptr;
141 if (range_error)
143 errno = ERANGE;
144 return (ULONG_MAX);
147 return (neg ? -ret : ret);