diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-02-27 19:17:39 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-02-27 19:17:39 -0500 |
commit | e1477898d0203dae4b2fe9ad62998bc27d4b4fc9 (patch) | |
tree | bda9ccc11307e41de7c5a4e6e4e92088fe4b1804 | |
parent | fcb83049b1942fafa784fc51869818651c04acbb (diff) |
add common _strtoi function body
-rw-r--r-- | src/inttypes/strtoimax.c | 17 | ||||
-rw-r--r-- | src/inttypes/strtoumax.c | 13 | ||||
-rw-r--r-- | src/inttypes/wcstoimax.c | 17 | ||||
-rw-r--r-- | src/inttypes/wcstoumax.c | 13 | ||||
-rw-r--r-- | src/stdlib/_strtoi.h | 43 | ||||
-rw-r--r-- | src/stdlib/atoll.c | 2 | ||||
-rw-r--r-- | src/stdlib/strtol.c | 24 | ||||
-rw-r--r-- | src/stdlib/strtoll.c | 15 | ||||
-rw-r--r-- | src/stdlib/strtoul.c | 20 | ||||
-rw-r--r-- | src/stdlib/strtoull.c | 13 | ||||
-rw-r--r-- | src/wchar/wcstol.c | 11 | ||||
-rw-r--r-- | src/wchar/wcstoll.c | 9 | ||||
-rw-r--r-- | src/wchar/wcstoul.c | 11 | ||||
-rw-r--r-- | src/wchar/wcstoull.c | 9 |
14 files changed, 137 insertions, 80 deletions
diff --git a/src/inttypes/strtoimax.c b/src/inttypes/strtoimax.c index 5a68c47c..e473c210 100644 --- a/src/inttypes/strtoimax.c +++ b/src/inttypes/strtoimax.c @@ -1,19 +1,16 @@ +#include "stddef.h" #include <inttypes.h> +#include "errno.h" intmax_t strtoimax(const char * restrict nptr, char ** restrict endptr, int base) { - /* like strotoll */ + intmax_t ret = 0; + intmax_t max = INTMAX_MAX; + intmax_t min = INTMAX_MIN; - /* if > INTMAX_MAX */ - /* errno = ERANGE; */ - /* return INTMAX_MAX */ + #include "../stdlib/_strtoi.h" - /* if < INTMAX_MIN */ - /* errno = ERANGE; */ - /* return INTMAX_MIN; */ - - /* if no conversion */ - return 0; + return ret; } /* diff --git a/src/inttypes/strtoumax.c b/src/inttypes/strtoumax.c index bc286cc0..60aec599 100644 --- a/src/inttypes/strtoumax.c +++ b/src/inttypes/strtoumax.c @@ -1,15 +1,16 @@ +#include "stddef.h" #include <inttypes.h> +#include "errno.h" uintmax_t strtoumax(const char *restrict nptr, char ** restrict endptr, int base) { - /* like strotull */ + uintmax_t ret = 0; + uintmax_t max = UINTMAX_MAX; + uintmax_t min = 0; - /* if > UINTMAX_MAX */ - /* errno = ERANGE; */ - /* return UINTMAX_MAX */ + #include "../stdlib/_strtoi.h" - /* if no conversion */ - return 0; + return ret; } /* diff --git a/src/inttypes/wcstoimax.c b/src/inttypes/wcstoimax.c index 12c9a850..d086ede4 100644 --- a/src/inttypes/wcstoimax.c +++ b/src/inttypes/wcstoimax.c @@ -1,19 +1,16 @@ +#include "stddef.h" #include <inttypes.h> +#include "errno.h" intmax_t wcstoimax(const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base) { - /* like wcstoll */ + intmax_t ret = 0; + intmax_t max = INTMAX_MAX; + intmax_t min = INTMAX_MIN; - /* if > INTMAX_MAX */ - /* errno = ERANGE; */ - /* return INTMAX_MAX */ + #include "../stdlib/_strtoi.h" - /* if < INTMAX_MIN */ - /* errno = ERANGE; */ - /* return INTMAX_MIN; */ - - /* if no conversion */ - return 0; + return ret; } /* diff --git a/src/inttypes/wcstoumax.c b/src/inttypes/wcstoumax.c index 8516da08..e35846ea 100644 --- a/src/inttypes/wcstoumax.c +++ b/src/inttypes/wcstoumax.c @@ -1,15 +1,16 @@ +#include "stddef.h" #include <inttypes.h> +#include "errno.h" uintmax_t wcstoumax(const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base) { - /* like wcstoll */ + uintmax_t ret = 0; + uintmax_t max = UINTMAX_MAX; + uintmax_t min = 0; - /* if > UINTMAX_MAX */ - /* errno = ERANGE; */ - /* return UINTMAX_MAX */ + #include "../stdlib/_strtoi.h" - /* if no conversion */ - return 0; + return ret; } /* diff --git a/src/stdlib/_strtoi.h b/src/stdlib/_strtoi.h new file mode 100644 index 00000000..7a2d5cdf --- /dev/null +++ b/src/stdlib/_strtoi.h @@ -0,0 +1,43 @@ + /* int iswide = (sizeof(*nptr) == sizeof(wchar_t)); */ + (void)max; + + if (base == 0) { + /* determine base from prefix */ + } + + if (min == 0) { + /* unsigned */ + } + + while (*nptr) { + int n = 0; + /* int c = iswide ? wctomb(*nptr) : *nptr; */ + + switch (*nptr) { + case '0': n = 0; break; + case '1': n = 1; break; + case '2': n = 2; break; + case '3': n = 3; break; + case '4': n = 4; break; + case '5': n = 5; break; + case '6': n = 6; break; + case '7': n = 7; break; + case '8': n = 8; break; + case '9': n = 9; break; + case 'a': case 'A': n = 0xa; break; + case 'b': case 'B': n = 0xb; break; + case 'c': case 'C': n = 0xc; break; + case 'd': case 'D': n = 0xd; break; + case 'e': case 'E': n = 0xe; break; + case 'f': case 'F': n = 0xf; break; + default: n = -1; break; + } + + if (n >= base || n < 0) { + *endptr = (void*)nptr; + break; + } + + ret = (ret * base) + n; + nptr++; + } diff --git a/src/stdlib/atoll.c b/src/stdlib/atoll.c index bc098876..0e79ef49 100644 --- a/src/stdlib/atoll.c +++ b/src/stdlib/atoll.c @@ -2,7 +2,7 @@ long long int atoll(const char *nptr) { - return strtoll(str, (char**)NULL, 10); + return strtoll(nptr, (char**)NULL, 10); } /* diff --git a/src/stdlib/strtol.c b/src/stdlib/strtol.c index 247ac5de..bec5f9d2 100644 --- a/src/stdlib/strtol.c +++ b/src/stdlib/strtol.c @@ -2,31 +2,17 @@ #include "limits.h" #include "errno.h" -#if defined __STDC_VERSION__ && 199912L <= __STDC_VERSION__ -#include "inttypes.h" -#else -typedef long intmax_t; -#define strtoimax(n, e, b) ((long)n & (long)e & (long)b) ? 0 : 0 -#endif - /** convert string to long integer **/ long int strtol(const char * restrict nptr, char ** restrict endptr, int base) { - /* FIXME: forward dependency on 9899-1999 */ - intmax_t ret = strtoimax(nptr, endptr, base); - - if (ret < LONG_MIN) { - ret = LONG_MIN; - errno = ERANGE; /* converted value out of range */ - } + long int ret = 0; + long int max = LONG_MAX; + long int min = LONG_MIN; - if (ret > LONG_MAX) { - ret = LONG_MAX; - errno = ERANGE; /* converted value out of range */ - } + #include "_strtoi.h" - return (long int)ret; + return ret; } /*** diff --git a/src/stdlib/strtoll.c b/src/stdlib/strtoll.c index 2f40229c..b0ea0f41 100644 --- a/src/stdlib/strtoll.c +++ b/src/stdlib/strtoll.c @@ -1,13 +1,16 @@ #include <stdlib.h> +#include "limits.h" +#include "errno.h" long long int strtoll(const char * restrict nptr, char ** restrict endptr, int base) { - intmax_t ret = strtoimax(nptr, endptr, base); - if (ret < LLONG_MIN) { - } - if (ret > LLONG_MAX) { - } - return (long long int)ret; + long long int ret = 0; + long long int max = LLONG_MAX; + long long int min = LLONG_MIN; + + #include "_strtoi.h" + + return ret; } /* diff --git a/src/stdlib/strtoul.c b/src/stdlib/strtoul.c index 9a00910b..414b5897 100644 --- a/src/stdlib/strtoul.c +++ b/src/stdlib/strtoul.c @@ -2,25 +2,17 @@ #include "errno.h" #include "limits.h" -#if defined __STDC_VERSION__ && 199912L <= __STDC_VERSION__ -#include "inttypes.h" -#else -typedef unsigned long uintmax_t; -#define strtoumax(n, e, b) ((long)n & (long)e & (long)b ? 0 : 0) -#endif - /** convert string to unsigned long integer **/ + unsigned long int strtoul(const char * nptr, char ** endptr, int base) { - /* FIXME: forward dependency on 9899-1999 */ - uintmax_t ret = strtoumax(nptr, endptr, base); + unsigned long int ret = 0; + unsigned long int max = ULONG_MAX; + unsigned long int min = 0; - if (ret > ULONG_MAX) { - ret = ULONG_MAX; - errno = ERANGE; /* converted value too large */ - } + #include "_strtoi.h" - return (unsigned long int)ret; + return ret; } /*** diff --git a/src/stdlib/strtoull.c b/src/stdlib/strtoull.c index c95bac77..d655c136 100644 --- a/src/stdlib/strtoull.c +++ b/src/stdlib/strtoull.c @@ -1,11 +1,16 @@ #include <stdlib.h> +#include "limits.h" +#include "errno.h" unsigned long long int strtoull(const char * restrict nptr, char ** restrict endptr, int base) { - uintmax_t ret = strtoumax(nptr, endptr, base); - if (ret > ULLONG_MAX) { - } - return (unsigned long long int)ret; + unsigned long long int ret = 0; + unsigned long long int max = ULLONG_MAX; + unsigned long long int min = 0; + + #include "_strtoi.h" + + return ret; } /* diff --git a/src/wchar/wcstol.c b/src/wchar/wcstol.c index 5d4a605d..d349ee9c 100644 --- a/src/wchar/wcstol.c +++ b/src/wchar/wcstol.c @@ -1,9 +1,16 @@ #include <wchar.h> +#include "limits.h" +#include "errno.h" long int wcstol(const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base) { - (void)nptr; (void)endptr; (void)base; - return 0L; + long int ret = 0; + long int max = LONG_MAX; + long int min = LONG_MIN; + + #include "../stdlib/_strtoi.h" + + return ret; } /* diff --git a/src/wchar/wcstoll.c b/src/wchar/wcstoll.c index fc231433..60c4a475 100644 --- a/src/wchar/wcstoll.c +++ b/src/wchar/wcstoll.c @@ -1,7 +1,16 @@ #include <wchar.h> +#include "limits.h" +#include "errno.h" long long int wcstoll(const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base) { + long long int ret = 0; + long long int max = LLONG_MAX; + long long int min = LLONG_MIN; + + #include "../stdlib/_strtoi.h" + + return ret; } /* diff --git a/src/wchar/wcstoul.c b/src/wchar/wcstoul.c index 8fcaeb8e..f8373c05 100644 --- a/src/wchar/wcstoul.c +++ b/src/wchar/wcstoul.c @@ -1,9 +1,16 @@ #include <wchar.h> +#include "limits.h" +#include "errno.h" unsigned long int wcstoul(const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base) { - (void)nptr; (void)endptr; (void)base; - return 0UL; + unsigned long int ret = 0; + unsigned long int max = ULONG_MAX; + unsigned long int min = 0; + + #include "../stdlib/_strtoi.h" + + return ret; } /* diff --git a/src/wchar/wcstoull.c b/src/wchar/wcstoull.c index 92e12e59..632ff2e9 100644 --- a/src/wchar/wcstoull.c +++ b/src/wchar/wcstoull.c @@ -1,7 +1,16 @@ #include <wchar.h> +#include "limits.h" +#include "errno.h" unsigned long long int wcstoull(const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base) { + unsigned long long int ret = 0; + unsigned long long int max = ULLONG_MAX; + unsigned long long int min = 0; + + #include "../stdlib/_strtoi.h" + + return ret; } /* |