summaryrefslogtreecommitdiffhomepage
path: root/docs
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-05-28 23:55:11 +0200
committerTyge Løvset <[email protected]>2021-05-28 23:55:11 +0200
commita476565257b68af526998d4ffd1b7e77bfe4ea96 (patch)
treeb13b51cea2bbac6d3d961f977f8bda7c66e4f789 /docs
parent11b690935b817a825ae057455309d45c48dcae77 (diff)
parent3b05bbbaf3b69b3079ea5900738cddb190a3f5ef (diff)
downloadSTC-modified-a476565257b68af526998d4ffd1b7e77bfe4ea96.tar.gz
STC-modified-a476565257b68af526998d4ffd1b7e77bfe4ea96.zip
Merge branch 'master' of https://github.com/tylov/STC
Diffstat (limited to 'docs')
-rw-r--r--docs/csview_api.md77
1 files changed, 37 insertions, 40 deletions
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 34366671..fc51702f 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -119,35 +119,30 @@ using_cset_sv()
## Example
```c
#include <stc/csview.h>
-#include <stc/cvec.h>
-#include <stc/cmap.h>
-
-// cmap<cstr, int> with csview as convertion type
-using_cmap_svkey(si, int);
-int main()
+int main ()
{
- csview text = c_lit("The length of this literal is evaluated at compile time and stored in csview text.");
- printf("%s\nLength: %zu\n\n", text.str, text.size);
+ cstr str1 = cstr_lit("We think in generalities, but we live in details.");
+ // (quoting Alfred N. Whitehead)
- c_forvar (cmap_si map = csmap_si_init(), csmap_si_del(&map))
- {
- cmap_si_emplace(&map, c_lit("hello"), 100);
- cmap_si_emplace(&map, c_lit("world"), 200);
- cmap_si_emplace(&map, c_lit("hello"), 300); // already in map, ignored
+ csview sv1 = cstr_substr(str1, 3, 5); // "think"
+ size_t pos = cstr_find(str1, "live"); // position of "live" in str1
+ csview sv2 = cstr_substr(str1, pos, 4); // get "live"
+ csview sv3 = cstr_slice(str1, -8, -1); // get "details"
+ printf("%.*s %.*s %.*s\n", csview_ARG(sv1), csview_ARG(sv2), csview_ARG(sv3));
- // Efficient lookup: no string allocation or strlen() takes place:
- cmap_si_value_t* v = cmap_si_get(&map, c_lit("world"));
- printf("\n%s: %d\n", v->first.str, v->second);
- }
+ cstr s1 = cstr_lit("Apples are red");
+ cstr s2 = cstr_from_v(cstr_substr(s1, -3, 3)); // "red"
+ cstr s3 = cstr_from_v(cstr_substr(s1, 0, 6)); // "Apples"
+ printf("%s %s\n", s2, s3.str);
+
+ c_del(cstr, &str1, &s1, &s2, &s3);
}
```
Output:
```
-A long and winded literal string
-Length: 32
-
-world: 200
+think live details
+red Apples
```
### Example 2: csview tokenizer (string split)
@@ -214,30 +209,32 @@ Output:
### Example 3
```c
#include <stc/csview.h>
+#include <stc/cvec.h>
+#include <stc/cmap.h>
-int main ()
-{
- cstr str1 = cstr_lit("We think in generalities, but we live in details.");
- // (quoting Alfred N. Whitehead)
-
- csview sv1 = cstr_substr(str1, 3, 5); // "think"
- size_t pos = cstr_find(str1, "live"); // position of "live" in str
- csview sv2 = cstr_substr(str1, pos, cstr_npos); // get from "live" to the end
+// cmap<cstr, int> with csview as convertion type
+using_cmap_svkey(si, int);
- printf("%.*s %.*s\n", csview_ARG(sv1), csview_ARG(sv2));
+int main()
+{
+ csview text = c_lit("The length of this literal is evaluated at compile time and stored in csview text.");
+ csview suffix = csview_substr(text, -12, cstr_npos); // from pos -12 to end
+ printf("%.*s\n", csview_ARG(suffix));
- cstr s1 = cstr_lit("Apples are red");
- cstr s2 = cstr_from_v(cstr_substr(s1, 11, 3)); // "red"
- printf("%s\n", s2.str);
- cstr s3 = cstr_from_v(cstr_substr(s1, 0, 6)); // "Apples"
- printf("%s\n", s3.str);
+ c_forvar (cmap_si map = cmap_si_init(), cmap_si_del(&map))
+ {
+ cmap_si_emplace(&map, c_lit("hello"), 100);
+ cmap_si_emplace(&map, c_lit("world"), 200);
+ cmap_si_emplace(&map, c_lit("hello"), 300); // already in map, ignored
- c_del(cstr, &str1, &s1, &s2, &s3);
+ // Efficient lookup: no string allocation or strlen() takes place:
+ cmap_si_value_t* v = cmap_si_get(&map, c_lit("hello"));
+ printf("%s: %d\n", v->first.str, v->second);
+ }
}
```
Output:
```
-think live in details.
-red
-Apples
-``` \ No newline at end of file
+csview text.
+hello: 100
+```