skip to content
==> include/tui/utf8.h <==
1/* @ajax/tui-utf8: UTF-8 as Rust handles it. Decoding follows the Unicode "maximal subpart" rule,
2 * the one String::from_utf8_lossy uses, so repaired text is byte-for-byte what Rust would show. */
3#ifndef TUI_UTF8_H
4#define TUI_UTF8_H
5
6#include <stdbool.h>
7#include <stddef.h>
8#include <stdint.h>
9
10#define TUI_UTF8_REPLACEMENT 0xFFFDu
11
12/* Decodes the character at s[*i] and advances *i past it; an ill-formed subpart decodes as
13 * U+FFFD and advances past exactly that subpart. *i must be < len. */
14uint32_t tui_utf8_next(const char *s, size_t len, size_t *i);
15/* Writes cp's UTF-8 (1-4 bytes) to out; returns the length. */
16size_t tui_utf8_encode(uint32_t cp, char out[4]);
17bool tui_utf8_valid(const char *s, size_t len);
18/* A NUL-terminated, valid UTF-8 copy (malloc'd; free it), each ill-formed subpart replaced by
19 * U+FFFD. *out_len (if given) receives its length. */
20char *tui_utf8_lossy(const void *bytes, size_t len, size_t *out_len);
21/* Characters (Unicode scalar values) in valid UTF-8. */
22size_t tui_utf8_count(const char *s, size_t len);
23
24#endif