diff options
| author | Tyge Løvset <[email protected]> | 2022-09-21 14:54:47 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-09-21 14:54:47 +0200 |
| commit | 307c0a111c8f063032ba90b2a7ae07122e2a2b1a (patch) | |
| tree | 0c989b52d7fe4a87d11dc593df5b16579dd1bd22 /examples | |
| parent | a1d6c85b72027c9cd09d6bf0b1e0f7c3942e4aee (diff) | |
| download | STC-modified-307c0a111c8f063032ba90b2a7ae07122e2a2b1a.tar.gz STC-modified-307c0a111c8f063032ba90b2a7ae07122e2a2b1a.zip | |
Recent macro renames:
c_foreach_token() => c_fortoken()
c_foreach_match() => c_formatch()
Added:
c_forfiltered()
c_forpred()
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/forfiltered.c | 93 | ||||
| -rw-r--r-- | examples/regex2.c | 2 | ||||
| -rw-r--r-- | examples/regex_match.c | 2 | ||||
| -rw-r--r-- | examples/splitstr.c | 8 |
4 files changed, 99 insertions, 6 deletions
diff --git a/examples/forfiltered.c b/examples/forfiltered.c new file mode 100644 index 00000000..d2a62c2a --- /dev/null +++ b/examples/forfiltered.c @@ -0,0 +1,93 @@ +#include <stdio.h> +#include <stc/csview.h> + +#define i_type IVec +#define i_val int +#include <stc/cstack.h> + +#define i_type SVec +#define i_val_bind csview +#include <stc/cstack.h> + +void demo1(void) +{ + c_auto (IVec, vec) { + c_forarray (int, v, {0, 1, 2, 3, 4, 5, 80, 6, 7, 80, 8, 9, 80, 10, 11, 12, 13}) + IVec_push(&vec, *v); + + c_forfiltered (i, IVec, vec, *i.ref != 80) + printf(" %d", *i.ref); + + puts(""); + c_forfiltered (i, IVec, vec, i.idx >= 3 // drop(3) + && i.idx != 5 + && *i.ref != 7, + i.count < 5) // take(5) + printf(" %d", *i.ref); + puts(""); + } +} + + +/* Rust: +fn main() { + let vector = (1..) // Infinite range of integers + .filter(|x| x % 2 != 0) // Collect odd numbers + .take(5) // Only take five numbers + .map(|x| x * x) // Square each number + .collect::<Vec<usize>>(); // Return as a new Vec<usize> + println!("{:?}", vector); // Print result +} +*/ + +void demo2(void) +{ + c_auto (IVec, vec) { + int n = 0; + for (size_t x=1;; ++x) + if (n == 5) break; + else if (x & 1) ++n, IVec_push(&vec, x*x); + + c_foreach (i, IVec, vec) printf(" %d", *i.ref); + puts(""); + } +} + +/* Rust: +fn main() { + let sentence = "This is a sentence in Rust."; + let words: Vec<&str> = sentence + .split_whitespace() + .collect(); + let words_containing_i: Vec<&str> = words + .into_iter() + .take(2) + .filter(|word| word.contains("i")) + .collect(); + println!("{:?}", words_containing_i); +} +*/ +void demo3(void) +{ + c_auto (SVec, words, words_containing_i) { + const char* sentence = "This is a sentence in Rust."; + c_fortoken(w, sentence, " ") + SVec_push(&words, *w.ref); + + c_forfiltered (w, SVec, words, csview_contains(*w.ref, c_sv("i")), + w.count < 2) // take(2) + SVec_push(&words_containing_i, *w.ref); + + c_foreach (w, SVec, words_containing_i) + printf(" %.*s", c_ARGsv(*w.ref)); + puts(""); + } +} + + +int main(void) +{ + demo1(); + demo2(); + demo3(); +} diff --git a/examples/regex2.c b/examples/regex2.c index f0869d24..28291915 100644 --- a/examples/regex2.c +++ b/examples/regex2.c @@ -24,7 +24,7 @@ int main() } printf("input: %s\n", s[i].input); - c_foreach_match (j, &re, s[i].input) { + c_formatch (j, &re, s[i].input) { c_forrange (k, cregex_captures(&re)) printf(" submatch %d: %.*s\n", (int)k, c_ARGsv(j.match[k])); puts(""); diff --git a/examples/regex_match.c b/examples/regex_match.c index 78af5c77..50fdba9a 100644 --- a/examples/regex_match.c +++ b/examples/regex_match.c @@ -22,7 +22,7 @@ int main() printf("%d: %s\n", res, pattern); // extract and convert all numbers in str to floats - c_foreach_match (i, &re, str) + c_formatch (i, &re, str) cstack_float_push(&vec, atof(i.match[0].str)); c_foreach (i, cstack_float, vec) diff --git a/examples/splitstr.c b/examples/splitstr.c index f2a1dad1..8e976892 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -6,16 +6,16 @@ int main() { - puts("Split with c_foreach_token (csview):"); + puts("Split with c_fortoken (csview):"); - c_foreach_token (i, "Hello World C99!", " ") + c_fortoken (i, "Hello World C99!", " ") printf("'%.*s'\n", c_ARGsv(i.token)); - puts("\nSplit with c_foreach_match (regex):"); + puts("\nSplit with c_formatch (regex):"); c_with (cregex re = cregex_from("[^ ]+", 0), cregex_drop(&re)) - c_foreach_match (i, &re, " Hello World C99! ") + c_formatch (i, &re, " Hello World C99! ") printf("'%.*s'\n", c_ARGsv(i.match[0])); } |
