feat(stdlib): numeric string conversions

This commit is contained in:
2026-09-03 21:53:59 -04:00
parent bcb4e259c3
commit 26786e9027
6 changed files with 2036 additions and 0 deletions
+96
View File
@@ -105,6 +105,102 @@ __attribute__((pure)) size_t
malloc_usable_size(void *ptr);
#endif /* VLIBC_LEVEL_GE(2) */
/* numeric conversions (todo 11) */
/*
* String-to-number conversions (C23 7.24.1). All are ISO C core / POSIX
* base and present in every profile.
*
* The ato* wrappers carry no error reporting: their behavior is undefined
* if the converted value cannot be represented (C23 7.24.1.1-2), so they
* read their input and nothing else — hence pure.
*
* The strto* functions report range errors through errno and store the
* scan position through endptr; both are real side effects, so they carry
* no intent attribute. errno behavior follows C23 7.24.1.4-5: ERANGE when
* the subject sequence is outside the representable range (the clamped
* maximum/minimum is returned); EINVAL when base is not 0 and not in
* [2, 36] (endptr left at nptr, 0 returned); endptr points at nptr when
* no subject sequence is present.
*/
/*
* Convert the initial decimal digits of nptr to int, discarding leading
* whitespace and an optional sign. Equivalent to (int)strtol(nptr, 0, 10).
* pure: reads memory, no side effects.
*/
__attribute__((pure)) int
atoi(const char *nptr);
/*
* As atoi, converted to long: strtol(nptr, 0, 10).
* pure: reads memory, no side effects.
*/
__attribute__((pure)) long
atol(const char *nptr);
/*
* As atoi, converted to long long: strtoll(nptr, 0, 10).
* pure: reads memory, no side effects.
*/
__attribute__((pure)) long long
atoll(const char *nptr);
/*
* As atoi, converted to double: strtod(nptr, 0).
* pure: reads memory, no side effects.
*/
__attribute__((pure)) double
atof(const char *nptr);
/*
* Convert the initial portion of nptr to long, stopping at the first
* character that is not part of the subject sequence and storing its
* position in *endptr (if endptr is not NULL). See the family comment for
* base, errno, and endptr semantics.
*/
long
strtol(const char *restrict nptr, char **restrict endptr, int base);
/*
* As strtol, converted to unsigned long. A subject sequence with a minus
* sign yields the negated value computed in the return type (modulo
* ULONG_MAX + 1) without a range error (C23 7.24.1.4p8).
*/
unsigned long
strtoul(const char *restrict nptr, char **restrict endptr, int base);
/*
* As strtol, converted to long long.
*/
long long
strtoll(const char *restrict nptr, char **restrict endptr, int base);
/*
* As strtol, converted to unsigned long long. Negative subject sequences
* wrap modulo ULLONG_MAX + 1 as for strtoul.
*/
unsigned long long
strtoull(const char *restrict nptr, char **restrict endptr, int base);
/*
* Convert the initial portion of nptr to float, double, or long double
* (C23 7.24.1.3): optional whitespace, optional sign, then either an
* "inf"/"infinity" or "nan"/"nan(n-char-sequence)" subject (case-
* insensitive), a hexadecimal floating subject ("0x1.8p1"), or a decimal
* floating subject with optional exponent. Overflow returns ±HUGE_VAL*
* with errno ERANGE; results too small to represent return a subnormal or
* zero value with errno ERANGE. no-conversion stores nptr in *endptr.
*/
double
strtod(const char *restrict nptr, char **restrict endptr);
float
strtof(const char *restrict nptr, char **restrict endptr);
long double
strtold(const char *restrict nptr, char **restrict endptr);
#ifdef __cplusplus
}
#endif