shithub: scc

Download patch

ref: c2f5b177c6ada717746917581b5d3d98e1ab91a0
parent: d7511b681b42b4367cb5818a5d949010c5910980
author: Quentin Rameau <quinq@fifth.space>
date: Fri Dec 28 19:23:45 EST 2018

[libc] Put strto* character to number convertion in _dtoi function

--- a/src/libc/libc.h
+++ b/src/libc/libc.h
@@ -34,6 +34,7 @@
 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);
--- a/src/libc/stdlib/Makefile
+++ b/src/libc/stdlib/Makefile
@@ -3,7 +3,8 @@
 include $(PROJECTDIR)/scripts/rules.mk
 include ../rules.mk
 
-OBJS = abort.o\
+OBJS = _dtoi.o\
+       abort.o\
        abs.o\
        atexit.o\
        atoi.o\
--- /dev/null
+++ b/src/libc/stdlib/_dtoi.c
@@ -1,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;
+}
--- 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 @@
 {
 	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 @@
 
 	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;
--- 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 @@
 {
 	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 @@
 
 	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;
--- 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 @@
 {
 	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 @@
 		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;
--- 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 @@
 {
 	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 @@
 		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;