diff options
| author | Tyge Løvset <[email protected]> | 2021-12-29 12:14:35 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-29 12:14:35 +0100 |
| commit | 48e9d858da36b0fee7787edd12e7f336ccdf04b4 (patch) | |
| tree | cea95d0d3240c073de0877da08bc0675900a22ea /examples | |
| parent | 390e99e911c46ad59ecb966bffb275c9cba92fe1 (diff) | |
| download | STC-modified-48e9d858da36b0fee7787edd12e7f336ccdf04b4.tar.gz STC-modified-48e9d858da36b0fee7787edd12e7f336ccdf04b4.zip | |
Fixed and simplified csview tokensizer call.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/new_smap.c | 50 | ||||
| -rw-r--r-- | examples/splitstr.c | 35 | ||||
| -rw-r--r-- | examples/sview_split.c | 10 |
3 files changed, 49 insertions, 46 deletions
diff --git a/examples/new_smap.c b/examples/new_smap.c index e1a9b13a..382d27ae 100644 --- a/examples/new_smap.c +++ b/examples/new_smap.c @@ -1,10 +1,11 @@ #include <stc/cstr.h>
#include <stc/forward.h>
-forward_csmap(csmap_pnt, struct Point, int);
+forward_csmap(PMap, struct Point, int);
+// Use forward declared PMap in struct
struct MyStruct {
- csmap_pnt pntmap;
+ PMap pntmap;
cstr name;
} typedef MyStruct;
@@ -20,50 +21,55 @@ int point_cmp(const Point* a, const Point* b) { return c ? c : a->y - b->y;
}
+#define i_type PMap
#define i_key Point
#define i_val int
#define i_cmp point_cmp
#define i_opt c_is_fwd
-#define i_tag pnt
#include <stc/csmap.h>
-// int => int map
+// cstr => cstr map
+#define i_type SMap
#define i_key_str
#define i_val_str
#include <stc/csmap.h>
-// string set
+// cstr set
+#define i_type SSet
#define i_key_str
#include <stc/csset.h>
int main()
{
- c_auto (csmap_int, map)
- csmap_int_insert(&map, 123, 321);
+ c_auto (csmap_int, imap) {
+ csmap_int_insert(&imap, 123, 321);
+ }
- c_auto (csmap_pnt, pmap) {
- c_apply(v, csmap_pnt_insert(&pmap, c_pair(v)), csmap_pnt_value, {
+ c_auto (PMap, pmap) {
+ c_apply(v, PMap_insert(&pmap, c_pair(v)), PMap_value, {
{{42, 14}, 1},
{{32, 94}, 2},
{{62, 81}, 3},
});
- c_foreach (i, csmap_pnt, pmap)
- printf(" (%d,%d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second);
+ c_forpair (p, i, PMap, pmap)
+ printf(" (%d,%d: %d)", _.p.x, _.p.y, _.i);
puts("");
}
- c_auto (csmap_str, smap) {
- csmap_str_emplace(&smap, "Hello, friend", "this is the mapped value");
- csmap_str_emplace(&smap, "The brown fox", "jumped");
- csmap_str_emplace(&smap, "This is the time", "for all good things");
- c_foreach (i, csmap_str, smap)
- printf(" (%s: %s)\n", i.ref->first.str, i.ref->second.str);
+ c_auto (SMap, smap) {
+ c_apply(v, SMap_emplace(&smap, c_pair(v)), SMap_raw, {
+ {"Hello, friend", "this is the mapped value"},
+ {"The brown fox", "jumped"},
+ {"This is the time", "for all good things"},
+ });
+ c_forpair (i, j, SMap, smap)
+ printf(" (%s: %s)\n", _.i.str, _.j.str);
}
- c_auto (csset_str, sset) {
- csset_str_emplace(&sset, "Hello, friend");
- csset_str_emplace(&sset, "Goodbye, foe");
- printf("Found? %s\n", csset_str_contains(&sset, "Hello, friend") ? "true" : "false");
+ c_auto (SSet, sset) {
+ SSet_emplace(&sset, "Hello, friend");
+ SSet_emplace(&sset, "Goodbye, foe");
+ printf("Found? %s\n", SSet_contains(&sset, "Hello, friend") ? "true" : "false");
}
-}
\ No newline at end of file +}
diff --git a/examples/splitstr.c b/examples/splitstr.c index 789b2c59..75ee3022 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -2,12 +2,11 @@ void print_split(csview str, csview sep)
{
- csview token = csview_first_token(str, sep);
- for (;;) {
+ size_t pos = 0;
+ while (pos != str.size) {
+ csview tok = csview_token(str, sep, &pos);
// print non-null-terminated csview
- printf("\t\"%.*s\"\n", c_svarg(token));
- if (csview_end(&token).ref == csview_end(&str).ref) break;
- token = csview_next_token(str, sep, token);
+ printf("[" c_svfmt "]\n", c_svarg(tok));
}
}
@@ -17,26 +16,22 @@ void print_split(csview str, csview sep) cvec_str string_split(csview str, csview sep)
{
cvec_str vec = cvec_str_init();
- csview token = csview_first_token(str, sep);
- for (;;) {
- cvec_str_push_back(&vec, cstr_from_v(token));
- if (csview_end(&token).ref == csview_end(&str).ref) break;
- token = csview_next_token(str, sep, token);
+ size_t pos = 0;
+ while (pos != str.size) {
+ csview tok = csview_token(str, sep, &pos);
+ cvec_str_push_back(&vec, cstr_from_v(tok));
}
return vec;
}
int main()
{
- puts("Output from print_split():");
- print_split(c_sv("//This is a//double-slash//separated//string"), c_sv("//")); puts("");
- print_split(c_sv("This has no matching separator"), c_sv("xx")); puts("");
+ print_split(c_sv("//This is a//double-slash//separated//string"), c_sv("//"));
+ puts("");
+ print_split(c_sv("This has no matching separator"), c_sv("xx"));
+ puts("");
- puts("Output from string_split():");
- cstr string = cstr_new("Split,this,,string,now,");
- cvec_str vec = string_split(cstr_sv(string), c_sv(","));
-
- c_autodefer (cvec_str_drop(&vec), cstr_drop(&string))
- c_foreach (i, cvec_str, vec)
- printf("\t\"%s\"\n", i.ref->str);
+ c_autovar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_drop(&v))
+ c_foreach (i, cvec_str, v)
+ printf("[%s]\n", i.ref->str);
}
\ No newline at end of file diff --git a/examples/sview_split.c b/examples/sview_split.c index 3a079583..ed0365ec 100644 --- a/examples/sview_split.c +++ b/examples/sview_split.c @@ -4,11 +4,13 @@ int main() {
// No memory allocations or string length calculations!
const csview date = c_sv("2021/03/12");
- const csview year = csview_first_token(date, c_sv("/"));
- const csview month = csview_next_token(date, c_sv("/"), year);
- const csview day = csview_next_token(date, c_sv("/"), month);
+ size_t pos = 0;
+ const csview year = csview_token(date, c_sv("/"), &pos);
+ const csview month = csview_token(date, c_sv("/"), &pos);
+ const csview day = csview_token(date, c_sv("/"), &pos);
- printf(c_svfmt ", " c_svfmt ", " c_svfmt "\n", c_svarg(year), c_svarg(month), c_svarg(day));
+ printf(c_svfmt ", " c_svfmt ", " c_svfmt "\n",
+ c_svarg(year), c_svarg(month), c_svarg(day));
c_auto (cstr, y, m, d) {
y = cstr_from_v(year), m = cstr_from_v(month), d = cstr_from_v(day);
|
