diff options
| author | Tyge Løvset <[email protected]> | 2023-03-26 22:33:45 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-03-26 22:33:45 +0200 |
| commit | ede39bc98a758674485796174ea860515ec281e6 (patch) | |
| tree | 9cf3cb80a09be277d1581345701d54dda6ecd4f6 /misc | |
| parent | f2f9eaab3e347d04dfa98978e20fcdcdcb38c72d (diff) | |
| download | STC-modified-ede39bc98a758674485796174ea860515ec281e6.tar.gz STC-modified-ede39bc98a758674485796174ea860515ec281e6.zip | |
Remove more c_with, c_auto
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/examples/books.c | 97 | ||||
| -rw-r--r-- | misc/examples/splitstr.c | 8 | ||||
| -rw-r--r-- | misc/tests/cregex_test.c | 25 |
3 files changed, 66 insertions, 64 deletions
diff --git a/misc/examples/books.c b/misc/examples/books.c index e43c0bc3..40fe877f 100644 --- a/misc/examples/books.c +++ b/misc/examples/books.c @@ -9,54 +9,53 @@ int main() { cmap_str book_reviews = {0}; - c_defer( - cmap_str_drop(&book_reviews) - ){ - // Review some books. - cmap_str_emplace(&book_reviews, - "Adventures of Huckleberry Finn", - "My favorite book." - ); - cmap_str_emplace(&book_reviews, - "Grimms' Fairy Tales", - "Masterpiece." - ); - cmap_str_emplace(&book_reviews, - "Pride and Prejudice", - "Very enjoyable" - ); - cmap_str_insert(&book_reviews, - cstr_lit("The Adventures of Sherlock Holmes"), - cstr_lit("Eye lyked it alot.") - ); - - // Check for a specific one. - // When collections store owned values (String), they can still be - // queried using references (&str). - if (cmap_str_contains(&book_reviews, "Les Misérables")) { - printf("We've got %" c_ZI " reviews, but Les Misérables ain't one.", - cmap_str_size(&book_reviews)); - } - - // oops, this review has a lot of spelling mistakes, let's delete it. - cmap_str_erase(&book_reviews, "The Adventures of Sherlock Holmes"); - - // Look up the values associated with some keys. - const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"}; - c_forrange (i, c_ARRAYLEN(to_find)) { - const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]); - if (b) - printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second)); - else - printf("%s is unreviewed.\n", to_find[i]); - } - - // Look up the value for a key (will panic if the key is not found). - printf("Review for Jane: %s\n", cstr_str(cmap_str_at(&book_reviews, "Pride and Prejudice"))); - - // Iterate over everything. - c_forpair (book, review, cmap_str, book_reviews) { - printf("%s: \"%s\"\n", cstr_str(_.book), cstr_str(_.review)); - } + + // Review some books. + cmap_str_emplace(&book_reviews, + "Adventures of Huckleberry Finn", + "My favorite book." + ); + cmap_str_emplace(&book_reviews, + "Grimms' Fairy Tales", + "Masterpiece." + ); + cmap_str_emplace(&book_reviews, + "Pride and Prejudice", + "Very enjoyable" + ); + cmap_str_insert(&book_reviews, + cstr_lit("The Adventures of Sherlock Holmes"), + cstr_lit("Eye lyked it alot.") + ); + + // Check for a specific one. + // When collections store owned values (String), they can still be + // queried using references (&str). + if (cmap_str_contains(&book_reviews, "Les Misérables")) { + printf("We've got %" c_ZI " reviews, but Les Misérables ain't one.", + cmap_str_size(&book_reviews)); + } + + // oops, this review has a lot of spelling mistakes, let's delete it. + cmap_str_erase(&book_reviews, "The Adventures of Sherlock Holmes"); + + // Look up the values associated with some keys. + const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"}; + c_forrange (i, c_ARRAYLEN(to_find)) { + const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]); + if (b) + printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second)); + else + printf("%s is unreviewed.\n", to_find[i]); } + + // Look up the value for a key (will panic if the key is not found). + printf("Review for Jane: %s\n", cstr_str(cmap_str_at(&book_reviews, "Pride and Prejudice"))); + + // Iterate over everything. + c_forpair (book, review, cmap_str, book_reviews) { + printf("%s: \"%s\"\n", cstr_str(_.book), cstr_str(_.review)); + } + + cmap_str_drop(&book_reviews); } diff --git a/misc/examples/splitstr.c b/misc/examples/splitstr.c index 5bf02d42..bf90b5d4 100644 --- a/misc/examples/splitstr.c +++ b/misc/examples/splitstr.c @@ -10,10 +10,10 @@ int main() c_fortoken (i, "Hello World C99!", " ") printf("'%.*s'\n", c_SV(i.token)); - puts("\nSplit with c_formatch (regex):"); - c_with (cregex re = cregex_from("[^ ]+"), cregex_drop(&re)) - c_formatch (i, &re, " Hello World C99! ") - printf("'%.*s'\n", c_SV(i.match[0])); + cregex re = cregex_from("[^ ]+"); + c_formatch (i, &re, " Hello World C99! ") + printf("'%.*s'\n", c_SV(i.match[0])); + cregex_drop(&re); } diff --git a/misc/tests/cregex_test.c b/misc/tests/cregex_test.c index b3cc9f0a..aa4b2a65 100644 --- a/misc/tests/cregex_test.c +++ b/misc/tests/cregex_test.c @@ -248,8 +248,12 @@ 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) { + cstr str = {0}; + cregex re = {0}; + c_defer( + cstr_drop(&str), + cregex_drop(&re) + ){ // replace with a fixed string, extended all-in-one call: cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD")); ASSERT_STREQ(cstr_str(&str), "start date: YYYY-MM-DD, end date: YYYY-MM-DD"); @@ -267,16 +271,15 @@ CTEST(cregex, replace) ASSERT_STREQ(cstr_str(&str), "52 ${apples} ${and} 31 ${mangoes}"); // Compile RE separately - c_with (cregex re = cregex_from(pattern), cregex_drop(&re)) { - ASSERT_EQ(cregex_captures(&re), 4); + re = cregex_from(pattern); + ASSERT_EQ(cregex_captures(&re), 4); - // European date format. - cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1")); - ASSERT_STREQ(cstr_str(&str), "start date: 31.12.2015, end date: 28.02.2022"); + // European date format. + cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1")); + ASSERT_STREQ(cstr_str(&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_STREQ(cstr_str(&str), "31.12.2015;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_STREQ(cstr_str(&str), "31.12.2015;28.02.2022;"); } } |
