diff options
| author | Tyge Lovset <[email protected]> | 2022-08-17 22:08:15 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-08-18 09:05:24 +0200 |
| commit | c5144785aaac11a30439064decabef62968b00a4 (patch) | |
| tree | e0f17c82ac1c529261d4a414158bae17c380060f | |
| parent | a06463c2f0747bc142a9d5b2bf455c64aaf39890 (diff) | |
| download | STC-modified-c5144785aaac11a30439064decabef62968b00a4.tar.gz STC-modified-c5144785aaac11a30439064decabef62968b00a4.zip | |
Some API updates cstr, csview with utf8. Added front()/back() to cstack.
| -rw-r--r-- | docs/cstr_api.md | 2 | ||||
| -rw-r--r-- | docs/csview_api.md | 6 | ||||
| -rw-r--r-- | examples/new_pque.c | 2 | ||||
| -rw-r--r-- | include/stc/cpque.h | 2 | ||||
| -rw-r--r-- | include/stc/cstack.h | 6 | ||||
| -rw-r--r-- | include/stc/cstr.h | 4 | ||||
| -rw-r--r-- | include/stc/csview.h | 14 |
7 files changed, 17 insertions, 19 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md index d4e292cd..8c28a601 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -99,7 +99,7 @@ size_t cstr_u8_size_n(const cstr self, size_t nbytes); // utf8 si size_t cstr_u8_to_pos(const cstr* self, size_t u8idx); // byte pos offset at utf8 codepoint index const char* cstr_u8_at(const cstr* self, size_t u8idx); // char* position at utf8 codepoint index csview cstr_u8_chr(const cstr* self, size_t u8idx); // get utf8 character as a csview -void cstr_u8_replace(cstr* self, size_t pos, size_t u8len, csview repl); // replace u8len utf8 chars +void cstr_u8_replace(cstr* self, size_t bytepos, size_t u8len, csview repl); // replace u8len utf8 chars // iterate utf8 codepoints cstr_iter cstr_begin(const cstr* self); diff --git a/docs/csview_api.md b/docs/csview_api.md index 128a1c9d..65e9a066 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -49,8 +49,7 @@ csview csview_token(csview sv, csview sep, size_t* start); // *start #### UTF8 methods ```c size_t csview_u8_size(csview sv); -csview csview_u8_substr(csview sv, size_t u8pos, size_t u8len); -csview csview_u8_slice(csview sv, size_t u8p1, size_t u8p2); +csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len); bool csview_valid_utf8(csview sv); // requires linking with src/utf8code.c csview_iter csview_begin(const csview* self); @@ -75,11 +74,10 @@ uint32_t utf8_peek(const char* s, int pos); // codepoint ```c csview cstr_substr(const cstr* self, size_t pos, size_t n); csview cstr_substr_ex(const cstr* s, intptr_t pos, size_t n); // negative pos count from end -csview cstr_u8_substr(const cstr* self, size_t u8pos, size_t u8len); +csview cstr_u8_substr(const cstr* self, size_t bytepos, size_t u8len); csview cstr_slice(const cstr* self, size_t p1, size_t p2); csview cstr_slice_ex(const cstr* s, intptr_t p, intptr_t q); // negative p or q count from end -csview cstr_u8_slice(const cstr* self, size_t u8p1, size_t u8p2); ``` #### Helper methods diff --git a/examples/new_pque.c b/examples/new_pque.c index b6226582..2bb1d729 100644 --- a/examples/new_pque.c +++ b/examples/new_pque.c @@ -37,7 +37,7 @@ int main() cpque_pnt_push(&pque, (Point){12, 62}); // print while (!cpque_pnt_empty(&pque)) { - cpque_pnt_value *v = cpque_pnt_top(&pque); + const cpque_pnt_value *v = cpque_pnt_top(&pque); printf(" (%d,%d)", v->x, v->y); cpque_pnt_pop(&pque); } diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 79d12b95..bfc09106 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -82,7 +82,7 @@ STC_INLINE bool _cx_memb(_empty)(const _cx_self* q) STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* q) { return q->capacity; } -STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) +STC_INLINE const _cx_value* _cx_memb(_top)(const _cx_self* self) { return &self->data[0]; } STC_INLINE void _cx_memb(_pop)(_cx_self* self) diff --git a/include/stc/cstack.h b/include/stc/cstack.h index e1839d37..9ed2beda 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -120,6 +120,12 @@ STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) STC_INLINE const _cx_value* _cx_memb(_top)(const _cx_self* self) { return &self->data[self->size - 1]; } +STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) + { return (_cx_value*) &self->data[self->size - 1]; } + +STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) + { return (_cx_value*) &self->data[0]; } + STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { if (self->size == _cx_memb(_capacity)(self)) if (!_cx_memb(_reserve)(self, self->size*3/2 + 4)) diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 99b60e49..b731289b 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -370,8 +370,8 @@ STC_INLINE void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* STC_INLINE void cstr_replace_at_s(cstr* self, size_t pos, size_t len, cstr repl) { cstr_replace_at_sv(self, pos, len, cstr_sv(&repl)); } -STC_INLINE void cstr_u8_replace(cstr* self, size_t pos, size_t u8len, csview repl) - { cstr_replace_at_sv(self, pos, utf8_pos(cstr_str(self) + pos, u8len), repl); } +STC_INLINE void cstr_u8_replace(cstr* self, size_t bytepos, size_t u8len, csview repl) + { cstr_replace_at_sv(self, bytepos, utf8_pos(cstr_str(self) + bytepos, u8len), repl); } STC_INLINE void cstr_insert(cstr* self, size_t pos, const char* str) diff --git a/include/stc/csview.h b/include/stc/csview.h index dbff8620..555dd538 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -90,15 +90,12 @@ STC_INLINE void csview_next(csview_iter* it) { STC_INLINE size_t csview_u8_size(csview sv) { return utf8_size_n(sv.str, sv.size); } -STC_INLINE csview csview_u8_substr(csview sv, size_t u8pos, size_t u8len) { - sv.str = utf8_at(sv.str, u8pos); +STC_INLINE csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len) { + sv.str += bytepos; sv.size = utf8_pos(sv.str, u8len); return sv; } -STC_INLINE csview csview_u8_slice(csview sv, size_t u8p1, size_t u8p2) - { return csview_u8_substr(sv, u8p1, u8p2 - u8p1); } - STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } @@ -121,11 +118,8 @@ STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, size_t n) STC_INLINE csview cstr_slice_ex(const cstr* self, intptr_t p1, intptr_t p2) { return csview_slice_ex(cstr_sv(self), p1, p2); } -STC_INLINE csview cstr_u8_substr(const cstr* self , size_t u8pos, size_t u8len) - { return csview_u8_substr(cstr_sv(self), u8pos, u8len); } - -STC_INLINE csview cstr_u8_slice(const cstr* self , size_t u8p1, size_t u8p2) - { return csview_u8_substr(cstr_sv(self), u8p1, u8p2 - u8p1); } +STC_INLINE csview cstr_u8_substr(const cstr* self , size_t bytepos, size_t u8len) + { return csview_u8_substr(cstr_sv(self), bytepos, u8len); } #endif /* ---- Container helper functions ---- */ |
