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
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 #ifdef __SDCC_LONGLONG
36 #include <stdckdint.h>
43 static signed char _isdigit(const wchar_t c
, unsigned char base
)
47 if (c
>= L
'0' && c
<= L
'9')
49 else if (c
>= L
'a' && c
<= L
'z')
51 else if (c
>= L
'A' && c
<= L
'Z')
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;
70 unsigned char b
= base
;
72 while (iswblank (*ptr
))
84 // base not specified.
87 if (!wcsncmp (ptr
, L
"0x", 2) || !wcsncmp (ptr
, L
"0X", 2))
92 else if (!wcsncmp (ptr
, L
"0b", 2) || !wcsncmp (ptr
, L
"0B", 2))
97 else if (*ptr
== L
'0')
105 // Handle optional hex prefix.
106 else if (b
== 16 && (!wcsncmp (ptr
, L
"0x", 2) || !wcsncmp (ptr
, L
"0X", 2)))
108 else if (b
== 2 && (!wcsncmp (ptr
, L
"0b", 2) || !wcsncmp (ptr
, L
"0B", 2)))
111 // Empty sequence conversion error
112 if (_isdigit (*ptr
, b
) < 0)
115 *endptr
= (wchar_t*)nptr
;
119 for (ret
= 0;; ptr
++)
121 signed char digit
= _isdigit (*ptr
, b
);
126 range_error
|= ckd_mul(&ret
, ret
, b
);
127 range_error
|= ckd_add (&ret
, ret
, digit
);
129 ret
+= (unsigned char)digit
;
133 *endptr
= (wchar_t*)ptr
;
141 return (neg
? -ret
: ret
);