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
Go to the documentation of this file.
1
9
13#include <options/options.h>
14#include <libplatform/terminal.h>
15#include <ascii-chat-deps/utf8proc/utf8proc.h>
17#include <limits.h>
18#include <stdbool.h>
19#include <string.h>
20#include <stdio.h>
21
23 utf8_setting_t setting = GET_OPTION(force_utf8);
24 return setting == UTF8_SETTING_TRUE || (setting == UTF8_SETTING_AUTO && terminal_supports_utf8());
25}
26
27int utf8_decode(const uint8_t *s, uint32_t *codepoint) {
28 utf8proc_int32_t decoded;
29 utf8proc_ssize_t count = utf8proc_iterate(s, -1, &decoded);
30 if (count < 0)
31 return -1;
32 *codepoint = (uint32_t)decoded;
33 return (int)count;
34}
35
36// Width measurement and truncation share the same bounded token walk.
37static size_t utf8_prefix_scan(const char *str, size_t max_bytes, int max_width, int *width) {
38 *width = 0;
39 if (!str || max_bytes == 0 || max_width < 0) {
40 return 0;
41 }
42
43 const char *p = str;
44 const char *end = str + max_bytes;
45 int previous_width = 0;
46 while (p < end && *p) {
47 if (*p == '\x1b') {
48 const char *next = ansi_skip_escape(p, end);
49 // Only complete CSI sequences are accepted; never emit a partial escape.
50 if (next - p < 3 || p[1] != '[' || next[-1] < '@' || next[-1] > '~') {
51 break;
52 }
53 if (next[-1] == 'b') {
54 unsigned repeats = 0;
55 const char *q = p + 2;
56 while (q < next - 1 && *q >= '0' && *q <= '9') {
57 unsigned digit = (unsigned)(*q++ - '0');
58 if (repeats > (UINT_MAX - digit) / 10)
59 return (size_t)(p - str);
60 repeats = repeats * 10 + digit;
61 }
62 if (q != next - 1 || previous_width <= 0)
63 break;
64 if (!repeats)
65 repeats = 1;
66 if (repeats > (unsigned)((max_width - *width) / previous_width))
67 break;
68 *width += (int)repeats * previous_width;
69 }
70 p = next;
71 continue;
72 }
73
74 utf8proc_int32_t codepoint;
75 utf8proc_ssize_t len = utf8proc_iterate((const utf8proc_uint8_t *)p, end - p, &codepoint);
76 if (len <= 0) {
77 break;
78 }
79 int char_width = utf8proc_charwidth(codepoint);
80 if (char_width < 0) {
81 char_width = 0;
82 }
83 if (char_width > max_width - *width) {
84 break;
85 }
86 *width += char_width;
87 previous_width = char_width;
88 p += len;
89 }
90 return (size_t)(p - str);
91}
92
93size_t utf8_prefix_bytes_for_width(const char *str, size_t max_bytes, int max_width) {
94 int width;
95 return utf8_prefix_scan(str, max_bytes, max_width, &width);
96}
97
98int utf8_display_width(const char *str) {
99 return utf8_display_width_n(str, str ? strlen(str) : 0);
100}
101
102int utf8_display_width_n(const char *str, size_t max_bytes) {
103 int width;
104 utf8_prefix_scan(str, max_bytes, INT_MAX, &width);
105 return width;
106}
107
108size_t utf8_char_count(const char *str) {
109 if (!str) {
110 SET_ERRNO(ERROR_INVALID_PARAM, "str is NULL");
111 return SIZE_MAX;
112 }
113
114 size_t count = 0;
115 const uint8_t *p = (const uint8_t *)str;
116 while (*p) {
117 uint32_t codepoint;
118 int decode_len = utf8_decode(p, &codepoint);
119 if (decode_len < 0) {
120 return SIZE_MAX; // Invalid UTF-8
121 }
122 count++;
123 p += decode_len;
124 }
125 return count;
126}
127
128bool utf8_is_valid(const char *str) {
129 if (!str) {
130 SET_ERRNO(ERROR_INVALID_PARAM, "str is NULL");
131 return false;
132 }
133 // Reuse utf8_char_count to validate without duplicating loop
134 return utf8_char_count(str) != SIZE_MAX;
135}
136
137bool utf8_is_ascii_only(const char *str) {
138 if (!str) {
139 SET_ERRNO(ERROR_INVALID_PARAM, "str is NULL");
140 return false;
141 }
142
143 // Fast path: Check if all bytes are in ASCII range (0x00-0x7F)
144 // ASCII characters are 0x00-0x7F (single byte, high bit clear)
145 // Multi-byte UTF-8 has continuation bytes with high bit set (10xxxxxx pattern)
146 // So if all bytes have high bit clear, it's guaranteed ASCII-only
147 const unsigned char *p = (const unsigned char *)str;
148 while (*p) {
149 if ((*p & ~0x7F) != 0) {
150 return false; // Non-ASCII byte found (high bit set)
151 }
152 p++;
153 }
154 return true;
155}
156
157size_t utf8_to_codepoints(const char *str, uint32_t *out_codepoints, size_t max_codepoints) {
158 if (!str || !out_codepoints || max_codepoints == 0) {
159 SET_ERRNO(ERROR_INVALID_PARAM, "str is NULL or out_codepoints is NULL or max_codepoints is 0");
160 return 0;
161 }
162
163 size_t count = 0;
164 const uint8_t *p = (const uint8_t *)str;
165 while (*p && count < max_codepoints) {
166 uint32_t codepoint;
167 int decode_len = utf8_decode(p, &codepoint);
168 if (decode_len < 0) {
169 return SIZE_MAX; // Invalid UTF-8
170 }
171 out_codepoints[count++] = codepoint;
172 p += decode_len;
173 }
174 return count;
175}
176
177int utf8_next_char_bytes(const char *str, size_t max_bytes) {
178 if (!str || max_bytes == 0) {
179 SET_ERRNO(ERROR_INVALID_PARAM, "str is NULL or max_bytes is 0");
180 return -1;
181 }
182
183 // Use utf8proc_iterate to get byte length of next character
184 utf8proc_int32_t codepoint;
185 utf8proc_ssize_t len = utf8proc_iterate((const utf8proc_uint8_t *)str, (utf8proc_ssize_t)max_bytes, &codepoint);
186
187 if (len <= 0) {
188 return -1; // Invalid UTF-8 or end of string
189 }
190
191 return (int)len;
192}
193
194/* ============================================================================
195 * UTF-8 String Search Functions
196 * ========================================================================== */
197
205const char *utf8_strcasestr(const char *haystack, const char *needle) {
206 if (!haystack || !needle) {
207 SET_ERRNO(ERROR_INVALID_PARAM, "invalid params");
208 return NULL;
209 }
210
211 // Empty needle matches at start of haystack
212 if (needle[0] == '\0') {
213 return haystack;
214 }
215
216 // Get lengths
217 size_t haystack_len = strlen(haystack);
218 size_t needle_len = strlen(needle);
219
220 if (needle_len > haystack_len) {
221 return NULL;
222 }
223
224 // Case-fold both strings using utf8proc
225 // UTF8PROC_CASEFOLD performs Unicode case folding
226 // UTF8PROC_STABLE ensures stable output
227 // UTF8PROC_COMPOSE normalizes composed characters
228 utf8proc_option_t options = UTF8PROC_CASEFOLD | UTF8PROC_STABLE | UTF8PROC_COMPOSE;
229
230 // Case-fold the needle (pattern to search for)
231 utf8proc_uint8_t *needle_folded = NULL;
232 utf8proc_ssize_t needle_folded_len = utf8proc_map((const utf8proc_uint8_t *)needle, (utf8proc_ssize_t)needle_len, &needle_folded, options);
233
234 if (needle_folded_len < 0 || !needle_folded) {
235 // Invalid UTF-8 in needle
236 if (needle_folded) {
237 free(needle_folded);
238 }
239 return NULL;
240 }
241
242 // Try each position in haystack
243 const char *haystack_pos = haystack;
244 while (*haystack_pos != '\0') {
245 // Calculate remaining haystack length
246 size_t remaining = haystack_len - (size_t)(haystack_pos - haystack);
247
248 if (remaining < needle_len) {
249 // Not enough characters left to match
250 break;
251 }
252
253 // Case-fold the current haystack window
254 utf8proc_uint8_t *haystack_folded = NULL;
255 utf8proc_ssize_t haystack_folded_len = utf8proc_map((const utf8proc_uint8_t *)haystack_pos, (utf8proc_ssize_t)needle_len, &haystack_folded, options);
256
257 if (haystack_folded_len >= 0 && haystack_folded) {
258 // Compare case-folded strings
259 if ((size_t)haystack_folded_len == (size_t)needle_folded_len && memcmp(haystack_folded, needle_folded, (size_t)needle_folded_len) == 0) {
260 // Match found!
261 free(haystack_folded);
262 free(needle_folded);
263 return haystack_pos;
264 }
265 free(haystack_folded);
266 }
267
268 // Move to next UTF-8 character in haystack
269 utf8proc_int32_t codepoint;
270 utf8proc_ssize_t bytes = utf8proc_iterate((const utf8proc_uint8_t *)haystack_pos, -1, &codepoint);
271 if (bytes <= 0) {
272 // Invalid UTF-8, move by one byte
273 haystack_pos++;
274 } else {
275 haystack_pos += bytes;
276 }
277 }
278
279 free(needle_folded);
280 return NULL;
281}
282
290void utf8_write_lowercase(FILE *output, const char *text) {
291 if (!text) {
292 return;
293 }
294
295 for (const unsigned char *p = (const unsigned char *)text; *p; p++) {
296 // Check if this is the start of a multi-byte UTF-8 sequence
297 if (*p >= 0xC0) {
298 // Multi-byte UTF-8 character - copy as-is without modification
299 fputc(*p, output);
300 // Copy continuation bytes (0x80-0xBF)
301 while (p[1] >= 0x80 && p[1] < 0xC0) {
302 fputc(*++p, output);
303 }
304 } else if (*p >= 'A' && *p <= 'Z') {
305 // ASCII uppercase - convert to lowercase
306 fputc(*p + 32, output);
307 } else {
308 // ASCII lowercase or control character - copy as-is
309 fputc(*p, output);
310 }
311 }
312}
ANSI escape sequence utilities and fast color code generation.
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
Error and exit codes - unified status values (0-255).
unsigned int width
Definition fft.c:24
const char * ansi_skip_escape(const char *ptr, const char *end)
Find the end of an ANSI CSI escape sequence.
Definition ansi.c:78
unsigned int uint32_t
unsigned char uint8_t
@ ERROR_INVALID_PARAM
utf8_setting_t
UTF-8 support setting (–utf8 flag values).
Definition options.h:263
#define GET_OPTION(field)
Definition options.h:1333
@ UTF8_SETTING_AUTO
Smart detection (default): UTF-8 if terminal supports it.
Definition options.h:265
@ UTF8_SETTING_TRUE
Force UTF-8 ON: always use UTF-8 regardless of terminal capability.
Definition options.h:267
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.
Definition utf8.c:102
bool utf8_is_ascii_only(const char *str)
Check if a string contains only ASCII characters.
Definition utf8.c:137
bool utf8_is_valid(const char *str)
Check if a string is valid UTF-8.
Definition utf8.c:128
int utf8_decode(const uint8_t *s, uint32_t *codepoint)
Decode a UTF-8 sequence to a Unicode codepoint.
Definition utf8.c:27
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.
Definition utf8.c:157
bool utf8_output_enabled(void)
Return whether terminal output should use UTF-8 according to –utf8.
Definition utf8.c:22
void utf8_write_lowercase(FILE *output, const char *text)
Write UTF-8 string to file with ASCII lowercase conversion.
Definition utf8.c:290
int utf8_display_width(const char *str)
Calculate terminal display width of a UTF-8 string.
Definition utf8.c:98
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:177
const char * utf8_strcasestr(const char *haystack, const char *needle)
Case-insensitive substring search with full Unicode support.
Definition utf8.c:205
size_t utf8_char_count(const char *str)
Count UTF-8 characters (not bytes).
Definition utf8.c:108
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
bool terminal_supports_utf8(void)
🔤 String Manipulation and Shell Escaping Utilities
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.
Definition utf8.c:93
🔤 UTF-8 Encoding and Decoding Utilities