struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / strtoull.c
blob63bbf0e322ada9631aa6263981e3a618661d4496
1 /*---------------------------------------------------------------------
2 strtoull() - convert a 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>
41 #if 0 // Bug 3798
42 static signed char _isdigit(const char c, unsigned char base)
44 unsigned char v;
46 if (c >= '0' && c <= '9')
47 v = c - '0';
48 else if (c >= 'a' && c <='z')
49 v = c - 'a' + 10;
50 else if (c >= 'A' && c <='Z')
51 v = c - '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 long int strtoull(const char *nptr, char **endptr, int base)
65 const char *ptr = nptr;
66 unsigned long long int ret;
67 bool range_error = false;
68 bool neg = false;
69 unsigned char b = base;
71 while (isblank (*ptr))
72 ptr++;
74 // Handle sign.
75 switch(*ptr)
77 case '-':
78 neg = true;
79 case '+':
80 ptr++;
83 // base not specified.
84 if (!b)
86 if (!strncmp (ptr, "0x", 2) || !strncmp (ptr, "0X", 2))
88 b = 16;
89 ptr += 2;
91 else if (!strncmp (ptr, "0b", 2) || !strncmp (ptr, "0B", 2))
93 b = 2;
94 ptr += 2;
96 else if (*ptr == '0')
98 b = 8;
99 ptr++;
101 else
102 b = 10;
104 // Handle optional hex prefix.
105 else if (b == 16 && (!strncmp (ptr, "0x", 2) || !strncmp (ptr, "0X", 2)))
106 ptr += 2;
107 else if (b == 2 && (!strncmp (ptr, "0b", 2) || !strncmp (ptr, "0B", 2)))
108 ptr += 2;
110 // Empty sequence conversion error
111 if (_isdigit (*ptr, b) < 0)
113 if (endptr)
114 *endptr = (char*)nptr;
115 return (0);
118 for (ret = 0;; ptr++)
120 signed char digit = _isdigit (*ptr, b);
122 if (digit < 0)
123 break;
125 range_error |= ckd_mul (&ret, ret, b);
126 range_error |= ckd_add (&ret, ret, digit);
128 ret += (unsigned char)digit;
131 if (endptr)
132 *endptr = (char*)ptr;
134 if (range_error)
136 errno = ERANGE;
137 return (ULLONG_MAX);
140 return (neg ? -ret : ret);
142 #endif