diff --git a/Library/Convert.c b/Library/Convert.c index c59edbcb..f296207b 100644 --- a/Library/Convert.c +++ b/Library/Convert.c @@ -1,6 +1,7 @@ #include #include +#include #include // TODO: Replace mem* with assembly code @@ -451,6 +452,96 @@ char *strdup(const char *String) return OutBuffer; } +int isalpha(int c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +int isupper(int c) +{ + return (c >= 'A' && c <= 'Z'); +} + +long int strtol(const char *str, char **endptr, int base) +{ + const char *s; + long acc, cutoff; + int c; + int neg, any, cutlim; + + s = str; + do + { + c = *s++; + } while (isspace(c)); + if (c == '-') + { + neg = 1; + c = *s++; + } + else + { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) + { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + cutoff = neg ? LONG_MIN : LONG_MAX; + cutlim = cutoff % base; + cutoff /= base; + for (acc = 0, any = 0;; c = *s++) + { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else + { + any = 1; + acc *= base; + acc += c; + } + } + if (any < 0) + { + acc = neg ? LONG_MIN : LONG_MAX; + } + else if (neg) + acc = -acc; + if (endptr != 0) + *endptr = (char *)(any ? s - 1 : str); + return (acc); + + // long int result = 0; + // int sign = 1; + // if (*str == '-') + // { + // sign = -1; + // str++; + // } + // while (*str) + // { + // result *= base; + // result += *str - '0'; + // str++; + // } + // return result * sign; +} + int isdigit(int c) { return c >= '0' && c <= '9'; diff --git a/include/convert.h b/include/convert.h index dcc290a1..ec887912 100644 --- a/include/convert.h +++ b/include/convert.h @@ -8,6 +8,8 @@ extern "C" int isdigit(int c); int isspace(int c); int isempty(char *str); + int isalpha(int c); + int isupper(int c); unsigned int isdelim(char c, char *delim); int abs(int i); void swap(char *x, char *y); @@ -40,6 +42,7 @@ extern "C" int strncasecmp(const char *lhs, const char *rhs, long unsigned int Count); int strcasecmp(const char *lhs, const char *rhs); char *strtok(char *src, const char *delim); + long int strtol(const char *str, char **endptr, int base); void *__memcpy_chk(void *dest, const void *src, size_t len, size_t slen); void *__memset_chk(void *dest, int val, size_t len, size_t slen);