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
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,
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 -------------------------------------------------------------------------*/
35 #if !defined(__SDCC_pic14) && !defined(__SDCC_pic16)
36 #include <stdckdint.h>
42 static signed char _isdigit(const wchar_t c
, unsigned char base
)
46 if (c
>= L
'0' && c
<= L
'9')
48 else if (c
>= L
'a' && c
<= L
'z')
50 else if (c
>= L
'A' && c
<= L
'Z')
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;
69 unsigned char b
= base
;
71 while (iswblank (*ptr
))
83 // base not specified.
86 if (!wcsncmp (ptr
, L
"0x", 2) || !wcsncmp (ptr
, L
"0X", 2))
91 else if (!wcsncmp (ptr
, L
"0b", 2) || !wcsncmp (ptr
, L
"0B", 2))
96 else if (*ptr
== L
'0')
104 // Handle optional hex prefix.
105 else if (b
== 16 && (!wcsncmp (ptr
, L
"0x", 2) || !wcsncmp (ptr
, L
"0X", 2)))
107 else if (b
== 2 && (!wcsncmp (ptr
, L
"0b", 2) || !wcsncmp (ptr
, L
"0B", 2)))
110 // Empty sequence conversion error
111 if (_isdigit (*ptr
, b
) < 0)
114 *endptr
= (wchar_t*)nptr
;
118 for (ret
= 0;; ptr
++)
120 signed char digit
= _isdigit (*ptr
, b
);
125 #if !defined(__SDCC_pic14) && !defined(__SDCC_pic16)
126 range_error
|= ckd_mul (&ret
, ret
, b
);
127 range_error
|= ckd_add (&ret
, ret
, digit
);
129 unsigned long int oldret
= ret
;
133 ret
+= (unsigned char)digit
;
134 #warning INEXACT RANGE ERROR CHECK WILL NOT REPORT ALL OVERFLOWS (fix by implementing ckd_mul support)
139 *endptr
= (wchar_t*)ptr
;
147 return (neg
? -ret
: ret
);