ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
utf8.c File Reference

🔤 UTF-8 encoding and decoding with multi-byte character support More...

Go to the source code of this file.

Functions

bool utf8_output_enabled (void)
 Return whether terminal output should use UTF-8 according to –utf8.
int utf8_decode (const uint8_t *s, uint32_t *codepoint)
 Decode a UTF-8 sequence to a Unicode codepoint.
size_t utf8_prefix_bytes_for_width (const char *str, size_t max_bytes, int max_width)
 Return the byte length of a prefix fitting a terminal column limit.
int utf8_display_width (const char *str)
 Calculate terminal display width of a UTF-8 string.
int utf8_display_width_n (const char *str, size_t max_bytes)
 Calculate terminal display width of a UTF-8 string up to byte limit.
size_t utf8_char_count (const char *str)
 Count UTF-8 characters (not bytes).
bool utf8_is_valid (const char *str)
 Check if a string is valid UTF-8.
bool utf8_is_ascii_only (const char *str)
 Check if a string contains only ASCII characters.
size_t utf8_to_codepoints (const char *str, uint32_t *out_codepoints, size_t max_codepoints)
 Convert UTF-8 string to array of Unicode codepoints.
int utf8_next_char_bytes (const char *str, size_t max_bytes)
 Get byte length of next UTF-8 character.
const char * utf8_strcasestr (const char *haystack, const char *needle)
 Case-insensitive substring search with full Unicode support.
void utf8_write_lowercase (FILE *output, const char *text)
 Write UTF-8 string to file with ASCII lowercase conversion.

Detailed Description

🔤 UTF-8 encoding and decoding with multi-byte character support

Uses utf8proc Unicode library for accurate character-width computation and UTF-8 handling.

Definition in file utf8.c.

Function Documentation

◆ utf8_prefix_bytes_for_width()

size_t utf8_prefix_bytes_for_width ( const char * str,
size_t max_bytes,
int max_width )

Return the byte length of a prefix fitting a terminal column limit.

Parameters
strInput string or bounded slice (may be NULL)
max_bytesMaximum readable bytes
max_widthMaximum terminal columns; negative values return zero
Returns
Prefix length in bytes, without modifying the input

Stops at NUL, invalid/incomplete UTF-8, or an incomplete/unsupported escape. Complete ANSI CSI sequences consume zero columns. Codepoints are never split; combining marks and trailing CSI sequences are included even at the width limit. Widths follow utf8proc_charwidth(), not grapheme-cluster or terminal-specific emoji shaping. A zero-column limit may include leading zero-width tokens.

Definition at line 93 of file utf8.c.

93 {
94 int width;
95 return utf8_prefix_scan(str, max_bytes, max_width, &width);
96}
unsigned int width
Definition fft.c:24

References width.

Referenced by ascii_create_grid(), and truncate_with_ellipsis_mode().