Blender V4.5
string_utf8.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 1999 Tom Tromey
2 * SPDX-FileCopyrightText: 2000 Red Hat, Inc. All rights reserved.
3 * SPDX-FileCopyrightText: 2011 Blender Authors
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 * Code from `gutf8.c` by Tom Tromey & Red Hat, Inc. */
8
12
13#include <algorithm>
14#include <cstdio>
15#include <cstdlib>
16#include <cstring>
17#include <cwchar>
18#include <cwctype>
19#include <wcwidth.h>
20
21#include "BLI_utildefines.h"
22
23#include "BLI_string.h" /* #BLI_string_debug_size. */
24#include "BLI_string_utf8.h" /* own include */
25#ifdef WIN32
26# include "utfconv.hh"
27#endif
28#ifdef __GNUC__
29# pragma GCC diagnostic error "-Wsign-conversion"
30#endif
31
32#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
33
34/* -------------------------------------------------------------------- */
55
57{
58 if (UNLIKELY(c >= 192)) {
59 if ((c & 0xe0) == 0xc0) {
60 return 2;
61 }
62 if ((c & 0xf0) == 0xe0) {
63 return 3;
64 }
65 if ((c & 0xf8) == 0xf0) {
66 return 4;
67 }
68 if ((c & 0xfc) == 0xf8) {
69 return 5;
70 }
71 if ((c & 0xfe) == 0xfc) {
72 return 6;
73 }
74 }
75 return 1;
76}
77
79{
80 if (c < 128) {
81 return 1;
82 }
83 if ((c & 0xe0) == 0xc0) {
84 return 2;
85 }
86 if ((c & 0xf0) == 0xe0) {
87 return 3;
88 }
89 if ((c & 0xf8) == 0xf0) {
90 return 4;
91 }
92 if ((c & 0xfc) == 0xf8) {
93 return 5;
94 }
95 if ((c & 0xfe) == 0xfc) {
96 return 6;
97 }
98 return -1;
99}
100
102{
103 /* Originally from GLIB `UTF8_COMPUTE` macro. */
104 if (c < 128) {
105 *r_mask = 0x7f;
106 return 1;
107 }
108 if ((c & 0xe0) == 0xc0) {
109 *r_mask = 0x1f;
110 return 2;
111 }
112 if ((c & 0xf0) == 0xe0) {
113 *r_mask = 0x0f;
114 return 3;
115 }
116 if ((c & 0xf8) == 0xf0) {
117 *r_mask = 0x07;
118 return 4;
119 }
120 if ((c & 0xfc) == 0xf8) {
121 *r_mask = 0x03;
122 return 5;
123 }
124 if ((c & 0xfe) == 0xfc) {
125 *r_mask = 0x01;
126 return 6;
127 }
128 return -1;
129}
130
134BLI_INLINE uint utf8_char_decode(const char *p, const char mask, const int len, const uint err)
135{
136 /* Originally from GLIB `UTF8_GET` macro, added an 'err' argument. */
137 uint result = p[0] & mask;
138 for (int count = 1; count < len; count++) {
139 if ((p[count] & 0xc0) != 0x80) {
140 return err;
141 }
142 result <<= 6;
143 result |= p[count] & 0x3f;
144 }
145 return result;
146}
147
149
150ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len)
151{
152 /* NOTE(@ideasman42): from libswish3, originally called `u8_isvalid()`,
153 * modified to return the index of the bad character (byte index not UTF).
154 * http://svn.swish-e.org/libswish3/trunk/src/libswish3/utf8.c r3044.
155 *
156 * Comment from code in: `libswish3`.
157 * Based on the `valid_utf8` routine from the PCRE library by Philip Hazel
158 *
159 * length is in bytes, since without knowing whether the string is valid
160 * it's hard to know how many characters there are! */
161
162 const uchar *p, *perr, *pend = (const uchar *)str + str_len;
163 uchar c;
164 int ab;
165
166 for (p = (const uchar *)str; p < pend; p++, str_len--) {
167 c = *p;
168 perr = p; /* Erroneous char is always the first of an invalid UTF8 sequence... */
169 if (ELEM(c, 0xfe, 0xff, 0x00)) {
170 /* Those three values are not allowed in UTF8 string. */
171 goto utf8_error;
172 }
173 if (c < 128) {
174 continue;
175 }
176 if ((c & 0xc0) != 0xc0) {
177 goto utf8_error;
178 }
179
180 /* Note that since we always increase p (and decrease length) by one byte in main loop,
181 * we only add/subtract extra UTF8 bytes in code below
182 * (ab number, aka number of bytes remaining in the UTF8 sequence after the initial one). */
183 ab = utf8_char_compute_skip(c) - 1;
184 if (str_len <= size_t(ab)) {
185 goto utf8_error;
186 }
187
188 /* Check top bits in the second byte */
189 p++;
190 str_len--;
191 if ((*p & 0xc0) != 0x80) {
192 goto utf8_error;
193 }
194
195 /* Check for overlong sequences for each different length */
196 switch (ab) {
197 case 1:
198 /* Check for: `XX00 000X`. */
199 if ((c & 0x3e) == 0) {
200 goto utf8_error;
201 }
202 continue; /* We know there aren't any more bytes to check */
203
204 case 2:
205 /* Check for: `1110 0000, XX0X XXXX`. */
206 if (c == 0xe0 && (*p & 0x20) == 0) {
207 goto utf8_error;
208 }
209 /* Some special cases, see section 5 of UTF8 decoder stress-test by Markus Kuhn
210 * (https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt). */
211 /* From section 5.1 (and 5.2) */
212 if (c == 0xed) {
213 if (*p == 0xa0 && *(p + 1) == 0x80) {
214 goto utf8_error;
215 }
216 if (*p == 0xad && *(p + 1) == 0xbf) {
217 goto utf8_error;
218 }
219 if (*p == 0xae && *(p + 1) == 0x80) {
220 goto utf8_error;
221 }
222 if (*p == 0xaf && *(p + 1) == 0xbf) {
223 goto utf8_error;
224 }
225 if (*p == 0xb0 && *(p + 1) == 0x80) {
226 goto utf8_error;
227 }
228 if (*p == 0xbe && *(p + 1) == 0x80) {
229 goto utf8_error;
230 }
231 if (*p == 0xbf && *(p + 1) == 0xbf) {
232 goto utf8_error;
233 }
234 }
235 /* From section 5.3 */
236 if (c == 0xef) {
237 if (*p == 0xbf && *(p + 1) == 0xbe) {
238 goto utf8_error;
239 }
240 if (*p == 0xbf && *(p + 1) == 0xbf) {
241 goto utf8_error;
242 }
243 }
244 break;
245
246 case 3:
247 /* Check for: `1111 0000, XX00 XXXX`. */
248 if (c == 0xf0 && (*p & 0x30) == 0) {
249 goto utf8_error;
250 }
251 break;
252
253 case 4:
254 /* Check for `1111 1000, XX00 0XXX`. */
255 if (c == 0xf8 && (*p & 0x38) == 0) {
256 goto utf8_error;
257 }
258 break;
259
260 case 5:
261 /* Check for: `1111 1100, XX00 00XX`. */
262 if (c == 0xfc && (*p & 0x3c) == 0) {
263 goto utf8_error;
264 }
265 break;
266 }
267
268 /* Check for valid bytes after the 2nd, if any; all must start 10. */
269 while (--ab > 0) {
270 p++;
271 str_len--;
272 if ((*p & 0xc0) != 0x80) {
273 goto utf8_error;
274 }
275 }
276 }
277
278 return -1;
279
280utf8_error:
281
282 return ((const char *)perr - (const char *)str);
283}
284
285int BLI_str_utf8_invalid_strip(char *str, size_t str_len)
286{
287 ptrdiff_t bad_char;
288 int tot = 0;
289
290 BLI_assert(str[str_len] == '\0');
291
292 while ((bad_char = BLI_str_utf8_invalid_byte(str, str_len)) != -1) {
293 str += bad_char;
294 str_len -= size_t(bad_char + 1);
295
296 if (str_len == 0) {
297 /* last character bad, strip it */
298 *str = '\0';
299 tot++;
300 break;
301 }
302 /* strip, keep looking */
303 memmove(str, str + 1, str_len + 1); /* +1 for null char! */
304 tot++;
305 }
306
307 return tot;
308}
309
310int BLI_str_utf8_invalid_substitute(char *str, size_t str_len, const char substitute)
311{
312 BLI_assert(substitute);
313 ptrdiff_t bad_char;
314 int tot = 0;
315
316 BLI_assert(str[str_len] == '\0');
317
318 while ((bad_char = BLI_str_utf8_invalid_byte(str, str_len)) != -1) {
319 str[bad_char] = substitute;
320 bad_char += 1; /* Step over the bad character. */
321 str += bad_char;
322 str_len -= size_t(bad_char);
323 tot++;
324 }
325
326 return tot;
327}
328
330 const size_t str_len,
331 const char substitute,
332 char *buf,
333 const size_t buf_maxncpy)
334{
335 BLI_assert(str[str_len] == '\0');
336 const ptrdiff_t bad_char = BLI_str_utf8_invalid_byte(str, str_len);
337 if (LIKELY(bad_char == -1)) {
338 return str;
339 }
340 BLI_assert(bad_char >= 0);
341
342 /* In the case a bad character is outside the buffer limit,
343 * simply perform a truncating UTF8 copy into the buffer and return that. */
344 if (UNLIKELY(size_t(bad_char) >= buf_maxncpy)) {
345 BLI_strncpy_utf8(buf, str, buf_maxncpy);
346 return buf;
347 }
348
349 size_t buf_len;
350 if (str_len < buf_maxncpy) {
351 memcpy(buf, str, str_len + 1);
352 buf_len = str_len;
353 }
354 else {
355 buf_len = BLI_strncpy_rlen(buf, str, buf_maxncpy);
356 }
357
358 /* Skip the good characters. */
359 BLI_str_utf8_invalid_substitute(buf + bad_char, buf_len - size_t(bad_char), substitute);
360 return buf;
361}
362
376BLI_INLINE char *str_utf8_copy_max_bytes_impl(char *dst, const char *src, size_t dst_maxncpy)
377{
378 /* Cast to `uint8_t` is a no-op, quiets array subscript of type `char` warning.
379 * No need to check `src` points to a nil byte as this will return from the switch statement. */
380 size_t utf8_size;
381 while ((utf8_size = size_t(utf8_char_compute_skip(*src))) <= dst_maxncpy) {
382 dst_maxncpy -= utf8_size;
383 /* Prefer more compact block. */
384 /* NOLINTBEGIN: bugprone-assignment-in-if-condition */
385 /* clang-format off */
386 switch (utf8_size) {
387 case 6: if (UNLIKELY(!(*dst = *src++))) { return dst; } dst++; ATTR_FALLTHROUGH;
388 case 5: if (UNLIKELY(!(*dst = *src++))) { return dst; } dst++; ATTR_FALLTHROUGH;
389 case 4: if (UNLIKELY(!(*dst = *src++))) { return dst; } dst++; ATTR_FALLTHROUGH;
390 case 3: if (UNLIKELY(!(*dst = *src++))) { return dst; } dst++; ATTR_FALLTHROUGH;
391 case 2: if (UNLIKELY(!(*dst = *src++))) { return dst; } dst++; ATTR_FALLTHROUGH;
392 case 1: if (UNLIKELY(!(*dst = *src++))) { return dst; } dst++;
393 }
394 /* clang-format on */
395 /* NOLINTEND: bugprone-assignment-in-if-condition */
396 }
397 return dst;
398}
399
400char *BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
401{
402 BLI_assert(dst_maxncpy != 0);
403 BLI_string_debug_size(dst, dst_maxncpy);
404
405 char *dst_end = str_utf8_copy_max_bytes_impl(dst, src, dst_maxncpy - 1);
406 *dst_end = '\0';
407 return dst;
408}
409
410size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
411{
412 BLI_assert(dst_maxncpy != 0);
413 BLI_string_debug_size(dst, dst_maxncpy);
414
415 char *r_dst = dst;
416 dst = str_utf8_copy_max_bytes_impl(dst, src, dst_maxncpy - 1);
417 *dst = '\0';
418
419 return size_t(dst - r_dst);
420}
421
422size_t BLI_strncpy_utf8_rlen_unterminated(char *__restrict dst,
423 const char *__restrict src,
424 size_t dst_maxncpy)
425{
426 BLI_string_debug_size(dst, dst_maxncpy);
427
428 char *r_dst = dst;
429 dst = str_utf8_copy_max_bytes_impl(dst, src, dst_maxncpy);
430
431 return size_t(dst - r_dst);
432}
433
434/* -------------------------------------------------------------------- */
435/* wchar_t / UTF8 functions */
436
437size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst,
438 const wchar_t *__restrict src,
439 const size_t dst_maxncpy)
440{
441 BLI_assert(dst_maxncpy != 0);
442 BLI_string_debug_size(dst, dst_maxncpy);
443
444 size_t len = 0;
445 while (*src && len < dst_maxncpy) {
446 len += BLI_str_utf8_from_unicode(uint(*src++), dst + len, dst_maxncpy - len);
447 }
448 dst[len] = '\0';
449 /* Return the correct length when part of the final byte did not fit into the string. */
450 while ((len > 0) && UNLIKELY(dst[len - 1] == '\0')) {
451 len--;
452 }
453 return len;
454}
455
456size_t BLI_wstrlen_utf8(const wchar_t *src)
457{
458 size_t len = 0;
459
460 while (*src) {
462 }
463
464 return len;
465}
466
467size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes)
468{
469 size_t len = 0;
470 const char *strc_orig = strc;
471
472 while (*strc) {
473 int step = BLI_str_utf8_size_safe(strc);
474
475 /* Detect null bytes within multi-byte sequences.
476 * This matches the behavior of #BLI_strncpy_utf8 for incomplete byte sequences. */
477 for (int i = 1; i < step; i++) {
478 if (UNLIKELY(strc[i] == '\0')) {
479 step = i;
480 break;
481 }
482 }
483
484 strc += step;
485 len++;
486 }
487
488 *r_len_bytes = size_t(strc - strc_orig);
489 return len;
490}
491
492size_t BLI_strlen_utf8(const char *strc)
493{
494 size_t len_bytes;
495 return BLI_strlen_utf8_ex(strc, &len_bytes);
496}
497
498size_t BLI_strnlen_utf8_ex(const char *strc, const size_t strc_maxlen, size_t *r_len_bytes)
499{
500 size_t len = 0;
501 const char *strc_orig = strc;
502 const char *strc_end = strc + strc_maxlen;
503
504 while (*strc) {
505 int step = BLI_str_utf8_size_safe(strc);
506 if (strc + step > strc_end) {
507 break;
508 }
509
510 /* Detect null bytes within multi-byte sequences.
511 * This matches the behavior of #BLI_strncpy_utf8 for incomplete byte sequences. */
512 for (int i = 1; i < step; i++) {
513 if (UNLIKELY(strc[i] == '\0')) {
514 step = i;
515 break;
516 }
517 }
518 strc += step;
519 len++;
520 }
521
522 *r_len_bytes = size_t(strc - strc_orig);
523 return len;
524}
525
526size_t BLI_strnlen_utf8(const char *strc, const size_t strc_maxlen)
527{
528 size_t len_bytes;
529 return BLI_strnlen_utf8_ex(strc, strc_maxlen, &len_bytes);
530}
531
532size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w,
533 const char *__restrict src_c,
534 const size_t dst_w_maxncpy)
535{
536#ifdef WIN32
537 BLI_string_debug_size(dst_w, dst_w_maxncpy);
538 conv_utf_8_to_16(src_c, dst_w, dst_w_maxncpy);
539 /* NOTE: it would be more efficient to calculate the length as part of #conv_utf_8_to_16. */
540 return wcslen(dst_w);
541#else
542 return BLI_str_utf8_as_utf32((char32_t *)dst_w, src_c, dst_w_maxncpy);
543#endif
544}
545
546/* End wchar_t / UTF8 functions. */
547/* -------------------------------------------------------------------- */
548
549int BLI_wcwidth_or_error(char32_t ucs)
550{
551 /* Treat private use areas (icon fonts), symbols, and emoticons as double-width. */
552 if (ucs >= 0xf0000 || (ucs >= 0xe000 && ucs < 0xf8ff) || (ucs >= 0x1f300 && ucs < 0x1fbff)) {
553 return 2;
554 }
555 return mk_wcwidth(ucs);
556}
557
558int BLI_wcwidth_safe(char32_t ucs)
559{
560 const int columns = BLI_wcwidth_or_error(ucs);
561 if (columns >= 0) {
562 return columns;
563 }
564 return 1;
565}
566
567int BLI_wcswidth_or_error(const char32_t *pwcs, size_t n)
568{
569 return mk_wcswidth(pwcs, n);
570}
571
573{
575 if (unicode == BLI_UTF8_ERR) {
576 return -1;
577 }
578
579 return BLI_wcwidth_or_error(char32_t(unicode));
580}
581
583{
585 if (unicode == BLI_UTF8_ERR) {
586 return 1;
587 }
588
589 return BLI_wcwidth_safe(char32_t(unicode));
590}
591
592/* -------------------------------------------------------------------- */
601
602char32_t BLI_str_utf32_char_to_upper(const char32_t wc)
603{
604 if (wc < U'\xFF') { /* Latin. */
605 if ((wc <= U'z' && wc >= U'a') || (wc <= U'\xF6' && wc >= U'\xE0') ||
606 /* Correct but the first case is know, only check the second */
607 // (wc <= U'\xFE' && wc >= U'\xF8')
608 (wc >= U'\xF8'))
609 {
610 return wc - 32;
611 }
612 return wc;
613 }
614
615 if ((wc <= U'\x137' && wc >= U'\x101') || (wc <= U'\x1E95' && wc >= U'\x1E01')) {
616 /* Latin Extended. */
617 return (wc & 1) ? wc - 1 : wc;
618 }
619 if ((wc <= U'\x586' && wc >= U'\x561') || (wc <= U'\x10F5' && wc >= U'\x10D0')) {
620 /* Armenian and Georgian */
621 return wc - 48;
622 }
623 if (wc <= U'\x24E9' && wc >= U'\x24D0') { /* Enclosed Numerals. */
624 return wc - 26;
625 }
626 if (wc <= U'\xFF5A' && wc >= U'\xFF41') { /* Full-width Forms. */
627 return wc - 32;
628 }
629
630 /* There are only three remaining ranges that contain capitalization. */
631 if (!(wc <= U'\x0292' && wc >= U'\x00FF') && !(wc <= U'\x04F9' && wc >= U'\x03AC') &&
632 !(wc <= U'\x1FE1' && wc >= U'\x1E01'))
633 {
634 return wc;
635 }
636
637 static const char32_t from[] =
638 U"\x00FF\x013A\x013C\x013E\x0140\x0142\x0144\x0146\x0148\x014B\x014D\x014F\x0151\x0153\x0155"
639 U"\x0157\x0159\x015B\x015D\x015F\x0161\x0163\x0165\x0167\x0169\x016B\x016D\x016F\x0171\x0173"
640 U"\x0175\x0177\x017A\x017C\x017E\x0183\x0185\x0188\x018C\x0192\x0199\x01A1\x01A3\x01A5\x01A8"
641 U"\x01AD\x01B0\x01B4\x01B6\x01B9\x01BD\x01C6\x01C9\x01CC\x01CE\x01D0\x01D2\x01D4\x01D6\x01D8"
642 U"\x01DA\x01DC\x01DF\x01E1\x01E3\x01E5\x01E7\x01E9\x01EB\x01ED\x01EF\x01F3\x01F5\x01FB\x01FD"
643 U"\x01FF\x0201\x0203\x0205\x0207\x0209\x020B\x020D\x020F\x0211\x0213\x0215\x0217\x0253\x0254"
644 U"\x0257\x0258\x0259\x025B\x0260\x0263\x0268\x0269\x026F\x0272\x0275\x0283\x0288\x028A\x028B"
645 U"\x0292\x03AC\x03AD\x03AE\x03AF\x03B1\x03B2\x03B3\x03B4\x03B5\x03B6\x03B7\x03B8\x03B9\x03BA"
646 U"\x03BB\x03BC\x03BD\x03BE\x03BF\x03C0\x03C1\x03C3\x03C4\x03C5\x03C6\x03C7\x03C8\x03C9\x03CA"
647 U"\x03CB\x03CC\x03CD\x03CE\x03E3\x03E5\x03E7\x03E9\x03EB\x03ED\x03EF\x0430\x0431\x0432\x0433"
648 U"\x0434\x0435\x0436\x0437\x0438\x0439\x043A\x043B\x043C\x043D\x043E\x043F\x0440\x0441\x0442"
649 U"\x0443\x0444\x0445\x0446\x0447\x0448\x0449\x044A\x044B\x044C\x044D\x044E\x044F\x0451\x0452"
650 U"\x0453\x0454\x0455\x0456\x0457\x0458\x0459\x045A\x045B\x045C\x045E\x045F\x0461\x0463\x0465"
651 U"\x0467\x0469\x046B\x046D\x046F\x0471\x0473\x0475\x0477\x0479\x047B\x047D\x047F\x0481\x0491"
652 U"\x0493\x0495\x0497\x0499\x049B\x049D\x049F\x04A1\x04A3\x04A5\x04A7\x04A9\x04AB\x04AD\x04AF"
653 U"\x04B1\x04B3\x04B5\x04B7\x04B9\x04BB\x04BD\x04BF\x04C2\x04C4\x04C8\x04CC\x04D1\x04D3\x04D5"
654 U"\x04D7\x04D9\x04DB\x04DD\x04DF\x04E1\x04E3\x04E5\x04E7\x04E9\x04EB\x04EF\x04F1\x04F3\x04F5"
655 U"\x04F9\x1EA1\x1EA3\x1EA5\x1EA7\x1EA9\x1EAB\x1EAD\x1EAF\x1EB1\x1EB3\x1EB5\x1EB7\x1EB9\x1EBB"
656 U"\x1EBD\x1EBF\x1EC1\x1EC3\x1EC5\x1EC7\x1EC9\x1ECB\x1ECD\x1ECF\x1ED1\x1ED3\x1ED5\x1ED7\x1ED9"
657 U"\x1EDB\x1EDD\x1EDF\x1EE1\x1EE3\x1EE5\x1EE7\x1EE9\x1EEB\x1EED\x1EEF\x1EF1\x1EF3\x1EF5\x1EF7"
658 U"\x1EF9\x1F00\x1F01\x1F02\x1F03\x1F04\x1F05\x1F06\x1F07\x1F10\x1F11\x1F12\x1F13\x1F14\x1F15"
659 U"\x1F20\x1F21\x1F22\x1F23\x1F24\x1F25\x1F26\x1F27\x1F30\x1F31\x1F32\x1F33\x1F34\x1F35\x1F36"
660 U"\x1F37\x1F40\x1F41\x1F42\x1F43\x1F44\x1F45\x1F51\x1F53\x1F55\x1F57\x1F60\x1F61\x1F62\x1F63"
661 U"\x1F64\x1F65\x1F66\x1F67\x1F80\x1F81\x1F82\x1F83\x1F84\x1F85\x1F86\x1F87\x1F90\x1F91\x1F92"
662 U"\x1F93\x1F94\x1F95\x1F96\x1F97\x1FA0\x1FA1\x1FA2\x1FA3\x1FA4\x1FA5\x1FA6\x1FA7\x1FB0\x1FB1"
663 U"\x1FD0\x1FD1\x1FE0\x1FE1";
664 static const char32_t to[] =
665 U"\x0178\x0139\x013B\x013D\x013F\x0141\x0143\x0145\x0147\x014A\x014C\x014E\x0150\x0152\x0154"
666 U"\x0156\x0158\x015A\x015C\x015E\x0160\x0162\x0164\x0166\x0168\x016A\x016C\x016E\x0170\x0172"
667 U"\x0174\x0176\x0179\x017B\x017D\x0182\x0184\x0187\x018B\x0191\x0198\x01A0\x01A2\x01A4\x01A7"
668 U"\x01AC\x01AF\x01B3\x01B5\x01B8\x01BC\x01C4\x01C7\x01CA\x01CD\x01CF\x01D1\x01D3\x01D5\x01D7"
669 U"\x01D9\x01DB\x01DE\x01E0\x01E2\x01E4\x01E6\x01E8\x01EA\x01EC\x01EE\x01F1\x01F4\x01FA\x01FC"
670 U"\x01FE\x0200\x0202\x0204\x0206\x0208\x020A\x020C\x020E\x0210\x0212\x0214\x0216\x0181\x0186"
671 U"\x018A\x018E\x018F\x0190\x0193\x0194\x0197\x0196\x019C\x019D\x019F\x01A9\x01AE\x01B1\x01B2"
672 U"\x01B7\x0386\x0388\x0389\x038A\x0391\x0392\x0393\x0394\x0395\x0396\x0397\x0398\x0399\x039A"
673 U"\x039B\x039C\x039D\x039E\x039F\x03A0\x03A1\x03A3\x03A4\x03A5\x03A6\x03A7\x03A8\x03A9\x03AA"
674 U"\x03AB\x038C\x038E\x038F\x03E2\x03E4\x03E6\x03E8\x03EA\x03EC\x03EE\x0410\x0411\x0412\x0413"
675 U"\x0414\x0415\x0416\x0417\x0418\x0419\x041A\x041B\x041C\x041D\x041E\x041F\x0420\x0421\x0422"
676 U"\x0423\x0424\x0425\x0426\x0427\x0428\x0429\x042A\x042B\x042C\x042D\x042E\x042F\x0401\x0402"
677 U"\x0403\x0404\x0405\x0406\x0407\x0408\x0409\x040A\x040B\x040C\x040E\x040F\x0460\x0462\x0464"
678 U"\x0466\x0468\x046A\x046C\x046E\x0470\x0472\x0474\x0476\x0478\x047A\x047C\x047E\x0480\x0490"
679 U"\x0492\x0494\x0496\x0498\x049A\x049C\x049E\x04A0\x04A2\x04A4\x04A6\x04A8\x04AA\x04AC\x04AE"
680 U"\x04B0\x04B2\x04B4\x04B6\x04B8\x04BA\x04BC\x04BE\x04C1\x04C3\x04C7\x04CB\x04D0\x04D2\x04D4"
681 U"\x04D6\x04D8\x04DA\x04DC\x04DE\x04E0\x04E2\x04E4\x04E6\x04E8\x04EA\x04EE\x04F0\x04F2\x04F4"
682 U"\x04F8\x1EA0\x1EA2\x1EA4\x1EA6\x1EA8\x1EAA\x1EAC\x1EAE\x1EB0\x1EB2\x1EB4\x1EB6\x1EB8\x1EBA"
683 U"\x1EBC\x1EBE\x1EC0\x1EC2\x1EC4\x1EC6\x1EC8\x1ECA\x1ECC\x1ECE\x1ED0\x1ED2\x1ED4\x1ED6\x1ED8"
684 U"\x1EDA\x1EDC\x1EDE\x1EE0\x1EE2\x1EE4\x1EE6\x1EE8\x1EEA\x1EEC\x1EEE\x1EF0\x1EF2\x1EF4\x1EF6"
685 U"\x1EF8\x1F08\x1F09\x1F0A\x1F0B\x1F0C\x1F0D\x1F0E\x1F0F\x1F18\x1F19\x1F1A\x1F1B\x1F1C\x1F1D"
686 U"\x1F28\x1F29\x1F2A\x1F2B\x1F2C\x1F2D\x1F2E\x1F2F\x1F38\x1F39\x1F3A\x1F3B\x1F3C\x1F3D\x1F3E"
687 U"\x1F3F\x1F48\x1F49\x1F4A\x1F4B\x1F4C\x1F4D\x1F59\x1F5B\x1F5D\x1F5F\x1F68\x1F69\x1F6A\x1F6B"
688 U"\x1F6C\x1F6D\x1F6E\x1F6F\x1F88\x1F89\x1F8A\x1F8B\x1F8C\x1F8D\x1F8E\x1F8F\x1F98\x1F99\x1F9A"
689 U"\x1F9B\x1F9C\x1F9D\x1F9E\x1F9F\x1FA8\x1FA9\x1FAA\x1FAB\x1FAC\x1FAD\x1FAE\x1FAF\x1FB8\x1FB9"
690 U"\x1FD8\x1FD9\x1FE8\x1FE9";
691
692 if (wc >= from[0] && wc <= from[ARRAY_SIZE(from) - 2]) {
693 /* Binary search since these are sorted. */
694 size_t min = 0;
695 size_t max = ARRAY_SIZE(from) - 2;
696 while (max >= min) {
697 const size_t mid = (min + max) / 2;
698 if (wc > from[mid]) {
699 min = mid + 1;
700 }
701 else if (wc < from[mid]) {
702 max = mid - 1;
703 }
704 else {
705 return to[mid];
706 }
707 }
708 }
709
710 return wc;
711}
712
713char32_t BLI_str_utf32_char_to_lower(const char32_t wc)
714{
715 if (wc < U'\xD8') { /* Latin. */
716 if ((wc <= U'Z' && wc >= U'A') || (wc <= U'\xD6' && wc >= U'\xC0')) {
717 return wc + 32;
718 }
719 return wc;
720 }
721 if ((wc <= U'\x136' && wc >= U'\x100') || (wc <= U'\x1E94' && wc >= U'\x1E00')) {
722 /* Latin Extended. */
723 return (wc % 2 == 0) ? wc + 1 : wc;
724 }
725 if ((wc <= U'\x556' && wc >= U'\x531') || (wc <= U'\x10C5' && wc >= U'\x10A0')) {
726 /* Armenian and Georgian. */
727 return wc + 48;
728 }
729 if (wc <= U'\x24CF' && wc >= U'\x24B6') { /* Enclosed Numerals. */
730 return wc + 26;
731 }
732 if (wc <= U'\xFF3A' && wc >= U'\xFF21') { /* Full-width Forms. */
733 return wc + 32;
734 }
735
736 /* There are only three remaining ranges that contain capitalization. */
737 if (!(wc <= U'\x0216' && wc >= U'\x00D8') && !(wc <= U'\x04F8' && wc >= U'\x0386') &&
738 !(wc <= U'\x1FE9' && wc >= U'\x1E00'))
739 {
740 return wc;
741 }
742
743 static const char32_t from[] =
744 U"\x00D8\x00D9\x00DA\x00DB\x00DC\x00DD\x00DE\x0139\x013B\x013D\x013F\x0141\x0143\x0145\x0147"
745 U"\x014A\x014C\x014E\x0150\x0152\x0154\x0156\x0158\x015A\x015C\x015E\x0160\x0162\x0164\x0166"
746 U"\x0168\x016A\x016C\x016E\x0170\x0172\x0174\x0176\x0178\x0179\x017B\x017D\x0181\x0182\x0184"
747 U"\x0186\x0187\x018A\x018B\x018E\x018F\x0190\x0191\x0193\x0194\x0196\x0197\x0198\x019C\x019D"
748 U"\x019F\x01A0\x01A2\x01A4\x01A7\x01A9\x01AC\x01AE\x01AF\x01B1\x01B2\x01B3\x01B5\x01B7\x01B8"
749 U"\x01BC\x01C4\x01C5\x01C7\x01C8\x01CA\x01CB\x01CD\x01CF\x01D1\x01D3\x01D5\x01D7\x01D9\x01DB"
750 U"\x01DE\x01E0\x01E2\x01E4\x01E6\x01E8\x01EA\x01EC\x01EE\x01F1\x01F4\x01FA\x01FC\x01FE\x0200"
751 U"\x0202\x0204\x0206\x0208\x020A\x020C\x020E\x0210\x0212\x0214\x0216\x0386\x0388\x0389\x038A"
752 U"\x038C\x038E\x038F\x0391\x0392\x0393\x0394\x0395\x0396\x0397\x0398\x0399\x039A\x039B\x039C"
753 U"\x039D\x039E\x039F\x03A0\x03A1\x03A3\x03A4\x03A5\x03A6\x03A7\x03A8\x03A9\x03AA\x03AB\x03E2"
754 U"\x03E4\x03E6\x03E8\x03EA\x03EC\x03EE\x0401\x0402\x0403\x0404\x0405\x0406\x0407\x0408\x0409"
755 U"\x040A\x040B\x040C\x040E\x040F\x0410\x0411\x0412\x0413\x0414\x0415\x0416\x0417\x0418\x0419"
756 U"\x041A\x041B\x041C\x041D\x041E\x041F\x0420\x0421\x0422\x0423\x0424\x0425\x0426\x0427\x0428"
757 U"\x0429\x042A\x042B\x042C\x042D\x042E\x042F\x0460\x0462\x0464\x0466\x0468\x046A\x046C\x046E"
758 U"\x0470\x0472\x0474\x0476\x0478\x047A\x047C\x047E\x0480\x0490\x0492\x0494\x0496\x0498\x049A"
759 U"\x049C\x049E\x04A0\x04A2\x04A4\x04A6\x04A8\x04AA\x04AC\x04AE\x04B0\x04B2\x04B4\x04B6\x04B8"
760 U"\x04BA\x04BC\x04BE\x04C1\x04C3\x04C7\x04CB\x04D0\x04D2\x04D4\x04D6\x04D8\x04DA\x04DC\x04DE"
761 U"\x04E0\x04E2\x04E4\x04E6\x04E8\x04EA\x04EE\x04F0\x04F2\x04F4\x04F8\x1EA0\x1EA2\x1EA4\x1EA6"
762 U"\x1EA8\x1EAA\x1EAC\x1EAE\x1EB0\x1EB2\x1EB4\x1EB6\x1EB8\x1EBA\x1EBC\x1EBE\x1EC0\x1EC2\x1EC4"
763 U"\x1EC6\x1EC8\x1ECA\x1ECC\x1ECE\x1ED0\x1ED2\x1ED4\x1ED6\x1ED8\x1EDA\x1EDC\x1EDE\x1EE0\x1EE2"
764 U"\x1EE4\x1EE6\x1EE8\x1EEA\x1EEC\x1EEE\x1EF0\x1EF2\x1EF4\x1EF6\x1EF8\x1F08\x1F09\x1F0A\x1F0B"
765 U"\x1F0C\x1F0D\x1F0E\x1F0F\x1F18\x1F19\x1F1A\x1F1B\x1F1C\x1F1D\x1F28\x1F29\x1F2A\x1F2B\x1F2C"
766 U"\x1F2D\x1F2E\x1F2F\x1F38\x1F39\x1F3A\x1F3B\x1F3C\x1F3D\x1F3E\x1F3F\x1F48\x1F49\x1F4A\x1F4B"
767 U"\x1F4C\x1F4D\x1F59\x1F5B\x1F5D\x1F5F\x1F68\x1F69\x1F6A\x1F6B\x1F6C\x1F6D\x1F6E\x1F6F\x1F88"
768 U"\x1F89\x1F8A\x1F8B\x1F8C\x1F8D\x1F8E\x1F8F\x1F98\x1F99\x1F9A\x1F9B\x1F9C\x1F9D\x1F9E\x1F9F"
769 U"\x1FA8\x1FA9\x1FAA\x1FAB\x1FAC\x1FAD\x1FAE\x1FAF\x1FB8\x1FB9\x1FD8\x1FD9\x1FE8\x1FE9";
770 static const char32_t to[] =
771 U"\x00F8\x00F9\x00FA\x00FB\x00FC\x00FD\x00FE\x013A\x013C\x013E\x0140\x0142\x0144\x0146\x0148"
772 U"\x014B\x014D\x014F\x0151\x0153\x0155\x0157\x0159\x015B\x015D\x015F\x0161\x0163\x0165\x0167"
773 U"\x0169\x016B\x016D\x016F\x0171\x0173\x0175\x0177\x00FF\x017A\x017C\x017E\x0253\x0183\x0185"
774 U"\x0254\x0188\x0257\x018C\x0258\x0259\x025B\x0192\x0260\x0263\x0269\x0268\x0199\x026f\x0272"
775 U"\x0275\x01A1\x01A3\x01A5\x01A8\x0283\x01AD\x0288\x01B0\x028A\x028B\x01B4\x01B6\x0292\x01B9"
776 U"\x01BD\x01C6\x01C6\x01C9\x01C9\x01CC\x01CC\x01CE\x01D0\x01D2\x01D4\x01D6\x01D8\x01DA\x01DC"
777 U"\x01DF\x01E1\x01E3\x01E5\x01E7\x01E9\x01EB\x01ED\x01EF\x01F3\x01F5\x01FB\x01FD\x01FF\x0201"
778 U"\x0203\x0205\x0207\x0209\x020B\x020D\x020F\x0211\x0213\x0215\x0217\x03AC\x03AD\x03AE\x03AF"
779 U"\x03CC\x03CD\x03CE\x03B1\x03B2\x03B3\x03B4\x03B5\x03B6\x03B7\x03B8\x03B9\x03BA\x03BB\x03BC"
780 U"\x03BD\x03BE\x03BF\x03C0\x03C1\x03C3\x03C4\x03C5\x03C6\x03C7\x03C8\x03C9\x03CA\x03CB\x03E3"
781 U"\x03E5\x03E7\x03E9\x03EB\x03ED\x03EF\x0451\x0452\x0453\x0454\x0455\x0456\x0457\x0458\x0459"
782 U"\x045A\x045B\x045C\x045E\x045F\x0430\x0431\x0432\x0433\x0434\x0435\x0436\x0437\x0438\x0439"
783 U"\x043A\x043B\x043C\x043D\x043E\x043F\x0440\x0441\x0442\x0443\x0444\x0445\x0446\x0447\x0448"
784 U"\x0449\x044A\x044B\x044C\x044D\x044E\x044F\x0461\x0463\x0465\x0467\x0469\x046B\x046D\x046F"
785 U"\x0471\x0473\x0475\x0477\x0479\x047B\x047D\x047F\x0481\x0491\x0493\x0495\x0497\x0499\x049B"
786 U"\x049D\x049F\x04A1\x04A3\x04A5\x04A7\x04A9\x04AB\x04AD\x04AF\x04B1\x04B3\x04B5\x04B7\x04B9"
787 U"\x04BB\x04BD\x04BF\x04C2\x04C4\x04C8\x04CC\x04D1\x04D3\x04D5\x04D7\x04D9\x04DB\x04DD\x04DF"
788 U"\x04E1\x04E3\x04E5\x04E7\x04E9\x04EB\x04EF\x04F1\x04F3\x04F5\x04F9\x1EA1\x1EA3\x1EA5\x1EA7"
789 U"\x1EA9\x1EAB\x1EAD\x1EAF\x1EB1\x1EB3\x1EB5\x1EB7\x1EB9\x1EBB\x1EBD\x1EBF\x1EC1\x1EC3\x1EC5"
790 U"\x1EC7\x1EC9\x1ECB\x1ECD\x1ECF\x1ED1\x1ED3\x1ED5\x1ED7\x1ED9\x1EDB\x1EDD\x1EDF\x1EE1\x1EE3"
791 U"\x1EE5\x1EE7\x1EE9\x1EEB\x1EED\x1EEF\x1EF1\x1EF3\x1EF5\x1EF7\x1EF9\x1F00\x1F01\x1F02\x1F03"
792 U"\x1F04\x1F05\x1F06\x1F07\x1F10\x1F11\x1F12\x1F13\x1F14\x1F15\x1F20\x1F21\x1F22\x1F23\x1F24"
793 U"\x1F25\x1F26\x1F27\x1F30\x1F31\x1F32\x1F33\x1F34\x1F35\x1F36\x1F37\x1F40\x1F41\x1F42\x1F43"
794 U"\x1F44\x1F45\x1F51\x1F53\x1F55\x1F57\x1F60\x1F61\x1F62\x1F63\x1F64\x1F65\x1F66\x1F67\x1F80"
795 U"\x1F81\x1F82\x1F83\x1F84\x1F85\x1F86\x1F87\x1F90\x1F91\x1F92\x1F93\x1F94\x1F95\x1F96\x1F97"
796 U"\x1FA0\x1FA1\x1FA2\x1FA3\x1FA4\x1FA5\x1FA6\x1FA7\x1FB0\x1FB1\x1FD0\x1FD1\x1FE0\x1FE1";
797
798 if (wc >= from[0] && wc <= from[ARRAY_SIZE(from) - 2]) {
799 /* Binary search since these are sorted. */
800 size_t min = 0;
801 size_t max = ARRAY_SIZE(from) - 2;
802 while (max >= min) {
803 const size_t mid = (min + max) / 2;
804 if (wc > from[mid]) {
805 min = mid + 1;
806 }
807 else if (wc < from[mid]) {
808 max = mid - 1;
809 }
810 else {
811 return to[mid];
812 }
813 }
814 }
815
816 return wc;
817}
818
819/* -------------------------------------------------------------------- */
825
827{
828 /* Invisible (and so can be removed at end of wrapped line) spacing characters
829 * according to the Unicode Line Breaking Algorithm (Standard Annex #14). Note
830 * to always ignore U+200B (zero-width space) and U+2060 (word joiner). */
831 return ELEM(codepoint,
832 ' ', /* Space. */
833 0x1680, /* Ogham space mark. */
834 0x2000, /* En quad. */
835 0x2001, /* Em quad. */
836 0x2002, /* En space. */
837 0x2003, /* Em space. */
838 0x2004, /* Three-per-em space. */
839 0x2005, /* Four-per-em space. */
840 0x2006, /* Six-per-em space. */
841 0x2008, /* Punctuation space. */
842 0x2009, /* Thin space. */
843 0x200A, /* Hair space. */
844 0x205F, /* Medium mathematical space. */
845 0x3000); /* Ideographic space. */
846}
847
848bool BLI_str_utf32_char_is_optional_break_after(char32_t codepoint, char32_t codepoint_prev)
849{
850 /* Subset of the characters that are line breaking opportunities
851 * according to the Unicode Line Breaking Algorithm (Standard Annex #14).
852 * Can be expanded but please no rules that differ by language. */
853
854 /* Punctuation. Backslash can be used as path separator */
855 if (ELEM(codepoint, '\\', '_')) {
856 return true;
857 }
858
859 /* Do not break on solidus if previous is a number. */
860 if (codepoint == '/' && !(codepoint_prev >= '0' && codepoint_prev <= '9')) {
861 return true;
862 }
863
864 /* Do not break on dash, hyphen, em dash if previous is space */
865 if (ELEM(codepoint, '-', 0x2010, 0x2014) &&
867 {
868 return true;
869 }
870
871 if ((codepoint >= 0x2E80 && codepoint <= 0x2FFF) || /* CJK, Kangxi Radicals. */
872 (codepoint >= 0x3040 && codepoint <= 0x309F) || /* Hiragana (except small characters). */
873 (codepoint >= 0x30A2 && codepoint <= 0x30FA) || /* Katakana (except small characters). */
874 (codepoint >= 0x3400 && codepoint <= 0x4DBF) || /* CJK Unified Ideographs Extension A. */
875 (codepoint >= 0x4E00 && codepoint <= 0x9FFF) || /* CJK Unified Ideographs. */
876 (codepoint >= 0x3040 && codepoint <= 0x309F) || /* CJK Unified Ideographs. */
877 (codepoint >= 0x3130 && codepoint <= 0x318F)) /* Hangul Compatibility Jamo. */
878 {
879 return true;
880 }
881
882 if (ELEM(codepoint, 0x0F0D, 0x0F0B)) {
883 return true; /* Tibetan shad mark and intersyllabic tsheg. */
884 }
885
886 return false;
887}
888
889bool BLI_str_utf32_char_is_optional_break_before(char32_t codepoint, char32_t codepoint_prev)
890{
891 /* Do not break on any of these if a space follows. */
893 return false;
894 }
895
896 /* Infix Numeric Separators. Allow break on these if not numbers afterward. */
897 if (ELEM(codepoint_prev,
898 ',', /* Comma. */
899 ':', /* Colon. */
900 ';', /* Semicolon. */
901 0x037E, /* Greek question mark. */
902 0x0589, /* Armenian full stop. */
903 0x060C, /* Arabic comma. */
904 0x060D, /* Arabic date separator. */
905 0x07F8, /* N'Ko comma. */
906 0x2044) /* Fraction slash. */
907 && !(codepoint >= '0' && codepoint <= '9'))
908 {
909 return true;
910 }
911
912 /* Break on full stop only if not followed by another, or by a number. */
913 if (codepoint_prev == '.' && codepoint != '.' && !(codepoint >= '0' && codepoint <= '9')) {
914 return true;
915 }
916
917 /* Close punctuation. */
918 if (ELEM(codepoint_prev,
919 0x3001, /* Ideographic comma. */
920 0x3002, /* Ideographic full stop. */
921 0xFE10, /* Presentation form for vertical ideographic comma. */
922 0xFE11, /* Presentation form for vertical ideographic full stop. */
923 0xFE12, /* Presentation form for vertical ideographic colon. */
924 0xFE50, /* Small comma. */
925 0xFE52, /* Small full stop. */
926 0xFF0C, /* Full-width comma. */
927 0xFF0E, /* Full-width full stop. */
928 0XFF61, /* Half-width ideographic full stop. */
929 0Xff64)) /* Half-width ideographic comma. */
930 {
931 return true;
932 }
933
934 /* Exclamation/Interrogation. */
935 if (ELEM(codepoint_prev,
936 '!', /* Exclamation mark. */
937 '?', /* Question mark. */
938 0x05C6, /* Hebrew punctuation `maqaf`. */
939 0x061B, /* Arabic semicolon. */
940 0x061E, /* Arabic triple dot. */
941 0x061F, /* Arabic question mark. */
942 0x06D4, /* Arabic full stop. */
943 0x07F9, /* N'Ko question mark. */
944 0x0F0D, /* Tibetan shad mark. */
945 0xFF01, /* Full-width exclamation mark. */
946 0xff1f)) /* full-width question mark. */
947 {
948 return true;
949 }
950
951 return false;
952}
953 /* -------------------------------------------------------------------- */
955
957{
959}
960
961int BLI_str_utf8_size_safe(const char *p)
962{
963 return utf8_char_compute_skip(*p);
964}
965
967{
968 /* Originally `g_utf8_get_char` in GLIB. */
969
970 const uchar c = uchar(*p);
971
972 char mask = 0;
974 if (UNLIKELY(len == -1)) {
975 return BLI_UTF8_ERR;
976 }
978}
979
981{
983 if (UNLIKELY(result == BLI_UTF8_ERR)) {
984 return *p;
985 }
986 return result;
987}
988
990 const size_t p_len,
991 size_t *__restrict index)
992{
993 const uchar c = uchar(*(p += *index));
994
995 BLI_assert(*index < p_len);
996 BLI_assert(c != '\0');
997
998 char mask = 0;
1000 if (UNLIKELY(len == -1) || (*index + size_t(len) > p_len)) {
1001 return BLI_UTF8_ERR;
1002 }
1003
1005 if (UNLIKELY(result == BLI_UTF8_ERR)) {
1006 return BLI_UTF8_ERR;
1007 }
1008 *index += size_t(len);
1009 BLI_assert(*index <= p_len);
1010 return result;
1011}
1012
1014 const size_t p_len,
1015 size_t *__restrict index)
1016{
1018 if (UNLIKELY(result == BLI_UTF8_ERR)) {
1019 result = uint(p[*index]);
1020 *index += 1;
1021 }
1022 BLI_assert(*index <= p_len);
1023 return result;
1024}
1025
1026/* was g_unichar_to_utf8 */
1027
1028#define UTF8_VARS_FROM_CHAR32(Char, First, Len) \
1029 if (Char < 0x80) { \
1030 First = 0; \
1031 Len = 1; \
1032 } \
1033 else if (Char < 0x800) { \
1034 First = 0xc0; \
1035 Len = 2; \
1036 } \
1037 else if (Char < 0x10000) { \
1038 First = 0xe0; \
1039 Len = 3; \
1040 } \
1041 else if (Char < 0x200000) { \
1042 First = 0xf0; \
1043 Len = 4; \
1044 } \
1045 else if (Char < 0x4000000) { \
1046 First = 0xf8; \
1047 Len = 5; \
1048 } \
1049 else { \
1050 First = 0xfc; \
1051 Len = 6; \
1052 } \
1053 (void)0
1054
1056{
1057 /* If this gets modified, also update the copy in g_string_insert_unichar() */
1058 uint len = 0;
1059 uint first;
1060
1061 UTF8_VARS_FROM_CHAR32(c, first, len);
1062 (void)first;
1063
1064 return len;
1065}
1066
1067size_t BLI_str_utf8_from_unicode(uint c, char *dst, const size_t dst_maxncpy)
1068
1069{
1070 BLI_string_debug_size(dst, dst_maxncpy);
1071
1072 /* If this gets modified, also update the copy in g_string_insert_unichar() */
1073 uint len = 0;
1074 uint first;
1075
1076 UTF8_VARS_FROM_CHAR32(c, first, len);
1077
1078 if (UNLIKELY(dst_maxncpy < len)) {
1079 /* Null terminate instead of writing a partial byte. */
1080 memset(dst, 0x0, dst_maxncpy);
1081 return dst_maxncpy;
1082 }
1083
1084 for (uint i = len - 1; i > 0; i--) {
1085 dst[i] = char((c & 0x3f) | 0x80);
1086 c >>= 6;
1087 }
1088 dst[0] = char(c | first);
1089
1090 return len;
1091}
1092
1093size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w,
1094 const char *__restrict src_c,
1095 const size_t dst_w_maxncpy)
1096{
1097 BLI_assert(dst_w_maxncpy != 0);
1098 BLI_string_debug_size(dst_w, dst_w_maxncpy);
1099
1100 const size_t maxlen = dst_w_maxncpy - 1;
1101 size_t len = 0;
1102
1103 const size_t src_c_len = strlen(src_c);
1104 const char *src_c_end = src_c + src_c_len;
1105 size_t index = 0;
1106 while ((index < src_c_len) && (len != maxlen)) {
1107 const uint unicode = BLI_str_utf8_as_unicode_step_or_error(src_c, src_c_len, &index);
1108 if (unicode != BLI_UTF8_ERR) {
1109 *dst_w = unicode;
1110 }
1111 else {
1112 *dst_w = '?';
1113 const char *src_c_next = BLI_str_find_next_char_utf8(src_c + index, src_c_end);
1114 index = size_t(src_c_next - src_c);
1115 }
1116 dst_w++;
1117 len++;
1118 }
1119
1120 *dst_w = 0;
1121
1122 return len;
1123}
1124
1125size_t BLI_str_utf32_as_utf8(char *__restrict dst,
1126 const char32_t *__restrict src,
1127 const size_t dst_maxncpy)
1128{
1129 BLI_assert(dst_maxncpy != 0);
1130 BLI_string_debug_size(dst, dst_maxncpy);
1131
1132 size_t len = 0;
1133 while (*src && len < dst_maxncpy) {
1134 len += BLI_str_utf8_from_unicode(uint(*src++), dst + len, dst_maxncpy - len);
1135 }
1136 dst[len] = '\0';
1137 /* Return the correct length when part of the final byte did not fit into the string. */
1138 while ((len > 0) && UNLIKELY(dst[len - 1] == '\0')) {
1139 len--;
1140 }
1141 return len;
1142}
1143
1144size_t BLI_str_utf32_as_utf8_len_ex(const char32_t *src, const size_t src_maxlen)
1145{
1146 size_t len = 0;
1147 const char32_t *src_end = src + src_maxlen;
1148
1149 while ((src < src_end) && *src) {
1151 }
1152
1153 return len;
1154}
1155
1156size_t BLI_str_utf32_as_utf8_len(const char32_t *src)
1157{
1158 size_t len = 0;
1159
1160 while (*src) {
1162 }
1163
1164 return len;
1165}
1166
1167const char *BLI_str_find_prev_char_utf8(const char *p, const char *str_start)
1168{
1169 /* Originally `g_utf8_find_prev_char` in GLIB. */
1170
1171 BLI_assert(p >= str_start);
1172 if (str_start < p) {
1173 for (--p; p >= str_start; p--) {
1174 if ((*p & 0xc0) != 0x80) {
1175 return (char *)p;
1176 }
1177 }
1178 }
1179 return p;
1180}
1181
1182const char *BLI_str_find_next_char_utf8(const char *p, const char *str_end)
1183{
1184 /* Originally `g_utf8_find_next_char` in GLIB. */
1185
1186 BLI_assert(p <= str_end);
1187 if ((p < str_end) && (*p != '\0')) {
1188 for (++p; p < str_end && (*p & 0xc0) == 0x80; p++) {
1189 /* do nothing */
1190 }
1191 }
1192 return p;
1193}
1194
1195size_t BLI_str_partition_utf8(const char *str,
1196 const uint delim[],
1197 const char **r_sep,
1198 const char **r_suf)
1199{
1200 return BLI_str_partition_ex_utf8(str, nullptr, delim, r_sep, r_suf, false);
1201}
1202
1203size_t BLI_str_rpartition_utf8(const char *str,
1204 const uint delim[],
1205 const char **r_sep,
1206 const char **r_suf)
1207{
1208 return BLI_str_partition_ex_utf8(str, nullptr, delim, r_sep, r_suf, true);
1209}
1210
1212 const char *end,
1213 const uint delim[],
1214 const char **r_sep,
1215 const char **r_suf,
1216 const bool from_right)
1217{
1218 const size_t str_len = end ? size_t(end - str) : strlen(str);
1219 if (end == nullptr) {
1220 end = str + str_len;
1221 }
1222
1223 /* Note that here, we assume end points to a valid UTF8 char! */
1225
1226 char *suf = (char *)(str + str_len);
1227 size_t index = 0;
1228 for (char *sep = (char *)(from_right ? BLI_str_find_prev_char_utf8(end, str) : str);
1229 from_right ? (sep > str) : ((sep < end) && (*sep != '\0'));
1230 sep = (char *)(from_right ? (str != sep ? BLI_str_find_prev_char_utf8(sep, str) : nullptr) :
1231 str + index))
1232 {
1233 size_t index_ofs = 0;
1234 const uint c = BLI_str_utf8_as_unicode_step_or_error(sep, size_t(end - sep), &index_ofs);
1235 if (UNLIKELY(c == BLI_UTF8_ERR)) {
1236 break;
1237 }
1238 index += index_ofs;
1239
1240 for (const uint *d = delim; *d != '\0'; d++) {
1241 if (*d == c) {
1242 /* `suf` is already correct in case from_right is true. */
1243 *r_sep = sep;
1244 *r_suf = from_right ? suf : (char *)(str + index);
1245 return size_t(sep - str);
1246 }
1247 }
1248
1249 suf = sep; /* Useful in 'from_right' case! */
1250 }
1251
1252 *r_suf = *r_sep = nullptr;
1253 return str_len;
1254}
1255
1256bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size)
1257{
1258 BLI_assert(str_size > 0);
1259 if (std::memchr(str, '\0', str_size)) {
1260 return false;
1261 }
1262
1263 size_t str_len_trim;
1264 BLI_strnlen_utf8_ex(str, str_size - 1, &str_len_trim);
1265 str[str_len_trim] = '\0';
1266 return true;
1267}
1268
1269/* -------------------------------------------------------------------- */
1278
1279int BLI_str_utf8_offset_to_index(const char *str, const size_t str_len, const int offset_target)
1280{
1281 BLI_assert(offset_target >= 0);
1282 const size_t offset_target_as_size = size_t(offset_target);
1283 size_t offset = 0;
1284 int index = 0;
1285 /* Note that `offset != offset_target_as_size` works for valid UTF8 strings. */
1286 while ((offset < str_len) && (offset < offset_target_as_size)) {
1287 /* Use instead of #BLI_str_utf8_size_safe to match behavior when limiting the string length. */
1288 const uint code = BLI_str_utf8_as_unicode_step_safe(str, str_len, &offset);
1289 UNUSED_VARS(code);
1290 index++;
1291 BLI_assert(offset <= offset_target_as_size); /* See DOXY section comment. */
1292 }
1293 return index;
1294}
1295
1296int BLI_str_utf8_offset_from_index(const char *str, const size_t str_len, const int index_target)
1297{
1298 BLI_assert(index_target >= 0);
1299 size_t offset = 0;
1300 int index = 0;
1301 while ((offset < str_len) && (index < index_target)) {
1302 /* Use instead of #BLI_str_utf8_size_safe to match behavior when limiting the string length. */
1303 const uint code = BLI_str_utf8_as_unicode_step_safe(str, str_len, &offset);
1304 UNUSED_VARS(code);
1305 index++;
1306 }
1307 return int(offset);
1308}
1309
1310int BLI_str_utf8_offset_to_column(const char *str, const size_t str_len, const int offset_target)
1311{
1312 BLI_assert(offset_target >= 0);
1313 const size_t offset_target_clamp = std::min(size_t(offset_target), str_len);
1314 size_t offset = 0;
1315 int column = 0;
1316 while (offset < offset_target_clamp) {
1317 const uint code = BLI_str_utf8_as_unicode_step_safe(str, str_len, &offset);
1318 column += BLI_wcwidth_safe(code);
1319 BLI_assert(offset <= size_t(offset_target)); /* See DOXY section comment. */
1320 }
1321 return column;
1322}
1323
1324int BLI_str_utf8_offset_from_column(const char *str, const size_t str_len, const int column_target)
1325{
1326 size_t offset = 0, offset_next = 0;
1327 int column = 0;
1328 while ((offset < str_len) && (column < column_target)) {
1329 const uint code = BLI_str_utf8_as_unicode_step_safe(str, str_len, &offset_next);
1330 column += BLI_wcwidth_safe(code);
1331 if (column > column_target) {
1332 break;
1333 }
1334 offset = offset_next;
1335 }
1336 return int(offset);
1337}
1338
1340 const size_t str_len,
1341 const int offset_target,
1342 const int tab_width)
1343{
1344 BLI_assert(offset_target >= 0);
1345 const size_t offset_target_clamp = std::min(size_t(offset_target), str_len);
1346 size_t offset = 0;
1347 int column = 0;
1348 while (offset < offset_target_clamp) {
1349 const uint code = BLI_str_utf8_as_unicode_step_safe(str, str_len, &offset);
1350 /* The following line is the only change compared with #BLI_str_utf8_offset_to_column. */
1351 column += (code == '\t') ? (tab_width - (column % tab_width)) : BLI_wcwidth_safe(code);
1352 BLI_assert(offset <= size_t(offset_target)); /* See DOXY section comment. */
1353 }
1354 return column;
1355}
1356
1358 const size_t str_len,
1359 const int column_target,
1360 const int tab_width)
1361{
1362 size_t offset = 0, offset_next = 0;
1363 int column = 0;
1364 while ((offset < str_len) && (column < column_target)) {
1365 const uint code = BLI_str_utf8_as_unicode_step_safe(str, str_len, &offset_next);
1366 /* The following line is the only change compared with #BLI_str_utf8_offset_from_column. */
1367 column += (code == '\t') ? (tab_width - (column % tab_width)) : BLI_wcwidth_safe(code);
1368 if (column > column_target) {
1369 break;
1370 }
1371 offset = offset_next;
1372 }
1373 return int(offset);
1374}
1375
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ATTR_FALLTHROUGH
#define BLI_INLINE
#define BLI_string_debug_size(str, str_maxncpy)
Definition BLI_string.h:671
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
#define BLI_UTF8_ERR
unsigned char uchar
unsigned int uint
#define ARRAY_SIZE(arr)
#define UNUSED_VARS(...)
#define UNLIKELY(x)
#define ELEM(...)
#define LIKELY(x)
#define U
#define str(s)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
int count
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define min(a, b)
Definition sort.cc:36
BLI_INLINE int utf8_char_compute_skip_or_error(const char c)
bool BLI_str_utf32_char_is_breaking_space(char32_t codepoint)
uint BLI_str_utf8_as_unicode_or_error(const char *p)
bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size)
int BLI_wcswidth_or_error(const char32_t *pwcs, size_t n)
int BLI_str_utf8_offset_from_column(const char *str, const size_t str_len, const int column_target)
int BLI_str_utf8_char_width_or_error(const char *p)
BLI_INLINE int utf8_char_compute_skip_or_error_with_mask(const char c, char *r_mask)
size_t BLI_str_utf8_from_unicode_len(const uint c)
BLI_INLINE int utf8_char_compute_skip(const char c)
size_t BLI_str_partition_ex_utf8(const char *str, const char *end, const uint delim[], const char **r_sep, const char **r_suf, const bool from_right)
size_t BLI_strlen_utf8_ex(const char *strc, size_t *r_len_bytes)
bool BLI_str_utf32_char_is_optional_break_before(char32_t codepoint, char32_t codepoint_prev)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
size_t BLI_strncpy_utf8_rlen_unterminated(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
size_t BLI_strlen_utf8(const char *strc)
int BLI_str_utf8_invalid_strip(char *str, size_t str_len)
char32_t BLI_str_utf32_char_to_lower(const char32_t wc)
#define UTF8_VARS_FROM_CHAR32(Char, First, Len)
size_t BLI_strnlen_utf8(const char *strc, const size_t strc_maxlen)
size_t BLI_str_utf32_as_utf8_len(const char32_t *src)
char32_t BLI_str_utf32_char_to_upper(const char32_t wc)
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len)
uint BLI_str_utf8_as_unicode_safe(const char *p)
BLI_INLINE uint utf8_char_decode(const char *p, const char mask, const int len, const uint err)
size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst, const wchar_t *__restrict src, const size_t dst_maxncpy)
size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w, const char *__restrict src_c, const size_t dst_w_maxncpy)
const char * BLI_str_find_next_char_utf8(const char *p, const char *str_end)
size_t BLI_str_utf32_as_utf8_len_ex(const char32_t *src, const size_t src_maxlen)
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start)
const char * BLI_str_utf8_invalid_substitute_as_needed(const char *str, const size_t str_len, const char substitute, char *buf, const size_t buf_maxncpy)
size_t BLI_wstrlen_utf8(const wchar_t *src)
int BLI_wcwidth_safe(char32_t ucs)
int BLI_str_utf8_offset_to_index(const char *str, const size_t str_len, const int offset_target)
int BLI_str_utf8_offset_to_column_with_tabs(const char *str, const size_t str_len, const int offset_target, const int tab_width)
size_t BLI_str_utf32_as_utf8(char *__restrict dst, const char32_t *__restrict src, const size_t dst_maxncpy)
int BLI_str_utf8_offset_to_column(const char *str, const size_t str_len, const int offset_target)
BLI_INLINE char * str_utf8_copy_max_bytes_impl(char *dst, const char *src, size_t dst_maxncpy)
uint BLI_str_utf8_as_unicode_step_safe(const char *__restrict p, const size_t p_len, size_t *__restrict index)
bool BLI_str_utf32_char_is_optional_break_after(char32_t codepoint, char32_t codepoint_prev)
size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w, const char *__restrict src_c, const size_t dst_w_maxncpy)
size_t BLI_str_utf8_from_unicode(uint c, char *dst, const size_t dst_maxncpy)
int BLI_str_utf8_size_safe(const char *p)
size_t BLI_str_rpartition_utf8(const char *str, const uint delim[], const char **r_sep, const char **r_suf)
int BLI_wcwidth_or_error(char32_t ucs)
uint BLI_str_utf8_as_unicode_step_or_error(const char *__restrict p, const size_t p_len, size_t *__restrict index)
size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
size_t BLI_str_partition_utf8(const char *str, const uint delim[], const char **r_sep, const char **r_suf)
int BLI_str_utf8_offset_from_index(const char *str, const size_t str_len, const int index_target)
int BLI_str_utf8_char_width_safe(const char *p)
int BLI_str_utf8_size_or_error(const char *p)
int BLI_str_utf8_offset_from_column_with_tabs(const char *str, const size_t str_len, const int column_target, const int tab_width)
size_t BLI_strnlen_utf8_ex(const char *strc, const size_t strc_maxlen, size_t *r_len_bytes)
int BLI_str_utf8_invalid_substitute(char *str, size_t str_len, const char substitute)
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
Definition utfconv.cc:182
uint len