diff options
| author | grunfink <grunfink@comam.es> | 2026-04-05 10:16:26 +0200 |
|---|---|---|
| committer | grunfink <grunfink@comam.es> | 2026-04-05 10:16:26 +0200 |
| commit | 8aca51dc19da377a82bbb4d93458bd4e525d291e (patch) | |
| tree | e97af64e6a8fbea11ce269661a01e87d127eb184 | |
| parent | 083c0e7df7aa67e4fdf18e823cce99e88e04406d (diff) | |
New utf8 functions.
| -rw-r--r-- | xs_unicode.h | 38 | ||||
| -rw-r--r-- | xs_version.h | 2 |
2 files changed, 39 insertions, 1 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 */ diff --git a/xs_version.h b/xs_version.h index 92a865e..2d349db 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 270f9376eabd4f8e0ed3ae22a1f8eb6e06ea8b8b 2026-01-10T20:39:12+01:00 */ +/* 8c7b288a0e0eac3a61f7c465633499b2fd44ca61 2026-04-05T10:14:27+02:00 */ |