src/string.c

changeset 1063
e453e717876e
parent 1061
c7d23892eab5
--- a/src/string.c	Sat Dec 28 17:31:28 2024 +0100
+++ b/src/string.c	Sat Dec 28 17:32:36 2024 +0100
@@ -34,6 +34,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <limits.h>
+#include <float.h>
 
 #ifndef _WIN32
 
@@ -961,18 +962,18 @@
 
     // if base is 2 or 16, some leading stuff may appear
     if (base == 2) {
-        if (str.ptr[0] == 'b' || str.ptr[0] == 'B') {
+        if ((str.ptr[0] | 32) == 'b') {
             start = 1;
         } else if (str.ptr[0] == '0' && str.length > 1) {
-            if (str.ptr[1] == 'b' || str.ptr[1] == 'B') {
+            if ((str.ptr[1] | 32) == 'b') {
                 start = 2;
             }
         }
     } else if (base == 16) {
-        if (str.ptr[0] == 'x' || str.ptr[0] == 'X' || str.ptr[0] == '#') {
+        if ((str.ptr[0] | 32) == 'x' || str.ptr[0] == '#') {
             start = 1;
         } else if (str.ptr[0] == '0' && str.length > 1) {
-            if (str.ptr[1] == 'x' || str.ptr[1] == 'X') {
+            if ((str.ptr[1] | 32) == 'x') {
                 start = 2;
             }
         }
@@ -1043,29 +1044,140 @@
 }
 
 int cx_strtof_lc(cxstring str, float *output, char decsep, const char *groupsep) {
-    // 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;
+    // use string to double and add a range check
+    double d;
+    int ret = cx_strtod_lc(str, &d, decsep, groupsep);
+    if (ret != 0) return ret;
+    // note: FLT_MIN is the smallest POSITIVE number that can be represented
+    double test = d < 0 ? -d : d;
+    if (test < FLT_MIN || test > FLT_MAX) {
+        errno = ERANGE;
+        return -1;
+    }
+    *output = (float) d;
+    return 0;
 }
 
 int cx_strtod_lc(cxstring str, double *output, char decsep, const char *groupsep) {
-    // 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;
+    // TODO: overflow check
+    // TODO: increase precision
+
+    // trim and check
+    str = cx_strtrim(str);
+    if (str.length == 0) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    double result = 0.;
+    int sign = 1;
+
+    // check if there is a sign
+    if (str.ptr[0] == '-') {
+        sign = -1;
+        str.ptr++;
+        str.length--;
+    } else if (str.ptr[0] == '+') {
+        str.ptr++;
+        str.length--;
+    }
+
+    // there must be at least one char to parse
+    if (str.length == 0) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    // parse all digits until we find the decsep
+    size_t pos = 0;
+    do {
+        if (isdigit(str.ptr[pos])) {
+            result = result * 10 + (str.ptr[pos] - '0');
+        } else if (strchr(groupsep, str.ptr[pos]) == NULL) {
+            break;
+        }
+    } while (++pos < str.length);
+
+    // already done?
+    if (pos == str.length) {
+        *output = result * sign;
+        return 0;
+    }
+
+    // is the next char the decsep?
+    if (str.ptr[pos] == decsep) {
+        pos++;
+        // it may end with the decsep, if it did not start with it
+        if (pos == str.length) {
+            if (str.length == 1) {
+                errno = EINVAL;
+                return -1;
+            } else {
+                *output = result * sign;
+                return 0;
+            }
+        }
+        // parse everything until exponent or end
+        double factor = 1.;
+        do {
+            if (isdigit(str.ptr[pos])) {
+                factor *= 0.1;
+                result = result + factor * (str.ptr[pos] - '0');
+            } else if (strchr(groupsep, str.ptr[pos]) == NULL) {
+                break;
+            }
+        } while (++pos < str.length);
+    }
+
+    // no exponent?
+    if (pos == str.length) {
+        *output = result * sign;
+        return 0;
+    }
+
+    // now the next separator MUST be the exponent separator
+    // and at least one char must follow
+    if ((str.ptr[pos] | 32) != 'e' || str.length <= pos + 1) {
+        errno = EINVAL;
+        return -1;
+    }
+    pos++;
+
+    // check if we have a sign for the exponent
+    double factor = 10.;
+    if (str.ptr[pos] == '-') {
+        factor = .1;
+        pos++;
+    } else if (str.ptr[pos] == '+') {
+        pos++;
+    }
+
+    // at least one digit must follow
+    if (pos == str.length) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    // parse the exponent
+    unsigned int exp = 0;
+    do {
+        if (isdigit(str.ptr[pos])) {
+            exp = 10 * exp + (str.ptr[pos] - '0');
+        } else if (strchr(groupsep, str.ptr[pos]) == NULL) {
+            errno = EINVAL;
+            return -1;
+        }
+    } while (++pos < str.length);
+
+    // apply the exponent by fast exponentiation
+    do {
+        if (exp & 1) {
+            result *= factor;
+        }
+        factor *= factor;
+    } while ((exp >>= 1) > 0);
+
+    // store the result and exit
+    *output = result * sign;
+    return 0;
 }
\ No newline at end of file

mercurial