summaryrefslogtreecommitdiff
path: root/libpsx/src/libc/string.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-10-24 02:50:44 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-10-24 03:10:03 +0200
commitcbd551e8a326c56472f1423b298cd032711578a4 (patch)
tree6834e5af68fc4415bf2405085172bcd48ea08acd /libpsx/src/libc/string.c
parenteaff9ccf1dfe8f9524a1f72d8c3ec9c57a6b8228 (diff)
downloadpsxsdk-cbd551e8a326c56472f1423b298cd032711578a4.tar.gz
Provide implementations for strtoul(3) and strtoull(3)
Diffstat (limited to 'libpsx/src/libc/string.c')
-rw-r--r--libpsx/src/libc/string.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/libpsx/src/libc/string.c b/libpsx/src/libc/string.c
index ab85a42..9b9fbb1 100644
--- a/libpsx/src/libc/string.c
+++ b/libpsx/src/libc/string.c
@@ -330,11 +330,72 @@ long long strtoll(const char *nptr, char **endptr, int base)
return n?-r:r;
}
+unsigned long long strtoull(const char *nptr, char **endptr, int base)
+{
+ int r = 0;
+ int t = 0;
+ int n = 0;
+
+ while(*nptr && isspace(*nptr))
+ nptr++;
+
+ if(base == 0)
+ {
+ if(*nptr == '0')
+ base = 8;
+ else
+ base = 10;
+ }
+
+ if(!(base >= 2 && base <= 36))
+ return 0;
+
+ if(base == 16 && *nptr == '0')
+ {
+ if(*(nptr+1) == 'x' || *(nptr+1) == 'X')
+ nptr+=2;
+ }
+
+ while(*nptr)
+ {
+ switch(*nptr)
+ {
+ case '0'...'9':
+ t = *nptr - '0';
+ break;
+ case 'a' ... 'z':
+ t = (*nptr - 'a') + 10;
+ break;
+ case 'A' ... 'Z':
+ t = (*nptr - 'A') + 10;
+ break;
+ default:
+ t = 1000;
+ break;
+ }
+
+ if(t>=base)
+ break;
+
+ r*=base;
+ r+=t;
+ nptr++;
+ }
+
+ if(endptr)*endptr = (char*)nptr;
+ return n?-r:r;
+}
+
long strtol(const char *nptr, char **endptr, int base)
{
return (long)strtoll(nptr, endptr, base);
}
+unsigned long strtoul(const char *nptr, char **endptr, int base)
+{
+ return strtoull(nptr, endptr, base);
+}
+
double strtod(const char *nptr, char **endptr)
{
char strbuf[64];