From 079aa69a085ddb478c15f20f6f0f4b0d790022b8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 20 May 2021 16:37:21 +0200 Subject: Final API changes for csview: Added csview_trimmed*(), removed a few. --- docs/csview_api.md | 10 +++++----- stc/ccommon.h | 1 + stc/csview.h | 11 ++++------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/docs/csview_api.md b/docs/csview_api.md index ba5daed8..fed7f6f4 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -9,6 +9,9 @@ Its lifetime is limited by the source string storage. It keeps the length of the 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()`. +Note that a **csview** may not be null-terminated, and should therefore be printed the following way: +`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 description. @@ -24,21 +27,18 @@ All csview definitions and prototypes are available by including a single header ```c csview c_lit(const char literal_only[]); // csview from literal, no strlen() csview c_sv(cstr s); // construct csview from cstr - csview csview_from(const char* str); // construct from (const char*) csview csview_from_n(const char* str, size_t n); // construct csview csview_from_s(cstr s); // same as c_sv() csview csview_lit(const char literal_only[]); // same as c_lit() -csview csview_remove_prefix(csview sv, size_t n); -csview csview_remove_suffix(csview sv, size_t n); -csview csview_substr(csview sv, size_t pos, size_t n); - size_t csview_size(csview sv); size_t csview_length(csview sv); bool csview_empty(csview sv); void csview_clear(csview* self); +csview csview_trimmed(csview sv, size_t left, size_t right); +csview csview_trimmed_s(cstr s, size_t left, size_t right); bool csview_equals(csview sv, csview sv2); size_t csview_find(csview sv, csview needle); diff --git a/stc/ccommon.h b/stc/ccommon.h index dc87471e..d29ce699 100644 --- a/stc/ccommon.h +++ b/stc/ccommon.h @@ -132,6 +132,7 @@ *b = (n)*sizeof *b > (BYTES) ? c_new_n(type, n) : _c_b \ ; b; b != _c_b ? c_free(b) : (void)0, b = NULL) #define c_breakwith continue +#define c_breakdefer continue #define c_var(CX, c, ...) \ CX c = CX##_init(); c_emplace(CX, c, __VA_ARGS__) diff --git a/stc/csview.h b/stc/csview.h index 19f89983..d4d3f65d 100644 --- a/stc/csview.h +++ b/stc/csview.h @@ -39,18 +39,15 @@ STC_INLINE csview csview_from(const char* str) { return c_make(csview){str, strlen(str)}; } STC_INLINE csview csview_from_n(const char* str, size_t n) { return c_make(csview){str, n}; } - #define csview_lit(literal) \ c_make(csview){literal, sizeof c_make(strlit_t){literal} - 1} STC_INLINE csview csview_from_s(cstr s) { return c_make(csview){s.str, _cstr_rep(&s)->size}; } -STC_INLINE csview csview_remove_prefix(csview sv, size_t n) - { sv.str += n, sv.size -= n; return sv; } -STC_INLINE csview csview_remove_suffix(csview sv, size_t n) - { sv.size -= n; return sv; } -STC_INLINE csview csview_substr(csview sv, size_t pos, size_t n) - { sv.str += pos, sv.size = n; return sv; } +STC_INLINE csview csview_trimmed(csview sv, size_t left, size_t right) + { sv.str += left, sv.size -= left + right; return sv; } +STC_INLINE csview csview_trimmed_s(cstr s, size_t left, size_t right) + { return c_make(csview){s.str + left, _cstr_rep(&s)->size - right - left}; } STC_INLINE size_t csview_size(csview sv) { return sv.size; } STC_INLINE size_t csview_length(csview sv) { return sv.size; } -- cgit v1.2.3