diff options
| author | Tyge Løvset <[email protected]> | 2023-01-22 16:22:51 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-01-22 16:22:51 +0100 |
| commit | af74e5f44af8f03ec3b0b2ffd3fd577210707e49 (patch) | |
| tree | 5905f4c80190161cdc9f814e78a7a87030cae1c7 /misc | |
| parent | 01bffb4d3b6b175db264c5cc30b1b7e756560639 (diff) | |
| download | STC-modified-af74e5f44af8f03ec3b0b2ffd3fd577210707e49.tar.gz STC-modified-af74e5f44af8f03ec3b0b2ffd3fd577210707e49.zip | |
Added cspan_init() for static initializing and a minor fix. Added cregex replace tests.
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/examples/multidim.c | 73 | ||||
| -rw-r--r-- | misc/tests/cregex_test.c | 47 |
2 files changed, 81 insertions, 39 deletions
diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index e882d62a..69f818cc 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -5,53 +5,48 @@ #include <stdio.h> using_cspan3(ispan, int); -/* -using_cspan(ispan, int, 1); -using_cspan(ispan2, int, 2); -using_cspan(ispan3, int, 3); -*/ int main() { - cstack_int v = {0}; - c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12}) - cstack_int_push(&v, *i.ref); + cstack_int v = {0}; + c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12}) + cstack_int_push(&v, *i.ref); - // View data as contiguous memory representing 12 ints - ispan ms1 = cspan_from(&v); - // View data as contiguous memory representing 2 rows of 6 ints each - ispan2 ms2 = cspan_make(v.data, 2, 6); - // View the same data as a 3D array 2 x 3 x 2 - ispan3 ms3 = cspan_make(v.data, 2, 3, 2); + // View data as contiguous memory representing 12 ints + ispan ms1 = cspan_from(&v); + // View data as contiguous memory representing 2 rows of 6 ints each + ispan2 ms2 = cspan_make(v.data, 2, 6); + // View the same data as a 3D array 2 x 3 x 2 + ispan3 ms3 = cspan_make(v.data, 2, 3, 2); - // write data using 2D view - for (unsigned i=0; i != ms2.dim[0]; i++) - for (unsigned j=0; j != ms2.dim[1]; j++) - *cspan_at(&ms2, i, j) = i*1000 + j; + // write data using 2D view + for (unsigned i=0; i != ms2.dim[0]; i++) + for (unsigned j=0; j != ms2.dim[1]; j++) + *cspan_at(&ms2, i, j) = i*1000 + j; - // print all items using 1D view - printf("all: "); - for (unsigned i=0; i != cspan_size(&ms1); i++) - printf(" %d", *cspan_at(&ms1, i)); - puts(""); + // print all items using 1D view + printf("all: "); + for (unsigned i=0; i != cspan_size(&ms1); i++) + printf(" %d", *cspan_at(&ms1, i)); + puts(""); - // or iterate a subspan... - ispan2 sub = cspan_subdim3(&ms3, 1); - printf("sub: "); - c_FOREACH (i, ispan2, sub) - printf(" %d", *i.ref); - puts(""); + // or iterate a subspan... + ispan2 sub = cspan_subdim3(&ms3, 1); + printf("sub: "); + c_FOREACH (i, ispan2, sub) + printf(" %d", *i.ref); + puts(""); - // read back using 3D view - for (unsigned i=0; i != ms3.dim[0]; i++) - { - printf("slice @ i = %u\n", i); - for (unsigned j=0; j != ms3.dim[1]; j++) + // read back using 3D view + for (unsigned i=0; i != ms3.dim[0]; i++) { - for (unsigned k=0; k != ms3.dim[2]; k++) - printf("%d ", *cspan_at(&ms3, i, j, k)); - puts(""); + printf("slice @ i = %u\n", i); + for (unsigned j=0; j != ms3.dim[1]; j++) + { + for (unsigned k=0; k != ms3.dim[2]; k++) + printf("%d ", *cspan_at(&ms3, i, j, k)); + puts(""); + } } - } - cstack_int_drop(&v); + cstack_int_drop(&v); } diff --git a/misc/tests/cregex_test.c b/misc/tests/cregex_test.c index 8ac268f1..bd13d16a 100644 --- a/misc/tests/cregex_test.c +++ b/misc/tests/cregex_test.c @@ -233,3 +233,50 @@ CTEST(cregex, captures_cap) ASSERT_TRUE(cregex_is_match(&re, "abcdcdcd")); } } + +static bool add_10_years(int i, csview match, cstr* out) { + if (i == 1) { // group 1 matches year + int year; + sscanf(match.str, "%4d", &year); // scan 4 chars only + cstr_printf(out, "%04d", year + 10); + return true; + } + return false; +} + +CTEST(cregex, replace) +{ + const char* pattern = "\\b(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])\\b"; + const char* input = "start date: 2015-12-31, end date: 2022-02-28"; + + c_AUTO (cstr, str) { + /* replace with a fixed string, extended all-in-one call: */ + cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD")); + ASSERT_TRUE(cstr_equals(&str, "start date: YYYY-MM-DD, end date: YYYY-MM-DD")); + + /* US date format, and add 10 years to dates: */ + cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT)); + ASSERT_TRUE(cstr_equals(&str, "start date: 2025/31/12, end date: 2032/28/02")); + + /* Wrap first date inside []: */ + cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT)); + ASSERT_TRUE(cstr_equals(&str, "start date: [2015-12-31], end date: 2022-02-28")); + + /* Wrap all words in ${} */ + cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}")); + ASSERT_TRUE(cstr_equals(&str, "52 ${apples} ${and} 31 ${mangoes}")); + + /* Compile RE separately */ + c_WITH (cregex re = cregex_from(pattern, CREG_DEFAULT), cregex_drop(&re)) { + ASSERT_EQ(cregex_captures(&re), 4); + + /* European date format. */ + cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1")); + ASSERT_TRUE(cstr_equals(&str, "start date: 31.12.2015, end date: 28.02.2022")); + + /* Strip out everything but the matches */ + cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, CREG_R_STRIP)); + ASSERT_TRUE(cstr_equals(&str, "31.12.2015;28.02.2022;")); + } + } +} |
