From ba68bbcf13982ebdb1131a5cc2b8c8ffc36148a0 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Wed, 10 Jul 2024 11:32:18 +0200 Subject: [PATCH] docs: add examples for common usecases --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 2923e8c..7b5d9c6 100644 --- a/README.md +++ b/README.md @@ -86,3 +86,37 @@ the [utf8proc issues page on Github](https://github.com/JuliaLang/utf8proc/issue ## See also An independent Lua translation of this library, [lua-mojibake](https://github.com/differentprogramming/lua-mojibake), is also available. + +## Examples + +### Convert codepoint to string +```c +// Convert codepoint `a` to utf8 string `str` +utf8proc_int32_t a = 223; +utf8proc_uint8_t str[16] = { 0 }; +utf8proc_encode_char(a, str); +printf("%s\n", str); +// ß +``` + +### Convert string to codepoint +```c +// Convert string `str` to pointer to codepoint `a` +utf8proc_uint8_t str[] = "ß"; +utf8proc_int32_t a; +utf8proc_iterate(str, -1, &a); +printf("%d\n", a); +// 223 +``` + +### Casefold + +```c +// Convert `ß` to its casefold variant `ss` +utf8proc_uint8_t str[] = "ß"; +utf8proc_uint8_t *fold_str; +utf8proc_map(str, 0, &fold_str, UTF8PROC_NULLTERM | UTF8PROC_CASEFOLD); +printf("%s\n", fold_str); +// ss +free(fold_str); +```