aboutsummaryrefslogtreecommitdiff
path: root/xs_unicode.h
diff options
context:
space:
mode:
authorgrunfink <grunfink@comam.es>2026-04-05 10:16:26 +0200
committergrunfink <grunfink@comam.es>2026-04-05 10:16:26 +0200
commit8aca51dc19da377a82bbb4d93458bd4e525d291e (patch)
treee97af64e6a8fbea11ce269661a01e87d127eb184 /xs_unicode.h
parent083c0e7df7aa67e4fdf18e823cce99e88e04406d (diff)
New utf8 functions.
Diffstat (limited to 'xs_unicode.h')
-rw-r--r--xs_unicode.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/xs_unicode.h b/xs_unicode.h
index 493cfde..b10a921 100644
--- a/xs_unicode.h
+++ b/xs_unicode.h
@@ -23,10 +23,12 @@
int xs_unicode_is_alpha(unsigned int cpoint);
int xs_unicode_is_right_to_left(unsigned int cpoint);
int xs_is_emoji(unsigned int cpoint);
+ int xs_utf8_len(const char *str);
#ifdef _XS_H
xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset);
xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint);
+ xs_str *xs_utf8_crop_i(xs_str *str, int begin, int end);
xs_str *xs_utf8_to_upper(const char *str);
xs_str *xs_utf8_to_lower(const char *str);
xs_str *xs_utf8_to_nfd(const char *str);
@@ -196,6 +198,18 @@ unsigned int xs_surrogate_enc(unsigned int cpoint)
}
+int xs_utf8_len(const char *str)
+/* counts the number of characters (codepoints) in str */
+{
+ int len = 0;
+
+ while (xs_utf8_dec(&str))
+ len++;
+
+ return len;
+}
+
+
#ifdef _XS_H
xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset)
@@ -221,6 +235,30 @@ xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint)
return xs_utf8_insert(str, cpoint, &offset);
}
+
+xs_str *xs_utf8_crop_i(xs_str *str, int begin, int end)
+/* crops str from begin to end by characters (codepoints) */
+{
+ const char *p = str;
+ int sz = xs_utf8_len(str);
+ unsigned int cpoint;
+
+ if (end <= 0)
+ end = sz + end;
+
+ xs_str *ns = xs_str_new(NULL);
+
+ for (int n = 0; n < end && (cpoint = xs_utf8_dec(&p)); n++) {
+ if (n >= begin)
+ ns = xs_utf8_cat(ns, cpoint);
+ }
+
+ xs_free(str);
+
+ return ns;
+}
+
+
#endif /* _XS_H */