summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/stc/algo/filter.h30
-rw-r--r--include/stc/cbits.h40
-rw-r--r--include/stc/cmap.h28
-rw-r--r--include/stc/csmap.h28
-rw-r--r--misc/examples/books.c97
-rw-r--r--misc/examples/splitstr.c8
-rw-r--r--misc/tests/cregex_test.c25
-rw-r--r--src/checkauto.l6
8 files changed, 130 insertions, 132 deletions
diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h
index 7729799f..111d3273 100644
--- a/include/stc/algo/filter.h
+++ b/include/stc/algo/filter.h
@@ -24,24 +24,24 @@
#include <stdio.h>
#define i_val int
#include <stc/cstack.h>
-#include <stc/algo/filter.h>
+#include <stc/calgo.h>
int main()
{
- c_with (cstack_int stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7, 8, 9}),
- cstack_int_drop(&stk))
- {
- c_foreach (i, cstack_int, stk)
- printf(" %d", *i.ref);
- puts("");
-
- c_forfilter (i, cstack_int, stk,
- c_flt_skipwhile(i, *i.ref < 3) &&
- (*i.ref & 1) == 0 && // even only
- c_flt_take(i, 2)) // break after 2
- printf(" %d", *i.ref);
- puts("");
- }
+ cstack_int stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7, 8, 9});
+
+ c_foreach (i, cstack_int, stk)
+ printf(" %d", *i.ref);
+ puts("");
+
+ c_forfilter (i, cstack_int, stk,
+ c_flt_skipwhile(i, *i.ref < 3) &&
+ (*i.ref & 1) == 0 && // even only
+ c_flt_take(i, 2)) // break after 2
+ printf(" %d", *i.ref);
+ puts("");
+
+ cstack_int_drop(&stk);
}
*/
#ifndef STC_FILTER_H_INCLUDED
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index fa0da665..826df847 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -27,26 +27,26 @@ Similar to boost::dynamic_bitset / std::bitset
#include "cbits.h"
int main() {
- c_with (cbits bset = cbits_with_size(23, true), cbits_drop(&bset))
- {
- cbits_reset(&bset, 9);
- cbits_resize(&bset, 43, false);
-
- printf("%4zu: ", cbits_size(&bset));
- c_forrange (i, cbits_size(&bset))
- printf("%d", cbits_at(&bset, i));
- puts("");
- cbits_set(&bset, 28);
- cbits_resize(&bset, 77, true);
- cbits_resize(&bset, 93, false);
- cbits_resize(&bset, 102, true);
- cbits_set_value(&bset, 99, false);
-
- printf("%4zu: ", cbits_size(&bset));
- c_forrange (i, cbits_size(&bset))
- printf("%d", cbits_at(&bset, i));
- puts("");
- }
+ cbits bset = cbits_with_size(23, true);
+ cbits_reset(&bset, 9);
+ cbits_resize(&bset, 43, false);
+
+ printf("%4zu: ", cbits_size(&bset));
+ c_forrange (i, cbits_size(&bset))
+ printf("%d", cbits_at(&bset, i));
+ puts("");
+ cbits_set(&bset, 28);
+ cbits_resize(&bset, 77, true);
+ cbits_resize(&bset, 93, false);
+ cbits_resize(&bset, 102, true);
+ cbits_set_value(&bset, 99, false);
+
+ printf("%4zu: ", cbits_size(&bset));
+ c_forrange (i, cbits_size(&bset))
+ printf("%d", cbits_at(&bset, i));
+ puts("");
+
+ cbits_drop(&bset);
}
*/
#ifndef CBITS_H_INCLUDED
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index d9081ae0..402038cb 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -31,20 +31,20 @@
#include <stc/cmap.h>
int main(void) {
- c_with (cmap_ichar m = cmap_ichar_init(), cmap_ichar_drop(&m))
- {
- cmap_ichar_emplace(&m, 5, 'a');
- cmap_ichar_emplace(&m, 8, 'b');
- cmap_ichar_emplace(&m, 12, 'c');
-
- cmap_ichar_value* v = cmap_ichar_get(&m, 10); // NULL
- char val = *cmap_ichar_at(&m, 5); // 'a'
- cmap_ichar_emplace_or_assign(&m, 5, 'd'); // update
- cmap_ichar_erase(&m, 8);
-
- c_foreach (i, cmap_ichar, m)
- printf("map %d: %c\n", i.ref->first, i.ref->second);
- }
+ cmap_ichar m = {0};
+ cmap_ichar_emplace(&m, 5, 'a');
+ cmap_ichar_emplace(&m, 8, 'b');
+ cmap_ichar_emplace(&m, 12, 'c');
+
+ cmap_ichar_value* v = cmap_ichar_get(&m, 10); // NULL
+ char val = *cmap_ichar_at(&m, 5); // 'a'
+ cmap_ichar_emplace_or_assign(&m, 5, 'd'); // update
+ cmap_ichar_erase(&m, 8);
+
+ c_foreach (i, cmap_ichar, m)
+ printf("map %d: %c\n", i.ref->first, i.ref->second);
+
+ cmap_ichar_drop(&m);
}
*/
#include "ccommon.h"
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 0f72ca2d..2b1910e9 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -32,20 +32,20 @@
#include <stc/csmap.h>
int main(void) {
- c_with (csmap_sx m = csmap_sx_init(), csmap_sx_drop(&m))
- {
- csmap_sx_emplace(&m, "Testing one", 1.234);
- csmap_sx_emplace(&m, "Testing two", 12.34);
- csmap_sx_emplace(&m, "Testing three", 123.4);
-
- csmap_sx_value *v = csmap_sx_get(&m, "Testing five"); // NULL
- double num = *csmap_sx_at(&m, "Testing one");
- csmap_sx_emplace_or_assign(&m, "Testing three", 1000.0); // update
- csmap_sx_erase(&m, "Testing two");
-
- c_foreach (i, csmap_sx, m)
- printf("map %s: %g\n", cstr_str(&i.ref->first), i.ref->second);
- }
+ csmap_sx m = {0};
+ csmap_sx_emplace(&m, "Testing one", 1.234);
+ csmap_sx_emplace(&m, "Testing two", 12.34);
+ csmap_sx_emplace(&m, "Testing three", 123.4);
+
+ csmap_sx_value *v = csmap_sx_get(&m, "Testing five"); // NULL
+ double num = *csmap_sx_at(&m, "Testing one");
+ csmap_sx_emplace_or_assign(&m, "Testing three", 1000.0); // update
+ csmap_sx_erase(&m, "Testing two");
+
+ c_foreach (i, csmap_sx, m)
+ printf("map %s: %g\n", cstr_str(&i.ref->first), i.ref->second);
+
+ csmap_sx_drop(&m);
}
*/
#ifdef STC_CSMAP_V1
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;");
}
}
diff --git a/src/checkauto.l b/src/checkauto.l
index 349da70b..a8f14abb 100644
--- a/src/checkauto.l
+++ b/src/checkauto.l
@@ -41,13 +41,9 @@ for |
while |
switch { block_type |= LOOP; state = BRACES; }
do { block_type |= LOOP; state = BRACESDONE; }
-c_with |
-c_scope |
c_defer |
-c_auto |
-c_with |
c_scope |
-c_defer |
+c_with |
c_auto { block_type = AUTO; state = BRACES; }
\( { if (state == BRACES) ++braces_lev; }
\) { if (state == BRACES && --braces_lev == 0) {