Skip to main content

UTF-8 and width

int lt_utf8_char_length(char c);
int lt_utf8_char_to_unicode(uint32_t *out, const char *c);
int lt_utf8_unicode_to_char(char *out, uint32_t c);
int lt_wcwidth(uint32_t ch);
int lt_iswprint(uint32_t ch);
int lt_attr_width(void);

These are the decoder, encoder and width tables libterm uses internally, exported for your own text handling. All of them are stateless and work before lt_init.

lt_utf8_char_length​

int lt_utf8_char_length(char c);

Tells how long the UTF-8 sequence that begins with the lead byte c is. It inspects only that one byte and does not validate the bytes that follow.

Parameters​

NameTypeMeaning
ccharthe first byte of a sequence

Returns​

1 to 4 for a valid lead byte. 0 if c is a continuation byte or one of 0xF8 to 0xFF.

Example​

const char *p = text;
while (*p) {
int n = lt_utf8_char_length(*p);
if (n == 0) { p++; continue; } /* skip a stray continuation byte */
/* p[0..n-1] is one character */
p += n;
}

See also​

lt_utf8_char_to_unicode.

lt_utf8_char_to_unicode​

int lt_utf8_char_to_unicode(uint32_t *out, const char *c);

Decodes one UTF-8 sequence into a codepoint.

Parameters​

NameTypeMeaning
outuint32_t *receives the codepoint
cconst char *the start of the sequence. Must be NUL-terminated

Returns​

ValueWhen
1 to 4success, the number of bytes consumed
0c is NULL or empty, or the sequence is malformed
-ithe i-th continuation byte is NUL, so the input was cut short

Example​

uint32_t cp;
int n = lt_utf8_char_to_unicode(&cp, "€uro");
/* n == 3, cp == 0x20AC */

See also​

lt_utf8_unicode_to_char, lt_print.

lt_utf8_unicode_to_char​

int lt_utf8_unicode_to_char(char *out, uint32_t c);

Encodes a codepoint as UTF-8 and NUL-terminates the result.

Parameters​

NameTypeMeaning
outchar *a buffer of at least 5 bytes, up to 4 encoded bytes plus the terminator
cuint32_tthe codepoint

Returns​

The number of bytes written, excluding the NUL. 0 for an unencodable codepoint, in which case out[0] is set to NUL.

Example​

char buf[5];
int n = lt_utf8_unicode_to_char(buf, 0x2588); /* the full block */
fwrite(buf, 1, (size_t)n, stdout);

See also​

lt_utf8_char_to_unicode.

lt_wcwidth​

int lt_wcwidth(uint32_t ch);

The number of terminal columns a codepoint occupies. It follows Markus Kuhn's reference ranges and does not depend on the locale, so it gives the same answer on every platform.

Parameters​

NameTypeMeaning
chuint32_tthe codepoint

Returns​

ValueWhen
0a combining mark or another zero-width character
1an ordinary character
2a wide East Asian or emoji character
-1a control character or another non-printable codepoint

Example​

int cols = 0;
for (const char *p = s; *p;) {
uint32_t cp;
int n = lt_utf8_char_to_unicode(&cp, p);
if (n <= 0) break;
int w = lt_wcwidth(cp);
cols += w > 0 ? w : 0;
p += n;
}

See also​

lt_iswprint, lt_print_ex, Text and Unicode.

lt_iswprint​

int lt_iswprint(uint32_t ch);

Reports whether a codepoint is printable, that is whether lt_wcwidth(ch) >= 0.

Parameters​

NameTypeMeaning
chuint32_tthe codepoint

Returns​

Non-zero if printable, 0 otherwise.

Example​

lt_set_cell(x, y, lt_iswprint(cp) ? cp : '?', fg, bg);

See also​

lt_wcwidth.

lt_attr_width​

int lt_attr_width(void);

The byte width of the lt_attr type used for foreground and background. It mirrors termbox2's tb_attr_width. libterm has no compile-time attribute-width option, so the value is constant.

Parameters​

None.

Returns​

4, always, because lt_attr is uint32_t.

Example​

assert(lt_attr_width() == sizeof(lt_attr));

See also​

lt_attr, Porting from termbox2.