Skip to content

Commit 5568eff

Browse files
authored
docs: add examples for common usecases (#267)
1 parent 9f2d8d3 commit 5568eff

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,50 @@ the [utf8proc issues page on Github](https://github.com/JuliaLang/utf8proc/issue
8686
## See also
8787

8888
An independent Lua translation of this library, [lua-mojibake](https://github.com/differentprogramming/lua-mojibake), is also available.
89+
90+
## Examples
91+
92+
### Convert codepoint to string
93+
```c
94+
// Convert codepoint `a` to utf8 string `str`
95+
utf8proc_int32_t a = 223;
96+
utf8proc_uint8_t str[16] = { 0 };
97+
utf8proc_encode_char(a, str);
98+
printf("%s\n", str);
99+
// ß
100+
```
101+
102+
### Convert string to codepoint
103+
```c
104+
// Convert string `str` to pointer to codepoint `a`
105+
utf8proc_uint8_t str[] = "ß";
106+
utf8proc_int32_t a;
107+
utf8proc_iterate(str, -1, &a);
108+
printf("%d\n", a);
109+
// 223
110+
```
111+
112+
### Casefold
113+
114+
```c
115+
// Convert "ß" (U+00DF) to its casefold variant "ss"
116+
utf8proc_uint8_t str[] = "ß";
117+
utf8proc_uint8_t *fold_str;
118+
utf8proc_map(str, 0, &fold_str, UTF8PROC_NULLTERM | UTF8PROC_CASEFOLD);
119+
printf("%s\n", fold_str);
120+
// ss
121+
free(fold_str);
122+
```
123+
124+
### Normalization Form C/D (NFC/NFD)
125+
```c
126+
// Decompose "\u00e4\u00f6\u00fc" = "äöü" into "a\u0308o\u0308u\u0308" (= "äöü" via combining char U+0308)
127+
utf8proc_uint8_t input[] = {0xc3, 0xa4, 0xc3, 0xb6, 0xc3, 0xbc}; // "\u00e4\u00f6\u00fc" = "äöü" in UTF-8
128+
utf8proc_uint8_t *nfd= utf8proc_NFD(input); // = {0x61, 0xcc, 0x88, 0x6f, 0xcc, 0x88, 0x75, 0xcc, 0x88}
129+
130+
// Compose "a\u0308o\u0308u\u0308" into "\u00e4\u00f6\u00fc" (= "äöü" via precomposed characters)
131+
utf8proc_uint8_t *nfc= utf8proc_NFC(nfd);
132+
133+
free(nfd);
134+
free(nfc);
135+
```

0 commit comments

Comments
 (0)