From be0e64a9a19d3ca459284c61c497d141a78df1d7 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Thu, 17 Aug 2023 05:34:38 +0200 Subject: Renamed "internal" csview member .str => .buf, as it is not null terminated like crawstr .str member. --- docs/crawstr_api.md | 32 +------------ docs/csview_api.md | 20 ++++---- include/stc/ccommon.h | 2 +- include/stc/cregex.h | 2 +- include/stc/cstr.h | 60 ++++++++++++------------ include/stc/csview.h | 46 +++++++++--------- include/stc/forward.h | 2 +- misc/examples/coroutines/cotasks1.c | 12 ++--- misc/examples/coroutines/cotasks2.c | 12 ++--- misc/examples/regularexpressions/regex_match.c | 2 +- misc/examples/regularexpressions/regex_replace.c | 2 +- misc/tests/cregex_test.c | 4 +- src/cregex.c | 34 +++++++------- src/utf8code.c | 6 +-- 14 files changed, 104 insertions(+), 132 deletions(-) diff --git a/docs/crawstr_api.md b/docs/crawstr_api.md index 59087d06..97804e23 100644 --- a/docs/crawstr_api.md +++ b/docs/crawstr_api.md @@ -15,8 +15,6 @@ storage. It keeps the length of the string, i.e. no need to call *strlen()* for All crawstr definitions and prototypes are available by including a single header file. ```c -#define i_implement -#include #include ``` ## Methods @@ -29,7 +27,6 @@ intptr_t crawstr_size(crawstr rs); bool crawstr_empty(crawstr rs); // check if size == 0 void crawstr_clear(crawstr* self); csview crawstr_sv(crawstr rs); // convert to csview type -const char* crawstr_str(crawstr rs); // get null-terminated const char* bool crawstr_equals(crawstr rs, const char* str); intptr_t crawstr_find(crawstr rs, const char* str); @@ -72,8 +69,8 @@ uint32_t utf8_peek_off(const char* s, int offset); // codep | Type name | Type definition | Used to represent... | |:----------------|:-------------------------------------------|:-------------------------| -| `crawstr` | `struct { const char *str; intptr_t size; }` | The string view type | -| `crawstr_value` | `char` | The string element type | +| `crawstr` | `struct { const char *str; intptr_t size; }` | Raw string view type | +| `crawstr_value` | `char` | Raw string element type | | `crawstr_iter` | `struct { crawstr_value *ref; }` | UTF8 iterator | ## Example: UTF8 iteration and case conversion @@ -93,7 +90,6 @@ int main(void) cstr str = cstr_toupper_sv(crawstr_sv(rs)); printf("%s\n", cstr_str(&str)); - cstr_drop(&str); } ``` @@ -103,27 +99,3 @@ Liberté, égalité, fraternité. L i b e r t é , é g a l i t é , f r a t e r n i t é . LIBERTÉ, ÉGALITÉ, FRATERNITÉ. ``` - -### Example 2: UTF8 replace -```c -#define i_import // include dependent utf8 definitions. -#include - -int main(void) -{ - cstr s1 = cstr_lit("hell😀 w😀rld"); - - cstr_u8_replace_at(&s1, cstr_find(&s1, "😀rld"), 1, c_rs("ø")); - printf("%s\n", cstr_str(&s1)); - - c_foreach (i, cstr, s1) - printf("%.*s,", c_SV(i.u8.chr)); // u8.chr is a csview - - cstr_drop(&s1); -} -``` -Output: -``` -hell😀 wørld -h,e,l,l,😀, ,w,ø,r,l,d, -``` diff --git a/docs/csview_api.md b/docs/csview_api.md index eafc6854..5202d6f5 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -1,14 +1,14 @@ # STC [csview](../include/stc/csview.h): Sub-string View ![String](pics/string.jpg) -The type **csview** is a non-null terminated string view and can refer to a constant contiguous sequence of -char-elements with the first element of the sequence at position zero. The implementation holds two members: -a pointer to constant char and a size. +The type **csview** is a non-null terminated string view and can refer to a constant contiguous +sequence of char-elements with the first element of the sequence at position zero. The implementation +holds two members: a pointer to constant char and a size. -Because **csview** is non-null terminated, it is not an ideal replacent view for `const char*` - see [crawstr](crawstr_api.md) -for that. **csview** never allocates memory, and therefore need not be destructed. Its lifetime is limited by -the source string storage. It keeps the length of the string, and does not need to call *strlen()* to acquire -the length. +Because **csview** is non-null terminated, it cannot be a replacent view for `const char*` - +see [crawstr](crawstr_api.md) for that. **csview** never allocates memory, and therefore need not be +destructed. Its lifetime is limited by the source string storage. It keeps the length of the string, +and does not need to call *strlen()* to acquire the length. - **csview** iterators works on UTF8 codepoints - like **cstr** and **crawstr** (see Example 2). - Because it is null-terminated, it must be printed the following way: @@ -16,8 +16,8 @@ the length. printf("%.*s", c_SV(sstr)); ``` -See the c++ class [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) for a functional -description. +See the c++ class [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) +for a functional description. ## Header file @@ -95,7 +95,7 @@ uint64_t csview_hash(const csview* x); | Type name | Type definition | Used to represent... | |:----------------|:-------------------------------------------|:-------------------------| -| `csview` | `struct { const char *str; intptr_t size; }` | The string view type | +| `csview` | `struct { const char *buf; intptr_t size; }` | The string view type | | `csview_value` | `char` | The string element type | | `csview_iter` | `struct { csview_value *ref; }` | UTF8 iterator | diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index e33e657a..5c5e87e6 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -133,7 +133,7 @@ typedef const char* ccharptr; #define c_sv(...) c_MACRO_OVERLOAD(c_sv, __VA_ARGS__) #define c_sv_1(literal) c_sv_2(literal, c_litstrlen(literal)) #define c_sv_2(str, n) (c_LITERAL(csview){str, n}) -#define c_SV(sv) (int)(sv).size, (sv).str // printf("%.*s\n", c_SV(sv)); +#define c_SV(sv) (int)(sv).size, (sv).buf // printf("%.*s\n", c_SV(sv)); #define c_rs(literal) c_rs_2(literal, c_litstrlen(literal)) #define c_rs_2(str, n) (c_LITERAL(crawstr){str, n}) diff --git a/include/stc/cregex.h b/include/stc/cregex.h index bce94b04..c8ad6dbe 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -121,7 +121,7 @@ int cregex_find_4(const cregex* re, const char* input, csview match[], int mflag STC_INLINE int cregex_find_sv(const cregex* re, csview input, csview match[]) { csview *mp = NULL; if (match) { match[0] = input; mp = match; } - return cregex_find(re, input.str, mp, CREG_STARTEND); + return cregex_find(re, input.buf, mp, CREG_STARTEND); } /* match + compile RE pattern */ diff --git a/include/stc/cstr.h b/include/stc/cstr.h index bc147469..eeb65c39 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -98,7 +98,7 @@ STC_INLINE csview cstr_sv(const cstr* s) { : c_sv_2(s->sml.data, cstr_s_size(s)); } STC_INLINE crawstr cstr_rs(const cstr* s) - { csview sv = cstr_sv(s); return c_rs_2(sv.str, sv.size); } + { csview sv = cstr_sv(s); return c_rs_2(sv.buf, sv.size); } STC_INLINE cstr cstr_init(void) { return cstr_null; } @@ -113,7 +113,7 @@ STC_INLINE cstr cstr_from(const char* str) { return cstr_from_n(str, c_strlen(str)); } STC_INLINE cstr cstr_from_sv(csview sv) - { return cstr_from_n(sv.str, sv.size); } + { return cstr_from_n(sv.buf, sv.size); } STC_INLINE cstr cstr_from_rs(crawstr rs) { return cstr_from_n(rs.str, rs.size); } @@ -145,7 +145,7 @@ STC_INLINE cstr cstr_move(cstr* self) { STC_INLINE cstr cstr_clone(cstr s) { csview sv = cstr_sv(&s); - return cstr_from_n(sv.str, sv.size); + return cstr_from_n(sv.buf, sv.size); } STC_INLINE void cstr_drop(cstr* self) { @@ -201,8 +201,8 @@ STC_INLINE const char* cstr_u8_at(const cstr* self, intptr_t u8idx) STC_INLINE csview cstr_u8_chr(const cstr* self, intptr_t u8idx) { const char* str = cstr_str(self); csview sv; - sv.str = utf8_at(str, u8idx); - sv.size = utf8_chr_size(sv.str); + sv.buf = utf8_at(str, u8idx); + sv.size = utf8_chr_size(sv.buf); return sv; } @@ -211,7 +211,7 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, intptr_t u8idx) { STC_INLINE cstr_iter cstr_begin(const cstr* self) { csview sv = cstr_sv(self); if (!sv.size) return c_LITERAL(cstr_iter){.ref = NULL}; - return c_LITERAL(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}}}; + return c_LITERAL(cstr_iter){.u8 = {{sv.buf, utf8_chr_size(sv.buf)}}}; } STC_INLINE cstr_iter cstr_end(const cstr* self) { (void)self; return c_LITERAL(cstr_iter){NULL}; @@ -242,7 +242,7 @@ STC_INLINE int cstr_icmp(const cstr* s1, const cstr* s2) STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) { csview x = cstr_sv(s1), y = cstr_sv(s2); - return x.size == y.size && !c_memcmp(x.str, y.str, x.size); + return x.size == y.size && !c_memcmp(x.buf, y.buf, x.size); } @@ -250,7 +250,7 @@ STC_INLINE bool cstr_equals(const cstr* self, const char* str) { return !strcmp(cstr_str(self), str); } STC_INLINE bool cstr_equals_sv(const cstr* self, csview sv) - { return sv.size == cstr_size(self) && !c_memcmp(cstr_str(self), sv.str, sv.size); } + { return sv.size == cstr_size(self) && !c_memcmp(cstr_str(self), sv.buf, sv.size); } STC_INLINE bool cstr_equals_s(const cstr* self, cstr s) { return !cstr_cmp(self, &s); } @@ -280,7 +280,7 @@ STC_INLINE bool cstr_contains_s(const cstr* self, cstr search) STC_INLINE bool cstr_starts_with_sv(const cstr* self, csview sub) { if (sub.size > cstr_size(self)) return false; - return !c_memcmp(cstr_str(self), sub.str, sub.size); + return !c_memcmp(cstr_str(self), sub.buf, sub.size); } STC_INLINE bool cstr_starts_with(const cstr* self, const char* sub) { @@ -302,7 +302,7 @@ STC_INLINE bool cstr_istarts_with(const cstr* self, const char* sub) { STC_INLINE bool cstr_ends_with_sv(const cstr* self, csview sub) { csview sv = cstr_sv(self); if (sub.size > sv.size) return false; - return !c_memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); + return !c_memcmp(sv.buf + sv.size - sub.size, sub.buf, sub.size); } STC_INLINE bool cstr_ends_with_s(const cstr* self, cstr sub) @@ -314,7 +314,7 @@ STC_INLINE bool cstr_ends_with(const cstr* self, const char* sub) STC_INLINE bool cstr_iends_with(const cstr* self, const char* sub) { csview sv = cstr_sv(self); intptr_t n = c_strlen(sub); - return n <= sv.size && !utf8_icmp(sv.str + sv.size - n, sub); + return n <= sv.size && !utf8_icmp(sv.buf + sv.size - n, sub); } @@ -322,11 +322,11 @@ STC_INLINE char* cstr_assign(cstr* self, const char* str) { return cstr_assign_n(self, str, c_strlen(str)); } STC_INLINE char* cstr_assign_sv(cstr* self, csview sv) - { return cstr_assign_n(self, sv.str, sv.size); } + { return cstr_assign_n(self, sv.buf, sv.size); } STC_INLINE char* cstr_copy(cstr* self, cstr s) { csview sv = cstr_sv(&s); - return cstr_assign_n(self, sv.str, sv.size); + return cstr_assign_n(self, sv.buf, sv.size); } @@ -335,16 +335,16 @@ STC_INLINE char* cstr_push(cstr* self, const char* chr) STC_INLINE void cstr_pop(cstr* self) { csview sv = cstr_sv(self); - const char* s = sv.str + sv.size; + const char* s = sv.buf + sv.size; while ((*--s & 0xC0) == 0x80) ; - _cstr_set_size(self, (s - sv.str)); + _cstr_set_size(self, (s - sv.buf)); } STC_INLINE char* cstr_append(cstr* self, const char* str) { return cstr_append_n(self, str, c_strlen(str)); } STC_INLINE char* cstr_append_sv(cstr* self, csview sv) - { return cstr_append_n(self, sv.str, sv.size); } + { return cstr_append_n(self, sv.buf, sv.size); } STC_INLINE char* cstr_append_s(cstr* self, cstr s) { return cstr_append_sv(self, cstr_sv(&s)); } @@ -358,7 +358,7 @@ STC_INLINE void cstr_replace_4(cstr* self, const char* search, const char* repl, STC_INLINE void cstr_replace_at_sv(cstr* self, intptr_t pos, intptr_t len, const csview repl) { char* d = _cstr_internal_move(self, pos + len, pos + repl.size); - c_memcpy(d + pos, repl.str, repl.size); + c_memcpy(d + pos, repl.buf, repl.size); } STC_INLINE void cstr_replace_at(cstr* self, intptr_t pos, intptr_t len, const char* repl) @@ -400,12 +400,12 @@ fn_tocase[] = {{tolower, utf8_casefold}, static cstr cstr_tocase(csview sv, int k) { cstr out = cstr_init(); char *buf = cstr_reserve(&out, sv.size*3/2); - const char *end = sv.str + sv.size; + const char *end = sv.buf + sv.size; uint32_t cp; intptr_t sz = 0; utf8_decode_t d = {.state=0}; - while (sv.str < end) { - do { utf8_decode(&d, (uint8_t)*sv.str++); } while (d.state); + while (sv.buf < end) { + do { utf8_decode(&d, (uint8_t)*sv.buf++); } while (d.state); if (d.codep < 128) buf[sz++] = (char)fn_tocase[k].conv_asc((int)d.codep); else { @@ -450,13 +450,13 @@ bool cstr_valid_utf8(const cstr* self) STC_DEF uint64_t cstr_hash(const cstr *self) { csview sv = cstr_sv(self); - return cfasthash(sv.str, sv.size); + return cfasthash(sv.buf, sv.size); } STC_DEF intptr_t cstr_find_sv(const cstr* self, csview search) { csview sv = cstr_sv(self); - char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); - return res ? (res - sv.str) : c_NPOS; + char* res = cstrnstrn(sv.buf, search.buf, sv.size, search.size); + return res ? (res - sv.buf) : c_NPOS; } STC_DEF char* _cstr_internal_move(cstr* self, const intptr_t pos1, const intptr_t pos2) { @@ -532,8 +532,8 @@ STC_DEF char* cstr_resize(cstr* self, const intptr_t size, const char value) { STC_DEF intptr_t cstr_find_at(const cstr* self, const intptr_t pos, const char* search) { csview sv = cstr_sv(self); if (pos > sv.size) return c_NPOS; - const char* res = strstr((char*)sv.str + pos, search); - return res ? (res - sv.str) : c_NPOS; + const char* res = strstr((char*)sv.buf + pos, search); + return res ? (res - sv.buf) : c_NPOS; } STC_DEF char* cstr_assign_n(cstr* self, const char* str, const intptr_t len) { @@ -588,13 +588,13 @@ STC_DEF cstr cstr_replace_sv(csview in, csview search, csview repl, int32_t coun intptr_t from = 0; char* res; if (!count) count = INT32_MAX; if (search.size) - while (count-- && (res = cstrnstrn(in.str + from, search.str, in.size - from, search.size))) { - const intptr_t pos = (res - in.str); - cstr_append_n(&out, in.str + from, pos - from); - cstr_append_n(&out, repl.str, repl.size); + while (count-- && (res = cstrnstrn(in.buf + from, search.buf, in.size - from, search.size))) { + const intptr_t pos = (res - in.buf); + cstr_append_n(&out, in.buf + from, pos - from); + cstr_append_n(&out, repl.buf, repl.size); from = pos + search.size; } - cstr_append_n(&out, in.str + from, in.size - from); + cstr_append_n(&out, in.buf + from, in.size - from); return out; } diff --git a/include/stc/csview.h b/include/stc/csview.h index 2a051ddd..5aba6926 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -46,7 +46,7 @@ STC_INLINE intptr_t csview_size(csview sv) { return sv.size; } STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } STC_INLINE bool csview_equals_sv(csview sv1, csview sv2) - { return sv1.size == sv2.size && !c_memcmp(sv1.str, sv2.str, sv1.size); } + { return sv1.size == sv2.size && !c_memcmp(sv1.buf, sv2.buf, sv1.size); } STC_INLINE bool csview_equals(csview sv, const char* str) { return csview_equals_sv(sv, c_sv_2(str, c_strlen(str))); } @@ -59,34 +59,34 @@ STC_INLINE bool csview_contains(csview sv, const char* str) STC_INLINE bool csview_starts_with(csview sv, const char* str) { intptr_t n = c_strlen(str); - return n > sv.size ? false : !c_memcmp(sv.str, str, n); + return n > sv.size ? false : !c_memcmp(sv.buf, str, n); } STC_INLINE bool csview_ends_with(csview sv, const char* str) { intptr_t n = c_strlen(str); - return n > sv.size ? false : !c_memcmp(sv.str + sv.size - n, str, n); + return n > sv.size ? false : !c_memcmp(sv.buf + sv.size - n, str, n); } STC_INLINE csview csview_substr(csview sv, intptr_t pos, intptr_t n) { if (pos + n > sv.size) n = sv.size - pos; - sv.str += pos, sv.size = n; + sv.buf += pos, sv.size = n; return sv; } STC_INLINE csview csview_slice(csview sv, intptr_t p1, intptr_t p2) { if (p2 > sv.size) p2 = sv.size; - sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; + sv.buf += p1, sv.size = p2 > p1 ? p2 - p1 : 0; return sv; } /* utf8 iterator */ STC_INLINE csview_iter csview_begin(const csview* self) { if (!self->size) return c_LITERAL(csview_iter){NULL}; - return c_LITERAL(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}, - self->str + self->size}}; + return c_LITERAL(csview_iter){.u8 = {{self->buf, utf8_chr_size(self->buf)}, + self->buf + self->size}}; } STC_INLINE csview_iter csview_end(const csview* self) { - return c_LITERAL(csview_iter){.u8 = {{NULL}, self->str + self->size}}; + return c_LITERAL(csview_iter){.u8 = {{NULL}, self->buf + self->size}}; } STC_INLINE void csview_next(csview_iter* it) { it->ref += it->u8.chr.size; @@ -96,21 +96,21 @@ STC_INLINE void csview_next(csview_iter* it) { /* utf8 */ STC_INLINE intptr_t csview_u8_size(csview sv) - { return utf8_size_n(sv.str, sv.size); } + { return utf8_size_n(sv.buf, sv.size); } STC_INLINE csview csview_u8_substr(csview sv, intptr_t bytepos, intptr_t u8len) { - sv.str += bytepos; - sv.size = utf8_pos(sv.str, u8len); + sv.buf += bytepos; + sv.size = utf8_pos(sv.buf, u8len); return sv; } STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c - { return utf8_valid_n(sv.str, sv.size); } + { return utf8_valid_n(sv.buf, sv.size); } #define c_fortoken_sv(it, inputsv, sep) \ for (struct { csview _inp, token, *ref; const char *_sep; intptr_t pos; } \ it = {._inp=inputsv, .token=it._inp, .ref=&it.token, ._sep=sep} \ - ; it.pos <= it._inp.size && (it.token = csview_token(it._inp, it._sep, &it.pos)).str ; ) + ; it.pos <= it._inp.size && (it.token = csview_token(it._inp, it._sep, &it.pos)).buf ; ) #define c_fortoken(it, input, sep) \ c_fortoken_sv(it, csview_from(input), sep) @@ -119,7 +119,7 @@ STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c STC_INLINE int csview_cmp(const csview* x, const csview* y) { intptr_t n = x->size < y->size ? x->size : y->size; - int c = c_memcmp(x->str, y->str, n); + int c = c_memcmp(x->buf, y->buf, n); return c ? c : (int)(x->size - y->size); } @@ -127,7 +127,7 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) { return utf8_icmp_sv(*x, *y); } STC_INLINE bool csview_eq(const csview* x, const csview* y) - { return x->size == y->size && !c_memcmp(x->str, y->str, x->size); } + { return x->size == y->size && !c_memcmp(x->buf, y->buf, x->size); } #endif // CSVIEW_H_INCLUDED @@ -165,12 +165,12 @@ STC_DEF csview_iter csview_advance(csview_iter it, intptr_t pos) { } STC_DEF intptr_t csview_find_sv(csview sv, csview search) { - char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); - return res ? (res - sv.str) : c_NPOS; + char* res = cstrnstrn(sv.buf, search.buf, sv.size, search.size); + return res ? (res - sv.buf) : c_NPOS; } STC_DEF uint64_t csview_hash(const csview *self) - { return cfasthash(self->str, self->size); } + { return cfasthash(self->buf, self->size); } STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n) { if (pos < 0) { @@ -179,7 +179,7 @@ STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n) { } if (pos > sv.size) pos = sv.size; if (pos + n > sv.size) n = sv.size - pos; - sv.str += pos, sv.size = n; + sv.buf += pos, sv.size = n; return sv; } @@ -190,15 +190,15 @@ STC_DEF csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) { } if (p2 < 0) p2 += sv.size; if (p2 > sv.size) p2 = sv.size; - sv.str += p1, sv.size = (p2 > p1 ? p2 - p1 : 0); + sv.buf += p1, sv.size = (p2 > p1 ? p2 - p1 : 0); return sv; } STC_DEF csview csview_token(csview sv, const char* sep, intptr_t* start) { intptr_t sep_size = c_strlen(sep); - csview slice = {sv.str + *start, sv.size - *start}; - const char* res = cstrnstrn(slice.str, sep, slice.size, sep_size); - csview tok = {slice.str, res ? (res - slice.str) : slice.size}; + csview slice = {sv.buf + *start, sv.size - *start}; + const char* res = cstrnstrn(slice.buf, sep, slice.size, sep_size); + csview tok = {slice.buf, res ? (res - slice.buf) : slice.size}; *start += tok.size + sep_size; return tok; } diff --git a/include/stc/forward.h b/include/stc/forward.h index 839be012..9f7a0063 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -42,7 +42,7 @@ // csview : non-null terminated string view typedef const char csview_value; typedef struct csview { - csview_value* str; + csview_value* buf; intptr_t size; } csview; diff --git a/misc/examples/coroutines/cotasks1.c b/misc/examples/coroutines/cotasks1.c index db9632e6..cffd6620 100644 --- a/misc/examples/coroutines/cotasks1.c +++ b/misc/examples/coroutines/cotasks1.c @@ -36,24 +36,24 @@ void print_time() struct produce_items { struct next_value next; - cstr str; + cstr text; int cco_state; }; int produce_items(struct produce_items* p) { cco_routine (p) { - p->str = cstr_init(); + p->text = cstr_init(); while (true) { cco_await(next_value(&p->next) != CCO_AWAIT); - cstr_printf(&p->str, "item %d", p->next.val); + cstr_printf(&p->text, "item %d", p->next.val); print_time(); - printf("produced %s\n", cstr_str(&p->str)); + printf("produced %s\n", cstr_str(&p->text)); cco_yield(); } cco_final: - cstr_drop(&p->str); + cstr_drop(&p->text); puts("done produce"); } return 0; @@ -74,7 +74,7 @@ int consume_items(struct consume_items* c, struct produce_items* p) printf("consume #%d\n", c->i); cco_await(produce_items(p) != CCO_AWAIT); print_time(); - printf("consumed %s\n", cstr_str(&p->str)); + printf("consumed %s\n", cstr_str(&p->text)); } cco_final: puts("done consume"); diff --git a/misc/examples/coroutines/cotasks2.c b/misc/examples/coroutines/cotasks2.c index 12c2c4a3..558df118 100644 --- a/misc/examples/coroutines/cotasks2.c +++ b/misc/examples/coroutines/cotasks2.c @@ -35,26 +35,26 @@ void print_time() cco_task_struct (produce_items, struct next_value next; - cstr str; + cstr text; ); int produce_items(struct produce_items* p, cco_runtime* rt) { cco_routine (p) { - p->str = cstr_init(); + p->text = cstr_init(); p->next.cco_func = next_value; while (true) { // await for next CCO_YIELD (or CCO_DONE) in next_value cco_await_task(&p->next, rt, CCO_YIELD); - cstr_printf(&p->str, "item %d", p->next.val); + cstr_printf(&p->text, "item %d", p->next.val); print_time(); - printf("produced %s\n", cstr_str(&p->str)); + printf("produced %s\n", cstr_str(&p->text)); cco_yield(); } cco_final: - cstr_drop(&p->str); + cstr_drop(&p->text); puts("done produce"); } return 0; @@ -77,7 +77,7 @@ int consume_items(struct consume_items* c, cco_runtime* rt) printf("consume #%d\n", c->i); cco_await_task(&c->produce, rt, CCO_YIELD); print_time(); - printf("consumed %s\n", cstr_str(&c->produce.str)); + printf("consumed %s\n", cstr_str(&c->produce.text)); } cco_final: diff --git a/misc/examples/regularexpressions/regex_match.c b/misc/examples/regularexpressions/regex_match.c index 11426d2d..9106ffbd 100644 --- a/misc/examples/regularexpressions/regex_match.c +++ b/misc/examples/regularexpressions/regex_match.c @@ -22,7 +22,7 @@ int main(void) // extract and convert all numbers in str to floats c_formatch (i, &re, str) - cstack_float_push(&vec, (float)atof(i.match[0].str)); + cstack_float_push(&vec, (float)atof(i.match[0].buf)); c_foreach (i, cstack_float, vec) printf(" %g\n", (double)*i.ref); diff --git a/misc/examples/regularexpressions/regex_replace.c b/misc/examples/regularexpressions/regex_replace.c index f1ea2711..087387d7 100644 --- a/misc/examples/regularexpressions/regex_replace.c +++ b/misc/examples/regularexpressions/regex_replace.c @@ -5,7 +5,7 @@ bool add_10_years(int i, csview match, cstr* out) { if (i == 1) { // group 1 matches year int year; - sscanf(match.str, "%4d", &year); // scan 4 chars only + sscanf(match.buf, "%4d", &year); // scan 4 chars only cstr_printf(out, "%04d", year + 10); return true; } diff --git a/misc/tests/cregex_test.c b/misc/tests/cregex_test.c index 4e192de6..a83b7593 100644 --- a/misc/tests/cregex_test.c +++ b/misc/tests/cregex_test.c @@ -4,7 +4,7 @@ #include #include "ctest.h" -#define M_START(m) ((m).str - inp) +#define M_START(m) ((m).buf - inp) #define M_END(m) (M_START(m) + (m).size) @@ -238,7 +238,7 @@ CTEST(cregex, captures_cap) static bool add_10_years(int i, csview match, cstr* out) { if (i == 1) { // group 1 matches year int year; - sscanf(match.str, "%4d", &year); // scan 4 chars only + sscanf(match.buf, "%4d", &year); // scan 4 chars only cstr_printf(out, "%04d", year + 10); return true; } diff --git a/src/cregex.c b/src/cregex.c index 551cb6f6..7907ddd9 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -264,8 +264,8 @@ _renewmatch(_Resub *mp, int ms, _Resublist *sp, int nsubids) { if (mp==NULL || ms==0) return; - if (mp[0].str == NULL || sp->m[0].str < mp[0].str || - (sp->m[0].str == mp[0].str && sp->m[0].size > mp[0].size)) { + if (mp[0].buf == NULL || sp->m[0].buf < mp[0].buf || + (sp->m[0].buf == mp[0].buf && sp->m[0].size > mp[0].size)) { for (int i=0; im[i]; } @@ -286,7 +286,7 @@ _renewthread(_Relist *lp, /* _relist to add to */ for (p=lp; p->inst; p++) { if (p->inst == ip) { - if (sep->m[0].str < p->se.m[0].str) { + if (sep->m[0].buf < p->se.m[0].buf) { if (ms > 1) p->se = *sep; else @@ -318,10 +318,10 @@ _renewemptythread(_Relist *lp, /* _relist to add to */ for (p=lp; p->inst; p++) { if (p->inst == ip) { - if (sp < p->se.m[0].str) { + if (sp < p->se.m[0].buf) { if (ms > 1) memset(&p->se, 0, sizeof(p->se)); - p->se.m[0].str = sp; + p->se.m[0].buf = sp; } return 0; } @@ -329,7 +329,7 @@ _renewemptythread(_Relist *lp, /* _relist to add to */ p->inst = ip; if (ms > 1) memset(&p->se, 0, sizeof(p->se)); - p->se.m[0].str = sp; + p->se.m[0].buf = sp; (++p)->inst = NULL; return p; } @@ -1005,7 +1005,7 @@ _regexec1(const _Reprog *progp, /* program to run */ checkstart = j->starttype; if (mp) for (i=0; irelist[0][0].inst = NULL; @@ -1066,10 +1066,10 @@ _regexec1(const _Reprog *progp, /* program to run */ icase = inst->type == TOK_ICASE; continue; case TOK_LBRA: - tlp->se.m[inst->r.subid].str = s; + tlp->se.m[inst->r.subid].buf = s; continue; case TOK_RBRA: - tlp->se.m[inst->r.subid].size = (s - tlp->se.m[inst->r.subid].str); + tlp->se.m[inst->r.subid].size = (s - tlp->se.m[inst->r.subid].buf); continue; case TOK_ANY: ok = (r != '\n'); @@ -1118,8 +1118,8 @@ _regexec1(const _Reprog *progp, /* program to run */ case TOK_END: /* Match! */ match = !(mflags & CREG_FULLMATCH) || ((s == j->eol || r == 0 || r == '\n') && - (tlp->se.m[0].str == bol || tlp->se.m[0].str[-1] == '\n')); - tlp->se.m[0].size = (s - tlp->se.m[0].str); + (tlp->se.m[0].buf == bol || tlp->se.m[0].buf[-1] == '\n')); + tlp->se.m[0].size = (s - tlp->se.m[0].buf); if (mp != NULL) _renewmatch(mp, ms, &tlp->se, progp->nsubids); break; @@ -1185,9 +1185,9 @@ _regexec(const _Reprog *progp, /* program to run */ if (mp && mp[0].size) { if (mflags & CREG_STARTEND) - j.starts = mp[0].str, j.eol = mp[0].str + mp[0].size; + j.starts = mp[0].buf, j.eol = mp[0].buf + mp[0].size; else if (mflags & CREG_NEXT) - j.starts = mp[0].str + mp[0].size; + j.starts = mp[0].buf + mp[0].size; } j.starttype = 0; @@ -1237,7 +1237,7 @@ _build_subst(const char* replace, int nmatch, const csview match[], if (len + m.size > cap) dst = cstr_reserve(subst, cap += cap/2 + m.size); for (int i = 0; i < m.size; ++i) - dst[len++] = m.str[i]; + dst[len++] = m.buf[i]; } ++replace; case '\0': @@ -1302,10 +1302,10 @@ cregex_replace_sv_6(const cregex* re, csview input, const char* replace, int cou while (count-- && cregex_find_sv(re, input, match) == CREG_OK) { _build_subst(replace, nmatch, match, mfun, &subst); - const intptr_t mpos = (match[0].str - input.str); - if (copy & (mpos > 0)) cstr_append_n(&out, input.str, mpos); + const intptr_t mpos = (match[0].buf - input.buf); + if (copy & (mpos > 0)) cstr_append_n(&out, input.buf, mpos); cstr_append_s(&out, subst); - input.str = match[0].str + match[0].size; + input.buf = match[0].buf + match[0].size; input.size -= mpos + match[0].size; } if (copy) cstr_append_sv(&out, input); diff --git a/src/utf8code.c b/src/utf8code.c index 4abf10ea..e326e6b9 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -105,10 +105,10 @@ int utf8_icmp_sv(const csview s1, const csview s2) { utf8_decode_t d1 = {.state=0}, d2 = {.state=0}; intptr_t j1 = 0, j2 = 0; while ((j1 < s1.size) & (j2 < s2.size)) { - do { utf8_decode(&d1, (uint8_t)s1.str[j1++]); } while (d1.state); - do { utf8_decode(&d2, (uint8_t)s2.str[j2++]); } while (d2.state); + do { utf8_decode(&d1, (uint8_t)s1.buf[j1++]); } while (d1.state); + do { utf8_decode(&d2, (uint8_t)s2.buf[j2++]); } while (d2.state); int32_t c = (int32_t)utf8_casefold(d1.codep) - (int32_t)utf8_casefold(d2.codep); - if (c || !s2.str[j2 - 1]) // OK if s1.size and s2.size are npos + if (c || !s2.buf[j2 - 1]) // OK if s1.size and s2.size are npos return (int)c; } return (int)(s1.size - s2.size); -- cgit v1.2.3