src/string.c

changeset 1052
e997862a57d8
parent 1050
3df63e95921a
--- a/src/string.c	Mon Dec 23 00:33:27 2024 +0100
+++ b/src/string.c	Mon Dec 23 00:34:05 2024 +0100
@@ -815,8 +815,8 @@
 }
 
 #define cx_strtoX_signed_impl(rtype, rmin, rmax) \
-    int64_t result; \
-    if (cx_strtoi64_lc(str, &result, base, groupsep)) { \
+    long long result; \
+    if (cx_strtoll_lc(str, &result, base, groupsep)) { \
         return -1; \
     } \
     if (result < rmin || result > rmax) { \
@@ -839,8 +839,17 @@
 }
 
 int cx_strtoll_lc(cxstring str, long long *output, int base, const char *groupsep) {
-    assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms
-    return cx_strtoi64_lc(str, (int64_t*) output, base, groupsep);
+    // TODO: replace temporary implementation
+    (void) groupsep; // unused in temp impl
+    char *s = malloc(str.length + 1);
+    memcpy(s, str.ptr, str.length);
+    s[str.length] = '\0';
+    char *e;
+    errno = 0;
+    *output = strtoll(s, &e, base);
+    int r = errno || !(e && *e == '\0');
+    free(s);
+    return r;
 }
 
 int cx_strtoi8_lc(cxstring str, int8_t *output, int base, const char *groupsep) {
@@ -856,15 +865,17 @@
 }
 
 int cx_strtoi64_lc(cxstring str, int64_t *output, int base, const char *groupsep) {
-    // TODO: implement
-    return -1;
+    assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms
+    return cx_strtoll_lc(str, (long long*) output, base, groupsep);
 }
 
 int cx_strtoz_lc(cxstring str, ssize_t *output, int base, const char *groupsep) {
-#if CX_WORDSIZE == 32
-    return cx_strtoi32_lc(str, output, base, groupsep);
+#if SSIZE_MAX == INT32_MAX
+    return cx_strtoi32_lc(str, (int32_t*) output, base, groupsep);
+#elif SSIZE_MAX == INT64_MAX
+    return cx_strtoll_lc(str, (long long*) output, base, groupsep);
 #else
-    return cx_strtoi64_lc(str, output, base, groupsep);
+#error "unsupported ssize_t size"
 #endif
 }
 
@@ -893,8 +904,16 @@
 }
 
 int cx_strtoull_lc(cxstring str, unsigned long long *output, int base, const char *groupsep) {
-    assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms
-    return cx_strtou64_lc(str, (uint64_t*) output, base, groupsep);
+    // TODO: replace temporary implementation
+    (void) groupsep; // unused in temp impl
+    char *s = malloc(str.length + 1);
+    memcpy(s, str.ptr, str.length);
+    s[str.length] = '\0';
+    char *e;
+    *output = strtoull(s, &e, base);
+    int r = !(e && *e == '\0');
+    free(s);
+    return r;
 }
 
 int cx_strtou8_lc(cxstring str, uint8_t *output, int base, const char *groupsep) {
@@ -910,24 +929,44 @@
 }
 
 int cx_strtou64_lc(cxstring str, uint64_t *output, int base, const char *groupsep) {
-    // TODO: implement
-    return -1;
+    assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms
+    return cx_strtoull_lc(str, (unsigned long long*) output, base, groupsep);
 }
 
 int cx_strtouz_lc(cxstring str, size_t *output, int base, const char *groupsep) {
-#if CX_WORDSIZE == 32
-    return cx_strtou32_lc(str, output, base, groupsep);
+#if SIZE_MAX == UINT32_MAX
+    return cx_strtou32_lc(str, (uint32_t*) output, base, groupsep);
+#elif SIZE_MAX == UINT64_MAX
+    return cx_strtoull_lc(str, (unsigned long long *) output, base, groupsep);
 #else
-    return cx_strtou64_lc(str, output, base, groupsep);
+#error "unsupported size_t size"
 #endif
 }
 
 int cx_strtof_lc(cxstring str, float *output, char decsep, const char *groupsep) {
-    // TODO: impl
-    return -1;
+    // TODO: replace temporary implementation
+    (void) groupsep; // unused in temp impl
+    (void) decsep; // unused in temp impl
+    char *s = malloc(str.length + 1);
+    memcpy(s, str.ptr, str.length);
+    s[str.length] = '\0';
+    char *e;
+    *output = strtof(s, &e);
+    int r = !(e && *e == '\0');
+    free(s);
+    return r;
 }
 
 int cx_strtod_lc(cxstring str, double *output, char decsep, const char *groupsep) {
-    // TODO: impl
-    return -1;
+    // TODO: replace temporary implementation
+    (void) groupsep; // unused in temp impl
+    (void) decsep; // unused in temp impl
+    char *s = malloc(str.length + 1);
+    memcpy(s, str.ptr, str.length);
+    s[str.length] = '\0';
+    char *e;
+    *output = strtod(s, &e);
+    int r = !(e && *e == '\0');
+    free(s);
+    return r;
 }
\ No newline at end of file

mercurial