]>
Commit | Line | Data |
---|---|---|
6952ebae | 1 | /* SPDX-License-Identifier: LGPL-2.0-or-later */ |
7f110ff9 | 2 | |
6952ebae | 3 | /* Parts of this file are based on the GLIB utf8 validation functions. The original copyright follows. |
7f110ff9 | 4 | * |
6952ebae | 5 | * gutf8.c - Operations on UTF-8 strings. |
7f110ff9 LP |
6 | * Copyright (C) 1999 Tom Tromey |
7 | * Copyright (C) 2000 Red Hat, Inc. | |
7f110ff9 LP |
8 | */ |
9 | ||
b5efdb8a | 10 | #include "alloc-util.h" |
3f536d5b | 11 | #include "gunicode.h" |
e4e73a63 | 12 | #include "hexdecoct.h" |
4c6d5139 | 13 | #include "string-util.h" |
7f110ff9 LP |
14 | #include "utf8.h" |
15 | ||
c932fb71 | 16 | bool unichar_is_valid(char32_t ch) { |
7f110ff9 LP |
17 | |
18 | if (ch >= 0x110000) /* End of unicode space */ | |
19 | return false; | |
20 | if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */ | |
21 | return false; | |
22 | if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */ | |
23 | return false; | |
24 | if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */ | |
25 | return false; | |
26 | ||
27 | return true; | |
28 | } | |
29 | ||
c932fb71 | 30 | static bool unichar_is_control(char32_t ch) { |
ba961854 ZJS |
31 | |
32 | /* | |
33 | 0 to ' '-1 is the C0 range. | |
34 | DEL=0x7F, and DEL+1 to 0x9F is C1 range. | |
35 | '\t' is in C0 range, but more or less harmless and commonly used. | |
36 | */ | |
37 | ||
4c701096 | 38 | return (ch < ' ' && !IN_SET(ch, '\t', '\n')) || |
ba961854 ZJS |
39 | (0x7F <= ch && ch <= 0x9F); |
40 | } | |
41 | ||
7991ac34 | 42 | /* count of characters used to encode one unicode char */ |
84319aa7 | 43 | static size_t utf8_encoded_expected_len(uint8_t c) { |
7991ac34 DR |
44 | if (c < 0x80) |
45 | return 1; | |
46 | if ((c & 0xe0) == 0xc0) | |
47 | return 2; | |
48 | if ((c & 0xf0) == 0xe0) | |
49 | return 3; | |
50 | if ((c & 0xf8) == 0xf0) | |
51 | return 4; | |
52 | if ((c & 0xfc) == 0xf8) | |
53 | return 5; | |
54 | if ((c & 0xfe) == 0xfc) | |
55 | return 6; | |
7e8185ef | 56 | |
7991ac34 DR |
57 | return 0; |
58 | } | |
ba961854 | 59 | |
7991ac34 | 60 | /* decode one unicode char */ |
c932fb71 SL |
61 | int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) { |
62 | char32_t unichar; | |
fe96c0f8 | 63 | size_t len; |
7e8185ef LP |
64 | |
65 | assert(str); | |
ba961854 | 66 | |
84319aa7 | 67 | len = utf8_encoded_expected_len(str[0]); |
7e8185ef | 68 | |
7991ac34 DR |
69 | switch (len) { |
70 | case 1: | |
c932fb71 | 71 | *ret_unichar = (char32_t)str[0]; |
9579e9a5 | 72 | return 1; |
7991ac34 DR |
73 | case 2: |
74 | unichar = str[0] & 0x1f; | |
75 | break; | |
76 | case 3: | |
c932fb71 | 77 | unichar = (char32_t)str[0] & 0x0f; |
7991ac34 DR |
78 | break; |
79 | case 4: | |
c932fb71 | 80 | unichar = (char32_t)str[0] & 0x07; |
7991ac34 DR |
81 | break; |
82 | case 5: | |
c932fb71 | 83 | unichar = (char32_t)str[0] & 0x03; |
7991ac34 DR |
84 | break; |
85 | case 6: | |
c932fb71 | 86 | unichar = (char32_t)str[0] & 0x01; |
7991ac34 DR |
87 | break; |
88 | default: | |
7e8185ef | 89 | return -EINVAL; |
ba961854 ZJS |
90 | } |
91 | ||
fe96c0f8 | 92 | for (size_t i = 1; i < len; i++) { |
c932fb71 | 93 | if (((char32_t)str[i] & 0xc0) != 0x80) |
7e8185ef | 94 | return -EINVAL; |
7c421857 | 95 | |
7991ac34 | 96 | unichar <<= 6; |
c932fb71 | 97 | unichar |= (char32_t)str[i] & 0x3f; |
7991ac34 DR |
98 | } |
99 | ||
c932fb71 | 100 | *ret_unichar = unichar; |
9579e9a5 | 101 | return len; |
ba961854 ZJS |
102 | } |
103 | ||
618727da | 104 | bool utf8_is_printable_newline(const char* str, size_t length, bool allow_newline) { |
7f110ff9 LP |
105 | assert(str); |
106 | ||
a456086b | 107 | for (const char *p = str; length > 0;) { |
9579e9a5 | 108 | int encoded_len; |
c932fb71 | 109 | char32_t val; |
7e8185ef | 110 | |
92e068b4 ZJS |
111 | encoded_len = utf8_encoded_valid_unichar(p, length); |
112 | if (encoded_len < 0) | |
144b3d9e | 113 | return false; |
92e068b4 | 114 | assert(encoded_len > 0 && (size_t) encoded_len <= length); |
144b3d9e | 115 | |
9579e9a5 | 116 | if (utf8_encoded_to_unichar(p, &val) < 0 || |
f3ee6297 | 117 | unichar_is_control(val) || |
618727da | 118 | (!allow_newline && val == '\n')) |
7991ac34 | 119 | return false; |
7f110ff9 | 120 | |
7991ac34 | 121 | length -= encoded_len; |
a7176505 | 122 | p += encoded_len; |
7f110ff9 LP |
123 | } |
124 | ||
7991ac34 | 125 | return true; |
7f110ff9 LP |
126 | } |
127 | ||
7ff71610 | 128 | char* utf8_is_valid_n(const char *str, size_t len_bytes) { |
80ab31a4 ZJS |
129 | /* Check if the string is composed of valid utf8 characters. If length len_bytes is given, stop after |
130 | * len_bytes. Otherwise, stop at NUL. */ | |
7f110ff9 LP |
131 | |
132 | assert(str); | |
133 | ||
7ff71610 | 134 | for (size_t i = 0; len_bytes != SIZE_MAX ? i < len_bytes : str[i] != '\0'; ) { |
faaa5728 LP |
135 | int len; |
136 | ||
7ff71610 | 137 | if (_unlikely_(str[i] == '\0')) |
80ab31a4 ZJS |
138 | return NULL; /* embedded NUL */ |
139 | ||
7ff71610 MY |
140 | len = utf8_encoded_valid_unichar(str + i, |
141 | len_bytes != SIZE_MAX ? len_bytes - i : SIZE_MAX); | |
80ab31a4 ZJS |
142 | if (_unlikely_(len < 0)) |
143 | return NULL; /* invalid character */ | |
7991ac34 | 144 | |
7ff71610 | 145 | i += len; |
7991ac34 | 146 | } |
7f110ff9 | 147 | |
e71fb4b3 | 148 | return (char*) str; |
7f110ff9 LP |
149 | } |
150 | ||
ff3f2953 | 151 | char* utf8_escape_invalid(const char *str) { |
550a40ec ZJS |
152 | char *p, *s; |
153 | ||
154 | assert(str); | |
155 | ||
156 | p = s = malloc(strlen(str) * 4 + 1); | |
157 | if (!p) | |
158 | return NULL; | |
159 | ||
160 | while (*str) { | |
161 | int len; | |
162 | ||
f5fbe71d | 163 | len = utf8_encoded_valid_unichar(str, SIZE_MAX); |
550a40ec ZJS |
164 | if (len > 0) { |
165 | s = mempcpy(s, str, len); | |
166 | str += len; | |
167 | } else { | |
3c6d3052 | 168 | s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER); |
550a40ec ZJS |
169 | str += 1; |
170 | } | |
171 | } | |
7e8185ef | 172 | |
550a40ec | 173 | *s = '\0'; |
523e1b14 | 174 | return str_realloc(p); |
550a40ec ZJS |
175 | } |
176 | ||
9632f8b4 | 177 | int utf8_char_console_width(const char *str) { |
da88f542 ZJS |
178 | char32_t c; |
179 | int r; | |
180 | ||
181 | r = utf8_encoded_to_unichar(str, &c); | |
182 | if (r < 0) | |
183 | return r; | |
184 | ||
b4aaba2b LP |
185 | if (c == '\t') |
186 | return 8; /* Assume a tab width of 8 */ | |
187 | ||
da88f542 ZJS |
188 | /* TODO: we should detect combining characters */ |
189 | ||
190 | return unichar_iswide(c) ? 2 : 1; | |
191 | } | |
192 | ||
ff3f2953 | 193 | char* utf8_escape_non_printable_full(const char *str, size_t console_width, bool force_ellipsis) { |
da88f542 ZJS |
194 | char *p, *s, *prev_s; |
195 | size_t n = 0; /* estimated print width */ | |
fec84576 WC |
196 | |
197 | assert(str); | |
198 | ||
da88f542 ZJS |
199 | if (console_width == 0) |
200 | return strdup(""); | |
201 | ||
202 | p = s = prev_s = malloc(strlen(str) * 4 + 1); | |
fec84576 WC |
203 | if (!p) |
204 | return NULL; | |
205 | ||
da88f542 | 206 | for (;;) { |
fec84576 | 207 | int len; |
da88f542 ZJS |
208 | char *saved_s = s; |
209 | ||
fc96e5c0 ZJS |
210 | if (!*str) { /* done! */ |
211 | if (force_ellipsis) | |
212 | goto truncation; | |
213 | else | |
214 | goto finish; | |
215 | } | |
fec84576 | 216 | |
f5fbe71d | 217 | len = utf8_encoded_valid_unichar(str, SIZE_MAX); |
fec84576 WC |
218 | if (len > 0) { |
219 | if (utf8_is_printable(str, len)) { | |
da88f542 ZJS |
220 | int w; |
221 | ||
222 | w = utf8_char_console_width(str); | |
223 | assert(w >= 0); | |
224 | if (n + w > console_width) | |
225 | goto truncation; | |
226 | ||
fec84576 WC |
227 | s = mempcpy(s, str, len); |
228 | str += len; | |
da88f542 ZJS |
229 | n += w; |
230 | ||
fec84576 | 231 | } else { |
da88f542 ZJS |
232 | for (; len > 0; len--) { |
233 | if (n + 4 > console_width) | |
234 | goto truncation; | |
235 | ||
fec84576 WC |
236 | *(s++) = '\\'; |
237 | *(s++) = 'x'; | |
238 | *(s++) = hexchar((int) *str >> 4); | |
239 | *(s++) = hexchar((int) *str); | |
fec84576 | 240 | |
3c6d3052 | 241 | str += 1; |
da88f542 | 242 | n += 4; |
3c6d3052 | 243 | } |
fec84576 WC |
244 | } |
245 | } else { | |
da88f542 ZJS |
246 | if (n + 1 > console_width) |
247 | goto truncation; | |
248 | ||
249 | s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER)); | |
fec84576 | 250 | str += 1; |
da88f542 | 251 | n += 1; |
fec84576 | 252 | } |
da88f542 ZJS |
253 | |
254 | prev_s = saved_s; | |
fec84576 WC |
255 | } |
256 | ||
da88f542 ZJS |
257 | truncation: |
258 | /* Try to go back one if we don't have enough space for the ellipsis */ | |
fc96e5c0 | 259 | if (n + 1 > console_width) |
da88f542 ZJS |
260 | s = prev_s; |
261 | ||
262 | s = mempcpy(s, "…", strlen("…")); | |
fec84576 | 263 | |
da88f542 ZJS |
264 | finish: |
265 | *s = '\0'; | |
523e1b14 | 266 | return str_realloc(p); |
fec84576 WC |
267 | } |
268 | ||
4d06bf59 MY |
269 | char* ascii_is_valid_n(const char *str, size_t len) { |
270 | /* Check whether the string consists of valid ASCII bytes, i.e values between 1 and 127, inclusive. | |
271 | * Stops at len, or NUL byte if len is SIZE_MAX. */ | |
294a3121 | 272 | |
7f110ff9 LP |
273 | assert(str); |
274 | ||
4d06bf59 MY |
275 | for (size_t i = 0; len != SIZE_MAX ? i < len : str[i] != '\0'; i++) |
276 | if ((unsigned char) str[i] >= 128 || str[i] == '\0') | |
294a3121 ZJS |
277 | return NULL; |
278 | ||
279 | return (char*) str; | |
280 | } | |
281 | ||
9b49a3b4 ZJS |
282 | int utf8_to_ascii(const char *str, char replacement_char, char **ret) { |
283 | /* Convert to a string that has only ASCII chars, replacing anything that is not ASCII | |
284 | * by replacement_char. */ | |
285 | ||
286 | _cleanup_free_ char *ans = new(char, strlen(str) + 1); | |
287 | if (!ans) | |
288 | return -ENOMEM; | |
289 | ||
290 | char *q = ans; | |
291 | ||
292 | for (const char *p = str; *p; q++) { | |
293 | int l; | |
294 | ||
295 | l = utf8_encoded_valid_unichar(p, SIZE_MAX); | |
296 | if (l < 0) /* Non-UTF-8, let's not even try to propagate the garbage */ | |
297 | return l; | |
298 | ||
299 | if (l == 1) | |
300 | *q = *p; | |
301 | else | |
302 | /* non-ASCII, we need to replace it */ | |
303 | *q = replacement_char; | |
304 | ||
305 | p += l; | |
306 | } | |
307 | *q = '\0'; | |
308 | ||
309 | *ret = TAKE_PTR(ans); | |
310 | return 0; | |
311 | } | |
312 | ||
2bb4c7e3 TG |
313 | /** |
314 | * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8 | |
315 | * @out_utf8: output buffer of at least 4 bytes or NULL | |
316 | * @g: UCS-4 character to encode | |
317 | * | |
318 | * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8. | |
319 | * The length of the character is returned. It is not zero-terminated! If the | |
320 | * output buffer is NULL, only the length is returned. | |
321 | * | |
322 | * Returns: The length in bytes that the UTF-8 representation does or would | |
323 | * occupy. | |
324 | */ | |
c932fb71 | 325 | size_t utf8_encode_unichar(char *out_utf8, char32_t g) { |
f3ee6297 | 326 | |
2bb4c7e3 TG |
327 | if (g < (1 << 7)) { |
328 | if (out_utf8) | |
329 | out_utf8[0] = g & 0x7f; | |
e7eebcfc | 330 | return 1; |
2bb4c7e3 TG |
331 | } else if (g < (1 << 11)) { |
332 | if (out_utf8) { | |
333 | out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f); | |
334 | out_utf8[1] = 0x80 | (g & 0x3f); | |
335 | } | |
e7eebcfc | 336 | return 2; |
2bb4c7e3 TG |
337 | } else if (g < (1 << 16)) { |
338 | if (out_utf8) { | |
339 | out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f); | |
340 | out_utf8[1] = 0x80 | ((g >> 6) & 0x3f); | |
341 | out_utf8[2] = 0x80 | (g & 0x3f); | |
342 | } | |
e7eebcfc | 343 | return 3; |
2bb4c7e3 TG |
344 | } else if (g < (1 << 21)) { |
345 | if (out_utf8) { | |
346 | out_utf8[0] = 0xf0 | ((g >> 18) & 0x07); | |
347 | out_utf8[1] = 0x80 | ((g >> 12) & 0x3f); | |
348 | out_utf8[2] = 0x80 | ((g >> 6) & 0x3f); | |
349 | out_utf8[3] = 0x80 | (g & 0x3f); | |
350 | } | |
351 | return 4; | |
e7eebcfc | 352 | } |
f3ee6297 LP |
353 | |
354 | return 0; | |
e7eebcfc LP |
355 | } |
356 | ||
ff3f2953 | 357 | char* utf16_to_utf8(const char16_t *s, size_t length /* bytes! */) { |
2e3d0692 | 358 | const uint8_t *f; |
e7eebcfc | 359 | char *r, *t; |
2e3d0692 | 360 | |
ba091282 LP |
361 | if (length == 0) |
362 | return new0(char, 1); | |
363 | ||
2ac2ff3f LP |
364 | assert(s); |
365 | ||
ba091282 LP |
366 | if (length == SIZE_MAX) { |
367 | length = char16_strlen(s); | |
368 | ||
369 | if (length > SIZE_MAX/2) | |
370 | return NULL; /* overflow */ | |
371 | ||
372 | length *= 2; | |
373 | } | |
374 | ||
2ac2ff3f LP |
375 | /* Input length is in bytes, i.e. the shortest possible character takes 2 bytes. Each unicode character may |
376 | * take up to 4 bytes in UTF-8. Let's also account for a trailing NUL byte. */ | |
ba091282 | 377 | if (length > (SIZE_MAX - 1) / 2) |
2ac2ff3f LP |
378 | return NULL; /* overflow */ |
379 | ||
380 | r = new(char, length * 2 + 1); | |
2e3d0692 LP |
381 | if (!r) |
382 | return NULL; | |
383 | ||
2ac2ff3f | 384 | f = (const uint8_t*) s; |
04166cb7 TG |
385 | t = r; |
386 | ||
2ac2ff3f | 387 | while (f + 1 < (const uint8_t*) s + length) { |
c932fb71 | 388 | char16_t w1, w2; |
04166cb7 TG |
389 | |
390 | /* see RFC 2781 section 2.2 */ | |
391 | ||
392 | w1 = f[1] << 8 | f[0]; | |
393 | f += 2; | |
394 | ||
395 | if (!utf16_is_surrogate(w1)) { | |
dcd12626 | 396 | t += utf8_encode_unichar(t, w1); |
04166cb7 TG |
397 | continue; |
398 | } | |
399 | ||
400 | if (utf16_is_trailing_surrogate(w1)) | |
2ac2ff3f LP |
401 | continue; /* spurious trailing surrogate, ignore */ |
402 | ||
403 | if (f + 1 >= (const uint8_t*) s + length) | |
04166cb7 TG |
404 | break; |
405 | ||
406 | w2 = f[1] << 8 | f[0]; | |
407 | f += 2; | |
408 | ||
409 | if (!utf16_is_trailing_surrogate(w2)) { | |
410 | f -= 2; | |
2ac2ff3f | 411 | continue; /* surrogate missing its trailing surrogate, ignore */ |
04166cb7 TG |
412 | } |
413 | ||
414 | t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2)); | |
415 | } | |
2e3d0692 LP |
416 | |
417 | *t = 0; | |
7b4d7cc0 | 418 | return r; |
2e3d0692 | 419 | } |
02a36bc9 | 420 | |
80b0a597 LP |
421 | size_t utf16_encode_unichar(char16_t *out, char32_t c) { |
422 | ||
423 | /* Note that this encodes as little-endian. */ | |
424 | ||
425 | switch (c) { | |
426 | ||
427 | case 0 ... 0xd7ffU: | |
428 | case 0xe000U ... 0xffffU: | |
429 | out[0] = htole16(c); | |
430 | return 1; | |
431 | ||
432 | case 0x10000U ... 0x10ffffU: | |
433 | c -= 0x10000U; | |
434 | out[0] = htole16((c >> 10) + 0xd800U); | |
435 | out[1] = htole16((c & 0x3ffU) + 0xdc00U); | |
436 | return 2; | |
437 | ||
438 | default: /* A surrogate (invalid) */ | |
439 | return 0; | |
440 | } | |
441 | } | |
442 | ||
443 | char16_t *utf8_to_utf16(const char *s, size_t length) { | |
444 | char16_t *n, *p; | |
80b0a597 LP |
445 | int r; |
446 | ||
ba091282 LP |
447 | if (length == 0) |
448 | return new0(char16_t, 1); | |
449 | ||
80b0a597 LP |
450 | assert(s); |
451 | ||
ba091282 LP |
452 | if (length == SIZE_MAX) |
453 | length = strlen(s); | |
454 | ||
455 | if (length > SIZE_MAX - 1) | |
456 | return NULL; /* overflow */ | |
457 | ||
80b0a597 LP |
458 | n = new(char16_t, length + 1); |
459 | if (!n) | |
460 | return NULL; | |
461 | ||
462 | p = n; | |
463 | ||
fe96c0f8 | 464 | for (size_t i = 0; i < length;) { |
80b0a597 LP |
465 | char32_t unichar; |
466 | size_t e; | |
467 | ||
84319aa7 | 468 | e = utf8_encoded_expected_len(s[i]); |
80b0a597 LP |
469 | if (e <= 1) /* Invalid and single byte characters are copied as they are */ |
470 | goto copy; | |
471 | ||
472 | if (i + e > length) /* sequence longer than input buffer, then copy as-is */ | |
473 | goto copy; | |
474 | ||
475 | r = utf8_encoded_to_unichar(s + i, &unichar); | |
476 | if (r < 0) /* sequence invalid, then copy as-is */ | |
477 | goto copy; | |
478 | ||
479 | p += utf16_encode_unichar(p, unichar); | |
480 | i += e; | |
481 | continue; | |
482 | ||
483 | copy: | |
484 | *(p++) = htole16(s[i++]); | |
485 | } | |
486 | ||
487 | *p = 0; | |
488 | return n; | |
489 | } | |
490 | ||
491 | size_t char16_strlen(const char16_t *s) { | |
492 | size_t n = 0; | |
493 | ||
494 | assert(s); | |
495 | ||
496 | while (*s != 0) | |
497 | n++, s++; | |
498 | ||
499 | return n; | |
500 | } | |
501 | ||
a07864a4 DDM |
502 | size_t char16_strsize(const char16_t *s) { |
503 | return s ? (char16_strlen(s) + 1) * sizeof(*s) : 0; | |
504 | } | |
505 | ||
02a36bc9 | 506 | /* expected size used to encode one unicode char */ |
c932fb71 | 507 | static int utf8_unichar_to_encoded_len(char32_t unichar) { |
7e8185ef | 508 | |
02a36bc9 DR |
509 | if (unichar < 0x80) |
510 | return 1; | |
511 | if (unichar < 0x800) | |
512 | return 2; | |
513 | if (unichar < 0x10000) | |
514 | return 3; | |
515 | if (unichar < 0x200000) | |
516 | return 4; | |
517 | if (unichar < 0x4000000) | |
518 | return 5; | |
7e8185ef | 519 | |
02a36bc9 DR |
520 | return 6; |
521 | } | |
522 | ||
523 | /* validate one encoded unicode char and return its length */ | |
92e068b4 | 524 | int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) { |
c932fb71 | 525 | char32_t unichar; |
fe96c0f8 | 526 | size_t len; |
7c421857 | 527 | int r; |
7e8185ef LP |
528 | |
529 | assert(str); | |
92e068b4 ZJS |
530 | assert(length > 0); |
531 | ||
f5fbe71d | 532 | /* We read until NUL, at most length bytes. SIZE_MAX may be used to disable the length check. */ |
02a36bc9 | 533 | |
84319aa7 | 534 | len = utf8_encoded_expected_len(str[0]); |
02a36bc9 | 535 | if (len == 0) |
7e8185ef | 536 | return -EINVAL; |
02a36bc9 | 537 | |
92e068b4 ZJS |
538 | /* Do we have a truncated multi-byte character? */ |
539 | if (len > length) | |
540 | return -EINVAL; | |
541 | ||
02a36bc9 DR |
542 | /* ascii is valid */ |
543 | if (len == 1) | |
544 | return 1; | |
545 | ||
546 | /* check if expected encoded chars are available */ | |
fe96c0f8 | 547 | for (size_t i = 0; i < len; i++) |
02a36bc9 | 548 | if ((str[i] & 0x80) != 0x80) |
7e8185ef | 549 | return -EINVAL; |
02a36bc9 | 550 | |
c932fb71 SL |
551 | r = utf8_encoded_to_unichar(str, &unichar); |
552 | if (r < 0) | |
553 | return r; | |
02a36bc9 DR |
554 | |
555 | /* check if encoded length matches encoded value */ | |
7c421857 | 556 | if (utf8_unichar_to_encoded_len(unichar) != (int) len) |
7e8185ef | 557 | return -EINVAL; |
02a36bc9 DR |
558 | |
559 | /* check if value has valid range */ | |
f3ee6297 | 560 | if (!unichar_is_valid(unichar)) |
7e8185ef | 561 | return -EINVAL; |
02a36bc9 | 562 | |
7c421857 | 563 | return (int) len; |
02a36bc9 | 564 | } |
65ee8660 LP |
565 | |
566 | size_t utf8_n_codepoints(const char *str) { | |
567 | size_t n = 0; | |
568 | ||
f5fbe71d | 569 | /* Returns the number of UTF-8 codepoints in this string, or SIZE_MAX if the string is not valid UTF-8. */ |
65ee8660 LP |
570 | |
571 | while (*str != 0) { | |
572 | int k; | |
573 | ||
f5fbe71d | 574 | k = utf8_encoded_valid_unichar(str, SIZE_MAX); |
65ee8660 | 575 | if (k < 0) |
f5fbe71d | 576 | return SIZE_MAX; |
65ee8660 LP |
577 | |
578 | str += k; | |
579 | n++; | |
580 | } | |
581 | ||
582 | return n; | |
583 | } | |
3f536d5b LP |
584 | |
585 | size_t utf8_console_width(const char *str) { | |
bba55185 LP |
586 | |
587 | if (isempty(str)) | |
588 | return 0; | |
3f536d5b LP |
589 | |
590 | /* Returns the approximate width a string will take on screen when printed on a character cell | |
591 | * terminal/console. */ | |
592 | ||
bba55185 | 593 | size_t n = 0; |
da88f542 ZJS |
594 | while (*str) { |
595 | int w; | |
3f536d5b | 596 | |
da88f542 ZJS |
597 | w = utf8_char_console_width(str); |
598 | if (w < 0) | |
f5fbe71d | 599 | return SIZE_MAX; |
3f536d5b | 600 | |
da88f542 | 601 | n += w; |
3f536d5b | 602 | str = utf8_next_char(str); |
3f536d5b LP |
603 | } |
604 | ||
605 | return n; | |
606 | } | |
104a6b8c LP |
607 | |
608 | size_t utf8_last_length(const char *s, size_t n) { | |
609 | int r; | |
610 | ||
012658fc MY |
611 | assert(s); |
612 | ||
104a6b8c LP |
613 | if (n == SIZE_MAX) |
614 | n = strlen(s); | |
615 | ||
616 | /* Determines length in bytes of last UTF-8 codepoint in string. If the string is empty, returns | |
617 | * zero. Treats invalid UTF-8 codepoints as 1 sized ones. */ | |
618 | ||
619 | for (size_t last = 0;;) { | |
620 | if (n == 0) | |
621 | return last; | |
622 | ||
623 | r = utf8_encoded_valid_unichar(s, n); | |
624 | if (r <= 0) | |
625 | r = 1; /* treat invalid UTF-8 as byte-wide */ | |
626 | ||
627 | s += r; | |
628 | n -= r; | |
629 | last = r; | |
630 | } | |
631 | } |