diff options
| author | Tyge Løvset <[email protected]> | 2022-08-07 08:03:46 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-07 08:20:34 +0200 |
| commit | c87898773d1af364a9847610401a9959f6019fe7 (patch) | |
| tree | 5e64c9d96fb9e12192ce298f1d2909d43b72571a /include/stc/csview.h | |
| parent | 618b5704e6f85cfe1b6e5c9c9373abe76a8bb628 (diff) | |
| download | STC-modified-c87898773d1af364a9847610401a9959f6019fe7.tar.gz STC-modified-c87898773d1af364a9847610401a9959f6019fe7.zip | |
Internal: moved some functions in csview/cstr to implementation sections.
Diffstat (limited to 'include/stc/csview.h')
| -rw-r--r-- | include/stc/csview.h | 87 |
1 files changed, 47 insertions, 40 deletions
diff --git a/include/stc/csview.h b/include/stc/csview.h index b4b701f2..2ebcaabe 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -42,13 +42,10 @@ STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } STC_INLINE bool csview_equals(csview sv, csview sv2) { return sv.size == sv2.size && !memcmp(sv.str, sv2.str, sv.size); } -STC_INLINE size_t csview_find(csview sv, csview search) { - char* res = c_strnstrn(sv.str, search.str, sv.size, search.size); - return res ? res - sv.str : csview_npos; -} +STC_API size_t csview_find(csview sv, csview search); STC_INLINE bool csview_contains(csview sv, csview search) - { return c_strnstrn(sv.str, search.str, sv.size, search.size) != NULL; } + { return csview_find(sv, search) != csview_npos; } STC_INLINE bool csview_starts_with(csview sv, csview sub) { if (sub.size > sv.size) return false; @@ -98,40 +95,9 @@ STC_INLINE csview csview_u8_slice(csview sv, size_t u8p1, size_t u8p2) STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } -/* "Rarely" used extended substr_ex(), slice_ex(), and token() function */ - -STC_INLINE csview -csview_substr_ex(csview sv, intptr_t pos, size_t n) { - if (pos < 0) { - pos += sv.size; - if (pos < 0) pos = 0; - } - if (pos > (intptr_t)sv.size) 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_ex(csview sv, intptr_t p1, intptr_t p2) { - if (p1 < 0) { - p1 += sv.size; - if (p1 < 0) p1 = 0; - } - if (p2 < 0) p2 += sv.size; - if (p2 > (intptr_t)sv.size) p2 = sv.size; - sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; - return sv; -} - -STC_INLINE csview -csview_token(csview sv, csview sep, size_t* start) { - csview slice = {sv.str + *start, sv.size - *start}; - const char* res = c_strnstrn(slice.str, sep.str, slice.size, sep.size); - csview tok = {slice.str, res ? res - slice.str : slice.size}; - *start += tok.size + sep.size; - return tok; -} +STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n); +STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); +STC_API csview csview_token(csview sv, csview sep, size_t* start); /* csview interaction with cstr: */ #ifdef CSTR_H_INCLUDED @@ -166,9 +132,50 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) STC_INLINE bool csview_eq(const csview* x, const csview* y) { return x->size == y->size && !memcmp(x->str, y->str, x->size); } -STC_INLINE uint64_t csview_hash(const csview *self) +STC_API uint64_t csview_hash(const csview *self); + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) || defined(i_extern) + +STC_DEF size_t csview_find(csview sv, csview search) { + char* res = c_strnstrn(sv.str, search.str, sv.size, search.size); + return res ? res - sv.str : csview_npos; +} + +STC_DEF uint64_t csview_hash(const csview *self) { return c_fasthash(self->str, self->size); } +STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, size_t n) { + if (pos < 0) { + pos += sv.size; + if (pos < 0) pos = 0; + } + if (pos > (intptr_t)sv.size) pos = sv.size; + if (pos + n > sv.size) n = sv.size - pos; + sv.str += pos, sv.size = n; + return sv; +} + +STC_DEF csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) { + if (p1 < 0) { + p1 += sv.size; + if (p1 < 0) p1 = 0; + } + if (p2 < 0) p2 += sv.size; + if (p2 > (intptr_t)sv.size) p2 = sv.size; + sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; + return sv; +} + +STC_DEF csview csview_token(csview sv, csview sep, size_t* start) { + csview slice = {sv.str + *start, sv.size - *start}; + const char* res = c_strnstrn(slice.str, sep.str, slice.size, sep.size); + csview tok = {slice.str, res ? res - slice.str : slice.size}; + *start += tok.size + sep.size; + return tok; +} + +#endif #endif #undef i_opt #undef i_header |
