Mon, 17 Apr 2023 21:47:34 +0200
change CX_STR: use compound literal in C and ctor in C++
src/cx/string.h | file | annotate | diff | comparison | revisions | |
tests/test_string.cpp | file | annotate | diff | comparison | revisions |
--- a/src/cx/string.h Sun Apr 16 21:35:08 2023 +0200 +++ b/src/cx/string.h Mon Apr 17 21:47:34 2023 +0200 @@ -129,6 +129,18 @@ */ typedef struct cx_strtok_ctx_s CxStrtokCtx; +#ifdef __cplusplus +extern "C" { + +/** + * A literal initializer for an UCX string structure. + * + * @param literal the string literal + */ +#define CX_STR(literal) cxstring{literal, sizeof(literal) - 1} + +#else // __cplusplus + /** * A literal initializer for an UCX string structure. * @@ -136,10 +148,8 @@ * * @param literal the string literal */ -#define CX_STR(literal) {literal, sizeof(literal) - 1} +#define CX_STR(literal) (cxstring){literal, sizeof(literal) - 1} -#ifdef __cplusplus -extern "C" { #endif
--- a/tests/test_string.cpp Sun Apr 16 21:35:08 2023 +0200 +++ b/tests/test_string.cpp Mon Apr 17 21:47:34 2023 +0200 @@ -34,7 +34,7 @@ #define EXPECT_ZERO_TERMINATED(str) EXPECT_EQ((str).ptr[(str).length], '\0') TEST(String, construct) { - cxstring s1 = cx_str("1234"); + cxstring s1 = CX_STR("1234"); cxstring s2 = cx_strn("abcd", 2); cxmutstr s3 = cx_mutstr((char *) "1234"); cxmutstr s4 = cx_mutstrn((char *) "abcd", 2); @@ -96,26 +96,26 @@ EXPECT_EQ(cx_strcmp(sub, str), 0); sub = cx_strsubs(str, 2); - EXPECT_EQ(cx_strcmp(sub, cx_str("test string")), 0); + EXPECT_EQ(cx_strcmp(sub, CX_STR("test string")), 0); sub = cx_strsubs(str, 7); - EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0); + EXPECT_EQ(cx_strcmp(sub, CX_STR("string")), 0); sub = cx_strsubs(str, 15); - EXPECT_EQ(cx_strcmp(sub, cx_str("")), 0); + EXPECT_EQ(cx_strcmp(sub, CX_STR("")), 0); sub = cx_strsubsl(str, 2, 4); - EXPECT_EQ(cx_strcmp(sub, cx_str("test")), 0); + EXPECT_EQ(cx_strcmp(sub, CX_STR("test")), 0); sub = cx_strsubsl(str, 7, 3); - EXPECT_EQ(cx_strcmp(sub, cx_str("str")), 0); + EXPECT_EQ(cx_strcmp(sub, CX_STR("str")), 0); sub = cx_strsubsl(str, 7, 20); - EXPECT_EQ(cx_strcmp(sub, cx_str("string")), 0); + EXPECT_EQ(cx_strcmp(sub, CX_STR("string")), 0); // just for coverage, call the _m variant auto m = cx_strsubs_m(cx_mutstrn(nullptr, 0), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0); } TEST(String, strchr) { @@ -130,7 +130,7 @@ // just for coverage, call the _m variant auto m = cx_strchr_m(cx_mutstrn(nullptr, 0), 'a'); - EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0); } TEST(String, strrchr) { @@ -145,7 +145,7 @@ // just for coverage, call the _m variant auto m = cx_strrchr_m(cx_mutstrn(nullptr, 0), 'a'); - EXPECT_EQ(cx_strcmp(cx_strcast(m), cx_str("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(m), CX_STR("")), 0); } TEST(String, strstr) { @@ -179,14 +179,14 @@ "wxyz1234567890" ); - cxstring notfound = cx_strstr(str, cx_str("no match")); + cxstring notfound = cx_strstr(str, CX_STR("no match")); EXPECT_EQ(notfound.length, 0); - cxstring result = cx_strstr(str, cx_str("match")); + cxstring result = cx_strstr(str, CX_STR("match")); EXPECT_EQ(result.length, 20); EXPECT_STREQ(result.ptr, "match in this string"); - result = cx_strstr(str, cx_str("")); + result = cx_strstr(str, CX_STR("")); EXPECT_EQ(result.length, str.length); EXPECT_STREQ(result.ptr, str.ptr); @@ -205,36 +205,36 @@ TEST(String, strcmp) { cxstring str = CX_STR("compare this"); - EXPECT_EQ(cx_strcmp(cx_str(""), cx_str("")), 0); - EXPECT_GT(cx_strcmp(str, cx_str("")), 0); - EXPECT_EQ(cx_strcmp(str, cx_str("compare this")), 0); - EXPECT_NE(cx_strcmp(str, cx_str("Compare This")), 0); - EXPECT_LT(cx_strcmp(str, cx_str("compare tool")), 0); - EXPECT_GT(cx_strcmp(str, cx_str("compare shit")), 0); - EXPECT_LT(cx_strcmp(str, cx_str("compare this not")), 0); - EXPECT_GT(cx_strcmp(str, cx_str("compare")), 0); + EXPECT_EQ(cx_strcmp(CX_STR(""), CX_STR("")), 0); + EXPECT_GT(cx_strcmp(str, CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(str, CX_STR("compare this")), 0); + EXPECT_NE(cx_strcmp(str, CX_STR("Compare This")), 0); + EXPECT_LT(cx_strcmp(str, CX_STR("compare tool")), 0); + EXPECT_GT(cx_strcmp(str, CX_STR("compare shit")), 0); + EXPECT_LT(cx_strcmp(str, CX_STR("compare this not")), 0); + EXPECT_GT(cx_strcmp(str, CX_STR("compare")), 0); - cxstring str2 = cx_str("Compare This"); + cxstring str2 = CX_STR("Compare This"); EXPECT_NE(cx_strcmp_p(&str, &str2), 0); - str2 = cx_str("compare this"); + str2 = CX_STR("compare this"); EXPECT_EQ(cx_strcmp_p(&str, &str2), 0); } TEST(String, strcasecmp) { cxstring str = CX_STR("compare this"); - EXPECT_EQ(cx_strcasecmp(cx_str(""), cx_str("")), 0); - EXPECT_GT(cx_strcasecmp(str, cx_str("")), 0); - EXPECT_EQ(cx_strcasecmp(str, cx_str("compare this")), 0); - EXPECT_EQ(cx_strcasecmp(str, cx_str("Compare This")), 0); - EXPECT_LT(cx_strcasecmp(str, cx_str("compare tool")), 0); - EXPECT_GT(cx_strcasecmp(str, cx_str("compare shit")), 0); - EXPECT_LT(cx_strcasecmp(str, cx_str("compare this not")), 0); - EXPECT_GT(cx_strcasecmp(str, cx_str("compare")), 0); + EXPECT_EQ(cx_strcasecmp(CX_STR(""), CX_STR("")), 0); + EXPECT_GT(cx_strcasecmp(str, CX_STR("")), 0); + EXPECT_EQ(cx_strcasecmp(str, CX_STR("compare this")), 0); + EXPECT_EQ(cx_strcasecmp(str, CX_STR("Compare This")), 0); + EXPECT_LT(cx_strcasecmp(str, CX_STR("compare tool")), 0); + EXPECT_GT(cx_strcasecmp(str, CX_STR("compare shit")), 0); + EXPECT_LT(cx_strcasecmp(str, CX_STR("compare this not")), 0); + EXPECT_GT(cx_strcasecmp(str, CX_STR("compare")), 0); - cxstring str2 = cx_str("Compare This"); + cxstring str2 = CX_STR("Compare This"); EXPECT_EQ(cx_strcasecmp_p(&str, &str2), 0); - str2 = cx_str("Compare Tool"); + str2 = CX_STR("Compare Tool"); EXPECT_LT(cx_strcasecmp_p(&str, &str2), 0); } @@ -247,22 +247,22 @@ CxTestingAllocator alloc; cxmutstr t1 = cx_strcat_a(&alloc, 2, s1, s2); - EXPECT_EQ(cx_strcmp(cx_strcast(t1), cx_str("1234")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(t1), CX_STR("1234")), 0); EXPECT_ZERO_TERMINATED(t1); cx_strfree_a(&alloc, &t1); cxmutstr t2 = cx_strcat_a(&alloc, 3, s1, s2, s3); - EXPECT_EQ(cx_strcmp(cx_strcast(t2), cx_str("123456")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(t2), CX_STR("123456")), 0); EXPECT_ZERO_TERMINATED(t2); cx_strfree_a(&alloc, &t2); cxmutstr t3 = cx_strcat_a(&alloc, 6, s1, sn, s2, sn, s3, sn); - EXPECT_EQ(cx_strcmp(cx_strcast(t3), cx_str("123456")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(t3), CX_STR("123456")), 0); EXPECT_ZERO_TERMINATED(t3); cx_strfree_a(&alloc, &t3); cxmutstr t4 = cx_strcat_a(&alloc, 2, sn, sn); - EXPECT_EQ(cx_strcmp(cx_strcast(t4), cx_str("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(t4), CX_STR("")), 0); EXPECT_ZERO_TERMINATED(t4); cx_strfree_a(&alloc, &t4); @@ -270,221 +270,221 @@ // use the macro cxmutstr t5 = cx_strcat(3, s3, s1, s2); - EXPECT_EQ(cx_strcmp(cx_strcast(t5), cx_str("561234")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(t5), CX_STR("561234")), 0); EXPECT_ZERO_TERMINATED(t5); cx_strfree(&t5); } TEST(String, strsplit) { - cxstring test = cx_str("this,is,a,csv,string"); + cxstring test = CX_STR("this,is,a,csv,string"); size_t capa = 8; cxstring list[8]; size_t n; // special case: empty string - n = cx_strsplit(test, cx_str(""), capa, list); + n = cx_strsplit(test, CX_STR(""), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); // no delimiter occurrence - n = cx_strsplit(test, cx_str("z"), capa, list); + n = cx_strsplit(test, CX_STR("z"), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); // partially matching delimiter - n = cx_strsplit(test, cx_str("is,not"), capa, list); + n = cx_strsplit(test, CX_STR("is,not"), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); // matching single-char delimiter - n = cx_strsplit(test, cx_str(","), capa, list); + n = cx_strsplit(test, CX_STR(","), capa, list); ASSERT_EQ(n, 5); - EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0); - EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); - EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("a")), 0); + EXPECT_EQ(cx_strcmp(list[3], CX_STR("csv")), 0); + EXPECT_EQ(cx_strcmp(list[4], CX_STR("string")), 0); // matching multi-char delimiter - n = cx_strsplit(test, cx_str("is"), capa, list); + n = cx_strsplit(test, CX_STR("is"), capa, list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR(",")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR(",a,csv,string")), 0); // bounded list using single-char delimiter - n = cx_strsplit(test, cx_str(","), 3, list); + n = cx_strsplit(test, CX_STR(","), 3, list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); // bounded list using multi-char delimiter - n = cx_strsplit(test, cx_str("is"), 2, list); + n = cx_strsplit(test, CX_STR("is"), 2, list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); // start with delimiter - n = cx_strsplit(test, cx_str("this"), capa, list); + n = cx_strsplit(test, CX_STR("this"), capa, list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); // end with delimiter - n = cx_strsplit(test, cx_str("string"), capa, list); + n = cx_strsplit(test, CX_STR("string"), capa, list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("this,is,a,csv,")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); // end with delimiter exceed bound - n = cx_strsplit(cx_str("a,b,c,"), cx_str(","), 3, list); + n = cx_strsplit(CX_STR("a,b,c,"), CX_STR(","), 3, list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("a")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("b")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("c,")), 0); // exact match - n = cx_strsplit(test, cx_str("this,is,a,csv,string"), capa, list); + n = cx_strsplit(test, CX_STR("this,is,a,csv,string"), capa, list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); // string to be split is only substring - n = cx_strsplit(test, cx_str("this,is,a,csv,string,with,extension"), capa, list); + n = cx_strsplit(test, CX_STR("this,is,a,csv,string,with,extension"), capa, list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); // subsequent encounter of delimiter (the string between is empty) - n = cx_strsplit(test, cx_str("is,"), capa, list); + n = cx_strsplit(test, CX_STR("is,"), capa, list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); // call the _m variant just for coverage auto mtest = cx_strdup(test); cxmutstr mlist[4]; - n = cx_strsplit_m(mtest, cx_str("is,"), 4, mlist); + n = cx_strsplit_m(mtest, CX_STR("is,"), 4, mlist); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")), 0); cx_strfree(&mtest); } TEST(String, strsplit_a) { CxTestingAllocator alloc; - cxstring test = cx_str("this,is,a,csv,string"); + cxstring test = CX_STR("this,is,a,csv,string"); size_t capa = 8; cxstring *list; size_t n; // special case: empty string - n = cx_strsplit_a(&alloc, test, cx_str(""), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR(""), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); // no delimiter occurrence - n = cx_strsplit_a(&alloc, test, cx_str("z"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("z"), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); // partially matching delimiter - n = cx_strsplit_a(&alloc, test, cx_str("is,not"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("is,not"), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); // matching single-char delimiter - n = cx_strsplit_a(&alloc, test, cx_str(","), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR(","), capa, &list); ASSERT_EQ(n, 5); - EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("a")), 0); - EXPECT_EQ(cx_strcmp(list[3], cx_str("csv")), 0); - EXPECT_EQ(cx_strcmp(list[4], cx_str("string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("a")), 0); + EXPECT_EQ(cx_strcmp(list[3], CX_STR("csv")), 0); + EXPECT_EQ(cx_strcmp(list[4], CX_STR("string")), 0); cxFree(&alloc, list); // matching multi-char delimiter - n = cx_strsplit_a(&alloc, test, cx_str("is"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("is"), capa, &list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str(",")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str(",a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR(",")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR(",a,csv,string")), 0); cxFree(&alloc, list); // bounded list using single-char delimiter - n = cx_strsplit_a(&alloc, test, cx_str(","), 3, &list); + n = cx_strsplit_a(&alloc, test, CX_STR(","), 3, &list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("this")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("is")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("this")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("is")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); cxFree(&alloc, list); // bounded list using multi-char delimiter - n = cx_strsplit_a(&alloc, test, cx_str("is"), 2, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("is"), 2, &list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); cxFree(&alloc, list); // start with delimiter - n = cx_strsplit_a(&alloc, test, cx_str("this"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("this"), capa, &list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str(",is,a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR(",is,a,csv,string")), 0); cxFree(&alloc, list); // end with delimiter - n = cx_strsplit_a(&alloc, test, cx_str("string"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("string"), capa, &list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("this,is,a,csv,")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("this,is,a,csv,")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); cxFree(&alloc, list); // end with delimiter exceed bound - n = cx_strsplit_a(&alloc, cx_str("a,b,c,"), cx_str(","), 3, &list); + n = cx_strsplit_a(&alloc, CX_STR("a,b,c,"), CX_STR(","), 3, &list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("a")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("b")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("c,")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("a")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("b")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("c,")), 0); cxFree(&alloc, list); // exact match - n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("this,is,a,csv,string"), capa, &list); ASSERT_EQ(n, 2); - EXPECT_EQ(cx_strcmp(list[0], cx_str("")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); cxFree(&alloc, list); // string to be split is only substring - n = cx_strsplit_a(&alloc, test, cx_str("this,is,a,csv,string,with,extension"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("this,is,a,csv,string,with,extension"), capa, &list); ASSERT_EQ(n, 1); EXPECT_EQ(cx_strcmp(list[0], test), 0); cxFree(&alloc, list); // subsequent encounter of delimiter (the string between is empty) - n = cx_strsplit_a(&alloc, test, cx_str("is,"), capa, &list); + n = cx_strsplit_a(&alloc, test, CX_STR("is,"), capa, &list); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(list[0], cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(list[1], cx_str("")), 0); - EXPECT_EQ(cx_strcmp(list[2], cx_str("a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(list[0], CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(list[1], CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(list[2], CX_STR("a,csv,string")), 0); cxFree(&alloc, list); // call the _m variant just for coverage auto mtest = cx_strdup(test); cxmutstr *mlist; - n = cx_strsplit_ma(&alloc, mtest, cx_str("is,"), 4, &mlist); + n = cx_strsplit_ma(&alloc, mtest, CX_STR("is,"), 4, &mlist); ASSERT_EQ(n, 3); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), cx_str("th")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), cx_str("")), 0); - EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), cx_str("a,csv,string")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(mlist[0]), CX_STR("th")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(mlist[1]), CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(mlist[2]), CX_STR("a,csv,string")), 0); cxFree(&alloc, mlist); cx_strfree(&mtest); @@ -492,63 +492,63 @@ } TEST(String, strtrim) { - cxstring t1 = cx_strtrim(cx_str(" ein test \t ")); - cxstring t2 = cx_strtrim(cx_str("abc")); - cxstring t3 = cx_strtrim(cx_str(" 123")); - cxstring t4 = cx_strtrim(cx_str("xyz ")); - cxstring t5 = cx_strtrim(cx_str(" ")); - cxstring empty = cx_strtrim(cx_str("")); + cxstring t1 = cx_strtrim(CX_STR(" ein test \t ")); + cxstring t2 = cx_strtrim(CX_STR("abc")); + cxstring t3 = cx_strtrim(CX_STR(" 123")); + cxstring t4 = cx_strtrim(CX_STR("xyz ")); + cxstring t5 = cx_strtrim(CX_STR(" ")); + cxstring empty = cx_strtrim(CX_STR("")); - EXPECT_EQ(cx_strcmp(t1, cx_str("ein test")), 0); - EXPECT_EQ(cx_strcmp(t2, cx_str("abc")), 0); - EXPECT_EQ(cx_strcmp(t3, cx_str("123")), 0); - EXPECT_EQ(cx_strcmp(t4, cx_str("xyz")), 0); - EXPECT_EQ(cx_strcmp(t5, cx_str("")), 0); - EXPECT_EQ(cx_strcmp(empty, cx_str("")), 0); + EXPECT_EQ(cx_strcmp(t1, CX_STR("ein test")), 0); + EXPECT_EQ(cx_strcmp(t2, CX_STR("abc")), 0); + EXPECT_EQ(cx_strcmp(t3, CX_STR("123")), 0); + EXPECT_EQ(cx_strcmp(t4, CX_STR("xyz")), 0); + EXPECT_EQ(cx_strcmp(t5, CX_STR("")), 0); + EXPECT_EQ(cx_strcmp(empty, CX_STR("")), 0); // call the _m variant just for coverage cxmutstr m1 = cx_strtrim_m(cx_mutstr((char *) " ein test \t ")); - EXPECT_EQ(cx_strcmp(cx_strcast(m1), cx_str("ein test")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(m1), CX_STR("ein test")), 0); } TEST(String, strprefix) { cxstring str = CX_STR("test my prefix and my suffix"); cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strprefix(empty, cx_str("pref"))); + EXPECT_FALSE(cx_strprefix(empty, CX_STR("pref"))); EXPECT_TRUE(cx_strprefix(str, empty)); EXPECT_TRUE(cx_strprefix(empty, empty)); - EXPECT_TRUE(cx_strprefix(str, cx_str("test "))); - EXPECT_FALSE(cx_strprefix(str, cx_str("8-) fsck "))); + EXPECT_TRUE(cx_strprefix(str, CX_STR("test "))); + EXPECT_FALSE(cx_strprefix(str, CX_STR("8-) fsck "))); } TEST(String, strsuffix) { cxstring str = CX_STR("test my prefix and my suffix"); cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strsuffix(empty, cx_str("suf"))); + EXPECT_FALSE(cx_strsuffix(empty, CX_STR("suf"))); EXPECT_TRUE(cx_strsuffix(str, empty)); EXPECT_TRUE(cx_strsuffix(empty, empty)); - EXPECT_TRUE(cx_strsuffix(str, cx_str("fix"))); - EXPECT_FALSE(cx_strsuffix(str, cx_str("fox"))); + EXPECT_TRUE(cx_strsuffix(str, CX_STR("fix"))); + EXPECT_FALSE(cx_strsuffix(str, CX_STR("fox"))); } TEST(String, strcaseprefix) { cxstring str = CX_STR("test my prefix and my suffix"); cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strcaseprefix(empty, cx_str("pREf"))); + EXPECT_FALSE(cx_strcaseprefix(empty, CX_STR("pREf"))); EXPECT_TRUE(cx_strcaseprefix(str, empty)); EXPECT_TRUE(cx_strcaseprefix(empty, empty)); - EXPECT_TRUE(cx_strcaseprefix(str, cx_str("TEST "))); - EXPECT_FALSE(cx_strcaseprefix(str, cx_str("8-) fsck "))); + EXPECT_TRUE(cx_strcaseprefix(str, CX_STR("TEST "))); + EXPECT_FALSE(cx_strcaseprefix(str, CX_STR("8-) fsck "))); } TEST(String, strcasesuffix) { cxstring str = CX_STR("test my prefix and my suffix"); cxstring empty = CX_STR(""); - EXPECT_FALSE(cx_strcasesuffix(empty, cx_str("sUf"))); + EXPECT_FALSE(cx_strcasesuffix(empty, CX_STR("sUf"))); EXPECT_TRUE(cx_strcasesuffix(str, empty)); EXPECT_TRUE(cx_strcasesuffix(empty, empty)); - EXPECT_TRUE(cx_strcasesuffix(str, cx_str("FIX"))); - EXPECT_FALSE(cx_strcasesuffix(str, cx_str("fox"))); + EXPECT_TRUE(cx_strcasesuffix(str, CX_STR("FIX"))); + EXPECT_FALSE(cx_strcasesuffix(str, CX_STR("fox"))); } TEST(String, strreplace) { @@ -561,44 +561,44 @@ cxstring astr = CX_STR("aaaaaaaaaa"); cxstring csstr = CX_STR("test AB ab TEST xyz"); - cxmutstr repl = cx_strreplace(str, cx_str("abab"), cx_str("muchlonger")); + cxmutstr repl = cx_strreplace(str, CX_STR("abab"), CX_STR("muchlonger")); auto expected = "test muchlongerab string aba"; - cxmutstr repln = cx_strreplacen(str, cx_str("ab"), cx_str("c"), 2); + cxmutstr repln = cx_strreplacen(str, CX_STR("ab"), CX_STR("c"), 2); auto expectedn = "test ccab string aba"; - cxmutstr longrepl = cx_strreplace(longstr, cx_str("a"), cx_str("z")); + cxmutstr longrepl = cx_strreplace(longstr, CX_STR("a"), CX_STR("z")); auto longexpect = "xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzcd"; - cxmutstr replnotrail = cx_strreplace(notrail, cx_str("ab"), cx_str("z")); + cxmutstr replnotrail = cx_strreplace(notrail, CX_STR("ab"), CX_STR("z")); auto notrailexpect = "test zz"; - cxmutstr repleq = cx_strreplace(str, str, cx_str("hello")); + cxmutstr repleq = cx_strreplace(str, str, CX_STR("hello")); auto eqexpect = "hello"; - cxmutstr replempty1 = cx_strreplace(empty, cx_str("ab"), cx_str("c")); // expect: empty - cxmutstr replempty2 = cx_strreplace(str, cx_str("abab"), empty); + cxmutstr replempty1 = cx_strreplace(empty, CX_STR("ab"), CX_STR("c")); // expect: empty + cxmutstr replempty2 = cx_strreplace(str, CX_STR("abab"), empty); auto emptyexpect2 = "test ab string aba"; - cxmutstr replpre = cx_strreplace(str, cx_str("test "), cx_str("TEST ")); + cxmutstr replpre = cx_strreplace(str, CX_STR("test "), CX_STR("TEST ")); auto preexpected = "TEST ababab string aba"; - cxmutstr replan1 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 1); + cxmutstr replan1 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 1); auto an1expected = "xaaaaaaaaa"; - cxmutstr replan4 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 4); + cxmutstr replan4 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 4); auto an4expected = "xxxxaaaaaa"; - cxmutstr replan9 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 9); + cxmutstr replan9 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 9); auto an9expected = "xxxxxxxxxa"; - cxmutstr replan10 = cx_strreplacen(astr, cx_str("a"), cx_str("x"), 10); + cxmutstr replan10 = cx_strreplacen(astr, CX_STR("a"), CX_STR("x"), 10); auto an10expected = "xxxxxxxxxx"; - cxmutstr repl1_a = cx_strreplace_a(&alloc, csstr, cx_str("AB"), cx_str("*")); + cxmutstr repl1_a = cx_strreplace_a(&alloc, csstr, CX_STR("AB"), CX_STR("*")); auto expeced1_a = "test * ab TEST xyz"; - cxmutstr repl2_a = cx_strreplace_a(&alloc, csstr, cx_str("test"), cx_str("TEST")); + cxmutstr repl2_a = cx_strreplace_a(&alloc, csstr, CX_STR("test"), CX_STR("TEST")); auto expected2_a = "TEST AB ab TEST xyz"; @@ -651,22 +651,22 @@ } TEST(String, strupper) { - cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t")); + cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t")); cx_strupper(str); EXPECT_STREQ(str.ptr, "THIS 1S @ TE$T"); cx_strfree(&str); } TEST(String, strlower) { - cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t")); + cxmutstr str = cx_strdup(CX_STR("thIs 1s @ Te$t")); cx_strlower(str); EXPECT_STREQ(str.ptr, "this 1s @ te$t"); cx_strfree(&str); } TEST(String, strtok) { - cxstring str = cx_str("a,comma,separated,string"); - cxstring delim = cx_str(","); + cxstring str = CX_STR("a,comma,separated,string"); + cxstring delim = CX_STR(","); CxStrtokCtx ctx = cx_strtok(str, delim, 3); EXPECT_EQ(ctx.str.ptr, str.ptr); EXPECT_EQ(ctx.str.length, str.length); @@ -681,8 +681,8 @@ } TEST(String, strtok_m) { - cxmutstr str = cx_strdup(cx_str("a,comma,separated,string")); - cxstring delim = cx_str(","); + cxmutstr str = cx_strdup(CX_STR("a,comma,separated,string")); + cxstring delim = CX_STR(","); CxStrtokCtx ctx = cx_strtok_m(str, delim, 3); EXPECT_EQ(ctx.str.ptr, str.ptr); EXPECT_EQ(ctx.str.length, str.length); @@ -698,8 +698,8 @@ } TEST(String, strtok_delim) { - cxstring str = cx_str("an,arbitrarily|separated;string"); - cxstring delim = cx_str(","); + cxstring str = CX_STR("an,arbitrarily|separated;string"); + cxstring delim = CX_STR(","); cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")}; CxStrtokCtx ctx = cx_strtok(str, delim, 3); cx_strtok_delim(&ctx, delim_more, 2); @@ -716,15 +716,15 @@ } TEST(String, strtok_next_easy) { - cxstring str = cx_str("a,comma,separated,string"); - cxstring delim = cx_str(","); + cxstring str = CX_STR("a,comma,separated,string"); + cxstring delim = CX_STR(","); CxStrtokCtx ctx = cx_strtok(str, delim, 3); bool ret; cxstring tok; ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("a")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("a")), 0); EXPECT_EQ(ctx.pos, 0); EXPECT_EQ(ctx.next_pos, 2); EXPECT_EQ(ctx.delim_pos, 1); @@ -732,7 +732,7 @@ ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("comma")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("comma")), 0); EXPECT_EQ(ctx.pos, 2); EXPECT_EQ(ctx.next_pos, 8); EXPECT_EQ(ctx.delim_pos, 7); @@ -740,7 +740,7 @@ ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("separated")), 0); EXPECT_EQ(ctx.pos, 8); EXPECT_EQ(ctx.next_pos, 18); EXPECT_EQ(ctx.delim_pos, 17); @@ -755,15 +755,15 @@ } TEST(String, strtok_next_unlimited) { - cxstring str = cx_str("some;-;otherwise;-;separated;-;string;-;"); - cxstring delim = cx_str(";-;"); + cxstring str = CX_STR("some;-;otherwise;-;separated;-;string;-;"); + cxstring delim = CX_STR(";-;"); CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX); bool ret; cxstring tok; ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("some")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("some")), 0); EXPECT_EQ(ctx.pos, 0); EXPECT_EQ(ctx.next_pos, 7); EXPECT_EQ(ctx.delim_pos, 4); @@ -771,7 +771,7 @@ ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("otherwise")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("otherwise")), 0); EXPECT_EQ(ctx.pos, 7); EXPECT_EQ(ctx.next_pos, 19); EXPECT_EQ(ctx.delim_pos, 16); @@ -779,7 +779,7 @@ ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("separated")), 0); EXPECT_EQ(ctx.pos, 19); EXPECT_EQ(ctx.next_pos, 31); EXPECT_EQ(ctx.delim_pos, 28); @@ -787,7 +787,7 @@ ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("string")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("string")), 0); EXPECT_EQ(ctx.pos, 31); EXPECT_EQ(ctx.next_pos, 40); EXPECT_EQ(ctx.delim_pos, 37); @@ -795,7 +795,7 @@ ret = cx_strtok_next(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(tok, cx_str("")), 0); + EXPECT_EQ(cx_strcmp(tok, CX_STR("")), 0); EXPECT_EQ(ctx.pos, 40); EXPECT_EQ(ctx.next_pos, 40); EXPECT_EQ(ctx.delim_pos, 40); @@ -809,8 +809,8 @@ } TEST(String, strtok_next_advanced) { - cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string")); - cxstring delim = cx_str(","); + cxmutstr str = cx_strdup(CX_STR("an,arbitrarily;||separated;string")); + cxstring delim = CX_STR(","); cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")}; CxStrtokCtx ctx = cx_strtok_m(str, delim, 10); cx_strtok_delim(&ctx, delim_more, 2); @@ -819,7 +819,7 @@ ret = cx_strtok_next_m(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("an")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("an")), 0); EXPECT_EQ(ctx.pos, 0); EXPECT_EQ(ctx.next_pos, 3); EXPECT_EQ(ctx.delim_pos, 2); @@ -828,7 +828,7 @@ ret = cx_strtok_next_m(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("arbitrarily")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("arbitrarily")), 0); EXPECT_EQ(ctx.pos, 3); EXPECT_EQ(ctx.next_pos, 15); EXPECT_EQ(ctx.delim_pos, 14); @@ -837,7 +837,7 @@ ret = cx_strtok_next_m(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("")), 0); EXPECT_EQ(ctx.pos, 15); EXPECT_EQ(ctx.next_pos, 17); EXPECT_EQ(ctx.delim_pos, 15); @@ -846,7 +846,7 @@ ret = cx_strtok_next_m(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("separated")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("separated")), 0); EXPECT_EQ(ctx.pos, 17); EXPECT_EQ(ctx.next_pos, 27); EXPECT_EQ(ctx.delim_pos, 26); @@ -855,7 +855,7 @@ ret = cx_strtok_next_m(&ctx, &tok); ASSERT_TRUE(ret); - EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("string")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(tok), CX_STR("string")), 0); EXPECT_EQ(ctx.pos, 27); EXPECT_EQ(ctx.next_pos, 33); EXPECT_EQ(ctx.delim_pos, 33); @@ -869,7 +869,7 @@ EXPECT_EQ(ctx.delim_pos, 33); EXPECT_EQ(ctx.found, 5); - EXPECT_EQ(cx_strcmp(cx_strcast(str), cx_str("AN,ARBITRARILY;||SEPARATED;STRING")), 0); + EXPECT_EQ(cx_strcmp(cx_strcast(str), CX_STR("AN,ARBITRARILY;||SEPARATED;STRING")), 0); cx_strfree(&str); }