mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 10:48:29 +00:00
Fix strtol
This commit is contained in:
parent
1df136323b
commit
5eddadafbd
17 changed files with 83 additions and 105 deletions
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
/**
|
||||
|
@ -29,5 +28,9 @@
|
|||
* flexibility in terms of inputs
|
||||
*/
|
||||
int atoi(const char *s) {
|
||||
return STRLOL(s, NULL, 10, INT_MIN, INT_MAX);
|
||||
int res;
|
||||
res = strtoimax(s, NULL, 10);
|
||||
if (res < INT_MIN) return INT_MIN;
|
||||
if (res > INT_MAX) return INT_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -17,9 +17,12 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
long atol(const char *s) {
|
||||
return STRLOL(s, NULL, 10, LONG_MIN, LONG_MAX);
|
||||
long res;
|
||||
res = strtoimax(s, NULL, 10);
|
||||
if (res < LONG_MIN) return LONG_MIN;
|
||||
if (res > LONG_MAX) return LONG_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -17,9 +17,12 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
long long atoll(const char *s) {
|
||||
return STRLOL(s, NULL, 10, LONG_LONG_MIN, LONG_LONG_MAX);
|
||||
long long res;
|
||||
res = strtoimax(s, NULL, 10);
|
||||
if (res < LONG_LONG_MIN) return LONG_LONG_MIN;
|
||||
if (res > LONG_LONG_MAX) return LONG_LONG_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_COMPAT_STRLOL_H_
|
||||
#define COSMOPOLITAN_LIBC_COMPAT_STRLOL_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define STRLOL(STR, ENDPTR, OPTIONAL_BASE, MIN, MAX) \
|
||||
({ \
|
||||
intmax_t res = strtoimax(STR, ENDPTR, OPTIONAL_BASE); \
|
||||
if (res < MIN) return MIN; \
|
||||
if (res > MAX) return MAX; \
|
||||
res; \
|
||||
})
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_COMPAT_STRLOL_H_ */
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
/**
|
||||
|
@ -26,5 +25,9 @@
|
|||
* @param optional_base is recommended as 0 for flexidecimal
|
||||
*/
|
||||
long strtol(const char *s, char **opt_out_end, int optional_base) {
|
||||
return STRLOL(s, opt_out_end, optional_base, LONG_MIN, LONG_MAX);
|
||||
long res;
|
||||
res = strtoimax(s, opt_out_end, optional_base);
|
||||
if (res < LONG_MIN) return LONG_MIN;
|
||||
if (res > LONG_MAX) return LONG_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -17,9 +17,12 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
long long strtoll(const char *s, char **endptr, int optional_base) {
|
||||
return STRLOL(s, endptr, optional_base, LONG_LONG_MIN, LONG_LONG_MAX);
|
||||
long long res;
|
||||
res = strtoimax(s, endptr, optional_base);
|
||||
if (res < LONG_LONG_MIN) return LONG_LONG_MIN;
|
||||
if (res > LONG_LONG_MAX) return LONG_LONG_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -17,9 +17,12 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
unsigned long strtoul(const char *s, char **endptr, int optional_base) {
|
||||
return STRLOL(s, endptr, optional_base, ULONG_MIN, ULONG_MAX);
|
||||
unsigned long long res;
|
||||
res = strtoimax(s, endptr, optional_base);
|
||||
if (res < ULONG_MIN) return ULONG_MIN;
|
||||
if (res > ULONG_MAX) return ULONG_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -17,9 +17,12 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/strlol.internal.h"
|
||||
#include "libc/limits.h"
|
||||
|
||||
unsigned long long strtoull(const char *s, char **endptr, int optional_base) {
|
||||
return STRLOL(s, endptr, optional_base, ULONG_LONG_MIN, ULONG_LONG_MAX);
|
||||
unsigned long long res;
|
||||
res = strtoimax(s, endptr, optional_base);
|
||||
if (res < ULONG_LONG_MIN) return ULONG_LONG_MIN;
|
||||
if (res > ULONG_LONG_MAX) return ULONG_LONG_MAX;
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue