diff --git a/src/libc/stdlib/stdlib_p.h b/src/libc/stdlib/stdlib_p.h index 8d28f12..5831031 100644 --- a/src/libc/stdlib/stdlib_p.h +++ b/src/libc/stdlib/stdlib_p.h @@ -4,23 +4,25 @@ #include #include -/* Parse an integer from a string. This is the base function for strtol, - strtoul, strtoll, and strtoull. - - This function does not set errno, and instead returns the error code - according to conversion rules. Setting errno is troublesome because it's a - global state that cannot be reverted and thus cannot be tested. - - If outl is non-NULL, strto_int produces a long or an unsigned long result - (depending on use_unsigned). Signedness only affects the range of values - that are considered to be ERANGE, and both results are stored in *outl. - Similarly, if outll is non-NULL, strto_int produces a long long or unsigned - long long result. Only one pointer should be non-NULL. - - On platforms where long is 32-bit, 64-bit operations are performed only if - outll is non-NULL. This is because multiplications with overflow can be - expensive. */ -int strto_int( +/* +** Parse an integer from a string. This is the base function for strtol, +** strtoul, strtoll, and strtoull. +** +** This function does not set errno, and instead returns the error code +** according to conversion rules. Setting errno is troublesome because it's a +** global state that cannot be reverted and thus cannot be tested. +** +** If outl is non-NULL, strto_int produces a long or an unsigned long result +** (depending on use_unsigned). Signedness only affects the range of values +** that are considered to be ERANGE, and both results are stored in *outl. +** Similarly, if outll is non-NULL, strto_int produces a long long or unsigned +** long long result. Only one pointer should be non-NULL. +** +** On platforms where long is 32-bit, 64-bit operations are performed only if +** outll is non-NULL. This is because multiplications with overflow can be +** expensive. +*/ +int __strto_int( char const * restrict __ptr, char ** restrict __endptr, int __base, @@ -28,17 +30,19 @@ int strto_int( long long *__outll, bool __use_unsigned); -/* Parse a floating-point value from a string. This is the base function for - strtod, strtof, and strtold. - - This function is similar to strto_int(). If returns the error code to set in - errno, and can produce one of three outputs depending on which of out, outf - and outl is set. */ -int strto_fp( +/* +** Parse a floating-point value from a string. This is the base function for +** strtod, strtof, and strtold. +** +** This function is similar to strto_int(). If returns the error code to set in +** errno, and can produce one of three outputs depending on which of out, outf +** and outl is set. +*/ +int __strto_fp( char const * restrict __ptr, char ** restrict __endptr, - double *out, - float *outf, - long double *outl); + double *__out, + float *__outf, + long double *__outl); #endif /*__STDLIB_P_H__*/ diff --git a/src/libc/stdlib/strto_fp.c b/src/libc/stdlib/strto_fp.c index b5522bb..d9b6cef 100644 --- a/src/libc/stdlib/strto_fp.c +++ b/src/libc/stdlib/strto_fp.c @@ -27,16 +27,6 @@ # error long double larger than 128 bits is not supported #endif -/* Basically strncasecmp. */ -static int ncasecmp(char const *left, char const *right, size_t n) -{ - for(size_t i = 0; i < n; i++) { - int diff = tolower(left[i]) - tolower(right[i]); - if(diff) return diff; - } - return 0; -} - /* ** Parse digits and exponent into integers, in decimal or hexadecimal notation. ** @@ -122,7 +112,7 @@ static void parse_digits(char const * restrict *ptr0, bool *valid, *valid = true; } -int strto_fp(char const * restrict ptr, char ** restrict endptr, double *out, +int __strto_fp(char const * restrict ptr, char ** restrict endptr, double *out, float *outf, long double *outl) { /* Save the value of ptr in endptr, in case format is invalid */ @@ -145,7 +135,7 @@ int strto_fp(char const * restrict ptr, char ** restrict endptr, double *out, if(outl) *outl = 0.0l; /* NaN possibly with an argument */ - if(!ncasecmp(ptr, "nan", 3)) { + if(!strncasecmp(ptr, "nan", 3)) { char const *arg = ""; ptr += 3; if(ptr[0] == '(') { @@ -159,13 +149,13 @@ int strto_fp(char const * restrict ptr, char ** restrict endptr, double *out, if(outl) *outl = __builtin_nanl(arg); } /* Infinity */ - else if(!ncasecmp(ptr, "infinity", 8)) { + else if(!strncasecmp(ptr, "infinity", 8)) { if(out) *out = __builtin_inf(); if(outf) *outf = __builtin_inff(); if(outl) *outl = __builtin_infl(); ptr += 8; } - else if(!ncasecmp(ptr, "inf", 3)) { + else if(!strncasecmp(ptr, "inf", 3)) { if(out) *out = __builtin_inf(); if(outf) *outf = __builtin_inff(); if(outl) *outl = __builtin_infl(); diff --git a/src/libc/stdlib/strto_int.c b/src/libc/stdlib/strto_int.c index ae4c9bd..8b6d298 100644 --- a/src/libc/stdlib/strto_int.c +++ b/src/libc/stdlib/strto_int.c @@ -4,14 +4,7 @@ #include #include -/* Parse an integer from a string. Base function for strtol, strtoul, strtoll, - and strtoull. This function: - -> Does not set errno, and instead return the potential error code. Setting - errno would prevent these functions from calling each other as they all - have different ranges, resulting in undue ERANGE. - -> Can parse into both long and long long, depending on what pointer is - non-NULL. */ -int strto_int(char const * restrict ptr, char ** restrict endptr, int base, +int __strto_int(char const * restrict ptr, char ** restrict endptr, int base, long *outl, long long *outll, bool use_unsigned) { /* Save the value of ptr in endptr now in case the format is invalid */ diff --git a/src/libc/stdlib/strtod.c b/src/libc/stdlib/strtod.c index 80dede8..80716fd 100644 --- a/src/libc/stdlib/strtod.c +++ b/src/libc/stdlib/strtod.c @@ -4,7 +4,7 @@ double strtod(char const * restrict ptr, char ** restrict endptr) { double d = 0; - int err = strto_fp(ptr, endptr, &d, NULL, NULL); + int err = __strto_fp(ptr, endptr, &d, NULL, NULL); if(err != 0) errno = err; return d; } diff --git a/src/libc/stdlib/strtof.c b/src/libc/stdlib/strtof.c index 476005d..271aed0 100644 --- a/src/libc/stdlib/strtof.c +++ b/src/libc/stdlib/strtof.c @@ -4,7 +4,7 @@ float strtof(char const * restrict ptr, char ** restrict endptr) { float f = 0; - int err = strto_fp(ptr, endptr, NULL, &f, NULL); + int err = __strto_fp(ptr, endptr, NULL, &f, NULL); if(err != 0) errno = err; return f; } diff --git a/src/libc/stdlib/strtol.c b/src/libc/stdlib/strtol.c index d0cb402..b7da918 100644 --- a/src/libc/stdlib/strtol.c +++ b/src/libc/stdlib/strtol.c @@ -4,7 +4,7 @@ long int strtol(char const * restrict ptr, char ** restrict endptr, int base) { long n = 0; - int err = strto_int(ptr, endptr, base, &n, NULL, false); + int err = __strto_int(ptr, endptr, base, &n, NULL, false); if(err != 0) errno = err; return n; } diff --git a/src/libc/stdlib/strtold.c b/src/libc/stdlib/strtold.c index bcb137c..fcb8474 100644 --- a/src/libc/stdlib/strtold.c +++ b/src/libc/stdlib/strtold.c @@ -4,7 +4,7 @@ long double strtold(char const * restrict ptr, char ** restrict endptr) { long double ld = 0; - int err = strto_fp(ptr, endptr, NULL, NULL, &ld); + int err = __strto_fp(ptr, endptr, NULL, NULL, &ld); if(err != 0) errno = err; return ld; } diff --git a/src/libc/stdlib/strtoll.c b/src/libc/stdlib/strtoll.c index 5fc2d5c..ad1c972 100644 --- a/src/libc/stdlib/strtoll.c +++ b/src/libc/stdlib/strtoll.c @@ -5,7 +5,7 @@ long long int strtoll(char const * restrict ptr, char ** restrict endptr, int base) { long long n = 0; - int err = strto_int(ptr, endptr, base, NULL, &n, false); + int err = __strto_int(ptr, endptr, base, NULL, &n, false); if(err != 0) errno = err; return n; } diff --git a/src/libc/stdlib/strtoul.c b/src/libc/stdlib/strtoul.c index 75c5ef0..2a6294b 100644 --- a/src/libc/stdlib/strtoul.c +++ b/src/libc/stdlib/strtoul.c @@ -5,7 +5,7 @@ unsigned long int strtoul(char const * restrict ptr, char ** restrict endptr, int base) { unsigned long n = 0; - int err = strto_int(ptr, endptr, base, (long *)&n, NULL, true); + int err = __strto_int(ptr, endptr, base, (long *)&n, NULL, true); if(err != 0) errno = err; return n; } diff --git a/src/libc/stdlib/strtoull.c b/src/libc/stdlib/strtoull.c index 791c73d..d643947 100644 --- a/src/libc/stdlib/strtoull.c +++ b/src/libc/stdlib/strtoull.c @@ -5,7 +5,7 @@ unsigned long long int strtoull(char const * restrict ptr, char ** restrict endptr, int base) { unsigned long long n = 0; - int err = strto_int(ptr, endptr, base, NULL, (long long *)&n, true); + int err = __strto_int(ptr, endptr, base, NULL, (long long *)&n, true); if(err != 0) errno = err; return n; }