diff options
| author | Tyge Løvset <[email protected]> | 2021-05-20 23:35:57 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-20 23:35:57 +0200 |
| commit | 188717d9c7ca04e7b0e63e48cea0c55e5db11f05 (patch) | |
| tree | 4cf706655ef40de018a2eedcb8141878ea348244 /include | |
| parent | 6db18f878aac53c192b10e43c7cda2465d5496c8 (diff) | |
| download | STC-modified-188717d9c7ca04e7b0e63e48cea0c55e5db11f05.tar.gz STC-modified-188717d9c7ca04e7b0e63e48cea0c55e5db11f05.zip | |
Added csview tokenizer (split string).
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/csview.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/include/stc/csview.h b/include/stc/csview.h index d4d3f65d..7111471b 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -56,6 +56,7 @@ STC_INLINE void csview_clear(csview* self) { *self = csview_null; } STC_INLINE const char* csview_front(const csview* self) { return self->str; }
STC_INLINE const char* csview_back(const csview* self) { return self->str + self->size - 1; }
+#define csview_hash(sv) c_default_hash((sv).str, (sv).size)
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 needle)
@@ -75,6 +76,22 @@ 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_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 - sv.str + token.size == 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;
+}
+
/* cstr interaction with csview: */
STC_INLINE cstr cstr_from_v(csview sv)
|
