diff options
author | Freya Murphy <freya@freyacat.org> | 2025-04-03 12:30:34 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-04-03 12:30:34 -0400 |
commit | ec3c37d1d40d7b288584c234f4c3e7a600f2353d (patch) | |
tree | bb587b33c4c793ff7a3317dfa958d69b0fa318a1 /lib/strtoux.c | |
parent | move boot only headers to boot (diff) | |
download | comus-ec3c37d1d40d7b288584c234f4c3e7a600f2353d.tar.gz comus-ec3c37d1d40d7b288584c234f4c3e7a600f2353d.tar.bz2 comus-ec3c37d1d40d7b288584c234f4c3e7a600f2353d.zip |
new libs
Diffstat (limited to 'lib/strtoux.c')
-rw-r--r-- | lib/strtoux.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/strtoux.c b/lib/strtoux.c new file mode 100644 index 0000000..7c2d7ee --- /dev/null +++ b/lib/strtoux.c @@ -0,0 +1,44 @@ +#include <stdlib.h> +#include <ctype.h> + +#define STRTOUX(name, type) \ + type name(const char *restrict s, char **restrict endptr, int radix) \ + { \ + const char *s_start = s; \ + for (; isspace(*s); s++) \ + ; \ + \ + if ((radix == 0 || radix == 16) && \ + (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))) { \ + radix = 16; \ + s += 2; \ + } else if (radix == 0) { \ + if (*s == '0') { \ + radix = 8; \ + s++; \ + } else { \ + radix = 10; \ + } \ + } \ + \ + type num = 0; \ + int has_digit = 0; \ + \ + while (1) { \ + int n = ctoi(*s++); \ + if (n < 0 || n >= radix) \ + break; \ + has_digit = 1; \ + num = num * radix + n; \ + } \ + \ + if (endptr != NULL) { \ + *endptr = has_digit ? (char *)(s - 1) : (char *)s_start; \ + } \ + \ + return num; \ + } + +STRTOUX(strtoui, unsigned int) +STRTOUX(strtoul, unsigned long int) +STRTOUX(strtoull, unsigned long long int) |