From 3b05bbbaf3b69b3079ea5900738cddb190a3f5ef Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylov@users.noreply.github.com> Date: Thu, 27 May 2021 15:21:06 +0200 Subject: Update csview_api.md examples. --- docs/csview_api.md | 77 ++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 40 deletions(-) (limited to 'docs') 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 -#include -#include - -// cmap 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 +#include +#include -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 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 +``` -- cgit v1.2.3 From 11b690935b817a825ae057455309d45c48dcae77 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 28 May 2021 23:54:52 +0200 Subject: Changed the csview front() and back() API. Added csview_npos constant. --- docs/csview_api.md | 11 ++++++----- include/stc/csview.h | 6 ++++-- 2 files changed, 10 insertions(+), 7 deletions(-) (limited to 'docs') diff --git a/docs/csview_api.md b/docs/csview_api.md index b23ec9c2..34366671 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -7,9 +7,9 @@ element of the sequence at position zero. The implementation holds two members: **csview** is an efficient replacent for `const char*`. It never allocates memory, and therefore need not be destructed. Its lifetime is limited by the source string storage. It keeps the length of the string, and does not call *strlen()* when passing it around. It is faster when using`csview` as convertion type (raw) than `const char*` in associative -containers with cstr keys. E.g. prefer `using_cmap_svkey()` over `using_cmap_strkey()`. +containers with cstr keys. `using_cmap_svkey()` may perform better than `using_cmap_strkey()`. -Note that a **csview** may not be null-terminated, and should therefore be printed the following way: +Note: a **csview** may ***not be null-terminated***, and must therefore be printed like: `printf("%.*s", csview_ARG(sv))`. See the c++ class [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) for a functional @@ -35,6 +35,9 @@ csview csview_lit(const char literal_only[]); // same as c size_t csview_size(csview sv); size_t csview_length(csview sv); bool csview_empty(csview sv); +char csview_front(csview sv); +char csview_back(csview sv); + void csview_clear(csview* self); csview csview_substr(csview sv, intptr_t pos, size_t n); // negative pos count from end @@ -48,9 +51,6 @@ bool csview_contains(csview sv, csview needle); bool csview_begins_with(csview sv, csview sub); bool csview_ends_with(csview sv, csview sub); -const char* csview_front(const csview* self); -const char* csview_back(const csview* self); - csview_iter_t csview_begin(const csview* self); csview_iter_t csview_end(const csview* self); void csview_next(csview_iter_t* it); @@ -91,6 +91,7 @@ uint64_t csview_hash_ref(const csview* x, size_t ignored); | Name | Value | Usage | |:-----------------|:--------------------|:----------------------------------| | `csview_null` | same as `c_lit("")` | `sview = csview_null;` | +| `csview_npos` | same as `cstr_npos` | | | `c_lit(literal)` | csview constructor | `sview = c_lit("hello, world");` | | `csview_ARG(sv)` | printf argument | `printf("%.*s", csview_ARG(sv));` | diff --git a/include/stc/csview.h b/include/stc/csview.h index 4a8f704b..4a9d1dff 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -29,6 +29,7 @@ typedef struct { const char* str; size_t size; } csview; typedef struct { const char *ref; } csview_iter_t; typedef char csview_value_t; #define csview_null c_make(csview){"", 0} +#define csview_npos cstr_npos #define csview_ARG(sv) (int)(sv).size, (sv).str #define c_lit(literal) csview_lit(literal) @@ -45,9 +46,10 @@ STC_INLINE csview csview_from_s(cstr s) STC_INLINE size_t csview_size(csview sv) { return sv.size; } STC_INLINE size_t csview_length(csview sv) { return sv.size; } STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } +STC_INLINE char csview_front(csview sv) { return sv.str[0]; } +STC_INLINE char csview_back(csview sv) { return sv.str[sv.size - 1]; } + STC_INLINE void csview_clear(csview* self) { *self = csview_null; } -STC_INLINE const char* csview_front(const csview* self) { return self->str; } -STC_INLINE const char* csview_back(const csview* self) { return self->str + self->size - 1; } #define csview_hash(sv) c_default_hash((sv).str, (sv).size) STC_INLINE bool csview_equals(csview sv, csview sv2) -- cgit v1.2.3