diff options
| author | Tyge Løvset <[email protected]> | 2022-07-31 09:15:36 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-31 10:32:12 +0200 |
| commit | 8a5478f7c14d4c7476d36b137fd7d2c8d95f2c13 (patch) | |
| tree | e54045852e198dfbecbd82a55a854698658c4a6b /include/stc/csview.h | |
| parent | 90b4371d8a36ab609c2200289634c171a19f7fff (diff) | |
| download | STC-modified-8a5478f7c14d4c7476d36b137fd7d2c8d95f2c13.tar.gz STC-modified-8a5478f7c14d4c7476d36b137fd7d2c8d95f2c13.zip | |
csview: csview_token(sv, sep, &start): now start > sv.size on last token. Removed usage of c_ARGsv() macro in examples => printf formatting ".*s". csview fully inlined.
Diffstat (limited to 'include/stc/csview.h')
| -rw-r--r-- | include/stc/csview.h | 75 |
1 files changed, 34 insertions, 41 deletions
diff --git a/include/stc/csview.h b/include/stc/csview.h index ec40a8be..b4b701f2 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -31,10 +31,6 @@ #define csview_new(literal) c_sv(literal) #define csview_npos (SIZE_MAX >> 1) -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); - STC_INLINE csview csview_init() { return csview_null; } STC_INLINE csview csview_from(const char* str) { return c_make(csview){str, strlen(str)}; } @@ -102,6 +98,40 @@ 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; +} /* csview interaction with cstr: */ #ifdef CSTR_H_INCLUDED @@ -139,43 +169,6 @@ STC_INLINE bool csview_eq(const csview* x, const csview* y) STC_INLINE uint64_t csview_hash(const csview *self) { return c_fasthash(self->str, self->size); } -/* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined(i_implement) || defined(i_extern) - -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 : (sep.size = 0, slice.size)}; - *start += tok.size + sep.size; - return tok; -} - -#endif #endif #undef i_opt #undef i_header |
