From 3fc80a9a28d5c9d7d66c7872e6e1de0967bc52aa Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 20 Jan 2020 20:01:35 +0100 Subject: [PATCH] up --- kaleid/libc/strtol.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/kaleid/libc/strtol.c b/kaleid/libc/strtol.c index fd465c4..09354d5 100644 --- a/kaleid/libc/strtol.c +++ b/kaleid/libc/strtol.c @@ -23,17 +23,21 @@ //----------------------------------------------------------------------------// #include +#include long strtol(const char *str, char **endp, int base) { - char c; ulong n; bool neg = 0; const char *save, *start = str; + + assert(str != NULL); + + char c = *str; // Ignore leading spaces - do { + while (isspace(c)) { c = *str++; - } while (isspace(c)); + } // Accept any +/-'s, whatever the base // In particular we accept things like "-0xF" @@ -100,13 +104,19 @@ long strtol(const char *str, char **endp, int base) { // Made from the code above by removing "neg" // ulong strtoul(const char *str, char **endp, int base) { - char c; ulong n; const char *save, *start = str; + + assert(str != NULL); - do { + char c = *str; + + while (isspace(c)) { + c = *str++; + } + + if (c == '+') c = *str++; - } while (isspace(c)); if (c == '0') { c = *str++; @@ -142,10 +152,9 @@ ulong strtoul(const char *str, char **endp, int base) { n = (n * base) + c; } } - if (endp != NULL) *endp = (char *)(str == save ? start : str); - return n; + return (long)n; }