diff options
| author | Tyge Løvset <[email protected]> | 2023-02-08 16:16:49 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-08 17:18:24 +0100 |
| commit | c4441f5fc665194fbd7a894a67a64a08c3beac42 (patch) | |
| tree | 82f231b6e8fcb75625166f98aa785baaa265a3d6 /docs | |
| parent | 673dd5319a488d4b702b94dd9aeda4e497ae4fbc (diff) | |
| download | STC-modified-c4441f5fc665194fbd7a894a67a64a08c3beac42.tar.gz STC-modified-c4441f5fc665194fbd7a894a67a64a08c3beac42.zip | |
Changed to use lowercase flow-control macros in examples (uppercase will still be supported). Improved many examples to use c_make() to init containers.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/carc_api.md | 16 | ||||
| -rw-r--r-- | docs/cbox_api.md | 15 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 132 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 8 | ||||
| -rw-r--r-- | docs/clist_api.md | 30 | ||||
| -rw-r--r-- | docs/cmap_api.md | 32 | ||||
| -rw-r--r-- | docs/cpque_api.md | 8 | ||||
| -rw-r--r-- | docs/cqueue_api.md | 6 | ||||
| -rw-r--r-- | docs/crandom_api.md | 4 | ||||
| -rw-r--r-- | docs/cregex_api.md | 10 | ||||
| -rw-r--r-- | docs/cset_api.md | 17 | ||||
| -rw-r--r-- | docs/csmap_api.md | 24 | ||||
| -rw-r--r-- | docs/cspan_api.md | 22 | ||||
| -rw-r--r-- | docs/csset_api.md | 15 | ||||
| -rw-r--r-- | docs/cstr_api.md | 2 | ||||
| -rw-r--r-- | docs/csview_api.md | 16 | ||||
| -rw-r--r-- | docs/cvec_api.md | 12 |
17 files changed, 179 insertions, 190 deletions
diff --git a/docs/carc_api.md b/docs/carc_api.md index f91975a7..cc6c9c32 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -98,24 +98,24 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); int main() { - c_AUTO (Stack, s1, s2) // RAII + c_auto (Stack, s1, s2) // RAII { // POPULATE s1 with shared pointers to Map: Map *map; map = Stack_push(&s1, Arc_make(Map_init()))->get; // push empty map to s1. - c_FORLIST (i, Map_raw, { {"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992} }) { + c_forlist (i, Map_raw, { {"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992} }) { Map_emplace(map, c_PAIR(i.ref)); // populate it. } map = Stack_push(&s1, Arc_make(Map_init()))->get; - c_FORLIST (i, Map_raw, { {"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980} }) { + c_forlist (i, Map_raw, { {"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980} }) { Map_emplace(map, c_PAIR(i.ref)); } // POPULATE s2: map = Stack_push(&s2, Arc_make(Map_init()))->get; - c_FORLIST (i, Map_raw, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }) { + c_forlist (i, Map_raw, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }) { Map_emplace(map, c_PAIR(i.ref)); } @@ -134,15 +134,15 @@ int main() Map_emplace_or_assign(s1.data[1].get, "Shared", 2022); puts("S1"); - c_FOREACH (i, Stack, s1) { - c_FORPAIR (name, year, Map, *i.ref->get) + c_foreach (i, Stack, s1) { + c_forpair (name, year, Map, *i.ref->get) printf(" %s:%d", cstr_str(_.name), *_.year); puts(""); } puts("S2"); - c_FOREACH (i, Stack, s2) { - c_FORPAIR (name, year, Map, *i.ref->get) + c_foreach (i, Stack, s2) { + c_forpair (name, year, Map, *i.ref->get) printf(" %s:%d", cstr_str(_.name), *_.year); puts(""); } diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 4430b9f8..eff0fbc1 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -92,18 +92,17 @@ void int_drop(int* x) { int main() { - c_AUTO (IVec, vec) // declare and init vec, call drop at scope exit - c_AUTO (ISet, set) // similar + c_auto (IVec, vec) // declare and init vec, call drop at scope exit + c_auto (ISet, set) // similar { - c_FORLIST (i, int, {2021, 2012, 2022, 2015}) - IVec_emplace(&vec, *i.ref); // same as: IVec_push(&vec, IBox_from(*i.ref)); + vec = c_make(Vec, {2021, 2012, 2022, 2015}); printf("vec:"); - c_FOREACH (i, IVec, vec) + c_foreach (i, IVec, vec) printf(" %d", *i.ref->get); // add odd numbers from vec to set - c_FOREACH (i, IVec, vec) + c_foreach (i, IVec, vec) if (*i.ref->get & 1) ISet_insert(&set, IBox_clone(*i.ref)); @@ -112,11 +111,11 @@ int main() IVec_pop(&vec); printf("\nvec:"); - c_FOREACH (i, IVec, vec) + c_foreach (i, IVec, vec) printf(" %d", *i.ref->get); printf("\nset:"); - c_FOREACH (i, ISet, set) + c_foreach (i, ISet, set) printf(" %d", *i.ref->get); } } diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index b0bd18e4..04af832b 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -3,27 +3,27 @@ The following macros are recommended to use, and they safe/have no side-effects. ## Scope macros (RAII) -### c_AUTO, c_WITH, c_SCOPE, c_DEFER +### c_auto, c_with, c_scope, c_defer General ***defer*** mechanics for resource acquisition. These macros allows you to specify the freeing of the resources at the point where the acquisition takes place. -The **checkauto** utility described below, ensures that the `c_AUTO*` macros are used correctly. +The **checkauto** utility described below, ensures that the `c_auto*` macros are used correctly. | Usage | Description | |:---------------------------------------|:----------------------------------------------------------| -| `c_WITH (Type var=init, drop)` | Declare `var`. Defer `drop...` to end of scope | -| `c_WITH (Type var=init, pred, drop)` | Adds a predicate in order to exit early if init failed | -| `c_AUTO (Type, var1,...,var4)` | `c_WITH (Type var1=Type_init(), Type_drop(&var1))` ... | -| `c_SCOPE (init, drop)` | Execute `init` and defer `drop` to end of scope | -| `c_DEFER (drop...)` | Defer `drop...` to end of scope | +| `c_with (Type var=init, drop)` | Declare `var`. Defer `drop...` to end of scope | +| `c_with (Type var=init, pred, drop)` | Adds a predicate in order to exit early if init failed | +| `c_auto (Type, var1,...,var4)` | `c_with (Type var1=Type_init(), Type_drop(&var1))` ... | +| `c_scope (init, drop)` | Execute `init` and defer `drop` to end of scope | +| `c_defer (drop...)` | Defer `drop...` to end of scope | | `continue` | Exit a block above without memory leaks | -For multiple variables, use either multiple **c_WITH** in sequence, or declare variable outside -scope and use **c_SCOPE**. For convenience, **c_AUTO** support up to 4 variables. +For multiple variables, use either multiple **c_with** in sequence, or declare variable outside +scope and use **c_scope**. For convenience, **c_auto** support up to 4 variables. ```c -// `c_WITH` is similar to python `with`: it declares and can drop a variable after going out of scope. +// `c_with` is similar to python `with`: it declares and can drop a variable after going out of scope. bool ok = false; -c_WITH (uint8_t* buf = malloc(BUF_SIZE), buf != NULL, free(buf)) -c_WITH (FILE* fp = fopen(fname, "rb"), fp != NULL, fclose(fp)) +c_with (uint8_t* buf = malloc(BUF_SIZE), buf != NULL, free(buf)) +c_with (FILE* fp = fopen(fname, "rb"), fp != NULL, fclose(fp)) { int n = fread(buf, 1, BUF_SIZE, fp); if (n <= 0) continue; // auto cleanup! NB do not break or return here. @@ -32,8 +32,8 @@ c_WITH (FILE* fp = fopen(fname, "rb"), fp != NULL, fclose(fp)) } return ok; -// `c_AUTO` automatically initialize and destruct up to 4 variables, like c_WITH. -c_AUTO (cstr, s1, s2) +// `c_auto` automatically initialize and destruct up to 4 variables, like c_with. +c_auto (cstr, s1, s2) { cstr_append(&s1, "Hello"); cstr_append(&s1, " world"); @@ -44,22 +44,22 @@ c_AUTO (cstr, s1, s2) printf("%s %s\n", cstr_str(&s1), cstr_str(&s2)); } -c_WITH (cstr str = cstr_lit("Hello"), cstr_drop(&str)) +c_with (cstr str = cstr_lit("Hello"), cstr_drop(&str)) { cstr_append(&str, " world"); printf("%s\n", cstr_str(&str)); } -// `c_SCOPE` is like `c_WITH` but works with an already declared variable. +// `c_scope` is like `c_with` but works with an already declared variable. static pthread_mutex_t mut; -c_SCOPE (pthread_mutex_lock(&mut), pthread_mutex_unlock(&mut)) +c_scope (pthread_mutex_lock(&mut), pthread_mutex_unlock(&mut)) { /* Do syncronized work. */ } -// `c_DEFER` executes the expressions when leaving scope. Prefer c_WITH or c_SCOPE. +// `c_defer` executes the expressions when leaving scope. Prefer c_with or c_scope. cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world"); -c_DEFER (cstr_drop(&s1), cstr_drop(&s2)) +c_defer (cstr_drop(&s1), cstr_drop(&s2)) { printf("%s %s\n", cstr_str(&s1), cstr_str(&s2)); } @@ -77,8 +77,8 @@ cvec_str readFile(const char* name) { cvec_str vec = cvec_str_init(); // returned - c_WITH (FILE* fp = fopen(name, "r"), fp != NULL, fclose(fp)) - c_WITH (cstr line = cstr_NULL, cstr_drop(&line)) + c_with (FILE* fp = fopen(name, "r"), fp != NULL, fclose(fp)) + c_with (cstr line = cstr_NULL, cstr_drop(&line)) while (cstr_getline(&line, fp)) cvec_str_emplace_back(&vec, cstr_str(&line)); return vec; @@ -86,30 +86,30 @@ cvec_str readFile(const char* name) int main() { - c_WITH (cvec_str x = readFile(__FILE__), cvec_str_drop(&x)) - c_FOREACH (i, cvec_str, x) + c_with (cvec_str x = readFile(__FILE__), cvec_str_drop(&x)) + c_foreach (i, cvec_str, x) printf("%s\n", cstr_str(i.ref)); } ``` ### The **checkauto** utility program (for RAII) -The **checkauto** program will check the source code for any misuses of the `c_AUTO*` macros which -may lead to resource leakages. The `c_AUTO*`- macros are implemented as one-time executed **for-loops**, +The **checkauto** program will check the source code for any misuses of the `c_auto*` macros which +may lead to resource leakages. The `c_auto*`- macros are implemented as one-time executed **for-loops**, so any `return` or `break` appearing within such a block will lead to resource leaks, as it will disable the cleanup/drop method to be called. A `break` may originally be intended to break a loop or switch -outside the `c_AUTO` scope. +outside the `c_auto` scope. -NOTE: One must always make sure to unwind temporary allocated resources before a `return` in C. However, by using `c_AUTO*`-macros, +NOTE: One must always make sure to unwind temporary allocated resources before a `return` in C. However, by using `c_auto*`-macros, - it is much easier to automatically detect misplaced return/break between resource acquisition and destruction. - it prevents forgetting to call the destructor at the end. The **checkauto** utility will report any misusages. The following example shows how to correctly break/return -from a `c_AUTO` scope: +from a `c_auto` scope: ```c int flag = 0; for (int i = 0; i<n; ++i) { - c_AUTO (cstr, text) - c_AUTO (List, list) + c_auto (cstr, text) + c_auto (List, list) { for (int j = 0; j<m; ++j) { List_push_back(&list, i*j); @@ -118,23 +118,23 @@ from a `c_AUTO` scope: } // WRONG: if (cond2()) - break; // checkauto ERROR! break inside c_AUTO. + break; // checkauto ERROR! break inside c_auto. if (cond3()) - return -1; // checkauto ERROR! return inside c_AUTO + return -1; // checkauto ERROR! return inside c_auto // CORRECT: if (cond2()) { flag = 1; // flag to break outer for-loop - continue; // cleanup and leave c_AUTO block + continue; // cleanup and leave c_auto block } if (cond3()) { flag = -1; // return -1 - continue; // cleanup and leave c_AUTO block + continue; // cleanup and leave c_auto block } ... } - // do the return/break outside of c_AUTO + // do the return/break outside of c_auto if (flag < 0) return flag; else if (flag > 0) break; ... @@ -142,33 +142,33 @@ from a `c_AUTO` scope: ``` ## Loop abstraction macros -### c_FORLIST +### c_forlist Iterate compound literal array elements. Additional to `i.ref`, you can access `i.data`, `i.size`, and `i.index` of the input list/element. ```c // apply multiple push_backs -c_FORLIST (i, int, {1, 2, 3}) +c_forlist (i, int, {1, 2, 3}) cvec_i_push_back(&vec, *i.ref); // insert in existing map -c_FORLIST (i, cmap_ii_raw, { {4, 5}, {6, 7} }) +c_forlist (i, cmap_ii_raw, { {4, 5}, {6, 7} }) cmap_ii_insert(&map, i.ref->first, i.ref->second); // string literals pushed to a stack of cstr: -c_FORLIST (i, const char*, {"Hello", "crazy", "world"}) +c_forlist (i, const char*, {"Hello", "crazy", "world"}) cstack_str_emplace(&stk, *i.ref); // reverse the list: -c_FORLIST (i, int, {1, 2, 3}) +c_forlist (i, int, {1, 2, 3}) cvec_i_push_back(&vec, i.data[i.size - 1 - i.index]); ``` -### c_FOREACH, c_FORPAIR +### c_foreach, c_forpair | Usage | Description | |:-----------------------------------------|:--------------------------------| -| `c_FOREACH (it, ctype, container)` | Iteratate all elements | -| `c_FOREACH (it, ctype, it1, it2)` | Iterate the range [it1, it2) | -| `c_FORPAIR (key, val, ctype, container)` | Iterate with structured binding | +| `c_foreach (it, ctype, container)` | Iteratate all elements | +| `c_foreach (it, ctype, it1, it2)` | Iterate the range [it1, it2) | +| `c_forpair (key, val, ctype, container)` | Iterate with structured binding | ```c #define i_key int @@ -176,52 +176,52 @@ c_FORLIST (i, int, {1, 2, 3}) #define i_tag ii #include <stc/csmap.h> ... -c_FORLIST (i, csmap_ii_raw, { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} }) +c_forlist (i, csmap_ii_raw, { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} }) csmap_ii_insert(&map, i.ref->first, i.ref->second); -c_FOREACH (i, csmap_ii, map) +c_foreach (i, csmap_ii, map) printf(" %d", i.ref->first); // 3 5 7 12 23 csmap_ii_iter it = csmap_ii_find(&map, 7); -c_FOREACH (i, csmap_ii, it, csmap_ii_end(&map)) +c_foreach (i, csmap_ii, it, csmap_ii_end(&map)) printf(" %d", i.ref->first); // 7 12 23 -c_FORPAIR (id, count, csmap_ii, map) +c_forpair (id, count, csmap_ii, map) printf(" (%d %d)", *_.id, *_.count); // (3 2) (5 4) (7 3) (12 5) (23 1) ``` -### c_FORRANGE +### c_forrange Abstraction for iterating sequence of numbers. Like python's **for** *i* **in** *range()* loop. | Usage | Python equivalent | |:--------------------------------------------|:-------------------------------------| -| `c_FORRANGE (stop)` | `for _ in range(stop):` | -| `c_FORRANGE (i, stop) // i type = long long` | `for i in range(stop):` | -| `c_FORRANGE (i, start, stop)` | `for i in range(start, stop):` | -| `c_FORRANGE (i, start, stop, step)` | `for i in range(start, stop, step):` | +| `c_forrange (stop)` | `for _ in range(stop):` | +| `c_forrange (i, stop) // i type = long long` | `for i in range(stop):` | +| `c_forrange (i, start, stop)` | `for i in range(start, stop):` | +| `c_forrange (i, start, stop, step)` | `for i in range(start, stop, step):` | ```c -c_FORRANGE (5) printf("x"); +c_forrange (5) printf("x"); // xxxxx -c_FORRANGE (i, 5) printf(" %lld", i); +c_forrange (i, 5) printf(" %lld", i); // 0 1 2 3 4 -c_FORRANGE (i, -3, 3) printf(" %lld", i); +c_forrange (i, -3, 3) printf(" %lld", i); // -3 -2 -1 0 1 2 -c_FORRANGE (i, 30, 0, -5) printf(" %lld", i); +c_forrange (i, 30, 0, -5) printf(" %lld", i); // 30 25 20 15 10 5 ``` -### c_FORWHILE, c_FORFILTER +### c_forwhile, c_forfilter Iterate containers with stop-criteria and chained range filtering. | Usage | Description | |:----------------------------------------------------|:---------------------------------------| -| `c_FORWHILE (it, ctype, start, pred)` | Iterate until pred is false | -| `c_FORFILTER (it, ctype, container, filter)` | Filter out items in chain with && | -| `c_FORFILTER (it, ctype, container, filter, pred)` | Filter and iterate until pred is false | +| `c_forwhile (it, ctype, start, pred)` | Iterate until pred is false | +| `c_forfilter (it, ctype, container, filter)` | Filter out items in chain with && | +| `c_forfilter (it, ctype, container, filter, pred)` | Filter and iterate until pred is false | | Built-in filter | Description | |:----------------------------------|:-------------------------------------| @@ -244,10 +244,10 @@ bool isPrime(int i) { #define isOdd(i) ((i) & 1) int main() { - c_AUTO (IVec, vec) { - c_FORRANGE (i, 1000) IVec_push(&vec, 1000000 + i); + c_auto (IVec, vec) { + c_forrange (i, 1000) IVec_push(&vec, 1000000 + i); - c_FORFILTER (i, IVec, vec, + c_forfilter (i, IVec, vec, isOdd(*i.ref) && c_flt_skip(i, 100) // built-in && isPrime(*i.ref) @@ -302,14 +302,14 @@ void crange_next(crange_iter* it); // 1. All primes less than 32: crange r1 = crange_make(3, 32, 2); printf("2"); // first prime -c_FORFILTER (i, crange, r1 +c_forfilter (i, crange, r1 , isPrime(*i.ref)) printf(" %lld", *i.ref); // 2 3 5 7 11 13 17 19 23 29 31 // 2. The 11 first primes: printf("2"); -c_FORFILTER (i, crange, crange_obj(3, INT64_MAX, 2) +c_forfilter (i, crange, crange_obj(3, INT64_MAX, 2) , isPrime(*i.ref) , c_flt_take(10)) printf(" %lld", *i.ref); diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 716a608c..fc11fe66 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -109,14 +109,14 @@ cdeq_X_value cdeq_X_value_clone(cdeq_X_value val); int main() { cdeq_i q = cdeq_i_init(); cdeq_i_push_front(&q, 10); - c_FOREACH (i, cdeq_i, q) + c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); puts(""); - c_FORLIST (i, int, {1, 4, 5, 22, 33, 2}) + c_forlist (i, int, {1, 4, 5, 22, 33, 2}) cdeq_i_push_back(&q, *i.ref) - c_FOREACH (i, cdeq_i, q) + c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); puts(""); @@ -125,7 +125,7 @@ int main() { cdeq_i_push_back(&q, 11); cdeq_i_push_front(&q, 8); - c_FOREACH (i, cdeq_i, q) + c_foreach (i, cdeq_i, q) printf(" %d", *i.ref); puts(""); cdeq_i_drop(&q); diff --git a/docs/clist_api.md b/docs/clist_api.md index 29bfd5ff..3e73d42b 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -119,24 +119,21 @@ Interleave *push_front()* / *push_back()* then *sort()*: #include <stdio.h> int main() { - DList list = DList_init(); + DList list = c_make(DList, {10., 20., 30., 40., 50., 60., 70., 80., 90.}); - c_FORLIST (i, double, {10., 20., 30., 40., 50., 60., 70., 80., 90.}) - DList_push_back(&list, *i.ref); - - c_FORRANGE (i, 1, 10) { + c_forrange (i, 1, 10) { if (i & 1) DList_push_front(&list, (double) i); else DList_push_back(&list, (double) i); } printf("initial: "); - c_FOREACH (i, DList, list) + c_foreach (i, DList, list) printf(" %g", *i.ref); DList_sort(&list); // mergesort O(n*log n) printf("\nsorted: "); - c_FOREACH (i, DList, list) + c_foreach (i, DList, list) printf(" %g", *i.ref); DList_drop(&list); @@ -160,10 +157,7 @@ Use of *erase_at()* and *erase_range()*: int main () { - clist_i L = clist_i_init(); - - c_FORLIST (i, int, {10, 20, 30, 40, 50}) - clist_i_push_back(&L, *i.ref); + clist_i L = c_make(clist_i, {10, 20, 30, 40, 50}); // 10 20 30 40 50 clist_i_iter it = clist_i_begin(&L); // ^ clist_i_next(&it); @@ -174,7 +168,7 @@ int main () it = clist_i_erase_range(&L, it, end); // 10 30 // ^ printf("mylist contains:"); - c_FOREACH (x, clist_i, L) + c_foreach (x, clist_i, L) printf(" %d", *x.ref); puts(""); @@ -197,21 +191,19 @@ Splice `[30, 40]` from *L2* into *L1* before `3`: #include <stdio.h> int main() { - c_AUTO (clist_i, L1, L2) + c_auto (clist_i, L1, L2) { - c_FORLIST (i, int, {1, 2, 3, 4, 5}) - clist_i_push_back(&L1, *i.ref); - c_FORLIST (i, int, {10, 20, 30, 40, 50}) - clist_i_push_back(&L2, *i.ref); + L1 = c_make(clist_i, {1, 2, 3, 4, 5}); + L2 = c_make(clist_i, {10, 20, 30, 40, 50}); clist_i_iter i = clist_i_advance(clist_i_begin(&L1), 2); clist_i_iter j1 = clist_i_advance(clist_i_begin(&L2), 2), j2 = clist_i_advance(j1, 2); clist_i_splice_range(&L1, i, &L2, j1, j2); - c_FOREACH (i, clist_i, L1) + c_foreach (i, clist_i, L1) printf(" %d", *i.ref); puts(""); - c_FOREACH (i, clist_i, L2) + c_foreach (i, clist_i, L2) printf(" %d", *i.ref); puts(""); } } diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 0b91abc7..263aff10 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -126,16 +126,16 @@ bool c_memcmp_eq(const i_keyraw* a, const i_keyraw* b); // ! int main() { // Create an unordered_map of three strings (that map to strings) - c_AUTO (cmap_str, u) + c_auto (cmap_str, u) { - c_FORLIST (i, cmap_str_raw, { + u = c_make(cmap_str, { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} - }) cmap_str_emplace(&u, c_PAIR(i.ref)); + }); // Iterate and print keys and values of unordered map - c_FOREACH (n, cmap_str, u) { + c_foreach (n, cmap_str, u) { printf("Key:[%s] Value:[%s]\n", cstr_str(&n.ref->first), cstr_str(&n.ref->second)); } @@ -172,9 +172,9 @@ int main() { uint32_t col = 0xcc7744ff; - c_AUTO (cmap_id, idnames) + c_auto (cmap_id, idnames) { - c_FORLIST (i, cmap_id_raw, { {100, "Red"}, {110, "Blue"} }) + c_forlist (i, cmap_id_raw, { {100, "Red"}, {110, "Blue"} }) cmap_id_emplace(&idnames, c_PAIR(i.ref)); // replace existing mapped value: @@ -186,7 +186,7 @@ int main() // emplace/insert does nothing if key already exist: cmap_id_emplace(&idnames, 100, "Green"); - c_FOREACH (i, cmap_id, idnames) + c_foreach (i, cmap_id, idnames) printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second)); } } @@ -213,14 +213,14 @@ typedef struct { int x, y, z; } Vec3i; int main() { // Define map with defered destruct - c_WITH (cmap_vi vecs = cmap_vi_init(), cmap_vi_drop(&vecs)) + c_with (cmap_vi vecs = cmap_vi_init(), cmap_vi_drop(&vecs)) { cmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); cmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); cmap_vi_insert(&vecs, (Vec3i){ 0, 0, 100}, 3); cmap_vi_insert(&vecs, (Vec3i){100, 100, 100}, 4); - c_FORPAIR (v3, num, cmap_vi, vecs) + c_forpair (v3, num, cmap_vi, vecs) printf("{ %3d, %3d, %3d }: %d\n", _.v3->x, _.v3->y, _.v3->z, *_.num); } } @@ -246,14 +246,14 @@ typedef struct { int x, y, z; } Vec3i; int main() { - c_AUTO (cmap_iv, vecs) // shorthand for c_WITH with _init(), _drop(). + c_auto (cmap_iv, vecs) // shorthand for c_with with _init(), _drop(). { cmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); cmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); cmap_iv_insert(&vecs, 3, (Vec3i){ 0, 0, 100}); cmap_iv_insert(&vecs, 4, (Vec3i){100, 100, 100}); - c_FORPAIR (num, v3, cmap_iv, vecs) + c_forpair (num, v3, cmap_iv, vecs) printf("%d: { %3d, %3d, %3d }\n", *_.num, _.v3->x, _.v3->y, _.v3->z); } } @@ -313,20 +313,20 @@ static inline void Viking_drop(Viking* vk) { int main() { // Use a HashMap to store the vikings' health points. - c_AUTO (Vikings, vikings) // uses Vikings_init(), Vikings_drop() + c_auto (Vikings, vikings) // uses Vikings_init(), Vikings_drop() { Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Norway")}, 25); Vikings_insert(&vikings, (Viking){cstr_lit("Olaf"), cstr_lit("Denmark")}, 24); Vikings_insert(&vikings, (Viking){cstr_lit("Harald"), cstr_lit("Iceland")}, 12); Vikings_insert(&vikings, (Viking){cstr_lit("Einar"), cstr_lit("Denmark")}, 21); - c_AUTO (Viking, lookup) { + c_auto (Viking, lookup) { lookup = (Viking){cstr_lit("Einar"), cstr_lit("Norway")}; printf("Lookup: Einar of Norway has %d hp\n\n", *Vikings_at(&vikings, lookup)); } // Print the status of the vikings. - c_FORPAIR (vik, hp, Vikings, vikings) { + c_forpair (vik, hp, Vikings, vikings) { printf("%s of %s has %d hp\n", cstr_str(&_.vik->name), cstr_str(&_.vik->country), *_.hp); } } @@ -396,7 +396,7 @@ static inline RViking Viking_toraw(const Viking* vp) { int main() { - c_AUTO (Vikings, vikings) + c_auto (Vikings, vikings) { Vikings_emplace(&vikings, (RViking){"Einar", "Norway"}, 20); Vikings_emplace(&vikings, (RViking){"Olaf", "Denmark"}, 24); @@ -406,7 +406,7 @@ int main() Vikings_value *v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); if (v) v->second += 3; // add 3 hp points to Einar - c_FORPAIR (vk, hp, Vikings, vikings) { + c_forpair (vk, hp, Vikings, vikings) { printf("%s of %s has %d hp\n", cstr_str(&_.vk->name), cstr_str(&_.vk->country), *_.hp); } } diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 48a5b29e..991623d7 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -76,19 +76,19 @@ int main() stc64_uniform_t dist = stc64_uniform_new(0, N * 10); // Declare heap, with defered drop() - c_AUTO (cpque_i, heap) + c_auto (cpque_i, heap) { // Push ten million random numbers to priority queue. - c_FORRANGE (N) + c_forrange (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); // Add some negative ones. int nums[] = {-231, -32, -873, -4, -343}; - c_FORRANGE (i, c_ARRAYLEN(nums)) + c_forrange (i, c_ARRAYLEN(nums)) cpque_i_push(&heap, nums[i]); // Extract and display the fifty smallest. - c_FORRANGE (50) { + c_forrange (50) { printf("%" PRId64 " ", *cpque_i_top(&heap)); cpque_i_pop(&heap); } diff --git a/docs/cqueue_api.md b/docs/cqueue_api.md index 212cdabe..9ea4b148 100644 --- a/docs/cqueue_api.md +++ b/docs/cqueue_api.md @@ -70,13 +70,13 @@ int main() { cqueue_i Q = cqueue_i_init(); // push() and pop() a few. - c_FORRANGE (i, 20) + c_forrange (i, 20) cqueue_i_push(&Q, i); - c_FORRANGE (5) + c_forrange (5) cqueue_i_pop(&Q); - c_FOREACH (i, cqueue_i, Q) + c_foreach (i, cqueue_i, Q) printf(" %d", *i.ref); cqueue_i_drop(&Q); diff --git a/docs/crandom_api.md b/docs/crandom_api.md index 391c485f..e69cc539 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -94,14 +94,14 @@ int main() // Create histogram map csmap_i mhist = csmap_i_init(); - c_FORRANGE (N) { + c_forrange (N) { int index = (int) round( stc64_normalf(&rng, &dist) ); csmap_i_emplace(&mhist, index, 0).ref->second += 1; } // Print the gaussian bar chart cstr bar = cstr_init(); - c_FOREACH (i, csmap_i, mhist) { + c_foreach (i, csmap_i, mhist) { int n = (int)(i.ref->second * StdDev * Scale * 2.5 / N); if (n > 0) { cstr_resize(&bar, n, '*'); diff --git a/docs/cregex_api.md b/docs/cregex_api.md index a115b4af..8cabb6fc 100644 --- a/docs/cregex_api.md +++ b/docs/cregex_api.md @@ -130,19 +130,19 @@ if (cregex_find_pattern(pattern, input, match, CREG_DEFAULT)) To compile, use: `gcc first_match.c src/cregex.c src/utf8code.c`. In order to use a callback function in the replace call, see `examples/regex_replace.c`. -### Iterate through regex matches, *c_FORMATCH* +### Iterate through regex matches, *c_formatch* To iterate multiple matches in an input string, you may use ```c csview match[5] = {0}; while (cregex_find(&re, input, match, CREG_M_NEXT) == CREG_OK) - c_FORRANGE (k, cregex_captures(&re)) + c_forrange (k, cregex_captures(&re)) printf("submatch %lld: %.*s\n", k, c_SVARG(match[k])); ``` There is also a safe macro which simplifies this: ```c -c_FORMATCH (it, &re, input) - c_FORRANGE (k, cregex_captures(&re)) +c_formatch (it, &re, input) + c_forrange (k, cregex_captures(&re)) printf("submatch %lld: %.*s\n", k, c_SVARG(it.match[k])); ``` @@ -223,7 +223,7 @@ For reference, **cregex** uses the following files: ## Limitations The main goal of **cregex** is to be small and fast with limited but useful unicode support. In order to reach these goals, **cregex** currently does not support the following features (non-exhaustive list): -- In order to limit table sizes, most general UTF8 character classes are missing, like \p{L}, \p{S}, and all specific scripts like \p{Greek} etc. Some/all of these may be added in the future as an alternative source file with unicode tables to link with. +- In order to limit table sizes, most general UTF8 character classes are missing, like \p{L}, \p{S}, and most specific scripts like \p{Tibetan}. Some/all of these may be added in the future as an alternative source file with unicode tables to link with. Currently, only characters from from the Basic Multilingual Plane (BMP) are supported, which contains most commonly used characters (i.e. none of the "supplementary planes"). - {n, m} syntax for repeating previous token min-max times. - Non-capturing groups - Lookaround and backreferences (cannot be implemented efficiently). diff --git a/docs/cset_api.md b/docs/cset_api.md index ef4df63b..1c38d000 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -86,15 +86,14 @@ cset_X_value cset_X_value_clone(cset_X_value val); int main () { - c_AUTO (cset_str, fifth) + c_auto (cset_str, fifth) { - c_AUTO (cset_str, first, second) - c_AUTO (cset_str, third, fourth) + c_auto (cset_str, first, second) + c_auto (cset_str, third, fourth) { - c_FORLIST (i, const char*, {"red", "green", "blue"}) - cset_str_emplace(&second, *i.ref); + second = c_make(cset_str, {"red", "green", "blue"}); - c_FORLIST (i, const char*, {"orange", "pink", "yellow"}) + c_forlist (i, const char*, {"orange", "pink", "yellow"}) cset_str_emplace(&third, *i.ref); cset_str_emplace(&fourth, "potatoes"); @@ -102,14 +101,14 @@ int main () cset_str_emplace(&fourth, "flour"); fifth = cset_str_clone(second); - c_FOREACH (i, cset_str, third) + c_foreach (i, cset_str, third) cset_str_emplace(&fifth, cstr_str(i.ref)); - c_FOREACH (i, cset_str, fourth) + c_foreach (i, cset_str, fourth) cset_str_emplace(&fifth, cstr_str(i.ref)); } printf("fifth contains:\n\n"); - c_FOREACH (i, cset_str, fifth) + c_foreach (i, cset_str, fifth) printf("%s\n", cstr_str(i.ref)); } } diff --git a/docs/csmap_api.md b/docs/csmap_api.md index b090f737..80fa96fb 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -111,16 +111,16 @@ csmap_X_raw csmap_X_value_toraw(csmap_X_value* pval); int main() { // Create a sorted map of three strings (maps to string) - c_AUTO (csmap_str, colors) // RAII + c_auto (csmap_str, colors) // RAII { - c_FORLIST (i, csmap_str_raw, { + colors = c_make(csmap_str, { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} - }) csmap_str_emplace(&colors, c_PAIR(i.ref)); + }); // Iterate and print keys and values of sorted map - c_FOREACH (i, csmap_str, colors) { + c_foreach (i, csmap_str, colors) { printf("Key:[%s] Value:[%s]\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); } @@ -157,9 +157,9 @@ int main() { uint32_t col = 0xcc7744ff; csmap_id idnames = csmap_id_init(); - c_DEFER (csmap_id_drop(&idnames)) + c_defer (csmap_id_drop(&idnames)) { - c_FORLIST (i, csmap_id_raw, { {100, "Red"}, {110, "Blue"} }) + c_forlist (i, csmap_id_raw, { {100, "Red"}, {110, "Blue"} }) csmap_id_emplace(&idnames, c_PAIR(i.ref)); // put replaces existing mapped value: @@ -171,7 +171,7 @@ int main() // emplace adds only when key does not exist: csmap_id_emplace(&idnames, 100, "Green"); - c_FOREACH (i, csmap_id, idnames) + c_foreach (i, csmap_id, idnames) printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second)); } } @@ -204,14 +204,14 @@ static int Vec3i_cmp(const Vec3i* a, const Vec3i* b) { int main() { - c_AUTO (csmap_vi, vecs) + c_auto (csmap_vi, vecs) { csmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); csmap_vi_insert(&vecs, (Vec3i){0, 100, 0}, 2); csmap_vi_insert(&vecs, (Vec3i){0, 0, 100}, 3); csmap_vi_insert(&vecs, (Vec3i){100, 100, 100}, 4); - c_FOREACH (i, csmap_vi, vecs) + c_foreach (i, csmap_vi, vecs) printf("{ %3d, %3d, %3d }: %d\n", i.ref->first.x, i.ref->first.y, i.ref->first.z, i.ref->second); } } @@ -237,15 +237,15 @@ typedef struct { int x, y, z; } Vec3i; int main() { - // equivalent to: c_AUTO (csmap_iv, vecs) - c_WITH (csmap_iv vecs = csmap_iv_init(), csmap_iv_drop(&vecs)) + // equivalent to: c_auto (csmap_iv, vecs) + c_with (csmap_iv vecs = csmap_iv_init(), csmap_iv_drop(&vecs)) { csmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); csmap_iv_insert(&vecs, 2, (Vec3i){0, 100, 0}); csmap_iv_insert(&vecs, 3, (Vec3i){0, 0, 100}); csmap_iv_insert(&vecs, 4, (Vec3i){100, 100, 100}); - c_FOREACH (i, csmap_iv, vecs) + c_foreach (i, csmap_iv, vecs) printf("%d: { %3d, %3d, %3d }\n", i.ref->first, i.ref->second.x, i.ref->second.y, i.ref->second.z); } } diff --git a/docs/cspan_api.md b/docs/cspan_api.md index d0463b0a..2bd93a2b 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -84,7 +84,7 @@ if __name__ == '__main__': # 19 20 23 24 # 19 20 23 24 ``` -... can be done in C with cspan: +... can be written in C using cspan: ```c #include <c11/fmt.h> #include <stc/cspan.h> @@ -97,12 +97,12 @@ int main() { myspan3 ss3 = cspan_slice(myspan3, &ms3, {c_ALL}, {1,3}, {2,c_END}); myspan2 ss2 = cspan_submd3(&ss3, 1); - c_FORRANGE (i, ss2.shape[0]) - c_FORRANGE (j, ss2.shape[1]) + c_forrange (i, ss2.shape[0]) + c_forrange (j, ss2.shape[1]) fmt_print(" {}", *cspan_at(&ss2, i, j)); puts(""); - c_FOREACH (i, myspan2, ss2) + c_foreach (i, myspan2, ss2) fmt_print(" {}", *i.ref); } ``` @@ -147,7 +147,7 @@ int main() Span2 span2 = cspan_submd3(&span3, 1); puts("\niterate span2 flat:"); - c_FOREACH (i, Span2, span2) + c_foreach (i, Span2, span2) fmt_print(" {}", *i.ref); puts(""); @@ -155,9 +155,9 @@ int main() Span3 ss3 = cspan_slice(Span3, &span3, {c_ALL}, {3,4}, {c_ALL}); puts("\niterate ss3 by dimensions:"); - c_FORRANGE (i, ss3.shape[0]) { - c_FORRANGE (j, ss3.shape[1]) { - c_FORRANGE (k, ss3.shape[2]) + c_forrange (i, ss3.shape[0]) { + c_forrange (j, ss3.shape[1]) { + c_forrange (k, ss3.shape[2]) fmt_print(" {:2}", *cspan_at(&ss3, i, j, k)); fmt_print(" |"); } @@ -167,8 +167,8 @@ int main() Span2 ss2 = cspan_slice(Span2, &span3, {c_ALL}, {3}, {c_ALL}); puts("\niterate ss2 by dimensions:"); - c_FORRANGE (i, ss2.shape[0]) { - c_FORRANGE (j, ss2.shape[1]) { + c_forrange (i, ss2.shape[0]) { + c_forrange (j, ss2.shape[1]) { fmt_print(" {:2}", *cspan_at(&ss2, i, j)); fmt_print(" |"); } @@ -176,7 +176,7 @@ int main() } puts("\niterate ss2 flat:"); - c_FOREACH (i, Span2, ss2) + c_foreach (i, Span2, ss2) fmt_print(" {:2}", *i.ref); puts(""); } diff --git a/docs/csset_api.md b/docs/csset_api.md index 7e068909..3d7698ed 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -83,13 +83,12 @@ csset_X_value csset_X_value_clone(csset_X_value val); int main () { - c_AUTO (csset_str, first, second, third) - c_AUTO (csset_str, fourth, fifth) + c_auto (csset_str, first, second, third) + c_auto (csset_str, fourth, fifth) { - c_FORLIST (i, const char*, {"red", "green", "blue"}) - csset_str_emplace(&second, *i.ref); + second = c_make(csset_str, {"red", "green", "blue"}); - c_FORLIST (i, const char*, {"orange", "pink", "yellow"}) + c_forlist (i, const char*, {"orange", "pink", "yellow"}) csset_str_emplace(&third, *i.ref); csset_str_emplace(&fourth, "potatoes"); @@ -97,13 +96,13 @@ int main () csset_str_emplace(&fourth, "flour"); fifth = csset_str_clone(second); - c_FOREACH (i, csset_str, third) + c_foreach (i, csset_str, third) csset_str_emplace(&fifth, cstr_str(i.ref)); - c_FOREACH (i, csset_str, fourth) + c_foreach (i, csset_str, fourth) csset_str_emplace(&fifth, cstr_str(i.ref)); printf("fifth contains:\n\n"); - c_FOREACH (i, csset_str, fifth) + c_foreach (i, csset_str, fifth) printf("%s\n", cstr_str(i.ref)); } } diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 37316d4d..64099675 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -160,7 +160,7 @@ char* cstrnstrn(const char* str, const char* search, intptr_t slen, intpt #include <stc/cstr.h> int main() { - c_AUTO (cstr, s0, s1, full_path) { + c_auto (cstr, s0, s1, full_path) { s0 = cstr_lit("Initialization without using strlen()."); printf("%s\nLength: %" c_ZI "\n\n", cstr_str(&s0), cstr_size(&s0)); diff --git a/docs/csview_api.md b/docs/csview_api.md index e3c65766..29c59d9c 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -83,11 +83,11 @@ csview cstr_u8_substr(const cstr* self, intptr_t bytepos, intptr_t u8le csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2); csview cstr_slice_ex(const cstr* s, intptr_t p, intptr_t q); // negative p or q count from end ``` -#### Iterate tokens with *c_FORTOKEN*, *c_FORTOKEN_SV* +#### Iterate tokens with *c_fortoken*, *c_fortoken_sv* To iterate tokens in an input string separated by a string: ```c -c_FORTOKEN (i, "hello, one, two, three", ", ") +c_fortoken (i, "hello, one, two, three", ", ") printf("token: %.*s\n", c_SVARG(i.token)); ``` @@ -151,12 +151,12 @@ red Apples int main() { - c_AUTO (cstr, s1) { + c_auto (cstr, s1) { s1 = cstr_lit("hell😀 w😀rld"); cstr_u8_replace_at(&s1, cstr_find(&s1, "😀rld"), 1, c_SV("ø")); printf("%s\n", cstr_str(&s1)); - c_FOREACH (i, cstr, s1) + c_foreach (i, cstr, s1) printf("%.*s,", c_SVARG(i.u8.chr)); } } @@ -176,7 +176,7 @@ and does not depend on null-terminated strings. *string_split()* function return void print_split(csview input, const char* sep) { - c_FORTOKEN_SV (i, input, sep) + c_fortoken_sv (i, input, sep) printf("[%.*s]\n", c_SVARG(i.token)); } @@ -188,7 +188,7 @@ cstack_str string_split(csview input, const char* sep) { cstack_str out = cstack_str_init(); - c_FORTOKEN_SV (i, input, sep) + c_fortoken_sv (i, input, sep) cstack_str_push(&out, cstr_from_sv(i.token)); return out; @@ -201,8 +201,8 @@ int main() print_split(c_SV("This has no matching separator"), "xx"); puts(""); - c_WITH (cstack_str s = string_split(c_SV("Split,this,,string,now,"), ","), cstack_str_drop(&s)) - c_FOREACH (i, cstack_str, s) + c_with (cstack_str s = string_split(c_SV("Split,this,,string,now,"), ","), cstack_str_drop(&s)) + c_foreach (i, cstack_str, s) printf("[%s]\n", cstr_str(i.ref)); } ``` diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 057caa7c..68e08cb2 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -119,18 +119,18 @@ cvec_X_value cvec_X_value_clone(cvec_X_value val); int main() { // Create a vector containing integers - c_AUTO (cvec_int, vec) + c_auto (cvec_int, vec) { // Add two integers to vector cvec_int_push(&vec, 25); cvec_int_push(&vec, 13); // Append a set of numbers - c_FORLIST (i, int, {7, 5, 16, 8}) + c_forlist (i, int, {7, 5, 16, 8}) cvec_int_push(&vec, *i.ref); printf("initial:"); - c_FOREACH (k, cvec_int, vec) { + c_foreach (k, cvec_int, vec) { printf(" %d", *k.ref); } @@ -138,7 +138,7 @@ int main() cvec_int_sort(&vec); printf("\nsorted:"); - c_FOREACH (k, cvec_int, vec) { + c_foreach (k, cvec_int, vec) { printf(" %d", *k.ref); } } @@ -170,7 +170,7 @@ int main() { printf("%s\n", cstr_str(&names.data[1])); // Access second element - c_FOREACH (i, cvec_str, names) + c_foreach (i, cvec_str, names) printf("item: %s\n", cstr_str(i.ref)); cvec_str_drop(&names); @@ -219,7 +219,7 @@ int main(void) { UVec vec2 = UVec_clone(vec); - c_FOREACH (i, UVec, vec2) + c_foreach (i, UVec, vec2) printf("%s: %d\n", cstr_str(&i.ref->name), i.ref->id); c_drop(UVec, &vec, &vec2); // cleanup |
