summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-24 11:48:07 +0200
committerTyge Løvset <[email protected]>2022-09-24 11:48:07 +0200
commit756120e349a892ef1b7655fc777f28ec13845300 (patch)
tree34d39e739f94ff52419ac431e1de26e1c431f2c0
parent26bc208248a0dd0f303d5b80bd298f5100b56e82 (diff)
downloadSTC-modified-756120e349a892ef1b7655fc777f28ec13845300.tar.gz
STC-modified-756120e349a892ef1b7655fc777f28ec13845300.zip
Fixes for csview and cstr sub-types (_value and _iter).
-rw-r--r--include/stc/ccommon.h8
-rw-r--r--include/stc/cstr.h10
-rw-r--r--include/stc/csview.h1
-rw-r--r--include/stc/forward.h24
4 files changed, 24 insertions, 19 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index f70bfcf4..ac55b3e9 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -187,14 +187,14 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
c_foreach_s(it, C, start) if (!(filter)) ; else
#define c_foreach_s(i, C, start) \
- for (struct {C##_iter it; const C##_value *ref; \
+ for (struct {C##_iter it; C##_value *ref; \
uint32_t index, taken[c_FLT_STACK+1], dropped[c_FLT_STACK]; \
int8_t dn, tn; bool dropwhile;} \
i = {.it=start, .ref=i.it.ref}; i.ref \
; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.dn=0, i.tn=0)
#define c_forwhile(i, C, cnt, cond) \
- for (struct {C##_iter it; const C##_value *ref; size_t index;} \
+ for (struct {C##_iter it; C##_value *ref; size_t index;} \
i = {.it=C##_begin(&cnt), .ref=i.it.ref}; i.ref && (cond) \
; C##_next(&i.it), i.ref = i.it.ref, ++i.index)
@@ -210,8 +210,8 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
#define c_forrange4(i, itype, start, stop) \
for (itype i=start, _end=stop; i < _end; ++i)
#define c_forrange5(i, itype, start, stop, step) \
- for (itype i=start, _inc=step, _end=(stop) - (0 < _inc) \
- ; (i <= _end) == (0 < _inc); i += _inc)
+ for (itype i=start, _inc=step, _end=(stop) - (_inc > 0) \
+ ; (_inc > 0) ^ (i > _end); i += _inc)
#define c_forlist(it, T, ...) \
for (struct {T* data; T* ref; size_t index;} \
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 54d55d4c..961ca344 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -227,22 +227,22 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, size_t u8idx) {
STC_INLINE cstr_iter cstr_begin(const cstr* self) {
csview sv = cstr_sv(self);
if (!sv.size) return c_make(cstr_iter){NULL};
- return c_make(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}, sv.str + sv.size}};
+ return c_make(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}}};
}
STC_INLINE cstr_iter cstr_end(const cstr* self) {
- csview sv = cstr_sv(self);
- return c_make(cstr_iter){.u8 = {{NULL}, sv.str + sv.size}};
+ return c_make(cstr_iter){NULL};
}
STC_INLINE void cstr_next(cstr_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;
+ if (!*it->ref) 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;
+ while (pos && *it.ref) pos += (*(it.ref += inc) & 0xC0) != 0x80;
it.u8.chr.size = utf8_chr_size(it.ref);
+ if (!*it.ref) it.ref = NULL;
return it;
}
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 2e90c69b..3bb8a542 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -91,6 +91,7 @@ STC_INLINE csview_iter csview_advance(csview_iter it, isize_t pos) {
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);
+ if (it.ref == it.u8.end) it.ref = NULL;
return it;
}
diff --git a/include/stc/forward.h b/include/stc/forward.h
index 167d1539..39628fa0 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -42,23 +42,27 @@
#define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL)
#define forward_cvec(CX, VAL) _c_cvec_types(CX, VAL)
-typedef struct { char* data; size_t size, cap; } cstr_buf;
+typedef const char csview_value;
+typedef struct { csview_value* str; size_t size; } csview;
+typedef union {
+ csview_value* ref;
+ struct { csview chr; csview_value* end; } u8;
+} csview_iter;
+
typedef char cstr_value;
+typedef struct { cstr_value* data; size_t size, cap; } cstr_buf;
#if defined STC_CSTR_V1
- typedef struct { char* str; } cstr;
+ typedef struct { cstr_value* str; } cstr;
#else
typedef union {
- struct { char data[sizeof(cstr_buf) - 1]; unsigned char last; } sml;
- struct { char* data; size_t size, ncap; } lon;
+ struct { cstr_value data[sizeof(cstr_buf) - 1]; unsigned char last; } sml;
+ struct { cstr_value* data; size_t size, ncap; } lon;
} cstr;
#endif
-
-typedef struct { const char* str; size_t size; } csview;
-typedef char csview_value;
typedef union {
- const char *ref;
- struct { csview chr; const char *end; } u8;
-} csview_iter, cstr_iter;
+ cstr_value* ref;
+ struct { csview chr; } u8;
+} cstr_iter;
#define c_true(...) __VA_ARGS__
#define c_false(...)