diff options
| author | Tyge Løvset <[email protected]> | 2022-09-22 13:04:58 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-09-22 13:04:58 +0200 |
| commit | 4c5f0ba23ba6294e144243555ed3c02a72259692 (patch) | |
| tree | 380b249e1c36b83b65507da5a2af4f843252a474 /include | |
| parent | 61c222312bb1cc03ab987fdbc541039723c9e243 (diff) | |
| download | STC-modified-4c5f0ba23ba6294e144243555ed3c02a72259692.tar.gz STC-modified-4c5f0ba23ba6294e144243555ed3c02a72259692.zip | |
Updated _advance() iter methods. Some have signed offsets.
utf8_peek()/utf8_peek_off() added/changed.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/ccommon.h | 8 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 2 | ||||
| -rw-r--r-- | include/stc/cstack.h | 2 | ||||
| -rw-r--r-- | include/stc/cstr.h | 7 | ||||
| -rw-r--r-- | include/stc/csview.h | 14 | ||||
| -rw-r--r-- | include/stc/cvec.h | 2 | ||||
| -rw-r--r-- | include/stc/utf8.h | 12 |
7 files changed, 37 insertions, 10 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 69871728..29cb0761 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -30,6 +30,14 @@ #include <string.h> #include <assert.h> +#if SIZE_MAX == UINT32_MAX + typedef int32_t isize_t; +# define ISIZE_MAX INT32_MAX +#elif SIZE_MAX == UINT64_MAX + typedef int64_t isize_t; +# define ISIZE_MAX INT64_MAX +#endif + #if defined(_MSC_VER) # pragma warning(disable: 4116 4996) // unnamed type definition in parentheses # define STC_FORCE_INLINE static __forceinline diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index b4f02081..49590477 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -154,7 +154,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) +STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, isize_t n) { if ((it.ref += n) >= it.end) it.ref = NULL; return it; } #if !defined _i_no_emplace diff --git a/include/stc/cstack.h b/include/stc/cstack.h index a0c6e042..c0611465 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -180,7 +180,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) +STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, isize_t n) { if ((it.ref += n) >= it.end) it.ref = NULL ; return it; } #include "template.h" diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 1e7a351f..54d55d4c 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -238,6 +238,13 @@ STC_INLINE void cstr_next(cstr_iter* it) { it->u8.chr.size = utf8_chr_size(it->ref); if (it->ref == it->u8.end) it->ref = NULL; } +STC_INLINE cstr_iter cstr_advance(cstr_iter it, isize_t pos) { + int inc = -1; + if (pos > 0) pos = -pos, inc = 1; + while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80; + it.u8.chr.size = utf8_chr_size(it.ref); + return it; +} STC_INLINE void cstr_clear(cstr* self) diff --git a/include/stc/csview.h b/include/stc/csview.h index 54a4c4cc..2e90c69b 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -78,15 +78,21 @@ STC_INLINE csview_iter csview_begin(const csview* self) { return c_make(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}, self->str + self->size}}; } - -STC_INLINE csview_iter csview_end(const csview* self) - { return c_make(csview_iter){.u8 = {{NULL}, self->str + self->size}}; } - +STC_INLINE csview_iter csview_end(const csview* self) { + return c_make(csview_iter){.u8 = {{NULL}, self->str + self->size}}; +} STC_INLINE void csview_next(csview_iter* it) { it->ref += it->u8.chr.size; it->u8.chr.size = utf8_chr_size(it->ref); if (it->ref == it->u8.end) it->ref = NULL; } +STC_INLINE csview_iter csview_advance(csview_iter it, isize_t pos) { + int inc = -1; + if (pos > 0) pos = -pos, inc = 1; + while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80; + it.u8.chr.size = utf8_chr_size(it.ref); + return it; +} /* utf8 */ diff --git a/include/stc/cvec.h b/include/stc/cvec.h index ff26de1e..effcc1d1 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -207,7 +207,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) +STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, isize_t n) { if ((it.ref += n) >= it.end) it.ref = NULL; return it; } STC_INLINE size_t _cx_memb(_index)(const _cx_self* cx, _cx_iter it) diff --git a/include/stc/utf8.h b/include/stc/utf8.h index c20b80cb..7a4ba921 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -17,13 +17,13 @@ extern uint32_t utf8_toupper(uint32_t c); extern bool utf8_valid_n(const char* s, size_t nbytes); extern int utf8_icmp_sv(csview s1, csview s2); extern unsigned utf8_encode(char *out, uint32_t c); -extern uint32_t utf8_peek(const char *s, int u8pos); +extern uint32_t utf8_peek_off(const char *s, int offset); + +/* following functions uses src/utf8code.c */ STC_INLINE bool utf8_isupper(uint32_t c) { return utf8_tolower(c) != c; } STC_INLINE bool utf8_islower(uint32_t c) { return utf8_toupper(c) != c; } -/* following functions uses src/utf8code.c */ - /* decode next utf8 codepoint. https://bjoern.hoehrmann.de/utf-8/decoder/dfa */ typedef struct { uint32_t state, codep; } utf8_decode_t; @@ -35,6 +35,12 @@ STC_INLINE uint32_t utf8_decode(utf8_decode_t* d, const uint32_t byte) { return d->state = utf8_dtab[256 + d->state + type]; } +STC_INLINE uint32_t utf8_peek(const char* s) { + utf8_decode_t d = {.state=0}; + do { utf8_decode(&d, (uint8_t)*s++); } while (d.state); + return d.codep; +} + /* case-insensitive utf8 string comparison */ STC_INLINE int utf8_icmp(const char* s1, const char* s2) { return utf8_icmp_sv(c_sv(s1, ~(size_t)0), c_sv(s2, ~(size_t)0)); |
