diff options
| author | Tyge Løvset <[email protected]> | 2021-05-24 19:43:49 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-24 19:43:49 +0200 |
| commit | 41ef6709c8371b6240b24b8258c12ba61c52cdcb (patch) | |
| tree | e1e70137cdd64ba5690867ef26fde3a6affb4f82 /examples/splitstr.c | |
| parent | cd51563000fdb3beae6a4c55fb95d903f8641583 (diff) | |
| download | STC-modified-41ef6709c8371b6240b24b8258c12ba61c52cdcb.tar.gz STC-modified-41ef6709c8371b6240b24b8258c12ba61c52cdcb.zip | |
Added splitstr.c example. Renamed macro csview_ARG() to csview_arg(): special for printing csviews with "%.*s" format.
Diffstat (limited to 'examples/splitstr.c')
| -rw-r--r-- | examples/splitstr.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/splitstr.c b/examples/splitstr.c new file mode 100644 index 00000000..67499d0f --- /dev/null +++ b/examples/splitstr.c @@ -0,0 +1,39 @@ +#include <stc/csview.h>
+#include <stc/cvec.h>
+
+void print_split(csview str, csview sep)
+{
+ csview token = csview_first_token(str, sep);
+ for (;;) {
+ // print non-null-terminated csview
+ printf("\t\"%.*s\"\n", csview_arg(token));
+ if (csview_end(&token).ref == csview_end(&str).ref) break;
+ token = csview_next_token(str, sep, token);
+ }
+}
+
+using_cvec_str();
+
+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);
+ }
+ return vec;
+}
+
+int main()
+{
+ puts("Output from print_split():");
+ print_split(c_lit("//This is a//double-slash//separated//string"), c_lit("//")); puts("");
+ print_split(c_lit("This has no matching separator"), c_lit("xx")); puts("");
+
+ puts("Output from string_split():");
+ c_forvar (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v))
+ c_foreach (i, cvec_str, v)
+ printf("\t\"%s\"\n", i.ref->str);
+}
\ No newline at end of file |
