diff options
| author | Tyge Løvset <[email protected]> | 2021-05-26 20:24:37 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-26 20:24:37 +0200 |
| commit | b840bc032d8500379888caf93702ab057e712937 (patch) | |
| tree | 7a1fa0fcc56e7d5c4e5ba333345ca3f803f800a6 /include | |
| parent | 3fbc5727832c0687d97a218e85d93857aadcea1c (diff) | |
| download | STC-modified-b840bc032d8500379888caf93702ab057e712937.tar.gz STC-modified-b840bc032d8500379888caf93702ab057e712937.zip | |
Removed cstr_trim, csview_trim, Changed cstr_substr to mutable. Allow negative pos on cstr_substr, csview_substr. Added cstr_slice, csview_slice.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/cstr.h | 14 | ||||
| -rw-r--r-- | include/stc/csview.h | 15 |
2 files changed, 20 insertions, 9 deletions
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; }
|
