mirror of
https://github.com/arendst/Tasmota.git
synced 2025-08-07 01:47:43 +00:00
Add stable strtoull to core 2.3.0
Add stable strtoull to core 2.3.0
This commit is contained in:
parent
f455b738a5
commit
ce4e9b0e30
@ -108,7 +108,7 @@ void* memchr(const void* ptr, int value, size_t num)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://clc-wiki.net/wiki/C_standard_library:string.h:strspn
|
// http://clc-wiki.net/wiki/C_standard_library:string.h:strcspn
|
||||||
// Get span until any character in string
|
// Get span until any character in string
|
||||||
size_t strcspn(const char *str1, const char *str2)
|
size_t strcspn(const char *str1, const char *str2)
|
||||||
{
|
{
|
||||||
@ -124,88 +124,76 @@ size_t strcspn(const char *str1, const char *str2)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// https://opensource.apple.com/source/Libc/Libc-583/stdlib/FreeBSD/strtoull.c
|
||||||
* Convert a string to an unsigned long long integer.
|
// Convert a string to an unsigned long long integer
|
||||||
*
|
|
||||||
* Assumes that the upper and lower case
|
|
||||||
* alphabets and digits are each contiguous.
|
|
||||||
* https://opensource.apple.com/source/Libc/Libc-583/stdlib/FreeBSD/strtoull.c
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __LONG_LONG_MAX__
|
#ifndef __LONG_LONG_MAX__
|
||||||
#define __LONG_LONG_MAX__ 9223372036854775807LL
|
#define __LONG_LONG_MAX__ 9223372036854775807LL
|
||||||
#endif
|
#endif
|
||||||
#undef ULLONG_MAX
|
#ifndef ULLONG_MAX
|
||||||
#define ULLONG_MAX (__LONG_LONG_MAX__ * 2ULL + 1)
|
#define ULLONG_MAX (__LONG_LONG_MAX__ * 2ULL + 1)
|
||||||
|
#endif
|
||||||
|
|
||||||
unsigned long long strtoull(const char *__restrict nptr, char **__restrict endptr, int base)
|
unsigned long long strtoull(const char *__restrict nptr, char **__restrict endptr, int base)
|
||||||
{
|
{
|
||||||
const char *s;
|
const char *s = nptr;
|
||||||
unsigned long long acc;
|
char c;
|
||||||
char c;
|
do { c = *s++; } while (isspace((unsigned char)c)); // Trim leading spaces
|
||||||
unsigned long long cutoff;
|
|
||||||
int neg, any, cutlim;
|
|
||||||
|
|
||||||
/*
|
int neg = 0;
|
||||||
* See strtoq for comments as to the logic used.
|
if (c == '-') { // Set minus flag and/or skip sign
|
||||||
*/
|
neg = 1;
|
||||||
s = nptr;
|
c = *s++;
|
||||||
do {
|
} else {
|
||||||
c = *s++;
|
if (c == '+') {
|
||||||
} while (isspace((unsigned char)c));
|
c = *s++;
|
||||||
if (c == '-') {
|
}
|
||||||
neg = 1;
|
}
|
||||||
c = *s++;
|
|
||||||
} else {
|
|
||||||
neg = 0;
|
|
||||||
if (c == '+')
|
|
||||||
c = *s++;
|
|
||||||
}
|
|
||||||
if ((base == 0 || base == 16) &&
|
|
||||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
|
||||||
c = s[1];
|
|
||||||
s += 2;
|
|
||||||
base = 16;
|
|
||||||
}
|
|
||||||
if (base == 0)
|
|
||||||
base = c == '0' ? 8 : 10;
|
|
||||||
acc = any = 0;
|
|
||||||
if (base < 2 || base > 36)
|
|
||||||
goto noconv;
|
|
||||||
|
|
||||||
cutoff = ULLONG_MAX / base;
|
if ((base == 0 || base == 16) && (c == '0') && (*s == 'x' || *s == 'X')) { // Set Hexadecimal
|
||||||
cutlim = ULLONG_MAX % base;
|
c = s[1];
|
||||||
for ( ; ; c = *s++) {
|
s += 2;
|
||||||
if (c >= '0' && c <= '9')
|
base = 16;
|
||||||
c -= '0';
|
}
|
||||||
else if (c >= 'A' && c <= 'Z')
|
if (base == 0) { base = (c == '0') ? 8 : 10; } // Set Octal or Decimal
|
||||||
c -= 'A' - 10;
|
|
||||||
else if (c >= 'a' && c <= 'z')
|
unsigned long long acc = 0;
|
||||||
c -= 'a' - 10;
|
int any = 0;
|
||||||
else
|
if (base > 1 && base < 37) {
|
||||||
break;
|
unsigned long long cutoff = ULLONG_MAX / base;
|
||||||
if (c >= base)
|
int cutlim = ULLONG_MAX % base;
|
||||||
break;
|
for ( ; ; c = *s++) {
|
||||||
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
|
if (c >= '0' && c <= '9')
|
||||||
any = -1;
|
c -= '0';
|
||||||
else {
|
else if (c >= 'A' && c <= 'Z')
|
||||||
any = 1;
|
c -= 'A' - 10;
|
||||||
acc *= base;
|
else if (c >= 'a' && c <= 'z')
|
||||||
acc += c;
|
c -= 'a' - 10;
|
||||||
}
|
else
|
||||||
}
|
break;
|
||||||
if (any < 0) {
|
|
||||||
acc = ULLONG_MAX;
|
if (c >= base)
|
||||||
} else if (!any) {
|
break;
|
||||||
noconv:
|
|
||||||
uint8_t dummy = 0;
|
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
|
||||||
} else if (neg)
|
any = -1;
|
||||||
acc = -acc;
|
else {
|
||||||
if (endptr != nullptr)
|
any = 1;
|
||||||
*endptr = (char *)(any ? s - 1 : nptr);
|
acc *= base;
|
||||||
return (acc);
|
acc += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (any < 0) {
|
||||||
|
acc = ULLONG_MAX; // Range error
|
||||||
|
}
|
||||||
|
else if (any && neg) {
|
||||||
|
acc = -acc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (endptr != nullptr) { *endptr = (char *)(any ? s - 1 : nptr); }
|
||||||
|
|
||||||
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
|
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
|
||||||
|
|
||||||
// Get span until single character in string
|
// Get span until single character in string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user