5#include "testing/testing.h"
54#define STR_MB_ALPHA_1 "\x41"
55#define STR_MB_ALPHA_2 "\xc2\xaa"
56#define STR_MB_ALPHA_3 "\xe0\xa0\x80"
57#define STR_MB_ALPHA_4 "\xf0\x90\x80\x80"
63#define STR_MB_ALPHA_5 "\xf8\x80\x80\x80\x80"
64#define STR_MB_ALPHA_6 "\xfc\x80\x80\x80\x80\x80"
81 {
"You should see the Greek word 'kosme': \"\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\" |",
82 "You should see the Greek word 'kosme': \"\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\" |",
"\x00"},
88 {
"2.1.1 1 byte (U-00000000): \"\x00\" |",
89 "2.1.1 1 byte (U-00000000): \"\" |",
"\x01"},
90 {
"2.1.2 2 bytes (U-00000080): \"\xc2\x80\" |",
91 "2.1.2 2 bytes (U-00000080): \"\xc2\x80\" |",
"\x00"},
92 {
"2.1.3 3 bytes (U-00000800): \"\xe0\xa0\x80\" |",
93 "2.1.3 3 bytes (U-00000800): \"\xe0\xa0\x80\" |",
"\x00"},
94 {
"2.1.4 4 bytes (U-00010000): \"\xf0\x90\x80\x80\" |",
95 "2.1.4 4 bytes (U-00010000): \"\xf0\x90\x80\x80\" |",
"\x00"},
96 {
"2.1.5 5 bytes (U-00200000): \"\xf8\x88\x80\x80\x80\" |",
97 "2.1.5 5 bytes (U-00200000): \"\xf8\x88\x80\x80\x80\" |",
"\x00"},
98 {
"2.1.6 6 bytes (U-04000000): \"\xfc\x84\x80\x80\x80\x80\" |",
99 "2.1.6 6 bytes (U-04000000): \"\xfc\x84\x80\x80\x80\x80\" |",
"\x00"},
101 {
"2.2.1 1 byte (U-0000007F): \"\x7f\" |",
102 "2.2.1 1 byte (U-0000007F): \"\x7f\" |",
"\x00"},
103 {
"2.2.2 2 bytes (U-000007FF): \"\xdf\xbf\" |",
104 "2.2.2 2 bytes (U-000007FF): \"\xdf\xbf\" |",
"\x00"},
105 {
"2.2.3 3 bytes (U-0000FFFF): \"\xef\xbf\xbf\" |",
106 "2.2.3 3 bytes (U-0000FFFF): \"\" |",
"\x03"},
107 {
"2.2.4 4 bytes (U-001FFFFF): \"\xf7\xbf\xbf\xbf\" |",
108 "2.2.4 4 bytes (U-001FFFFF): \"\xf7\xbf\xbf\xbf\" |",
"\x00"},
109 {
"2.2.5 5 bytes (U-03FFFFFF): \"\xfb\xbf\xbf\xbf\xbf\" |",
110 "2.2.5 5 bytes (U-03FFFFFF): \"\xfb\xbf\xbf\xbf\xbf\" |",
"\x00"},
111 {
"2.2.6 6 bytes (U-7FFFFFFF): \"\xfd\xbf\xbf\xbf\xbf\xbf\" |",
112 "2.2.6 6 bytes (U-7FFFFFFF): \"\xfd\xbf\xbf\xbf\xbf\xbf\" |",
"\x00"},
114 {
"2.3.1 U-0000D7FF = ed 9f bf = \"\xed\x9f\xbf\" |",
115 "2.3.1 U-0000D7FF = ed 9f bf = \"\xed\x9f\xbf\" |",
"\x00"},
116 {
"2.3.2 U-0000E000 = ee 80 80 = \"\xee\x80\x80\" |",
117 "2.3.2 U-0000E000 = ee 80 80 = \"\xee\x80\x80\" |",
"\x00"},
118 {
"2.3.3 U-0000FFFD = ef bf bd = \"\xef\xbf\xbd\" |",
119 "2.3.3 U-0000FFFD = ef bf bd = \"\xef\xbf\xbd\" |",
"\x00"},
120 {
"2.3.4 U-0010FFFF = f4 8f bf bf = \"\xf4\x8f\xbf\xbf\" |",
121 "2.3.4 U-0010FFFF = f4 8f bf bf = \"\xf4\x8f\xbf\xbf\" |",
"\x00"},
122 {
"2.3.5 U-00110000 = f4 90 80 80 = \"\xf4\x90\x80\x80\" |",
123 "2.3.5 U-00110000 = f4 90 80 80 = \"\xf4\x90\x80\x80\" |",
"\x00"},
128 {
"3.1.1 First continuation byte 0x80: \"\x80\" |",
129 "3.1.1 First continuation byte 0x80: \"\" |",
"\x01"},
130 {
"3.1.2 Last continuation byte 0xbf: \"\xbf\" |",
131 "3.1.2 Last continuation byte 0xbf: \"\" |",
"\x01"},
132 {
"3.1.3 2 continuation bytes: \"\x80\xbf\" |",
133 "3.1.3 2 continuation bytes: \"\" |",
"\x02"},
134 {
"3.1.4 3 continuation bytes: \"\x80\xbf\x80\" |",
135 "3.1.4 3 continuation bytes: \"\" |",
"\x03"},
136 {
"3.1.5 4 continuation bytes: \"\x80\xbf\x80\xbf\" |",
137 "3.1.5 4 continuation bytes: \"\" |",
"\x04"},
138 {
"3.1.6 5 continuation bytes: \"\x80\xbf\x80\xbf\x80\" |",
139 "3.1.6 5 continuation bytes: \"\" |",
"\x05"},
140 {
"3.1.7 6 continuation bytes: \"\x80\xbf\x80\xbf\x80\xbf\" |",
141 "3.1.7 6 continuation bytes: \"\" |",
"\x06"},
142 {
"3.1.8 7 continuation bytes: \"\x80\xbf\x80\xbf\x80\xbf\x80\" |",
143 "3.1.8 7 continuation bytes: \"\" |",
"\x07"},
145 {
"3.1.9 \"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
146 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
147 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
148 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\" |",
149 "3.1.9 \"\" |",
"\x40"},
152 {
"3.2.1 \"\xc0 \xc1 \xc2 \xc3 \xc4 \xc5 \xc6 \xc7 \xc8 \xc9 \xca \xcb \xcc \xcd \xce \xcf "
153 "\xd0 \xd1 \xd2 \xd3 \xd4 \xd5 \xd6 \xd7 \xd8 \xd9 \xda \xdb \xdc \xdd \xde \xdf \" |",
154 "3.2.1 \" \" |",
"\x20"},
156 {
"3.2.2 \"\xe0 \xe1 \xe2 \xe3 \xe4 \xe5 \xe6 \xe7 \xe8 \xe9 \xea \xeb \xec \xed \xee \xef \" |",
157 "3.2.2 \" \" |",
"\x10"},
159 {
"3.2.3 \"\xf0 \xf1 \xf2 \xf3 \xf4 \xf5 \xf6 \xf7 \" |",
160 "3.2.3 \" \" |",
"\x08"},
162 {
"3.2.4 \"\xf8 \xf9 \xfa \xfb \" |",
163 "3.2.4 \" \" |",
"\x04"},
165 {
"3.2.4 \"\xfc \xfd \" |",
166 "3.2.4 \" \" |",
"\x02"},
171 {
"3.3.1 2-byte sequence with last byte missing (U+0000): \"\xc0\" |",
172 "3.3.1 2-byte sequence with last byte missing (U+0000): \"\" |",
"\x01"},
173 {
"3.3.2 3-byte sequence with last byte missing (U+0000): \"\xe0\x80\" |",
174 "3.3.2 3-byte sequence with last byte missing (U+0000): \"\" |",
"\x02"},
175 {
"3.3.3 4-byte sequence with last byte missing (U+0000): \"\xf0\x80\x80\" |",
176 "3.3.3 4-byte sequence with last byte missing (U+0000): \"\" |",
"\x03"},
177 {
"3.3.4 5-byte sequence with last byte missing (U+0000): \"\xf8\x80\x80\x80\" |",
178 "3.3.4 5-byte sequence with last byte missing (U+0000): \"\" |",
"\x04"},
179 {
"3.3.5 6-byte sequence with last byte missing (U+0000): \"\xfc\x80\x80\x80\x80\" |",
180 "3.3.5 6-byte sequence with last byte missing (U+0000): \"\" |",
"\x05"},
181 {
"3.3.6 2-byte sequence with last byte missing (U-000007FF): \"\xdf\" |",
182 "3.3.6 2-byte sequence with last byte missing (U-000007FF): \"\" |",
"\x01"},
183 {
"3.3.7 3-byte sequence with last byte missing (U-0000FFFF): \"\xef\xbf\" |",
184 "3.3.7 3-byte sequence with last byte missing (U-0000FFFF): \"\" |",
"\x02"},
185 {
"3.3.8 4-byte sequence with last byte missing (U-001FFFFF): \"\xf7\xbf\xbf\" |",
186 "3.3.8 4-byte sequence with last byte missing (U-001FFFFF): \"\" |",
"\x03"},
187 {
"3.3.9 5-byte sequence with last byte missing (U-03FFFFFF): \"\xfb\xbf\xbf\xbf\" |",
188 "3.3.9 5-byte sequence with last byte missing (U-03FFFFFF): \"\" |",
"\x04"},
189 {
"3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF): \"\xfd\xbf\xbf\xbf\xbf\" |",
190 "3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF): \"\" |",
"\x05"},
193 {
"3.4 \"\xc0\xe0\x80\xf0\x80\x80\xf8\x80\x80\x80\xfc\x80\x80\x80\x80"
194 "\xdf\xef\xbf\xf7\xbf\xbf\xfb\xbf\xbf\xbf\xfd\xbf\xbf\xbf\xbf\""
196 "3.4 \"\" |",
"\x1e"},
199 {
"3.5.1 fe = \"\xfe\" |",
200 "3.5.1 fe = \"\" |",
"\x01"},
201 {
"3.5.2 ff = \"\xff\" |",
202 "3.5.2 ff = \"\" |",
"\x01"},
203 {
"3.5.3 fe fe ff ff = \"\xfe\xfe\xff\xff\" |",
204 "3.5.3 fe fe ff ff = \"\" |",
"\x04"},
224 {
"4.1.1 U+002F = c0 af = \"\xc0\xaf\" |",
225 "4.1.1 U+002F = c0 af = \"\" |",
"\x02"},
226 {
"4.1.2 U+002F = e0 80 af = \"\xe0\x80\xaf\" |",
227 "4.1.2 U+002F = e0 80 af = \"\" |",
"\x03"},
228 {
"4.1.3 U+002F = f0 80 80 af = \"\xf0\x80\x80\xaf\" |",
229 "4.1.3 U+002F = f0 80 80 af = \"\" |",
"\x04"},
230 {
"4.1.4 U+002F = f8 80 80 80 af = \"\xf8\x80\x80\x80\xaf\" |",
231 "4.1.4 U+002F = f8 80 80 80 af = \"\" |",
"\x05"},
232 {
"4.1.5 U+002F = fc 80 80 80 80 af = \"\xfc\x80\x80\x80\x80\xaf\" |",
233 "4.1.5 U+002F = fc 80 80 80 80 af = \"\" |",
"\x06"},
238 {
"4.2.1 U-0000007F = c1 bf = \"\xc1\xbf\" |",
239 "4.2.1 U-0000007F = c1 bf = \"\" |",
"\x02"},
240 {
"4.2.2 U-000007FF = e0 9f bf = \"\xe0\x9f\xbf\" |",
241 "4.2.2 U-000007FF = e0 9f bf = \"\" |",
"\x03"},
242 {
"4.2.3 U-0000FFFF = f0 8f bf bf = \"\xf0\x8f\xbf\xbf\" |",
243 "4.2.3 U-0000FFFF = f0 8f bf bf = \"\" |",
"\x04"},
244 {
"4.2.4 U-001FFFFF = f8 87 bf bf bf = \"\xf8\x87\xbf\xbf\xbf\" |",
245 "4.2.4 U-001FFFFF = f8 87 bf bf bf = \"\" |",
"\x05"},
246 {
"4.2.5 U+0000 = fc 83 bf bf bf bf = \"\xfc\x83\xbf\xbf\xbf\xbf\" |",
247 "4.2.5 U+0000 = fc 83 bf bf bf bf = \"\" |",
"\x06"},
251 {
"4.3.1 U+0000 = c0 80 = \"\xc0\x80\" |",
252 "4.3.1 U+0000 = c0 80 = \"\" |",
"\x02"},
253 {
"4.3.2 U+0000 = e0 80 80 = \"\xe0\x80\x80\" |",
254 "4.3.2 U+0000 = e0 80 80 = \"\" |",
"\x03"},
255 {
"4.3.3 U+0000 = f0 80 80 80 = \"\xf0\x80\x80\x80\" |",
256 "4.3.3 U+0000 = f0 80 80 80 = \"\" |",
"\x04"},
257 {
"4.3.4 U+0000 = f8 80 80 80 80 = \"\xf8\x80\x80\x80\x80\" |",
258 "4.3.4 U+0000 = f8 80 80 80 80 = \"\" |",
"\x05"},
259 {
"4.3.5 U+0000 = fc 80 80 80 80 80 = \"\xfc\x80\x80\x80\x80\x80\" |",
260 "4.3.5 U+0000 = fc 80 80 80 80 80 = \"\" |",
"\x06"},
267 {
"5.1.1 U+D800 = ed a0 80 = \"\xed\xa0\x80\" |",
268 "5.1.1 U+D800 = ed a0 80 = \"\" |",
"\x03"},
269 {
"5.1.2 U+DB7F = ed ad bf = \"\xed\xad\xbf\" |",
270 "5.1.2 U+DB7F = ed ad bf = \"\" |",
"\x03"},
271 {
"5.1.3 U+DB80 = ed ae 80 = \"\xed\xae\x80\" |",
272 "5.1.3 U+DB80 = ed ae 80 = \"\" |",
"\x03"},
273 {
"5.1.4 U+DBFF = ed af bf = \"\xed\xaf\xbf\" |",
274 "5.1.4 U+DBFF = ed af bf = \"\" |",
"\x03"},
275 {
"5.1.5 U+DC00 = ed b0 80 = \"\xed\xb0\x80\" |",
276 "5.1.5 U+DC00 = ed b0 80 = \"\" |",
"\x03"},
277 {
"5.1.6 U+DF80 = ed be 80 = \"\xed\xbe\x80\" |",
278 "5.1.6 U+DF80 = ed be 80 = \"\" |",
"\x03"},
279 {
"5.1.7 U+DFFF = ed bf bf = \"\xed\xbf\xbf\" |",
280 "5.1.7 U+DFFF = ed bf bf = \"\" |",
"\x03"},
282 {
"5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80 = \"\xed\xa0\x80\xed\xb0\x80\" |",
283 "5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80 = \"\" |",
"\x06"},
284 {
"5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf = \"\xed\xa0\x80\xed\xbf\xbf\" |",
285 "5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf = \"\" |",
"\x06"},
286 {
"5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80 = \"\xed\xad\xbf\xed\xb0\x80\" |",
287 "5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80 = \"\" |",
"\x06"},
288 {
"5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf = \"\xed\xad\xbf\xed\xbf\xbf\" |",
289 "5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf = \"\" |",
"\x06"},
290 {
"5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80 = \"\xed\xae\x80\xed\xb0\x80\" |",
291 "5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80 = \"\" |",
"\x06"},
292 {
"5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf = \"\xed\xae\x80\xed\xbf\xbf\" |",
293 "5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf = \"\" |",
"\x06"},
294 {
"5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80 = \"\xed\xaf\xbf\xed\xb0\x80\" |",
295 "5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80 = \"\" |",
"\x06"},
296 {
"5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf = \"\xed\xaf\xbf\xed\xbf\xbf\" |",
297 "5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf = \"\" |",
"\x06"},
312 {
"5.3.1 U+FFFE = ef bf be = \"\xef\xbf\xbe\" |",
313 "5.3.1 U+FFFE = ef bf be = \"\" |",
"\x03"},
314 {
"5.3.2 U+FFFF = ef bf bf = \"\xef\xbf\xbf\" |",
315 "5.3.2 U+FFFF = ef bf bf = \"\" |",
"\x03"},
319 {
nullptr,
nullptr,
nullptr},
324TEST(
string, Utf8InvalidBytesStrip)
332 memcpy(buff, tst,
sizeof(buff));
336 printf(
"[%02d] -> [%02d] \"%s\" -> \"%s\"\n", errors_num, errors_found_num, tst, buff);
338 EXPECT_STREQ(buff, tst_stripped);
343TEST(
string, Utf8InvalidBytesSubstitute)
350 memcpy(buff, tst,
sizeof(buff));
356 EXPECT_EQ(strlen(buff),
sizeof(buff) - 1);
360TEST(
string, Utf8InvalidBytesSubstitutePatterns)
362#define TEST_SIMPLE(src_chars, expected_error_count, expected_str) \
364 char buff[] = src_chars; \
365 EXPECT_EQ(BLI_str_utf8_invalid_substitute(buff, strlen(buff), '?'), expected_error_count); \
366 EXPECT_STREQ(buff, expected_str); \
370#define ARRAY_ARG(...) __VA_ARGS__
402TEST(
string, StringNLenUTF8_NoNil)
405 const char str[] = {
'A',
'B',
'C'};
411TEST(
string, StringNLenUTF8_Incomplete)
413 const char *ref_str =
416 const size_t ref_str_len = 21;
418#define EXPECT_BYTE_OFFSET(truncate_ofs, expect_nchars) \
420 size_t buf_ofs = 0; \
421 STRNCPY(buf, ref_str); \
422 buf[truncate_ofs] = '\0'; \
423 EXPECT_EQ(BLI_strnlen_utf8_ex(buf, ref_str_len, &buf_ofs), expect_nchars); \
424 EXPECT_EQ(buf_ofs, truncate_ofs); \
455#undef EXPECT_BYTE_OFFSET
466#define STRNCPY_UTF8_ASCII(...) \
468 const char src[] = {__VA_ARGS__, 0}; \
469 char dst[sizeof(src)]; \
470 memset(dst, 0xff, sizeof(dst)); \
471 STRNCPY_UTF8(dst, src); \
472 EXPECT_EQ(strlen(dst), sizeof(dst) - 1); \
473 EXPECT_STREQ(dst, src); \
479#undef STRNCPY_UTF8_ASCII
482TEST(
string, StrCopyUTF8_ASCII_Truncate)
484#define STRNCPY_UTF8_ASCII_TRUNCATE(maxncpy, ...) \
486 char src[] = {__VA_ARGS__}; \
487 char dst[sizeof(src)]; \
488 memset(dst, 0xff, sizeof(dst)); \
489 BLI_strncpy_utf8(dst, src, maxncpy); \
490 int len_expect = std::min<int>(sizeof(src), maxncpy) - 1; \
491 src[len_expect] = '\0'; \
492 EXPECT_EQ(strlen(dst), len_expect); \
493 EXPECT_STREQ(dst, src); \
499#undef STRNCPY_UTF8_ASCII_TRUNCATE
502TEST(
string, StrCopyUTF8_TruncateEncoding)
505#define STRNCPY_UTF8_TRUNCATE(byte_size, ...) \
507 const char src[] = {__VA_ARGS__, 0}; \
508 EXPECT_EQ(BLI_str_utf8_size_or_error(src), byte_size); \
509 char dst[sizeof(src)]; \
510 memset(dst, 0xff, sizeof(dst)); \
511 STRNCPY_UTF8(dst, src); \
512 EXPECT_EQ(strlen(dst), sizeof(dst) - 1); \
513 EXPECT_STREQ(dst, src); \
514 BLI_strncpy_utf8(dst, src, sizeof(dst) - 1); \
515 EXPECT_STREQ(dst, ""); \
525#undef STRNCPY_UTF8_TRUNCATE
528TEST(
string, StrCopyUTF8_TruncateEncodingMulti)
530#define STRNCPY_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy) \
532 char dst[dst_maxncpy + 1]; \
533 dst[dst_maxncpy] = 0xff; \
534 size_t len = BLI_strncpy_utf8_rlen(dst, src, dst_maxncpy); \
535 EXPECT_EQ(len, strlen(dst)); \
536 EXPECT_STREQ(dst, dst_expect); \
537 EXPECT_EQ(dst[dst_maxncpy], 0xff); \
625#undef STRNCPY_UTF8_TRUNC_EXPECT
628TEST(
string, StrCopyUTF8_TerminateEncodingEarly)
632#define STRNCPY_UTF8_TERMINATE_EARLY(byte_size, ...) \
634 char src[] = {__VA_ARGS__, 0}; \
635 EXPECT_EQ(BLI_str_utf8_size_or_error(src), byte_size); \
636 char dst[sizeof(src)]; \
637 memset(dst, 0xff, sizeof(dst)); \
638 STRNCPY_UTF8(dst, src); \
639 EXPECT_EQ(strlen(dst), sizeof(dst) - 1); \
640 EXPECT_STREQ(dst, src); \
641 for (int i = sizeof(dst) - 1; i > 1; i--) { \
643 memset(dst, 0xff, sizeof(dst)); \
644 const int dst_copied = STRNCPY_UTF8_RLEN(dst, src); \
645 EXPECT_STREQ(dst, src); \
646 EXPECT_EQ(strlen(dst), i); \
647 EXPECT_EQ(dst_copied, i); \
658#undef STRNCPY_UTF8_TERMINATE_EARLY
667TEST(
string, StrUTF32AsUTF8_Truncate)
669#define STR_UTF32_AS_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy) \
671 char dst[dst_maxncpy + 1]; \
672 dst[dst_maxncpy] = 0xff; \
673 size_t len = BLI_str_utf32_as_utf8(dst, src, dst_maxncpy); \
674 EXPECT_EQ(len, strlen(dst)); \
675 EXPECT_STREQ(dst, dst_expect); \
676 EXPECT_EQ(dst[dst_maxncpy], 0xff); \
699#undef STR_UTF32_AS_UTF8_TRUNC_EXPECT
708TEST(
string, StrCopyWCharAsUTF8_Truncate)
711#define STRNCPY_WCHAR_AS_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy) \
713 char dst[dst_maxncpy + 1]; \
714 dst[dst_maxncpy] = 0xff; \
715 size_t len = BLI_strncpy_wchar_as_utf8(dst, src, dst_maxncpy); \
716 EXPECT_EQ(len, strlen(dst)); \
717 EXPECT_STREQ(dst, dst_expect); \
718 EXPECT_EQ(dst[dst_maxncpy], 0xff); \
735#undef STRNCPY_WCHAR_AS_UTF8_TRUNC_EXPECT
744TEST(
string, StrPrintfUTF8_ASCII)
746#define SNPRINTF_UTF8_ASCII(...) \
748 const char src[] = {__VA_ARGS__, 0}; \
749 char dst[sizeof(src)]; \
750 memset(dst, 0xff, sizeof(dst)); \
751 SNPRINTF_UTF8(dst, "%s", src); \
752 EXPECT_EQ(strlen(dst), sizeof(dst) - 1); \
753 EXPECT_STREQ(dst, src); \
759#undef SNPRINTF_UTF8_ASCII
762TEST(
string, StrPrintfUTF8_TerminateEncodingEarly)
766#define SNPRINTF_UTF8_TERMINATE_EARLY(byte_size, ...) \
768 char src[] = {__VA_ARGS__, 0}; \
769 EXPECT_EQ(BLI_str_utf8_size_or_error(src), byte_size); \
770 char dst[sizeof(src)]; \
771 memset(dst, 0xff, sizeof(dst)); \
772 SNPRINTF_UTF8(dst, "%s", src); \
773 EXPECT_EQ(strlen(dst), sizeof(dst) - 1); \
774 EXPECT_STREQ(dst, src); \
775 for (int i = sizeof(dst) - 1; i > 1; i--) { \
777 memset(dst, 0xff, sizeof(dst)); \
778 const int dst_copied = SNPRINTF_UTF8_RLEN(dst, "%s", src); \
779 EXPECT_STREQ(dst, src); \
780 EXPECT_EQ(strlen(dst), i); \
781 EXPECT_EQ(dst_copied, i); \
792#undef STRNCPY_UTF8_TERMINATE_EARLY
795TEST(
string, StrPrintfUTF8_TruncateEncodingMulti)
797#define SNPRINTF_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy) \
799 char dst[dst_maxncpy + 1]; \
800 dst[dst_maxncpy] = 0xff; \
801 size_t len = BLI_snprintf_utf8_rlen(dst, dst_maxncpy, "%s", src); \
802 EXPECT_EQ(len, strlen(dst)); \
803 EXPECT_STREQ(dst, dst_expect); \
804 EXPECT_EQ(dst[dst_maxncpy], 0xff); \
841#undef STRNCPY_UTF8_TRUNC_EXPECT
850TEST(
string, Utf8OffsetFromIndex_ClampedIndex)
854 const char *test_strings[] = {
864 for (
int i = 0; i <
ARRAY_SIZE(test_strings); i++) {
865 const char *
str = test_strings[i];
866 const size_t str_len = strlen(
str);
881TEST(
string, StrFindPrevCharUTF8)
892TEST(
string, StrFindPrevCharUTF8_ContinuationOnly)
895 const char str[] = {char(0x80), char(0x80),
'A', 0};
908 size_t i = 0, result_len = 0;
909 while ((i < str_len) && (
str[i] !=
'\0')) {
915template<
size_t Size,
size_t SizeWithPadding>
918 char utf8_src_with_pad[SizeWithPadding] = {0};
920 memcpy(utf8_src_with_pad, utf8_src, Size);
922 char32_t unicode_dst_a[Size], unicode_dst_b[Size];
924 memset(unicode_dst_a, 0xff,
sizeof(unicode_dst_a));
925 const size_t index_a =
utf8_as_char32(utf8_src, Size, unicode_dst_a);
929 for (
int pass = 0; pass < 2; pass++) {
930 memset(unicode_dst_b, 0xff,
sizeof(unicode_dst_b));
932 utf8_src_with_pad, pass ? Size : SizeWithPadding, unicode_dst_b);
935 EXPECT_EQ_ARRAY(unicode_dst_a, unicode_dst_b, Size);
955 for (
int i = 0; i <= 0xff; i++) {
956 memset(utf8_src, i,
sizeof(utf8_src));
961 for (
int mul = 1; mul < 256; mul *= 2) {
962 for (
int ofs = 1; ofs < int(Size); ofs++) {
963 utf8_src[ofs] = char(i + (ofs * mul));
967 for (
int ofs = 1; ofs < int(Size); ofs++) {
968 utf8_src[ofs] = char(i - (ofs * mul));
977 for (
int i = 0; i < 256; i++) {
1008TEST(
string, StrCursorStepNextUtf32Empty)
1010 const char32_t empty[] =
U"";
1011 const size_t len = 0;
1024TEST(
string, StrCursorStepNextUtf32Single)
1027 const char32_t single[] =
U"0";
1028 const size_t len = 1;
1040TEST(
string, StrCursorStepNextUtf32Simple)
1042 const char32_t simple[] =
U"012";
1043 const size_t len = 3;
1058TEST(
string, StrCursorStepNextUtf32AllCombining)
1060 const char32_t allcombining[] =
U"\u0300\u0300\u0300";
1061 const size_t len = 3;
1078TEST(
string, StrCursorStepNextUtf32Complex)
1081 const char32_t complex[] =
U"\u0300\u0041\u0300\u0320\u0042";
1082 const size_t len = 5;
1103TEST(
string, StrCursorStepNextUtf32Invalid)
1106 const char32_t invalid[] =
U"\u00C0\u0300\u0009\u0300\u000D\u0300\u000A\u0300";
1107 const size_t len = 8;
1134TEST(
string, StrCursorStepPrevUtf32Empty)
1136 const char32_t empty[] =
U"";
1137 const size_t len = 0;
1148TEST(
string, StrCursorStepPrevUtf32Single)
1150 const char32_t single[] =
U"0";
1151 const size_t len = 1;
1163TEST(
string, StrCursorStepPrevUtf32Simple)
1165 const char32_t simple[] =
U"012";
1166 const size_t len = 3;
1180TEST(
string, StrCursorStepPrevUtf32AllCombining)
1182 const char32_t allcombining[] =
U"\u0300\u0300\u0300";
1183 const size_t len = 3;
1200TEST(
string, StrCursorStepPrevUtf32Complex)
1203 const char32_t complex[] =
U"\u0300\u0041\u0300\u0320\u0042";
1204 const size_t len = 5;
1225TEST(
string, StrCursorStepPrevUtf32Invalid)
1228 const char32_t invalid[] =
U"\u00C0\u0300\u0009\u0300\u000D\u0300\u000A\u0300";
1229 const size_t len = 8;
1255TEST(
string, StrCursorStepNextUtf8Empty)
1257 const char empty[] =
"";
1258 const size_t len = 0;
1270TEST(
string, StrCursorStepNextUtf8Single)
1272 const char single[] =
"0";
1273 const size_t len = 1;
1285TEST(
string, StrCursorStepNextUtf8Simple)
1287 const char simple[] =
"012";
1288 const size_t len = 3;
1303TEST(
string, StrCursorStepNextUtf8AllCombining)
1305 const char allcombining[] =
"\xCC\x80\xCC\x80\xCC\x80";
1306 const size_t len = 6;
1329TEST(
string, StrCursorStepNextUtf8AllComplex)
1332 const char complex[] =
"\xCC\x80\x41\xC2\xA9\xCC\x80\xCC\xA0\x42";
1333 const size_t len = 10;
1364TEST(
string, StrCursorStepNextUtf8Invalid)
1367 const char invalid[] =
"\xC0\xCC\x80\x09\x0D\x0A\xCC\x80";
1368 const size_t len = 8;
1395TEST(
string, StrCursorStepPrevUtf8Empty)
1397 const char empty[] =
"";
1398 const size_t len = 0;
1411TEST(
string, StrCursorStepPrevUtf8Single)
1413 const char single[] =
"0";
1414 const size_t len = 1;
1426TEST(
string, StrCursorStepPrevUtf8Simple)
1428 const char simple[] =
"012";
1429 const size_t len = 3;
1443TEST(
string, StrCursorStepPrevUtf8AllCombining)
1445 const char allcombining[] =
"\xCC\x80\xCC\x80\xCC\x80";
1446 const size_t len = 6;
1469TEST(
string, StrCursorStepPrevUtf8Complex)
1472 const char complex[] =
"\xCC\x80\x41\xC2\xA9\xCC\x80\xCC\xA0\x42";
1473 const size_t len = 10;
1504TEST(
string, StrCursorStepPrevUtf8Invalid)
1507 const char invalid[] =
"\xC0\xCC\x80\x09\x0D\x0A\xCC\x80";
1508 const size_t len = 8;
#define SNPRINTF_UTF8_ASCII(...)
#define STRNCPY_UTF8_TRUNCATE(byte_size,...)
#define SNPRINTF_UTF8_TERMINATE_EARLY(byte_size,...)
#define STR_UTF32_AS_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy)
#define EXPECT_BYTE_OFFSET(truncate_ofs, expect_nchars)
#define STRNCPY_UTF8_ASCII_TRUNCATE(maxncpy,...)
#define STRNCPY_UTF8_ASCII(...)
#define STRNCPY_UTF8_TERMINATE_EARLY(byte_size,...)
#define STRNCPY_WCHAR_AS_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy)
#define SNPRINTF_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy)
#define STRNCPY_UTF8_TRUNC_EXPECT(src, dst_expect, dst_maxncpy)
#define TEST_SIMPLE(src_chars, expected_error_count, expected_str)
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
static const char * utf8_invalid_tests[][3]
void utf8_as_char32_test_compare(const char utf8_src[Size])
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
bool BLI_str_cursor_step_next_utf8(const char *str, int str_maxlen, int *pos)
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static size_t utf8_as_char32(const char *str, const char str_len, char32_t *r_result)
unsigned int BLI_str_utf8_as_unicode_step_safe(const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
struct RNG * BLI_rng_new(unsigned int seed)
void utf8_as_char32_test_compare_with_pad_bytes(const char utf8_src[Size])
void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len) ATTR_NONNULL(1
int BLI_str_utf8_invalid_substitute(char *str, size_t str_len, const char substitute) ATTR_NONNULL(1)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool BLI_str_cursor_step_prev_utf8(const char *str, int str_maxlen, int *pos)
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
bool BLI_str_cursor_step_next_utf32(const char32_t *str, int str_maxlen, int *pos)
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void utf8_as_char32_test_at_buffer_size()
bool BLI_str_cursor_step_prev_utf32(const char32_t *str, int str_maxlen, int *pos)
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes) ATTR_NONNULL(1