commit 8aca51d
grunfink
·
2026-04-05 08:16:26 +0000 UTC
parent 083c0e7
New utf8 functions.
2 files changed,
+39,
-1
+38,
-0
1@@ -23,10 +23,12 @@
2 int xs_unicode_is_alpha(unsigned int cpoint);
3 int xs_unicode_is_right_to_left(unsigned int cpoint);
4 int xs_is_emoji(unsigned int cpoint);
5+ int xs_utf8_len(const char *str);
6
7 #ifdef _XS_H
8 xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset);
9 xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint);
10+ xs_str *xs_utf8_crop_i(xs_str *str, int begin, int end);
11 xs_str *xs_utf8_to_upper(const char *str);
12 xs_str *xs_utf8_to_lower(const char *str);
13 xs_str *xs_utf8_to_nfd(const char *str);
14@@ -196,6 +198,18 @@ unsigned int xs_surrogate_enc(unsigned int cpoint)
15 }
16
17
18+int xs_utf8_len(const char *str)
19+/* counts the number of characters (codepoints) in str */
20+{
21+ int len = 0;
22+
23+ while (xs_utf8_dec(&str))
24+ len++;
25+
26+ return len;
27+}
28+
29+
30 #ifdef _XS_H
31
32 xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset)
33@@ -221,6 +235,30 @@ xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint)
34 return xs_utf8_insert(str, cpoint, &offset);
35 }
36
37+
38+xs_str *xs_utf8_crop_i(xs_str *str, int begin, int end)
39+/* crops str from begin to end by characters (codepoints) */
40+{
41+ const char *p = str;
42+ int sz = xs_utf8_len(str);
43+ unsigned int cpoint;
44+
45+ if (end <= 0)
46+ end = sz + end;
47+
48+ xs_str *ns = xs_str_new(NULL);
49+
50+ for (int n = 0; n < end && (cpoint = xs_utf8_dec(&p)); n++) {
51+ if (n >= begin)
52+ ns = xs_utf8_cat(ns, cpoint);
53+ }
54+
55+ xs_free(str);
56+
57+ return ns;
58+}
59+
60+
61 #endif /* _XS_H */
62
63
+1,
-1
1@@ -1 +1 @@
2-/* 270f9376eabd4f8e0ed3ae22a1f8eb6e06ea8b8b 2026-01-10T20:39:12+01:00 */
3+/* 8c7b288a0e0eac3a61f7c465633499b2fd44ca61 2026-04-05T10:14:27+02:00 */