summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-29 12:14:35 +0100
committerTyge Løvset <[email protected]>2021-12-29 12:14:35 +0100
commit48e9d858da36b0fee7787edd12e7f336ccdf04b4 (patch)
treecea95d0d3240c073de0877da08bc0675900a22ea
parent390e99e911c46ad59ecb966bffb275c9cba92fe1 (diff)
downloadSTC-modified-48e9d858da36b0fee7787edd12e7f336ccdf04b4.tar.gz
STC-modified-48e9d858da36b0fee7787edd12e7f336ccdf04b4.zip
Fixed and simplified csview tokensizer call.
-rw-r--r--docs/csview_api.md23
-rw-r--r--examples/new_smap.c50
-rw-r--r--examples/splitstr.c35
-rw-r--r--examples/sview_split.c10
-rw-r--r--include/stc/csview.h21
5 files changed, 65 insertions, 74 deletions
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 4e5d3b0e..a582505f 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -41,8 +41,7 @@ void csview_clear(csview* self);
csview csview_substr(csview sv, intptr_t pos, size_t n); // negative pos count from end
csview csview_slice(csview sv, intptr_t p1, intptr_t p2); // negative p1, p2 count from end
-csview csview_first_token(csview sv, csview sep); // see split example below.
-csview csview_next_token(csview sv, csview sep, csview token);
+csview csview_token(csview sv, csview sep, size_t* start); // see split example below.
bool csview_equals(csview sv, csview sv2);
size_t csview_find(csview sv, csview needle);
@@ -135,12 +134,11 @@ and does not depend on null-terminated strings. *string_split()* function return
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("\"%.*s\"\n", csview_ARG(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));
}
}
@@ -150,11 +148,10 @@ 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;
}
@@ -168,7 +165,7 @@ int main()
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);
+ printf("[%s]\n", i.ref->str);
}
```
Output:
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);
diff --git a/include/stc/csview.h b/include/stc/csview.h
index d0152c3d..69de5749 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -38,8 +38,7 @@ typedef char csview_value;
STC_API csview csview_substr(csview sv, intptr_t pos, size_t n);
STC_API csview csview_slice(csview sv, intptr_t p1, intptr_t p2);
-STC_API csview csview_first_token(csview sv, csview sep);
-STC_API csview csview_next_token(csview sv, csview sep, csview tok);
+STC_API csview csview_token(csview sv, csview sep, size_t* start);
#define csview_new(literal) \
c_make(csview){literal, sizeof c_make(c_strlit){literal} - 1}
@@ -141,19 +140,11 @@ csview_slice(csview sv, intptr_t p1, intptr_t p2) {
}
STC_DEF csview
-csview_first_token(csview sv, csview sep) {
- const char* res = c_strnstrn(sv.str, sep.str, sv.size, sep.size);
- return c_make(csview){sv.str, (res ? res - sv.str : sv.size)};
-}
-
-STC_DEF csview
-csview_next_token(csview sv, csview sep, csview tok) {
- if (&tok.str[tok.size] == &sv.str[sv.size])
- return c_make(csview){&sv.str[sv.size], 0};
- tok.str += tok.size + sep.size;
- size_t n = sv.size - (tok.str - sv.str);
- const char* res = c_strnstrn(tok.str, sep.str, n, sep.size);
- tok.size = res ? res - tok.str : n;
+csview_token(csview sv, csview sep, size_t* start) {
+ csview slice = {sv.str + *start, sv.size - *start};
+ const char* res = c_strnstrn(slice.str, sep.str, slice.size, sep.size);
+ csview tok = {slice.str, (res ? res - slice.str : slice.size)};
+ *start += tok.size + (res ? sep.size : 0);
return tok;
}