do not trim strings before conversion to number

Sun, 26 Jan 2025 12:24:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Jan 2025 12:24:49 +0100
changeset 1155
b77d56a27e9c
parent 1154
afd12f31d28a
child 1156
96f16b5a0029

do not trim strings before conversion to number

src/string.c file | annotate | diff | comparison | revisions
tests/test_string.c file | annotate | diff | comparison | revisions
--- a/src/string.c	Sun Jan 26 11:59:16 2025 +0100
+++ b/src/string.c	Sun Jan 26 12:24:49 2025 +0100
@@ -847,8 +847,7 @@
     bool neg = false;
     size_t start_unsigned = 0;
 
-    // trim already, to search for a sign character
-    str = cx_strtrim(str);
+    // emptiness check
     if (str.length == 0) {
         errno = EINVAL;
         return -1;
@@ -941,7 +940,6 @@
 
 int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep) {
     // some sanity checks
-    str = cx_strtrim(str);
     if (str.length == 0) {
         errno = EINVAL;
         return -1;
@@ -1070,8 +1068,7 @@
     // TODO: overflow check
     // TODO: increase precision
 
-    // trim and check
-    str = cx_strtrim(str);
+    // emptiness check
     if (str.length == 0) {
         errno = EINVAL;
         return -1;
--- a/tests/test_string.c	Sun Jan 26 11:59:16 2025 +0100
+++ b/tests/test_string.c	Sun Jan 26 12:24:49 2025 +0100
@@ -1064,28 +1064,28 @@
         CX_TEST_ASSERT(i64 == INT64_MIN);
 
         // group separators
-        CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("  -123,456"), &i32, 10));
+        CX_TEST_ASSERT(0 == cx_strtoi32(cx_str("-123,456"), &i32, 10));
         CX_TEST_ASSERT(i32 == -123456);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("  -Xab,cd"), &i16, 16, "'"));
+        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-Xab,cd"), &i16, 16, "'"));
         CX_TEST_ASSERT(errno == EINVAL);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("  -X'ab'cd"), &i16, 16, "'"));
+        CX_TEST_ASSERT(0 != cx_strtoi16_lc(cx_str("-X'ab'cd"), &i16, 16, "'"));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("  -X'67'89"), &i16, 16, "'"));
+        CX_TEST_ASSERT(0 == cx_strtoi16_lc(cx_str("-X'67'89"), &i16, 16, "'"));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(i16 == -0x6789);
 
         // binary and (unusual notation of) signed binary
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" -1010 1011"), &i8, 2, " "));
+        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("-1010 1011"), &i8, 2, " "));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str(" 1010 1011"), &i8, 2, " "));
+        CX_TEST_ASSERT(0 != cx_strtoi8_lc(cx_str("1010 1011"), &i8, 2, " "));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str(" -0101 0101"), &i8, 2, " "));
+        CX_TEST_ASSERT(0 == cx_strtoi8_lc(cx_str("-0101 0101"), &i8, 2, " "));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(i8 == -0x55);
     }
@@ -1132,13 +1132,13 @@
         // --------------------------
 
         // group separators
-        CX_TEST_ASSERT(0 == cx_strtou32(cx_str("  123,456"), &u32, 10));
+        CX_TEST_ASSERT(0 == cx_strtou32(cx_str("123,456"), &u32, 10));
         CX_TEST_ASSERT(u32 == 123456);
         errno = 0;
-        CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("  ab,cd"), &u16, 16, "'"));
+        CX_TEST_ASSERT(0 != cx_strtou16_lc(cx_str("ab,cd"), &u16, 16, "'"));
         CX_TEST_ASSERT(errno == EINVAL);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("  ab'cd"), &u16, 16, "'"));
+        CX_TEST_ASSERT(0 == cx_strtou16_lc(cx_str("ab'cd"), &u16, 16, "'"));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(u16 == 0xabcd);
 
@@ -1147,7 +1147,7 @@
         CX_TEST_ASSERT(0 != cx_strtou8_lc(cx_str("1 1010 1011"), &u8, 2, " "));
         CX_TEST_ASSERT(errno == ERANGE);
         errno = 0;
-        CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str(" 1010 1011"), &u8, 2, " "));
+        CX_TEST_ASSERT(0 == cx_strtou8_lc(cx_str("1010 1011"), &u8, 2, " "));
         CX_TEST_ASSERT(errno == 0);
         CX_TEST_ASSERT(u8 == 0xAB);
     }
@@ -1226,6 +1226,34 @@
     }
 }
 
+CX_TEST(test_string_to_number_notrim) {
+    long long i;
+    unsigned long long u;
+    float f;
+    double d;
+    CX_TEST_DO {
+        CX_TEST_ASSERT(0 != cx_strtoll(cx_str("-42 "), &i, 10));
+        CX_TEST_ASSERT(0 != cx_strtoll(cx_str(" -42"), &i, 10));
+        CX_TEST_ASSERT(0 == cx_strtoll(cx_str("-42"), &i, 10));
+        CX_TEST_ASSERT(i == -42);
+
+        CX_TEST_ASSERT(0 != cx_strtoull(cx_str("42 "), &u, 10));
+        CX_TEST_ASSERT(0 != cx_strtoull(cx_str(" 42"), &u, 10));
+        CX_TEST_ASSERT(0 == cx_strtoull(cx_str("42"), &u, 10));
+        CX_TEST_ASSERT(u == 42);
+
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str("13.37 "), &f));
+        CX_TEST_ASSERT(0 != cx_strtof(cx_str(" 13.37"), &f));
+        CX_TEST_ASSERT(0 == cx_strtof(cx_str("13.37"), &f));
+        CX_TEST_ASSERT(0 == cx_vcmp_float(f, 13.37f));
+
+        CX_TEST_ASSERT(0 != cx_strtod(cx_str("13.37 "), &d));
+        CX_TEST_ASSERT(0 != cx_strtod(cx_str(" 13.37"), &d));
+        CX_TEST_ASSERT(0 == cx_strtod(cx_str("13.37"), &d));
+        CX_TEST_ASSERT(0 == cx_vcmp_double(d, 13.37));
+    }
+}
+
 CxTestSuite *cx_test_suite_string(void) {
     CxTestSuite *suite = cx_test_suite_new("string");
 
@@ -1268,6 +1296,7 @@
     cx_test_register(suite, test_string_to_unsigned_integer);
     cx_test_register(suite, test_string_to_float);
     cx_test_register(suite, test_string_to_double);
+    cx_test_register(suite, test_string_to_number_notrim);
 
     return suite;
 }

mercurial