summaryrefslogtreecommitdiffhomepage
path: root/include/stc/utf8.h
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-05-29 00:36:08 +0200
committerTyge Lovset <[email protected]>2022-05-29 00:42:06 +0200
commita341dbc0ce456198d5773c7260e93e8433228ee2 (patch)
tree7759f0cf00201b348bb90868e4553aaed0e3c98e /include/stc/utf8.h
parent06c8dffeb571a3aa6143425704062de4aa879d2c (diff)
downloadSTC-modified-a341dbc0ce456198d5773c7260e93e8433228ee2.tar.gz
STC-modified-a341dbc0ce456198d5773c7260e93e8433228ee2.zip
Renamed cstr_replace_first() => cstr_replace_one().
cstr.h now #include "utf8.h". Added iterator (utf8) to cstr and other utf8 functions postfixed by _u8(). Also renamed some utf8 functions in csview to better names.
Diffstat (limited to 'include/stc/utf8.h')
-rw-r--r--include/stc/utf8.h29
1 files changed, 16 insertions, 13 deletions
diff --git a/include/stc/utf8.h b/include/stc/utf8.h
index 02f24711..f11af046 100644
--- a/include/stc/utf8.h
+++ b/include/stc/utf8.h
@@ -13,9 +13,8 @@ int main()
cstr_replace_sv(&s1, utf8_substr(cstr_str(&s1), 7, 1), c_sv("🐨"));
printf("%s\n", cstr_str(&s1));
- csview sv = csview_from_s(s1);
- c_foreach (i, csview, sv)
- printf("%" c_PRIsv ",", c_ARGsv(i.codep));
+ c_foreach (i, cstr, s1)
+ printf("%" c_PRIsv ",", c_ARGsv(i.chr));
}
}
// Output:
@@ -54,19 +53,16 @@ STC_INLINE const char* utf8_at(const char *s, size_t index) {
return s;
}
-STC_INLINE size_t utf8_pos(const char* s, size_t index)
+STC_INLINE size_t utf8_pos(const char* s, size_t index)
{ return utf8_at(s, index) - s; }
-STC_INLINE uint32_t utf8_peek(const char *s, unsigned* codep_size) {
- utf8_decode_t d = {UTF8_OK};
- utf8_decode(&d, (uint8_t)*s++);
- switch (d.size) {
- case 4: utf8_decode(&d, (uint8_t)*s++);
- case 3: utf8_decode(&d, (uint8_t)*s++);
- case 2: utf8_decode(&d, (uint8_t)*s++);
+STC_INLINE void utf8_peek(const char *s, utf8_decode_t* d) {
+ utf8_decode(d, (uint8_t)*s++);
+ switch (d->size) {
+ case 4: utf8_decode(d, (uint8_t)*s++);
+ case 3: utf8_decode(d, (uint8_t)*s++);
+ case 2: utf8_decode(d, (uint8_t)*s++);
}
- *codep_size = d.size;
- return d.codep;
}
STC_INLINE unsigned utf8_codep_size(const char *s) {
@@ -82,6 +78,13 @@ STC_INLINE bool utf8_valid(const char* s) {
return d.state == UTF8_OK;
}
+STC_INLINE bool utf8_valid_n(const char* s, size_t n) {
+ utf8_decode_t d = {UTF8_OK};
+ while ((n-- != 0) & (*s != 0))
+ utf8_decode(&d, (uint8_t)*s++);
+ return d.state == UTF8_OK;
+}
+
// --------------------------- IMPLEMENTATION ---------------------------------
#ifdef i_implement
// https://news.ycombinator.com/item?id=15423674