summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-22 13:04:58 +0200
committerTyge Løvset <[email protected]>2022-09-22 13:04:58 +0200
commit4c5f0ba23ba6294e144243555ed3c02a72259692 (patch)
tree380b249e1c36b83b65507da5a2af4f843252a474
parent61c222312bb1cc03ab987fdbc541039723c9e243 (diff)
downloadSTC-modified-4c5f0ba23ba6294e144243555ed3c02a72259692.tar.gz
STC-modified-4c5f0ba23ba6294e144243555ed3c02a72259692.zip
Updated _advance() iter methods. Some have signed offsets.
utf8_peek()/utf8_peek_off() added/changed.
-rw-r--r--docs/cdeq_api.md1
-rw-r--r--docs/cmap_api.md1
-rw-r--r--docs/cstr_api.md1
-rw-r--r--docs/csview_api.md4
-rw-r--r--docs/cvec_api.md1
-rw-r--r--examples/forfilter.c (renamed from examples/forfiltered.c)4
-rw-r--r--include/stc/ccommon.h8
-rw-r--r--include/stc/cdeq.h2
-rw-r--r--include/stc/cstack.h2
-rw-r--r--include/stc/cstr.h7
-rw-r--r--include/stc/csview.h14
-rw-r--r--include/stc/cvec.h2
-rw-r--r--include/stc/utf8.h12
-rw-r--r--src/cregex.c4
-rw-r--r--src/utf8code.c7
15 files changed, 50 insertions, 20 deletions
diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md
index 6a47313d..bfe2447c 100644
--- a/docs/cdeq_api.md
+++ b/docs/cdeq_api.md
@@ -81,6 +81,7 @@ void cdeq_X_sort_range(cdeq_X_iter i1, cdeq_X_iter i2,
cdeq_X_iter cdeq_X_begin(const cdeq_X* self);
cdeq_X_iter cdeq_X_end(const cdeq_X* self);
void cdeq_X_next(cdeq_X_iter* it);
+cdeq_X_iter cdeq_X_advance(cdeq_X_iter it, isize_t n);
cdeq_X_raw cdeq_X_value_toraw(cdeq_X_value* pval);
cdeq_X_value cdeq_X_value_clone(cdeq_X_value val);
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 792d6c8c..a9676e68 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -83,6 +83,7 @@ void cmap_X_erase_entry(cmap_X* self, cmap_X_value* entry);
cmap_X_iter cmap_X_begin(const cmap_X* self);
cmap_X_iter cmap_X_end(const cmap_X* self);
void cmap_X_next(cmap_X_iter* it);
+cmap_X_iter cmap_X_advance(cmap_X_iter it, size_t n);
cmap_X_value cmap_X_value_clone(cmap_X_value val);
cmap_X_raw cmap_X_value_toraw(cmap_X_value* pval);
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index dbf6f462..53850e04 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -108,6 +108,7 @@ void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); // erase u
cstr_iter cstr_begin(const cstr* self);
cstr_iter cstr_end(const cstr* self);
void cstr_next(cstr_iter* it);
+cstr_iter cstr_advance(cstr_iter it, isize_t n);
// utf8 functions requires linking with src/utf8code.c symbols:
bool cstr_valid_utf8(const cstr* self); // check if str is valid utf8
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 5cc05c02..37ebf79a 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -55,6 +55,7 @@ bool csview_valid_utf8(csview sv); // requires
csview_iter csview_begin(const csview* self);
csview_iter csview_end(const csview* self);
void csview_next(csview_iter* it); // utf8 codepoint step, not byte!
+csview_iter csview_advance(csview_iter it, isize_t n);
// from utf8.h
size_t utf8_size(const char *s);
@@ -67,7 +68,8 @@ bool utf8_valid(const char* s);
bool utf8_valid_n(const char* s, size_t nbytes);
uint32_t utf8_decode(utf8_decode_t *d, uint8_t byte); // decode next byte to utf8, return state.
unsigned utf8_encode(char *out, uint32_t codepoint); // encode unicode cp into out buffer
-uint32_t utf8_peek(const char* s, int pos); // codepoint value at utf8 pos (may be negative)
+uint32_t utf8_peek(const char* s); // codepoint value of character at s
+uint32_t utf8_peek_off(const char* s, int offset); // codepoint value at utf8 pos (may be negative)
```
#### Extended cstr methods
diff --git a/docs/cvec_api.md b/docs/cvec_api.md
index 579f6eeb..588299fe 100644
--- a/docs/cvec_api.md
+++ b/docs/cvec_api.md
@@ -92,6 +92,7 @@ void cvec_X_sort_range(cvec_X_iter i1, cvec_X_iter i2,
cvec_X_iter cvec_X_begin(const cvec_X* self);
cvec_X_iter cvec_X_end(const cvec_X* self);
void cvec_X_next(cvec_X_iter* iter);
+cvec_X_iter cvec_X_advance(cvec_X_iter it, isize_t n);
cvec_X_raw cvec_X_value_toraw(cvec_X_value* pval);
cvec_X_value cvec_X_value_clone(cvec_X_value val);
diff --git a/examples/forfiltered.c b/examples/forfilter.c
index 2bea9a0a..e588e315 100644
--- a/examples/forfiltered.c
+++ b/examples/forfilter.c
@@ -89,7 +89,7 @@ void demo4(void)
{
csview s = c_sv("ab123cReAghNGnΩoEp");
cstr out = cstr_null;
- c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref, 0)))
+ c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref)))
cstr_push(&out, i.ref);
//cstr_append_sv(&out, i.it.u8.chr);
@@ -105,4 +105,4 @@ int main(void)
demo4();
}
-#include "../src/utf8code.c" \ No newline at end of file
+#include "../src/utf8code.c"
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));
diff --git a/src/cregex.c b/src/cregex.c
index 6b4e9f27..17284dfe 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -1028,8 +1028,8 @@ regexec1(const Reprog *progp, /* program to run */
case NWBOUND:
ok = true;
case WBOUND: /* fallthrough */
- if (ok ^ (s == bol || s == j->eol || ((utf8_isalnum(utf8_peek(s, -1)) || s[-1] == '_')
- ^ (utf8_isalnum(utf8_peek(s, 0)) || s[0] == '_'))))
+ if (ok ^ (s == bol || s == j->eol || ((utf8_isalnum(utf8_peek_off(s, -1)) || s[-1] == '_')
+ ^ (utf8_isalnum(utf8_peek(s)) || s[0] == '_'))))
continue;
break;
case NCCLASS:
diff --git a/src/utf8code.c b/src/utf8code.c
index 25244f07..abd21736 100644
--- a/src/utf8code.c
+++ b/src/utf8code.c
@@ -46,14 +46,11 @@ unsigned utf8_encode(char *out, uint32_t c)
return 0;
}
-uint32_t utf8_peek(const char* s, int pos) {
+uint32_t utf8_peek_off(const char* s, int pos) {
int inc = -1;
if (pos > 0) pos = -pos, inc = 1;
while (pos) pos += (*(s += inc) & 0xC0) != 0x80;
-
- utf8_decode_t d = {.state=0};
- do { utf8_decode(&d, (uint8_t)*s++); } while (d.state);
- return d.codep;
+ return utf8_peek(s);
}
bool utf8_valid_n(const char* s, size_t nbytes) {