src/string.c

changeset 1132
b7fea9b2874c
parent 1127
1fd31909a3f8
child 1134
60edcd57d54c
--- a/src/string.c	Fri Jan 17 17:55:21 2025 +0100
+++ b/src/string.c	Sat Jan 18 13:30:51 2025 +0100
@@ -30,7 +30,6 @@
 
 #include <string.h>
 #include <stdarg.h>
-#include <ctype.h>
 #include <assert.h>
 #include <errno.h>
 #include <limits.h>
@@ -529,14 +528,19 @@
     return result;
 }
 
+static bool str_isspace(char c) {
+    // TODO: remove once UCX has public API for this
+    return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
+}
+
 cxstring cx_strtrim(cxstring string) {
     cxstring result = string;
     // TODO: optimize by comparing multiple bytes at once
-    while (result.length > 0 && isspace(*result.ptr)) {
+    while (result.length > 0 && str_isspace(*result.ptr)) {
         result.ptr++;
         result.length--;
     }
-    while (result.length > 0 && isspace(result.ptr[result.length - 1])) {
+    while (result.length > 0 && str_isspace(result.ptr[result.length - 1])) {
         result.length--;
     }
     return result;
@@ -592,13 +596,17 @@
 
 void cx_strlower(cxmutstr string) {
     for (size_t i = 0; i < string.length; i++) {
-        string.ptr[i] = (char) tolower(string.ptr[i]);
+        if ((unsigned int) (string.ptr[i] - 'A') < 26u) {
+            string.ptr[i] += 'a' - 'A';
+        }
     }
 }
 
 void cx_strupper(cxmutstr string) {
     for (size_t i = 0; i < string.length; i++) {
-        string.ptr[i] = (char) toupper(string.ptr[i]);
+        if ((unsigned int) (string.ptr[i] - 'a') < 26u) {
+            string.ptr[i] += 'A' - 'a';
+        }
     }
 }
 
@@ -1055,6 +1063,11 @@
     return 0;
 }
 
+static bool str_isdigit(char c) {
+    // TODO: remove once UCX has public API for this
+    return c >= '0' && c <= '9';
+}
+
 int cx_strtod_lc(cxstring str, double *output, char decsep, const char *groupsep) {
     // TODO: overflow check
     // TODO: increase precision
@@ -1088,7 +1101,7 @@
     // parse all digits until we find the decsep
     size_t pos = 0;
     do {
-        if (isdigit(str.ptr[pos])) {
+        if (str_isdigit(str.ptr[pos])) {
             result = result * 10 + (str.ptr[pos] - '0');
         } else if (strchr(groupsep, str.ptr[pos]) == NULL) {
             break;
@@ -1117,7 +1130,7 @@
         // parse everything until exponent or end
         double factor = 1.;
         do {
-            if (isdigit(str.ptr[pos])) {
+            if (str_isdigit(str.ptr[pos])) {
                 factor *= 0.1;
                 result = result + factor * (str.ptr[pos] - '0');
             } else if (strchr(groupsep, str.ptr[pos]) == NULL) {
@@ -1158,7 +1171,7 @@
     // parse the exponent
     unsigned int exp = 0;
     do {
-        if (isdigit(str.ptr[pos])) {
+        if (str_isdigit(str.ptr[pos])) {
             exp = 10 * exp + (str.ptr[pos] - '0');
         } else if (strchr(groupsep, str.ptr[pos]) == NULL) {
             errno = EINVAL;

mercurial