commit c65211248fca64724baa92e125955408971080f9
parent 368a6a533e6414844d59e85f3aaca0d99bc54713
Author: Quentin Rameau <quinq@fifth.space>
Date: Sat, 29 Dec 2018 00:23:45 +0100
[libc] Put strto* character to number convertion in _dtoi function
Diffstat:
7 files changed, 34 insertions(+), 21 deletions(-)
diff --git a/src/libc/libc.h b/src/libc/libc.h
@@ -34,6 +34,7 @@ extern struct tzone *_tzone(struct tm *tm);
extern int _daysyear(int year);
extern int _newyear(int year);
extern void *_getheap(void);
+extern int _dtoi(char);
#ifdef FILE
extern int _flsbuf(FILE *fp);
extern void _allocbuf(FILE *fp);
diff --git a/src/libc/stdlib/Makefile b/src/libc/stdlib/Makefile
@@ -3,7 +3,8 @@ PROJECTDIR =../../..
include $(PROJECTDIR)/scripts/rules.mk
include ../rules.mk
-OBJS = abort.o\
+OBJS = _dtoi.o\
+ abort.o\
abs.o\
atexit.o\
atoi.o\
diff --git a/src/libc/stdlib/_dtoi.c b/src/libc/stdlib/_dtoi.c
@@ -0,0 +1,15 @@
+#include <ctype.h>
+#include <limits.h>
+#include <string.h>
+
+int
+_dtoi(char c)
+{
+ static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ char *p;
+
+ if (p = strchr(digits, toupper(c)))
+ return p - digits;
+
+ return INT_MAX;
+}
diff --git a/src/libc/stdlib/strtol.c b/src/libc/stdlib/strtol.c
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
+#include "../libc.h"
+
#undef strtol
long
@@ -11,8 +13,7 @@ strtol(const char *s, char **end, int base)
{
int d, sign = -1;
long n;
- static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char *t, *p;
+ const char *t;
if (end)
*end = s;
@@ -38,9 +39,7 @@ strtol(const char *s, char **end, int base)
n = 0;
/* Compute n as a negative number to avoid overflow on LONG_MIN */
- for (t = s; p = strchr(digits, toupper(*t)); ++t) {
- if ((d = p - digits) >= base)
- break;
+ for (t = s; (d = _dtoi(*t)) < base; ++t) {
if (n < LONG_MIN/base)
goto overflow;
n *= base;
diff --git a/src/libc/stdlib/strtoll.c b/src/libc/stdlib/strtoll.c
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
+#include "../libc.h"
+
#undef strtoll
long long
@@ -11,8 +13,7 @@ strtoll(const char *s, char **end, int base)
{
int d, sign = -1;
long long n;
- static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char *t, *p;
+ const char *t;
if (end)
*end = s;
@@ -38,9 +39,7 @@ strtoll(const char *s, char **end, int base)
n = 0;
/* Compute n as a negative number to avoid overflow on LLONG_MIN */
- for (t = s; p = strchr(digits, toupper(*t)); ++t) {
- if ((d = p - digits) >= base)
- break;
+ for (t = s; (d = _dtoi(*t)) < base; ++t) {
if (n < LLONG_MIN/base)
goto overflow;
n *= base;
diff --git a/src/libc/stdlib/strtoul.c b/src/libc/stdlib/strtoul.c
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
+#include "../libc.h"
+
#undef strtoul
unsigned long
@@ -11,8 +13,7 @@ strtoul(const char *s, char **end, int base)
{
int d, sign = 1;
unsigned long n;
- static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char *t, *p;
+ const char *t;
if (end)
*end = s;
@@ -37,9 +38,7 @@ strtoul(const char *s, char **end, int base)
s += 2;
n = 0;
- for (t = s; p = strchr(digits, toupper(*t)); ++t) {
- if ((d = p - digits) >= base)
- break;
+ for (t = s; (d = _dtoi(*t)) < base; ++t) {
if (n > ULONG_MAX/base)
goto overflow;
n *= base;
diff --git a/src/libc/stdlib/strtoull.c b/src/libc/stdlib/strtoull.c
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
+#include "../libc.h"
+
#undef strtoull
unsigned long long
@@ -11,8 +13,7 @@ strtoull(const char *s, char **end, int base)
{
int d, sign = 1;
unsigned long long n;
- static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char *t, *p;
+ const char *t;
if (end)
*end = s;
@@ -37,9 +38,7 @@ strtoull(const char *s, char **end, int base)
s += 2;
n = 0;
- for (t = s; p = strchr(digits, toupper(*t)); ++t) {
- if ((d = p - digits) >= base)
- break;
+ for (t = s; (d = _dtoi(*t)) < base; ++t) {
if (n > ULLONG_MAX/base)
goto overflow;
n *= base;