From 47cd9cad6672f68c892ed0e38bf1b4ced1c8a692 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 21 May 2021 21:54:27 +0200 Subject: Added substr() and cleanup of trimmed() API. --- include/stc/cstr.h | 2 ++ include/stc/csview.h | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 131a3d78..a188b1cf 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -78,6 +78,8 @@ STC_INLINE void cstr_del(cstr* self) { if (_cstr_rep(self)->cap) c_free(_cstr_rep(self)); } STC_INLINE cstr cstr_clone(cstr s) { return cstr_from_n(s.str, _cstr_rep(&s)->size); } +STC_INLINE cstr cstr_substr(cstr s, size_t pos, size_t n) + { return cstr_from_n(s.str + pos, n); } STC_INLINE void cstr_clear(cstr* self) { self->str[_cstr_rep(self)->size = 0] = '\0'; } STC_INLINE cstr* cstr_assign(cstr* self, const char* str) diff --git a/include/stc/csview.h b/include/stc/csview.h index 2e52ad73..324498e0 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -43,11 +43,10 @@ STC_INLINE csview csview_from_n(const char* str, size_t n) 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_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; } @@ -98,6 +97,8 @@ STC_INLINE cstr cstr_from_v(csview sv) { return cstr_from_n(sv.str, sv.size); } STC_INLINE csview cstr_to_v(const cstr* self) { return c_make(csview){self->str, _cstr_rep(self)->size}; } +STC_INLINE csview cstr_trimmed(cstr s, size_t left, size_t right) + { return csview_trimmed(c_sv(s), left, right); } STC_INLINE cstr* cstr_assign_v(cstr* self, csview sv) { return cstr_assign_n(self, sv.str, sv.size); } STC_INLINE cstr* cstr_append_v(cstr* self, csview sv) @@ -120,7 +121,6 @@ STC_INLINE bool cstr_ends_with_v(cstr s, csview sub) { if (sub.size > cstr_size(s)) return false; return !memcmp(s.str + cstr_size(s) - sub.size, sub.str, sub.size); } - /* ---- Adaptor functions ---- */ #define csview_compare_ref(xp, yp) strcmp((xp)->str, (yp)->str) -- cgit v1.2.3