add constant for reading out strstr sbo size - relates to #343

Sun, 14 Jan 2024 13:50:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Jan 2024 13:50:17 +0100
changeset 806
e06249e09f99
parent 805
26500fc24058
child 807
c8d692131b1e

add constant for reading out strstr sbo size - relates to #343

also fixes the related test which was working with the old SBO size of 256 and was broken after increasing it to 512

docs/src/install.md file | annotate | diff | comparison | revisions
src/cx/string.h file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
tests/test_string.c file | annotate | diff | comparison | revisions
--- a/docs/src/install.md	Sun Jan 14 13:13:12 2024 +0100
+++ b/docs/src/install.md	Sun Jan 14 13:50:17 2024 +0100
@@ -32,7 +32,7 @@
 
 CX_PRINTF_SBO_SIZE                The maximum string length printf.h uses stack memory for.             512
 
-CX_STRSTR_SBO_SIZE                The maximum length of the "needle" in strstr that can use SBO.        512
+CX_STRSTR_SBO_SIZE                The maximum length of the "needle" in cx_strstr that can use SBO.     512
 --------------------------------- --------------------------------------------------------------------- ----------
 
 You can also tweak some other buffer sizes with the same technique:
--- a/src/cx/string.h	Sun Jan 14 13:13:12 2024 +0100
+++ b/src/cx/string.h	Sun Jan 14 13:50:17 2024 +0100
@@ -40,6 +40,11 @@
 #include "allocator.h"
 
 /**
+ * The maximum length of the "needle" in cx_strstr() that can use SBO.
+ */
+extern unsigned const cx_strstr_sbo_size;
+
+/**
  * The UCX string structure.
  */
 struct cx_mutstr_s {
--- a/src/string.c	Sun Jan 14 13:13:12 2024 +0100
+++ b/src/string.c	Sun Jan 14 13:50:17 2024 +0100
@@ -236,6 +236,7 @@
 #ifndef CX_STRSTR_SBO_SIZE
 #define CX_STRSTR_SBO_SIZE 512
 #endif
+unsigned const cx_strstr_sbo_size = CX_STRSTR_SBO_SIZE;
 
 cxstring cx_strstr(
         cxstring haystack,
--- a/tests/test_string.c	Sun Jan 14 13:13:12 2024 +0100
+++ b/tests/test_string.c	Sun Jan 14 13:50:17 2024 +0100
@@ -175,34 +175,37 @@
 
 CX_TEST(test_strstr) {
     cxstring str = CX_STR("find the match in this string");
-    cxstring longstr = CX_STR(
-            "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
-            "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
-            "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
-            "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "wxyz1234567890");
-    cxstring longstrpattern = CX_STR(
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
+
+    size_t const longstrpatternlen = 64 + cx_strstr_sbo_size;
+    size_t const longstrlen = 320 + longstrpatternlen + 14;
+
+    char *longstrc = malloc(longstrlen+1);
+    char *longstrpatternc = malloc(longstrpatternlen+1);
+
+    memcpy(longstrc,
+           "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
+           "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
+           "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
+           "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
+           "abababababababababababababababababababababababababababababababab",
+           320
     );
-    cxstring longstrresult = CX_STR(
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "abababababababababababababababababababababababababababababababab"
-            "wxyz1234567890"
+    memcpy(longstrpatternc,
+           "abababababababababababababababababababababababababababababababab",
+           64
     );
+    char x = 'a', y='b', z;
+    for (size_t i = 0; i < cx_strstr_sbo_size ; i++) {
+        longstrpatternc[64+i] = x;
+        longstrc[320+i] = x;
+        z=x; x=y; y=z;
+    }
+    longstrpatternc[longstrpatternlen] = '\0';
+    memcpy(longstrc+longstrlen-14, "wxyz1234567890", 15);
+
+    cxmutstr longstr = cx_mutstrn(longstrc, longstrlen);
+    cxstring longstrpattern = cx_strn(longstrpatternc, longstrpatternlen);
+    cxmutstr longstrresult = cx_mutstrn(longstrc+256, longstrlen-256);
 
     CX_TEST_DO {
         cxstring notfound = cx_strstr(str, CX_STR("no match"));
@@ -216,17 +219,13 @@
         CX_TEST_ASSERT(result.length == str.length);
         CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr));
 
-        result = cx_strstr(longstr, longstrpattern);
-        CX_TEST_ASSERT(result.length == longstrresult.length);
-        CX_TEST_ASSERT(0 == strcmp(result.ptr, longstrresult.ptr));
+        cxmutstr resultm = cx_strstr_m(longstr, longstrpattern);
+        CX_TEST_ASSERT(resultm.length == longstrresult.length);
+        CX_TEST_ASSERT(0 == strcmp(resultm.ptr, longstrresult.ptr));
+    }
 
-        // just for coverage, call the _m variant
-        cxmutstr mstr = cx_strdup(longstr);
-        cxmutstr m = cx_strstr_m(mstr, longstrpattern);
-        CX_TEST_ASSERT(m.length == longstrresult.length);
-        CX_TEST_ASSERT(0 == strcmp(m.ptr, longstrresult.ptr));
-        cx_strfree(&mstr);
-    }
+    free(longstrc);
+    free(longstrpatternc);
 }
 
 CX_TEST(test_strcmp) {

mercurial