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
     1.1 --- a/docs/src/install.md	Sun Jan 14 13:13:12 2024 +0100
     1.2 +++ b/docs/src/install.md	Sun Jan 14 13:50:17 2024 +0100
     1.3 @@ -32,7 +32,7 @@
     1.4  
     1.5  CX_PRINTF_SBO_SIZE                The maximum string length printf.h uses stack memory for.             512
     1.6  
     1.7 -CX_STRSTR_SBO_SIZE                The maximum length of the "needle" in strstr that can use SBO.        512
     1.8 +CX_STRSTR_SBO_SIZE                The maximum length of the "needle" in cx_strstr that can use SBO.     512
     1.9  --------------------------------- --------------------------------------------------------------------- ----------
    1.10  
    1.11  You can also tweak some other buffer sizes with the same technique:
     2.1 --- a/src/cx/string.h	Sun Jan 14 13:13:12 2024 +0100
     2.2 +++ b/src/cx/string.h	Sun Jan 14 13:50:17 2024 +0100
     2.3 @@ -40,6 +40,11 @@
     2.4  #include "allocator.h"
     2.5  
     2.6  /**
     2.7 + * The maximum length of the "needle" in cx_strstr() that can use SBO.
     2.8 + */
     2.9 +extern unsigned const cx_strstr_sbo_size;
    2.10 +
    2.11 +/**
    2.12   * The UCX string structure.
    2.13   */
    2.14  struct cx_mutstr_s {
     3.1 --- a/src/string.c	Sun Jan 14 13:13:12 2024 +0100
     3.2 +++ b/src/string.c	Sun Jan 14 13:50:17 2024 +0100
     3.3 @@ -236,6 +236,7 @@
     3.4  #ifndef CX_STRSTR_SBO_SIZE
     3.5  #define CX_STRSTR_SBO_SIZE 512
     3.6  #endif
     3.7 +unsigned const cx_strstr_sbo_size = CX_STRSTR_SBO_SIZE;
     3.8  
     3.9  cxstring cx_strstr(
    3.10          cxstring haystack,
     4.1 --- a/tests/test_string.c	Sun Jan 14 13:13:12 2024 +0100
     4.2 +++ b/tests/test_string.c	Sun Jan 14 13:50:17 2024 +0100
     4.3 @@ -175,34 +175,37 @@
     4.4  
     4.5  CX_TEST(test_strstr) {
     4.6      cxstring str = CX_STR("find the match in this string");
     4.7 -    cxstring longstr = CX_STR(
     4.8 -            "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
     4.9 -            "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
    4.10 -            "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
    4.11 -            "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
    4.12 -            "abababababababababababababababababababababababababababababababab"
    4.13 -            "abababababababababababababababababababababababababababababababab"
    4.14 -            "abababababababababababababababababababababababababababababababab"
    4.15 -            "abababababababababababababababababababababababababababababababab"
    4.16 -            "abababababababababababababababababababababababababababababababab"
    4.17 -            "abababababababababababababababababababababababababababababababab"
    4.18 -            "wxyz1234567890");
    4.19 -    cxstring longstrpattern = CX_STR(
    4.20 -            "abababababababababababababababababababababababababababababababab"
    4.21 -            "abababababababababababababababababababababababababababababababab"
    4.22 -            "abababababababababababababababababababababababababababababababab"
    4.23 -            "abababababababababababababababababababababababababababababababab"
    4.24 -            "abababababababababababababababababababababababababababababababab"
    4.25 +
    4.26 +    size_t const longstrpatternlen = 64 + cx_strstr_sbo_size;
    4.27 +    size_t const longstrlen = 320 + longstrpatternlen + 14;
    4.28 +
    4.29 +    char *longstrc = malloc(longstrlen+1);
    4.30 +    char *longstrpatternc = malloc(longstrpatternlen+1);
    4.31 +
    4.32 +    memcpy(longstrc,
    4.33 +           "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
    4.34 +           "mnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
    4.35 +           "yzabcdeababababnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghij"
    4.36 +           "klmnopqrstuvwxyzaababababababababrstuvwxyzabcdefghijklmnopqrstuv"
    4.37 +           "abababababababababababababababababababababababababababababababab",
    4.38 +           320
    4.39      );
    4.40 -    cxstring longstrresult = CX_STR(
    4.41 -            "abababababababababababababababababababababababababababababababab"
    4.42 -            "abababababababababababababababababababababababababababababababab"
    4.43 -            "abababababababababababababababababababababababababababababababab"
    4.44 -            "abababababababababababababababababababababababababababababababab"
    4.45 -            "abababababababababababababababababababababababababababababababab"
    4.46 -            "abababababababababababababababababababababababababababababababab"
    4.47 -            "wxyz1234567890"
    4.48 +    memcpy(longstrpatternc,
    4.49 +           "abababababababababababababababababababababababababababababababab",
    4.50 +           64
    4.51      );
    4.52 +    char x = 'a', y='b', z;
    4.53 +    for (size_t i = 0; i < cx_strstr_sbo_size ; i++) {
    4.54 +        longstrpatternc[64+i] = x;
    4.55 +        longstrc[320+i] = x;
    4.56 +        z=x; x=y; y=z;
    4.57 +    }
    4.58 +    longstrpatternc[longstrpatternlen] = '\0';
    4.59 +    memcpy(longstrc+longstrlen-14, "wxyz1234567890", 15);
    4.60 +
    4.61 +    cxmutstr longstr = cx_mutstrn(longstrc, longstrlen);
    4.62 +    cxstring longstrpattern = cx_strn(longstrpatternc, longstrpatternlen);
    4.63 +    cxmutstr longstrresult = cx_mutstrn(longstrc+256, longstrlen-256);
    4.64  
    4.65      CX_TEST_DO {
    4.66          cxstring notfound = cx_strstr(str, CX_STR("no match"));
    4.67 @@ -216,17 +219,13 @@
    4.68          CX_TEST_ASSERT(result.length == str.length);
    4.69          CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr));
    4.70  
    4.71 -        result = cx_strstr(longstr, longstrpattern);
    4.72 -        CX_TEST_ASSERT(result.length == longstrresult.length);
    4.73 -        CX_TEST_ASSERT(0 == strcmp(result.ptr, longstrresult.ptr));
    4.74 +        cxmutstr resultm = cx_strstr_m(longstr, longstrpattern);
    4.75 +        CX_TEST_ASSERT(resultm.length == longstrresult.length);
    4.76 +        CX_TEST_ASSERT(0 == strcmp(resultm.ptr, longstrresult.ptr));
    4.77 +    }
    4.78  
    4.79 -        // just for coverage, call the _m variant
    4.80 -        cxmutstr mstr = cx_strdup(longstr);
    4.81 -        cxmutstr m = cx_strstr_m(mstr, longstrpattern);
    4.82 -        CX_TEST_ASSERT(m.length == longstrresult.length);
    4.83 -        CX_TEST_ASSERT(0 == strcmp(m.ptr, longstrresult.ptr));
    4.84 -        cx_strfree(&mstr);
    4.85 -    }
    4.86 +    free(longstrc);
    4.87 +    free(longstrpatternc);
    4.88  }
    4.89  
    4.90  CX_TEST(test_strcmp) {

mercurial