From 1bee46444b24c75a944251459a9ba57120db4d10 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 15 Apr 2024 18:35:22 +0200 Subject: Implement casefolding --- lib/unicode/string/u8casefold.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/unicode/string/u8casefold.c (limited to 'lib/unicode') diff --git a/lib/unicode/string/u8casefold.c b/lib/unicode/string/u8casefold.c new file mode 100644 index 0000000..6c0b61d --- /dev/null +++ b/lib/unicode/string/u8casefold.c @@ -0,0 +1,24 @@ +#include "mbstring.h" +#include "unicode/prop.h" +#include "unicode/string.h" + +size_t +u8casefold(char8_t *restrict dst, size_t dstn, const char8_t *src, size_t srcn, + enum caseflags flags) +{ + rune ch; + size_t n = 0; + + while (u8next(&ch, &src, &srcn)) { + struct rview rv = uprop_get_cf(ch, flags & CF_LANG_AZ); + for (size_t i = 0; i < rv.len; i++) { + if (n >= dstn) { + char8_t buf[U8_LEN_MAX]; + n += rtou8(buf, sizeof(buf), rv.p[i]); + } else + n += rtou8(dst + n, dstn - n, rv.p[i]); + } + } + + return n; +} -- cgit v1.2.3