From b840bc032d8500379888caf93702ab057e712937 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 26 May 2021 20:24:37 +0200 Subject: Removed cstr_trim, csview_trim, Changed cstr_substr to mutable. Allow negative pos on cstr_substr, csview_substr. Added cstr_slice, csview_slice. --- docs/cstr_api.md | 6 +++--- docs/csview_api.md | 6 +++--- include/stc/cstr.h | 14 ++++++++++---- include/stc/csview.h | 15 ++++++++++----- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 49ece6e0..26450ac7 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -23,7 +23,6 @@ cstr cstr_with_capacity(size_t cap); cstr cstr_with_size(size_t len, char fill); // repeat fill len times cstr cstr_from_fmt(const char* fmt, ...); // printf() formatting cstr cstr_clone(cstr s); -cstr cstr_substr(cstr s, size_t pos, size_t n); cstr* cstr_take(cstr* self, cstr s); // take the constructed or moved string cstr cstr_move(cstr* self); // move string to caller, leave empty string @@ -42,8 +41,9 @@ cstr* cstr_assign(cstr* self, const char* str); cstr* cstr_assign_n(cstr* self, const char* str, size_t n); // assign n first chars of str cstr* cstr_assign_fmt(cstr* self, const char* fmt, ...); // printf() formatting cstr* cstr_copy(cstr* self, cstr s); // cstr_take(self, cstr_clone(s)) -cstr* cstr_trim(cstr* self, size_t left, size_t right); - +cstr* cstr_substr(cstr* self, intptr_t pos, size_t n); // negative pos count from end +cstr* cstr_slice(cstr* self, intptr_t p1, intptr_t p2); // negative p1,p2 count from end. + // substr(), slice() modifies self cstr* cstr_append(cstr* self, const char* str); cstr* cstr_append_s(cstr* self, cstr s); cstr* cstr_append_n(cstr* self, const char* str, size_t n); diff --git a/docs/csview_api.md b/docs/csview_api.md index 64c42973..63b84a1d 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -37,9 +37,9 @@ size_t csview_length(csview sv); bool csview_empty(csview sv); void csview_clear(csview* self); -csview csview_substr(csview sv, size_t pos, size_t n); -csview csview_trimmed(csview sv, size_t left, size_t right); -csview csview_first_token(csview sv, csview sep); +csview csview_substr(csview sv, intptr_t pos, size_t n); // negative pos count from end +csview csview_slice(csview sv, intptr_t p1, intptr_t p2); // negative p1,p2 count from end +csview csview_first_token(csview sv, csview sep); // see split example below. csview csview_next_token(csview sv, csview sep, csview token); bool csview_equals(csview sv, csview sv2); diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 695385a2..7b7fdaa6 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -78,10 +78,16 @@ 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 cstr* cstr_trim(cstr* self, size_t left, size_t right) - { return cstr_assign_n(self, self->str + left, _cstr_rep(self)->size - left - right); } +STC_INLINE cstr* cstr_substr(cstr* self, intptr_t pos, size_t n) { + size_t sz = _cstr_rep(self)->size; + if (pos < 0) pos += sz; if (pos + n > sz) n = sz - pos; + return cstr_assign_n(self, self->str + pos, n); + } +STC_INLINE cstr* cstr_slice(cstr* self, intptr_t p1, intptr_t p2) { + size_t sz = _cstr_rep(self)->size; + if (p1 < 0) p1 += sz; if (p2 < 0) p2 += sz; if (p2 > sz) p2 = sz; + return cstr_assign_n(self, self->str + p1, p2 > p1 ? p2 - p1 : 0); + } 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 c851cf31..7d6946ca 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -42,11 +42,16 @@ STC_INLINE csview csview_from_n(const char* str, size_t n) { return c_make(csview){str, n}; } 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_substr(csview sv, intptr_t pos, size_t n) { + if (pos < 0) pos += sv.size; + if (pos + n > sv.size) n = sv.size - pos; + sv.str += pos, sv.size = n; return sv; + } +STC_INLINE csview csview_slice(csview sv, intptr_t p1, intptr_t p2) { + if (p1 < 0) p1 += sv.size; if (p2 < 0) p2 += sv.size; + if (p2 > sv.size) p2 = sv.size; + sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; return sv; + } 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; } -- cgit v1.2.3