Add stable strtoull to core 2.3.0

Add stable strtoull to core 2.3.0
This commit is contained in:
Theo Arends 2019-04-04 17:52:14 +02:00
parent f455b738a5
commit ce4e9b0e30

View File

@ -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,57 +124,43 @@ 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;
unsigned long long cutoff; do { c = *s++; } while (isspace((unsigned char)c)); // Trim leading spaces
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
*/
s = nptr;
do {
c = *s++;
} while (isspace((unsigned char)c));
if (c == '-') {
neg = 1; neg = 1;
c = *s++; c = *s++;
} else { } else {
neg = 0; if (c == '+') {
if (c == '+')
c = *s++; c = *s++;
} }
if ((base == 0 || base == 16) && }
c == '0' && (*s == 'x' || *s == 'X')) {
if ((base == 0 || base == 16) && (c == '0') && (*s == 'x' || *s == 'X')) { // Set Hexadecimal
c = s[1]; c = s[1];
s += 2; s += 2;
base = 16; base = 16;
} }
if (base == 0) if (base == 0) { base = (c == '0') ? 8 : 10; } // Set Octal or Decimal
base = c == '0' ? 8 : 10;
acc = any = 0;
if (base < 2 || base > 36)
goto noconv;
cutoff = ULLONG_MAX / base; unsigned long long acc = 0;
cutlim = ULLONG_MAX % base; int any = 0;
if (base > 1 && base < 37) {
unsigned long long cutoff = ULLONG_MAX / base;
int cutlim = ULLONG_MAX % base;
for ( ; ; c = *s++) { for ( ; ; c = *s++) {
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
c -= '0'; c -= '0';
@ -184,8 +170,10 @@ unsigned long long strtoull(const char *__restrict nptr, char **__restrict endpt
c -= 'a' - 10; c -= 'a' - 10;
else else
break; break;
if (c >= base) if (c >= base)
break; break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1; any = -1;
else { else {
@ -195,17 +183,17 @@ unsigned long long strtoull(const char *__restrict nptr, char **__restrict endpt
} }
} }
if (any < 0) { if (any < 0) {
acc = ULLONG_MAX; acc = ULLONG_MAX; // Range error
} else if (!any) { }
noconv: else if (any && neg) {
uint8_t dummy = 0;
} else if (neg)
acc = -acc; acc = -acc;
if (endptr != nullptr) }
*endptr = (char *)(any ? s - 1 : nptr);
return (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