Browse Source

stdlib: formatting on the strto* functions

* Name the private function __strto_{int,fp} (with leading double
  underscores) to avoid name conflicts
* Fix comments of the wrong style
* Fix missing leading double underscores in stdlib_p.h
master
Lephenixnoir 8 months ago
parent
commit
3a9a60db78
Signed by: Lephenixnoir GPG Key ID: 1BBA026E13FC0495
  1. 58
      src/libc/stdlib/stdlib_p.h
  2. 18
      src/libc/stdlib/strto_fp.c
  3. 9
      src/libc/stdlib/strto_int.c
  4. 2
      src/libc/stdlib/strtod.c
  5. 2
      src/libc/stdlib/strtof.c
  6. 2
      src/libc/stdlib/strtol.c
  7. 2
      src/libc/stdlib/strtold.c
  8. 2
      src/libc/stdlib/strtoll.c
  9. 2
      src/libc/stdlib/strtoul.c
  10. 2
      src/libc/stdlib/strtoull.c

58
src/libc/stdlib/stdlib_p.h

@ -4,23 +4,25 @@
#include <stdlib.h>
#include <stdbool.h>
/* 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__*/

18
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();

9
src/libc/stdlib/strto_int.c

@ -4,14 +4,7 @@
#include <errno.h>
#include <limits.h>
/* 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 */

2
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;
}

2
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;
}

2
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;
}

2
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;
}

2
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;
}

2
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;
}

2
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;
}

Loading…
Cancel
Save