summaryrefslogtreecommitdiffhomepage
path: root/docs/csview_api.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-05-27 15:21:06 +0200
committerGitHub <[email protected]>2021-05-27 15:21:06 +0200
commit3b05bbbaf3b69b3079ea5900738cddb190a3f5ef (patch)
treee3d1afd36b30fc230622bca8ce39ec3b4b893eee /docs/csview_api.md
parent032b6d654a1125fe172ef225fdacb87ac3e1fdc5 (diff)
downloadSTC-modified-3b05bbbaf3b69b3079ea5900738cddb190a3f5ef.tar.gz
STC-modified-3b05bbbaf3b69b3079ea5900738cddb190a3f5ef.zip
Update csview_api.md examples.
Diffstat (limited to 'docs/csview_api.md')
-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 b23ec9c2..a3be3b96 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -118,35 +118,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)
@@ -213,30 +208,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
+```