From c7a6f7c17e2d2a2b0144cbec87f4b2c93e8150dd Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 27 May 2021 10:07:57 +0200 Subject: Made substr() and slice() only returning csview. Added Both cstr and csview input argument variants. Changed def of cstr_npos. --- include/stc/clist.h | 6 +++--- include/stc/cstr.h | 13 ++----------- include/stc/csview.h | 41 +++++++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 32 deletions(-) (limited to 'include') diff --git a/include/stc/clist.h b/include/stc/clist.h index f07aa9d6..496fc9d5 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -110,7 +110,7 @@ STC_API size_t _clist_count(const clist_VOID* self); STC_API CX##_iter_t CX##_erase_range(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ STC_API size_t CX##_remove(CX* self, RawValue val); \ STC_API CX##_iter_t CX##_splice(CX* self, CX##_iter_t it, CX* other); \ - STC_API CX CX##_split(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ + STC_API CX CX##_split_off(CX* self, CX##_iter_t it1, CX##_iter_t it2); \ STC_API void CX##_sort(CX* self); \ STC_API CX##_iter_t CX##_find_in(CX##_iter_t it1, CX##_iter_t it2, RawValue val); \ STC_API CX##_node_t* CX##_erase_after_(CX* self, CX##_node_t* node); \ @@ -168,7 +168,7 @@ STC_API size_t _clist_count(const clist_VOID* self); STC_INLINE CX##_iter_t \ CX##_splice_range(CX* self, CX##_iter_t it, \ CX* other, CX##_iter_t it1, CX##_iter_t it2) { \ - CX tmp = CX##_split(other, it1, it2); \ + CX tmp = CX##_split_off(other, it1, it2); \ return CX##_splice(self, it, &tmp); \ } \ \ @@ -297,7 +297,7 @@ STC_API size_t _clist_count(const clist_VOID* self); } \ \ STC_DEF CX \ - CX##_split(CX* self, CX##_iter_t it1, CX##_iter_t it2) { \ + CX##_split_off(CX* self, CX##_iter_t it1, CX##_iter_t it2) { \ CX cx = {NULL}; \ if (it1.ref == it2.ref) return cx; \ CX##_node_t *p1 = it1.prev, \ diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 7b7fdaa6..8f5841eb 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -34,7 +34,7 @@ typedef struct { char* str; } cstr; typedef struct { char *ref; } cstr_iter_t; typedef char cstr_value_t; -#define cstr_npos ((size_t) (-1)) +#define cstr_npos INTPTR_MAX STC_LIBRARY_ONLY( extern const cstr cstr_null; ) struct cstr_rep { size_t size, cap; char str[sizeof(size_t)]; }; @@ -61,6 +61,7 @@ STC_API size_t cstr_find(cstr s, const char* needle); STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax); STC_API size_t cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t nmax); STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream); + STC_API int c_strncasecmp(const char* s1, const char* s2, size_t nmax); STC_API char* c_strnstrn(const char* s, const char* needle, size_t slen, size_t nmax); STC_API char* c_strncasestrn(const char* s, const char* needle, size_t slen, size_t nmax); @@ -78,16 +79,6 @@ 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* 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 7d6946ca..9cfef3b6 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -29,7 +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_arg(sv) (int)(sv).size, (sv).str +#define csview_ARG(sv) (int)(sv).size, (sv).str #define c_lit(literal) csview_lit(literal) #define c_sv(s) csview_from_s(s) @@ -42,16 +42,6 @@ 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, 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; } @@ -79,20 +69,31 @@ STC_INLINE csview_iter_t csview_end(const csview* self) { return c_make(csview_iter_t){self->str + self->size}; } STC_INLINE void csview_next(csview_iter_t* it) { ++it->ref; } +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 csview csview_first_token(csview sv, csview sep) { const char* res = c_strnstrn(sv.str, sep.str, sv.size, sep.size); return c_make(csview){sv.str, (res ? res - sv.str : sv.size)}; } -STC_INLINE csview csview_next_token(csview sv, csview sep, csview token) { - if (&token.str[token.size] == &sv.str[sv.size]) +STC_INLINE csview csview_next_token(csview sv, csview sep, csview tok) { + if (&tok.str[tok.size] == &sv.str[sv.size]) return c_make(csview){&sv.str[sv.size], 0}; - token.str += token.size + sep.size; - size_t n = sv.size - (token.str - sv.str); - const char* res = c_strnstrn(token.str, sep.str, n, sep.size); - token.size = res ? res - token.str : n; - return token; + tok.str += tok.size + sep.size; + size_t n = sv.size - (tok.str - sv.str); + const char* res = c_strnstrn(tok.str, sep.str, n, sep.size); + tok.size = res ? res - tok.str : n; + return tok; } /* cstr interaction with csview: */ @@ -101,6 +102,10 @@ 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_substr(cstr s, intptr_t pos, size_t n) + { return csview_substr(csview_from_s(s), pos, n); }; +STC_INLINE csview cstr_slice(cstr s, intptr_t p1, intptr_t p2) + { return csview_slice(csview_from_s(s), p1, p2); }; 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) -- cgit v1.2.3