diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/carc_api.md | 26 | ||||
| -rw-r--r-- | docs/carray_api.md | 6 | ||||
| -rw-r--r-- | docs/cbox_api.md | 28 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 186 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 8 | ||||
| -rw-r--r-- | docs/clist_api.md | 22 | ||||
| -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 | 12 | ||||
| -rw-r--r-- | docs/cset_api.md | 16 | ||||
| -rw-r--r-- | docs/csmap_api.md | 22 | ||||
| -rw-r--r-- | docs/csset_api.md | 40 | ||||
| -rw-r--r-- | docs/cstr_api.md | 43 | ||||
| -rw-r--r-- | docs/csview_api.md | 20 | ||||
| -rw-r--r-- | docs/cvec_api.md | 14 |
17 files changed, 246 insertions, 247 deletions
diff --git a/docs/carc_api.md b/docs/carc_api.md index 15e2c452..f91975a7 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -36,14 +36,14 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory ## Methods ```c carc_X carc_X_init(); // empty shared pointer -carc_X carc_X_new(i_valraw raw); // create an carc from raw type (available if i_valraw defined by user). -carc_X carc_X_from(i_val val); // create an carc from constructed val object. Faster than from_ptr(). +carc_X carc_X_from(i_valraw raw); // create an carc from raw type (available if i_valraw defined by user). carc_X carc_X_from_ptr(i_val* p); // create an carc from raw pointer. Takes ownership of p. +carc_X carc_X_make(i_val val); // create an carc from constructed val object. Faster than from_ptr(). carc_X carc_X_clone(carc_X other); // return other with increased use count -carc_X carc_X_move(carc_X* self); // transfer ownership to another carc. -void carc_X_take(carc_X* self, carc_X other); // take ownership of other. -void carc_X_copy(carc_X* self, carc_X other); // shared assign (increase use count) +carc_X carc_X_move(carc_X* self); // transfer ownership to receiver; self becomes NULL +void carc_X_take(carc_X* self, carc_X unowned); // take ownership of unowned. +void carc_X_assign(carc_X* self, carc_X other); // shared assign (increases use count) void carc_X_drop(carc_X* self); // destruct (decrease use count, free at 0) long carc_X_use_count(const carc_X* self); @@ -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/carray_api.md b/docs/carray_api.md index fb2c6a89..cd4e0e45 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -90,7 +90,7 @@ int main() // Ex1 int xd = 30, yd = 20, zd = 10; // define arr3[30][20][10], initialized with zeros. - c_autodrop (carr3_f, arr3, carr3_f_with_size(xd, yd, zd, 0.0f)) { + c_AUTODROP (carr3_f, arr3, carr3_f_with_size(xd, yd, zd, 0.0f)) { arr3.data[5][4][3] = 3.14f; float *arr1 = arr3.data[5][4]; @@ -103,9 +103,9 @@ int main() // Ex2 int w = 256, h = 128; - c_with (carr2_i image = carr2_i_new_uninit(w, h), carr2_i_drop(&image)) { + c_WITH (carr2_i image = carr2_i_new_uninit(w, h), carr2_i_drop(&image)) { int n = 0; - c_foreach (i, carr2_i, image) { + c_FOREACH (i, carr2_i, image) { uint32_t t = n++ % 256; *i.ref = t | t << 8 | t << 16 | 255; } diff --git a/docs/cbox_api.md b/docs/cbox_api.md index b3314433..8906f154 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -35,15 +35,15 @@ compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd ## Methods ```c cbox_X cbox_X_init(); // return an empty cbox -cbox_X cbox_X_new(i_valraw raw); // create a cbox from raw type. Avail if i_valraw user defined. -cbox_X cbox_X_from(i_val val); // create a cbox from constructed val object. -cbox_X cbox_X_from_ptr(i_val* p); // create a cbox from a pointer. Takes ownership of p. +cbox_X cbox_X_from(i_valraw raw); // create a cbox from raw type. Avail if i_valraw user defined. +cbox_X cbox_X_from_ptr(i_val* ptr); // create a cbox from a pointer. Takes ownership of ptr. +cbox_X cbox_X_make(i_val val); // create a cbox from unowned val object. cbox_X cbox_X_clone(cbox_X other); // return deep copied clone -cbox_X cbox_X_move(cbox_X* self); // transfer ownership to another cbox. -void cbox_X_take(cbox_X* self, cbox_X other); // take ownership of other. -void cbox_X_copy(cbox_X* self, cbox_X other); // deep copy to self -void cbox_X_drop(cbox_X* self); // destruct the contained object and free's it. +cbox_X cbox_X_move(cbox_X* self); // transfer ownership to receiving cbox returned. self becomes NULL. +void cbox_X_take(cbox_X* self, cbox_X unowned); // take ownership of unowned box object. +void cbox_X_assign(cbox_X* self, cbox_X* dying); // transfer ownership from dying to self; dying becomes NULL. +void cbox_X_drop(cbox_X* self); // destruct the contained object and free its heap memory. void cbox_X_reset(cbox_X* self); void cbox_X_reset_to(cbox_X* self, i_val* p); // assign new cbox from ptr. Takes ownership of p. @@ -92,18 +92,18 @@ 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}) + c_FORLIST (i, int, {2021, 2012, 2022, 2015}) IVec_emplace(&vec, *i.ref); // same as: IVec_push(&vec, IBox_from(*i.ref)); 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 +112,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 2c6db85f..474c80ac 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_autodrop, c_with, c_scope, c_defer +### c_AUTO, c_AUTODROP, 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_autodrop (Type, var, init...)` | Like `c_with (Type var=init..., Type_drop(&var))` | -| `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_AUTODROP (Type, var, init...)` | Like `c_WITH (Type var=init..., Type_drop(&var))` | +| `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 (uint8_t* buf = malloc(BUF_SIZE), free(buf)) -c_with (FILE* fp = fopen(fname, "rb"), fclose(fp)) +// `c_WITH` is similar to python `with`: it declares and can drop a variable after going out of scope. +c_WITH (uint8_t* buf = malloc(BUF_SIZE), free(buf)) +c_WITH (FILE* fp = fopen(fname, "rb"), fclose(fp)) { int n = 0; if (fp && buf) { @@ -32,14 +32,14 @@ c_with (FILE* fp = fopen(fname, "rb"), fclose(fp)) } } -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_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"); @@ -50,22 +50,22 @@ c_auto (cstr, s1, s2) printf("%s %s\n", cstr_str(&s1), cstr_str(&s2)); } -c_autodrop (cstr, str, cstr_lit("Hello")) +c_AUTODROP (cstr, str, cstr_lit("Hello")) { 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. +// `c_DEFER` executes the expressions when leaving 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)); } @@ -83,8 +83,8 @@ cvec_str readFile(const char* name) { cvec_str vec = cvec_str_init(); // returned - c_with (FILE* fp = fopen(name, "r"), fclose(fp)) - c_with (cstr line = cstr_NULL, cstr_drop(&line)) + c_WITH (FILE* fp = fopen(name, "r"), 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; @@ -92,30 +92,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); @@ -124,23 +124,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; ... @@ -148,33 +148,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 @@ -182,59 +182,59 @@ 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 | |:----------------------------------|:-------------------------------------| -| `c_flt_skip(it, numItems)` | Skip numItems | -| `c_flt_take(it, numItems)` | Take numItems | -| `c_flt_skipwhile(it, predicate)` | Skip items until predicate is false | -| `c_flt_takewhile(it, predicate)` | Take items until predicate is false | +| `c_FLT_SKIP(it, numItems)` | Skip numItems | +| `c_FLT_TAKE(it, numItems)` | Take numItems | +| `c_FLT_SKIPWHILE(it, predicate)` | Skip items until predicate is false | +| `c_FLT_TAKEWHILE(it, predicate)` | Take items until predicate is false | `it.index` holds the index of the source item, and `it.count` the current number of items taken. ```c @@ -250,14 +250,14 @@ 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 + && c_FLT_SKIP(i, 100) // built-in && isPrime(*i.ref) - , c_flt_take(i, 10)) { // breaks loop on false. + , c_FLT_TAKE(i, 10)) { // breaks loop on false. printf(" %d", *i.ref); } puts(""); @@ -265,7 +265,7 @@ int main() { } // Out: 1000211 1000213 1000231 1000249 1000253 1000273 1000289 1000291 1000303 1000313 ``` -Note that `c_flt_take()` is given as an optional argument, which makes the loop stop when it becomes false (for efficiency). Chaining it after `isPrime()` instead will give same result, but the full input is processed. +Note that `c_FLT_TAKE()` is given as an optional argument, which makes the loop stop when it becomes false (for efficiency). Chaining it after `isPrime()` instead will give same result, but the full input is processed. ### crange **crange** is a number sequence generator type. The **crange_value** type is `long long`. Below, *start*, *stop*, *step* are type *crange_value*: @@ -281,57 +281,57 @@ 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_literal(3, crange_MAX, 2) +c_FORFILTER (i, crange, crange_LITERAL(3, INT64_MAX, 2) , isPrime(*i.ref) - , c_flt_take(10)) + , c_FLT_TAKE(10)) printf(" %lld", *i.ref); // 2 3 5 7 11 13 17 19 23 29 31 ``` -### c_find_if, c_find_in, c_erase_if +### c_FIND_IF, c_ERASE_IF Find or erase linearily in containers using a predicate ```c // Search vec for first value > 2: cvec_i_iter i; -c_find_if(i, cvec_i, vec, *i.ref > 2); +c_FIND_IF(i, cvec_i, vec, *i.ref > 2); if (i.ref) printf("%d\n", *i.ref); // Search map for a string containing "hello" and erase it: cmap_str_iter it, it1 = ..., it2 = ...; -c_find_if(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello")); +c_FIND_IF(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello")); if (it.ref) cmap_str_erase_at(&map, it); // Erase all strings containing "hello": // Note 1: iter i need not be declared. // Note 2: variables index and count can be accessed in predicate. -c_erase_if(i, csmap_str, map, cstr_contains(i.ref, "hello")); +c_ERASE_IF(i, csmap_str, map, cstr_contains(i.ref, "hello")); ``` -### c_new, c_alloc, c_alloc_n, c_drop +### c_NEW, c_ALLOC, c_ALLOC_N, c_DROP | Usage | Meaning | |:-------------------------------|:----------------------------------------| -| `c_new (type, value)` | Move value to a new object on the heap | -| `c_alloc (type)` | `(type *) c_malloc(sizeof(type))` | -| `c_alloc_n (type, N)` | `(type *) c_malloc((N)*sizeof(type))` | -| `c_drop (ctype, &c1, ..., &cN)` | `ctype_drop(&c1); ... ctype_drop(&cN)` | +| `c_NEW (type, value)` | Move value to a new object on the heap | +| `c_ALLOC (type)` | `(type *) c_MALLOC(sizeof(type))` | +| `c_ALLOC_N (type, N)` | `(type *) c_MALLOC((N)*sizeof(type))` | +| `c_DROP (ctype, &c1, ..., &cN)` | `ctype_drop(&c1); ... ctype_drop(&cN)` | ```c struct Pnt { double x, y, z; }; -struct Pnt *pnt = c_new (struct Pnt, {1.2, 3.4, 5.6}); -c_free(pnt); +struct Pnt *pnt = c_NEW (struct Pnt, {1.2, 3.4, 5.6}); +c_FREE(pnt); -int* array = c_alloc_n (int, 100); -c_free(array); +int* array = c_ALLOC_N (int, 100); +c_FREE(array); cstr a = cstr_lit("Hello"), b = cstr_lit("World"); -c_drop(cstr, &a, &b); +c_DROP(cstr, &a, &b); ``` ### General predefined template parameter functions @@ -347,9 +347,9 @@ bool crawstr_eq(const crawstr* x, const crawstr* y); uint64_t crawstr_hash(const crawstr* x); ``` -### c_malloc, c_calloc, c_realloc, c_free +### c_MALLOC, c_CALLOC, c_REALLOC, c_FREE Memory allocator for the entire library. Macros can be overloaded by the user. -### c_swap, c_arraylen +### c_swap, c_ARRAYLEN - **c_swap(type, x, y)**: Simple macro for swapping internals of two objects. -- **c_arraylen(array)**: Return number of elements in an array, e.g. `int array[] = {1, 2, 3, 4};` +- **c_ARRAYLEN(array)**: Return number of elements in an array, e.g. `int array[] = {1, 2, 3, 4};` diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 7ce4c7f1..e39a9d5d 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -110,14 +110,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(""); @@ -126,7 +126,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 aae72224..9745fcdb 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -119,22 +119,22 @@ Interleave *push_front()* / *push_back()* then *sort()*: int main() { DList list = DList_init(); - c_forlist (i, double, {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,7 +160,7 @@ int main () { clist_i L = clist_i_init(); - c_forlist (i, int, {10, 20, 30, 40, 50}) + c_FORLIST (i, int, {10, 20, 30, 40, 50}) clist_i_push_back(&L, *i.ref); // 10 20 30 40 50 clist_i_iter it = clist_i_begin(&L); // ^ @@ -172,7 +172,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(""); @@ -195,11 +195,11 @@ 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}) + 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}) + c_FORLIST (i, int, {10, 20, 30, 40, 50}) clist_i_push_back(&L2, *i.ref); clist_i_iter i = clist_i_advance(clist_i_begin(&L1), 2); @@ -207,9 +207,9 @@ int main() { 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 520d9046..3946196b 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -127,16 +127,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, { + c_FORLIST (i, cmap_str_raw, { {"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)); } @@ -173,9 +173,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: @@ -187,7 +187,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)); } } @@ -214,14 +214,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 (vec, num, cmap_vi, vecs) + c_FORPAIR (vec, num, cmap_vi, vecs) printf("{ %3d, %3d, %3d }: %d\n", _.vec->x, _.vec->y, _.vec->z, *_.num); } } @@ -247,14 +247,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, vec, cmap_iv, vecs) + c_FORPAIR (num, vec, cmap_iv, vecs) printf("%d: { %3d, %3d, %3d }\n", *_.num, _.vec->x, _.vec->y, _.vec->z); } } @@ -314,20 +314,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); } } @@ -354,7 +354,7 @@ typedef struct Viking { } Viking; static inline void Viking_drop(Viking* v) { - c_drop(cstr, &v->name, &v->country); + c_DROP(cstr, &v->name, &v->country); } // Define Viking raw struct with cmp, hash, and convertion functions between Viking and RViking structs: @@ -397,7 +397,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); @@ -407,7 +407,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 a68e3392..790b902c 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 89ca7d5c..977fa855 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 3e6ae8f0..bd3bf848 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) { size_t n = (size_t) (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 ce218721..3197a59e 100644 --- a/docs/cregex_api.md +++ b/docs/cregex_api.md @@ -48,8 +48,8 @@ cstr cregex_replace_sv(const cregex* re, csview input, const char* replac /* All-in-one replacement (compile + find/replace + drop) */ cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace); -cstr cregex_replace_pattern_n(const char* pattern, const char* input, const char* replace, unsigned count, - bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); +cstr cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count, + bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); void cregex_drop(cregex* self); /* destroy */ ``` @@ -129,19 +129,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_ARGSV(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_ARGSV(it.match[k])); ``` diff --git a/docs/cset_api.md b/docs/cset_api.md index 266895be..43cbdead 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -87,15 +87,15 @@ 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"}) + c_FORLIST (i, const char*, {"red", "green", "blue"}) cset_str_emplace(&second, *i.ref); - 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"); @@ -103,14 +103,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 bb0253c7..394d3451 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -112,16 +112,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, { + c_FORLIST (i, csmap_str_raw, { {"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)); } @@ -158,9 +158,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: @@ -172,7 +172,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)); } } @@ -205,14 +205,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); } } @@ -238,15 +238,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/csset_api.md b/docs/csset_api.md index 6b2bdfdf..5b833af5 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -84,29 +84,27 @@ csset_X_value csset_X_value_clone(csset_X_value val); int main () { -c_auto (csset_str, fifth) + c_AUTO (csset_str, first, second, third) + c_AUTO (csset_str, fourth, fifth) { - c_auto (csset_str, first, second) - c_auto (csset_str, third, fourth) - { - c_forlist (i, const char*, {"red", "green", "blue"}) - csset_str_emplace(&second, *i.ref); - - c_forlist (i, const char*, {"orange", "pink", "yellow"}) - csset_str_emplace(&third, *i.ref); - - csset_str_emplace(&fourth, "potatoes"); - csset_str_emplace(&fourth, "milk"); - csset_str_emplace(&fourth, "flour"); - - fifth = csset_str_clone(second); - c_foreach (i, csset_str, third) - csset_str_emplace(&fifth, cstr_str(i.ref)); - c_foreach (i, csset_str, fourth) - csset_str_emplace(&fifth, cstr_str(i.ref)); - } + c_FORLIST (i, const char*, {"red", "green", "blue"}) + csset_str_emplace(&second, *i.ref); + + c_FORLIST (i, const char*, {"orange", "pink", "yellow"}) + csset_str_emplace(&third, *i.ref); + + csset_str_emplace(&fourth, "potatoes"); + csset_str_emplace(&fourth, "milk"); + csset_str_emplace(&fourth, "flour"); + + fifth = csset_str_clone(second); + c_FOREACH (i, csset_str, third) + csset_str_emplace(&fifth, cstr_str(i.ref)); + 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 0f9589d9..4f895549 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -68,15 +68,16 @@ void cstr_insert_s(cstr* self, size_t pos, cstr ins); void cstr_erase(cstr* self, size_t pos, size_t len); // erase len bytes from pos -void cstr_replace(cstr* self, const char* search, const char* repl, unsigned count); // count==0: replace all. +void cstr_replace(cstr* self, const char* search, const char* repl); +void cstr_replace_ex(cstr* self, const char* search, const char* repl, unsigned count); cstr cstr_replace_sv(csview in, csview search, csview repl, unsigned count); void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* repl); // replace at a position void cstr_replace_at_sv(cstr* self, size_t pos, size_t len, const csview repl); void cstr_replace_at_s(cstr* self, size_t pos, size_t len, cstr repl); bool cstr_equals(const cstr* self, const char* str); -bool cstr_equals_s(const cstr* self, cstr s); bool cstr_equals_sv(const cstr* self, csview sv); +bool cstr_equals_s(const cstr* self, cstr s); size_t cstr_find(const cstr* self, const char* search); size_t cstr_find_at(const cstr* self, size_t pos, const char* search); // search from pos @@ -101,7 +102,7 @@ size_t cstr_u8_size_n(const cstr self, size_t nbytes); // utf8 si size_t cstr_u8_to_pos(const cstr* self, size_t u8idx); // byte pos offset at utf8 codepoint index const char* cstr_u8_at(const cstr* self, size_t u8idx); // char* position at utf8 codepoint index csview cstr_u8_chr(const cstr* self, size_t u8idx); // get utf8 character as a csview -void cstr_u8_replace(cstr* self, size_t bytepos, size_t u8len, csview repl); // replace u8len utf8 chars +void cstr_u8_replace_at(cstr* self, size_t bytepos, size_t u8len, csview repl); // replace u8len utf8 chars void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); // erase u8len codepoints from pos // iterate utf8 codepoints @@ -160,30 +161,30 @@ char* cstrnstrn(const char* str, const char* search, size_t slen, size_t #include <stc/cstr.h> int main() { - cstr s0 = cstr_lit("Initialization without using strlen()."); - printf("%s\nLength: %" c_ZU "\n\n", cstr_str(&s0), cstr_size(&s0)); - - cstr s1 = cstr_lit("one-nine-three-seven-five."); - printf("%s\n", cstr_str(&s1)); + c_AUTO (cstr, s0, s1, full_path) { + s0 = cstr_lit("Initialization without using strlen()."); + printf("%s\nLength: %" c_ZU "\n\n", cstr_str(&s0), cstr_size(&s0)); - cstr_insert(&s1, 3, "-two"); - printf("%s\n", cstr_str(&s1)); + s1 = cstr_lit("one-nine-three-seven-five."); + printf("%s\n", cstr_str(&s1)); - cstr_erase(&s1, 7, 5); // -nine - printf("%s\n", cstr_str(&s1)); + cstr_insert(&s1, 3, "-two"); + printf("%s\n", cstr_str(&s1)); - cstr_replace(&s1, "seven", "four", 1); - printf("%s\n", cstr_str(&s1)); + cstr_erase(&s1, 7, 5); // -nine + printf("%s\n", cstr_str(&s1)); - // reassign: - cstr_assign(&s1, "one two three four five six seven"); - cstr_append(&s1, " eight"); - printf("append: %s\n", cstr_str(&s1)); + cstr_replace_ex(&s1, "seven", "four", 1); + printf("%s\n", cstr_str(&s1)); - cstr full_path = cstr_from_fmt("%s/%s.%s", "directory", "filename", "ext"); - printf("%s\n", cstr_str(&full_path)); + // reassign: + cstr_assign(&s1, "one two three four five six seven"); + cstr_append(&s1, " eight"); + printf("append: %s\n", cstr_str(&s1)); - c_drop(cstr, &s0, &s1, &full_path); + full_path = cstr_from_fmt("%s/%s.%s", "directory", "filename", "ext"); + printf("%s\n", cstr_str(&full_path)); + } } ``` Output: diff --git a/docs/csview_api.md b/docs/csview_api.md index 4851152a..b5508ace 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -83,11 +83,11 @@ csview cstr_u8_substr(const cstr* self, size_t bytepos, size_t u8len); csview cstr_slice(const cstr* self, size_t p1, size_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_ARGSV(i.token)); ``` @@ -135,7 +135,7 @@ int main () cstr s3 = cstr_from_sv(cstr_substr(&s1, 0, 6)); // "Apples" printf("%s %s\n", cstr_str(&s2), cstr_str(&s3)); - c_drop(cstr, &str1, &s1, &s2, &s3); + c_DROP(cstr, &str1, &s1, &s2, &s3); } ``` Output: @@ -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(&s1, cstr_find(&s1, "😀rld"), 1, c_SV("ø")); + 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_ARGSV(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_ARGSV(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 9b2614af..23722275 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -120,18 +120,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); } @@ -139,7 +139,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); } } @@ -171,7 +171,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); @@ -220,9 +220,9 @@ 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 + c_DROP(UVec, &vec, &vec2); // cleanup } ```
\ No newline at end of file |
