Blender V4.3
BLI_string_utf8_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "BLI_rand.h"
8#include "BLI_string.h"
10#include "BLI_string_utf8.h"
11#include "BLI_utildefines.h"
12
13/* Note that 'common' utf-8 variants of string functions (like copy, etc.) are tested in
14 * BLI_string_test.cc However, tests below are specific utf-8 conformance ones, and since they eat
15 * quite their share of lines, they deserved their own file. */
16
52#define STR_MB_ALPHA_1 "\x41"
53#define STR_MB_ALPHA_2 "\xc2\xaa"
54#define STR_MB_ALPHA_3 "\xe0\xa0\x80"
55#define STR_MB_ALPHA_4 "\xf0\x90\x80\x80"
56
57/* These don't decode into valid code-points and wont work in all UTF8 functions.
58 * Use them for functions which support up to #BLI_UTF8_MAX, where failure to test
59 * 5 & 6 byte sequences would cause test coverage to be incomplete.
60 * See https://stackoverflow.com/a/35027139 for details. */
61#define STR_MB_ALPHA_5 "\xf8\x80\x80\x80\x80"
62#define STR_MB_ALPHA_6 "\xfc\x80\x80\x80\x80\x80"
63
64/* -------------------------------------------------------------------- */
68/* Breaking strings is confusing here, prefer over-long lines. */
69/* clang-format off */
70
71/* Each test is made of a 79 bytes (80 with null char) string to test, expected string result after
72 * stripping invalid utf8 bytes, and a single-byte string encoded with expected number of errors.
73 *
74 * Based on utf-8 decoder stress-test (https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt)
75 * by Markus Kuhn <http://www.cl.cam.ac.uk/~mgk25/> - 2015-08-28 - CC BY 4.0
76 */
77static const char *utf8_invalid_tests[][3] = {
78/* 1 Some correct UTF-8 text. */
79 {"You should see the Greek word 'kosme': \"\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\" |",
80 "You should see the Greek word 'kosme': \"\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\" |", "\x00"},
81
82/* 2 Boundary condition test cases
83 * Note that those will pass for us, those are not erroneous unicode code points
84 * (aside from \x00, which is only valid as string terminator).
85 * 2.1 First possible sequence of a certain length */
86 {"2.1.1 1 byte (U-00000000): \"\x00\" |",
87 "2.1.1 1 byte (U-00000000): \"\" |", "\x01"},
88 {"2.1.2 2 bytes (U-00000080): \"\xc2\x80\" |",
89 "2.1.2 2 bytes (U-00000080): \"\xc2\x80\" |", "\x00"},
90 {"2.1.3 3 bytes (U-00000800): \"\xe0\xa0\x80\" |",
91 "2.1.3 3 bytes (U-00000800): \"\xe0\xa0\x80\" |", "\x00"},
92 {"2.1.4 4 bytes (U-00010000): \"\xf0\x90\x80\x80\" |",
93 "2.1.4 4 bytes (U-00010000): \"\xf0\x90\x80\x80\" |", "\x00"},
94 {"2.1.5 5 bytes (U-00200000): \"\xf8\x88\x80\x80\x80\" |",
95 "2.1.5 5 bytes (U-00200000): \"\xf8\x88\x80\x80\x80\" |", "\x00"},
96 {"2.1.6 6 bytes (U-04000000): \"\xfc\x84\x80\x80\x80\x80\" |",
97 "2.1.6 6 bytes (U-04000000): \"\xfc\x84\x80\x80\x80\x80\" |", "\x00"},
98/* 2.2 Last possible sequence of a certain length */
99 {"2.2.1 1 byte (U-0000007F): \"\x7f\" |",
100 "2.2.1 1 byte (U-0000007F): \"\x7f\" |", "\x00"},
101 {"2.2.2 2 bytes (U-000007FF): \"\xdf\xbf\" |",
102 "2.2.2 2 bytes (U-000007FF): \"\xdf\xbf\" |", "\x00"},
103 {"2.2.3 3 bytes (U-0000FFFF): \"\xef\xbf\xbf\" |",
104 "2.2.3 3 bytes (U-0000FFFF): \"\" |", "\x03"}, /* matches one of 5.3 sequences... */
105 {"2.2.4 4 bytes (U-001FFFFF): \"\xf7\xbf\xbf\xbf\" |",
106 "2.2.4 4 bytes (U-001FFFFF): \"\xf7\xbf\xbf\xbf\" |", "\x00"},
107 {"2.2.5 5 bytes (U-03FFFFFF): \"\xfb\xbf\xbf\xbf\xbf\" |",
108 "2.2.5 5 bytes (U-03FFFFFF): \"\xfb\xbf\xbf\xbf\xbf\" |", "\x00"},
109 {"2.2.6 6 bytes (U-7FFFFFFF): \"\xfd\xbf\xbf\xbf\xbf\xbf\" |",
110 "2.2.6 6 bytes (U-7FFFFFFF): \"\xfd\xbf\xbf\xbf\xbf\xbf\" |", "\x00"},
111/* 2.3 Other boundary conditions */
112 {"2.3.1 U-0000D7FF = ed 9f bf = \"\xed\x9f\xbf\" |",
113 "2.3.1 U-0000D7FF = ed 9f bf = \"\xed\x9f\xbf\" |", "\x00"},
114 {"2.3.2 U-0000E000 = ee 80 80 = \"\xee\x80\x80\" |",
115 "2.3.2 U-0000E000 = ee 80 80 = \"\xee\x80\x80\" |", "\x00"},
116 {"2.3.3 U-0000FFFD = ef bf bd = \"\xef\xbf\xbd\" |",
117 "2.3.3 U-0000FFFD = ef bf bd = \"\xef\xbf\xbd\" |", "\x00"},
118 {"2.3.4 U-0010FFFF = f4 8f bf bf = \"\xf4\x8f\xbf\xbf\" |",
119 "2.3.4 U-0010FFFF = f4 8f bf bf = \"\xf4\x8f\xbf\xbf\" |", "\x00"},
120 {"2.3.5 U-00110000 = f4 90 80 80 = \"\xf4\x90\x80\x80\" |",
121 "2.3.5 U-00110000 = f4 90 80 80 = \"\xf4\x90\x80\x80\" |", "\x00"},
122
123/* 3 Malformed sequences
124 * 3.1 Unexpected continuation bytes
125 * Each unexpected continuation byte should be separately signaled as a malformed sequence of its own. */
126 {"3.1.1 First continuation byte 0x80: \"\x80\" |",
127 "3.1.1 First continuation byte 0x80: \"\" |", "\x01"},
128 {"3.1.2 Last continuation byte 0xbf: \"\xbf\" |",
129 "3.1.2 Last continuation byte 0xbf: \"\" |", "\x01"},
130 {"3.1.3 2 continuation bytes: \"\x80\xbf\" |",
131 "3.1.3 2 continuation bytes: \"\" |", "\x02"},
132 {"3.1.4 3 continuation bytes: \"\x80\xbf\x80\" |",
133 "3.1.4 3 continuation bytes: \"\" |", "\x03"},
134 {"3.1.5 4 continuation bytes: \"\x80\xbf\x80\xbf\" |",
135 "3.1.5 4 continuation bytes: \"\" |", "\x04"},
136 {"3.1.6 5 continuation bytes: \"\x80\xbf\x80\xbf\x80\" |",
137 "3.1.6 5 continuation bytes: \"\" |", "\x05"},
138 {"3.1.7 6 continuation bytes: \"\x80\xbf\x80\xbf\x80\xbf\" |",
139 "3.1.7 6 continuation bytes: \"\" |", "\x06"},
140 {"3.1.8 7 continuation bytes: \"\x80\xbf\x80\xbf\x80\xbf\x80\" |",
141 "3.1.8 7 continuation bytes: \"\" |", "\x07"},
142/* 3.1.9 Sequence of all 64 possible continuation bytes (0x80-0xbf): | */
143 {"3.1.9 \"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
144 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
145 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
146 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\" |",
147 "3.1.9 \"\" |", "\x40"}, /* NOLINT: modernize-raw-string-literal. */
148/* 3.2 Lonely start characters
149 * 3.2.1 All 32 first bytes of 2-byte sequences (0xc0-0xdf), each followed by a space character: */
150 {"3.2.1 \"\xc0 \xc1 \xc2 \xc3 \xc4 \xc5 \xc6 \xc7 \xc8 \xc9 \xca \xcb \xcc \xcd \xce \xcf "
151 "\xd0 \xd1 \xd2 \xd3 \xd4 \xd5 \xd6 \xd7 \xd8 \xd9 \xda \xdb \xdc \xdd \xde \xdf \" |",
152 "3.2.1 \" \" |", "\x20"}, /* NOLINT: modernize-raw-string-literal. */
153/* 3.2.2 All 16 first bytes of 3-byte sequences (0xe0-0xef), each followed by a space character: */
154 {"3.2.2 \"\xe0 \xe1 \xe2 \xe3 \xe4 \xe5 \xe6 \xe7 \xe8 \xe9 \xea \xeb \xec \xed \xee \xef \" |",
155 "3.2.2 \" \" |", "\x10"},
156/* 3.2.3 All 8 first bytes of 4-byte sequences (0xf0-0xf7), each followed by a space character: */
157 {"3.2.3 \"\xf0 \xf1 \xf2 \xf3 \xf4 \xf5 \xf6 \xf7 \" |",
158 "3.2.3 \" \" |", "\x08"},
159/* 3.2.4 All 4 first bytes of 5-byte sequences (0xf8-0xfb), each followed by a space character: */
160 {"3.2.4 \"\xf8 \xf9 \xfa \xfb \" |",
161 "3.2.4 \" \" |", "\x04"},
162/* 3.2.5 All 2 first bytes of 6-byte sequences (0xfc-0xfd), each followed by a space character: */
163 {"3.2.4 \"\xfc \xfd \" |",
164 "3.2.4 \" \" |", "\x02"},
165/* 3.3 Sequences with last continuation byte missing
166 * All bytes of an incomplete sequence should be signaled as a single malformed sequence,
167 * i.e., you should see only a single replacement character in each of the next 10 tests.
168 * (Characters as in section 2) */
169 {"3.3.1 2-byte sequence with last byte missing (U+0000): \"\xc0\" |",
170 "3.3.1 2-byte sequence with last byte missing (U+0000): \"\" |", "\x01"},
171 {"3.3.2 3-byte sequence with last byte missing (U+0000): \"\xe0\x80\" |",
172 "3.3.2 3-byte sequence with last byte missing (U+0000): \"\" |", "\x02"},
173 {"3.3.3 4-byte sequence with last byte missing (U+0000): \"\xf0\x80\x80\" |",
174 "3.3.3 4-byte sequence with last byte missing (U+0000): \"\" |", "\x03"},
175 {"3.3.4 5-byte sequence with last byte missing (U+0000): \"\xf8\x80\x80\x80\" |",
176 "3.3.4 5-byte sequence with last byte missing (U+0000): \"\" |", "\x04"},
177 {"3.3.5 6-byte sequence with last byte missing (U+0000): \"\xfc\x80\x80\x80\x80\" |",
178 "3.3.5 6-byte sequence with last byte missing (U+0000): \"\" |", "\x05"},
179 {"3.3.6 2-byte sequence with last byte missing (U-000007FF): \"\xdf\" |",
180 "3.3.6 2-byte sequence with last byte missing (U-000007FF): \"\" |", "\x01"},
181 {"3.3.7 3-byte sequence with last byte missing (U-0000FFFF): \"\xef\xbf\" |",
182 "3.3.7 3-byte sequence with last byte missing (U-0000FFFF): \"\" |", "\x02"},
183 {"3.3.8 4-byte sequence with last byte missing (U-001FFFFF): \"\xf7\xbf\xbf\" |",
184 "3.3.8 4-byte sequence with last byte missing (U-001FFFFF): \"\" |", "\x03"},
185 {"3.3.9 5-byte sequence with last byte missing (U-03FFFFFF): \"\xfb\xbf\xbf\xbf\" |",
186 "3.3.9 5-byte sequence with last byte missing (U-03FFFFFF): \"\" |", "\x04"},
187 {"3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF): \"\xfd\xbf\xbf\xbf\xbf\" |",
188 "3.3.10 6-byte sequence with last byte missing (U-7FFFFFFF): \"\" |", "\x05"},
189/* 3.4 Concatenation of incomplete sequences
190 * All the 10 sequences of 3.3 concatenated, you should see 10 malformed sequences being signaled: */
191 {"3.4 \"\xc0\xe0\x80\xf0\x80\x80\xf8\x80\x80\x80\xfc\x80\x80\x80\x80"
192 "\xdf\xef\xbf\xf7\xbf\xbf\xfb\xbf\xbf\xbf\xfd\xbf\xbf\xbf\xbf\""
193 " |",
194 "3.4 \"\" |", "\x1e"},
195/* 3.5 Impossible bytes
196 * The following two bytes cannot appear in a correct UTF-8 string */
197 {"3.5.1 fe = \"\xfe\" |",
198 "3.5.1 fe = \"\" |", "\x01"},
199 {"3.5.2 ff = \"\xff\" |",
200 "3.5.2 ff = \"\" |", "\x01"},
201 {"3.5.3 fe fe ff ff = \"\xfe\xfe\xff\xff\" |",
202 "3.5.3 fe fe ff ff = \"\" |", "\x04"},
203
204/* 4 Overlong sequences
205 * The following sequences are not malformed according to the letter of the Unicode 2.0 standard.
206 * However, they are longer then necessary and a correct UTF-8 encoder is not allowed to produce them.
207 * A "safe UTF-8 decoder" should reject them just like malformed sequences for two reasons:
208 * (1) It helps to debug applications if overlong sequences are not treated as valid representations
209 * of characters, because this helps to spot problems more quickly. (2) Overlong sequences provide
210 * alternative representations of characters, that could maliciously be used to bypass filters that check
211 * only for ASCII characters. For instance, a 2-byte encoded line feed (LF) would not be caught by a
212 * line counter that counts only 0x0a bytes, but it would still be processed as a line feed by an unsafe
213 * UTF-8 decoder later in the pipeline. From a security point of view, ASCII compatibility of UTF-8
214 * sequences means also, that ASCII characters are *only* allowed to be represented by ASCII bytes
215 * in the range 0x00-0x7f. To ensure this aspect of ASCII compatibility, use only "safe UTF-8 decoders"
216 * that reject overlong UTF-8 sequences for which a shorter encoding exists.
217 *
218 * 4.1 Examples of an overlong ASCII character
219 * With a safe UTF-8 decoder, all of the following five overlong representations of the ASCII character
220 * slash ("/") should be rejected like a malformed UTF-8 sequence, for instance by substituting it with
221 * a replacement character. If you see a slash below, you do not have a safe UTF-8 decoder! */
222 {"4.1.1 U+002F = c0 af = \"\xc0\xaf\" |",
223 "4.1.1 U+002F = c0 af = \"\" |", "\x02"},
224 {"4.1.2 U+002F = e0 80 af = \"\xe0\x80\xaf\" |",
225 "4.1.2 U+002F = e0 80 af = \"\" |", "\x03"},
226 {"4.1.3 U+002F = f0 80 80 af = \"\xf0\x80\x80\xaf\" |",
227 "4.1.3 U+002F = f0 80 80 af = \"\" |", "\x04"},
228 {"4.1.4 U+002F = f8 80 80 80 af = \"\xf8\x80\x80\x80\xaf\" |",
229 "4.1.4 U+002F = f8 80 80 80 af = \"\" |", "\x05"},
230 {"4.1.5 U+002F = fc 80 80 80 80 af = \"\xfc\x80\x80\x80\x80\xaf\" |",
231 "4.1.5 U+002F = fc 80 80 80 80 af = \"\" |", "\x06"},
232/* 4.2 Maximum overlong sequences
233 * Below you see the highest Unicode value that is still resulting in an overlong sequence if represented
234 * with the given number of bytes. This is a boundary test for safe UTF-8 decoders. All five characters
235 * should be rejected like malformed UTF-8 sequences. */
236 {"4.2.1 U-0000007F = c1 bf = \"\xc1\xbf\" |",
237 "4.2.1 U-0000007F = c1 bf = \"\" |", "\x02"},
238 {"4.2.2 U-000007FF = e0 9f bf = \"\xe0\x9f\xbf\" |",
239 "4.2.2 U-000007FF = e0 9f bf = \"\" |", "\x03"},
240 {"4.2.3 U-0000FFFF = f0 8f bf bf = \"\xf0\x8f\xbf\xbf\" |",
241 "4.2.3 U-0000FFFF = f0 8f bf bf = \"\" |", "\x04"},
242 {"4.2.4 U-001FFFFF = f8 87 bf bf bf = \"\xf8\x87\xbf\xbf\xbf\" |",
243 "4.2.4 U-001FFFFF = f8 87 bf bf bf = \"\" |", "\x05"},
244 {"4.2.5 U+0000 = fc 83 bf bf bf bf = \"\xfc\x83\xbf\xbf\xbf\xbf\" |",
245 "4.2.5 U+0000 = fc 83 bf bf bf bf = \"\" |", "\x06"},
246/* 4.3 Overlong representation of the NUL character
247 * The following five sequences should also be rejected like malformed UTF-8 sequences and should not be
248 * treated like the ASCII NUL character. */
249 {"4.3.1 U+0000 = c0 80 = \"\xc0\x80\" |",
250 "4.3.1 U+0000 = c0 80 = \"\" |", "\x02"},
251 {"4.3.2 U+0000 = e0 80 80 = \"\xe0\x80\x80\" |",
252 "4.3.2 U+0000 = e0 80 80 = \"\" |", "\x03"},
253 {"4.3.3 U+0000 = f0 80 80 80 = \"\xf0\x80\x80\x80\" |",
254 "4.3.3 U+0000 = f0 80 80 80 = \"\" |", "\x04"},
255 {"4.3.4 U+0000 = f8 80 80 80 80 = \"\xf8\x80\x80\x80\x80\" |",
256 "4.3.4 U+0000 = f8 80 80 80 80 = \"\" |", "\x05"},
257 {"4.3.5 U+0000 = fc 80 80 80 80 80 = \"\xfc\x80\x80\x80\x80\x80\" |",
258 "4.3.5 U+0000 = fc 80 80 80 80 80 = \"\" |", "\x06"},
259
260/* 5 Illegal code positions
261 * The following UTF-8 sequences should be rejected like malformed sequences, because they never represent
262 * valid ISO 10646 characters and a UTF-8 decoder that accepts them might introduce security problems
263 * comparable to overlong UTF-8 sequences.
264 * 5.1 Single UTF-16 surrogates */
265 {"5.1.1 U+D800 = ed a0 80 = \"\xed\xa0\x80\" |",
266 "5.1.1 U+D800 = ed a0 80 = \"\" |", "\x03"},
267 {"5.1.2 U+DB7F = ed ad bf = \"\xed\xad\xbf\" |",
268 "5.1.2 U+DB7F = ed ad bf = \"\" |", "\x03"},
269 {"5.1.3 U+DB80 = ed ae 80 = \"\xed\xae\x80\" |",
270 "5.1.3 U+DB80 = ed ae 80 = \"\" |", "\x03"},
271 {"5.1.4 U+DBFF = ed af bf = \"\xed\xaf\xbf\" |",
272 "5.1.4 U+DBFF = ed af bf = \"\" |", "\x03"},
273 {"5.1.5 U+DC00 = ed b0 80 = \"\xed\xb0\x80\" |",
274 "5.1.5 U+DC00 = ed b0 80 = \"\" |", "\x03"},
275 {"5.1.6 U+DF80 = ed be 80 = \"\xed\xbe\x80\" |",
276 "5.1.6 U+DF80 = ed be 80 = \"\" |", "\x03"},
277 {"5.1.7 U+DFFF = ed bf bf = \"\xed\xbf\xbf\" |",
278 "5.1.7 U+DFFF = ed bf bf = \"\" |", "\x03"},
279/* 5.2 Paired UTF-16 surrogates */
280 {"5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80 = \"\xed\xa0\x80\xed\xb0\x80\" |",
281 "5.2.1 U+D800 U+DC00 = ed a0 80 ed b0 80 = \"\" |", "\x06"},
282 {"5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf = \"\xed\xa0\x80\xed\xbf\xbf\" |",
283 "5.2.2 U+D800 U+DFFF = ed a0 80 ed bf bf = \"\" |", "\x06"},
284 {"5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80 = \"\xed\xad\xbf\xed\xb0\x80\" |",
285 "5.2.3 U+DB7F U+DC00 = ed ad bf ed b0 80 = \"\" |", "\x06"},
286 {"5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf = \"\xed\xad\xbf\xed\xbf\xbf\" |",
287 "5.2.4 U+DB7F U+DFFF = ed ad bf ed bf bf = \"\" |", "\x06"},
288 {"5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80 = \"\xed\xae\x80\xed\xb0\x80\" |",
289 "5.2.5 U+DB80 U+DC00 = ed ae 80 ed b0 80 = \"\" |", "\x06"},
290 {"5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf = \"\xed\xae\x80\xed\xbf\xbf\" |",
291 "5.2.6 U+DB80 U+DFFF = ed ae 80 ed bf bf = \"\" |", "\x06"},
292 {"5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80 = \"\xed\xaf\xbf\xed\xb0\x80\" |",
293 "5.2.7 U+DBFF U+DC00 = ed af bf ed b0 80 = \"\" |", "\x06"},
294 {"5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf = \"\xed\xaf\xbf\xed\xbf\xbf\" |",
295 "5.2.8 U+DBFF U+DFFF = ed af bf ed bf bf = \"\" |", "\x06"},
296/* 5.3 Non-character code positions
297 * The following "non-characters" are "reserved for internal use" by applications, and according to older versions
298 * of the Unicode Standard "should never be interchanged". Unicode Corrigendum #9 dropped the latter restriction.
299 * Nevertheless, their presence in incoming UTF-8 data can remain a potential security risk, depending
300 * on what use is made of these codes subsequently. Examples of such internal use:
301 * - Some file APIs with 16-bit characters may use the integer value -1 = U+FFFF to signal
302 * an end-of-file (EOF) or error condition.
303 * - In some UTF-16 receivers, code point U+FFFE might trigger a byte-swap operation
304 * (to convert between UTF-16LE and UTF-16BE).
305 * With such internal use of non-characters, it may be desirable and safer to block those code points in
306 * UTF-8 decoders, as they should never occur legitimately in incoming UTF-8 data, and could trigger
307 * unsafe behavior in subsequent processing.
308 *
309 * Particularly problematic non-characters in 16-bit applications: */
310 {"5.3.1 U+FFFE = ef bf be = \"\xef\xbf\xbe\" |",
311 "5.3.1 U+FFFE = ef bf be = \"\" |", "\x03"},
312 {"5.3.2 U+FFFF = ef bf bf = \"\xef\xbf\xbf\" |",
313 "5.3.2 U+FFFF = ef bf bf = \"\" |", "\x03"},
314 /* For now, we ignore those, they do not seem to be crucial anyway... */
315/* 5.3.3 U+FDD0 .. U+FDEF
316 * 5.3.4 U+nFFFE U+nFFFF (for n = 1..10) */
317 {nullptr, nullptr, nullptr},
318};
319/* clang-format on */
320
321/* BLI_str_utf8_invalid_strip (and indirectly, BLI_str_utf8_invalid_byte). */
322TEST(string, Utf8InvalidBytes)
323{
324 for (int i = 0; utf8_invalid_tests[i][0] != nullptr; i++) {
325 const char *tst = utf8_invalid_tests[i][0];
326 const char *tst_stripped = utf8_invalid_tests[i][1];
327 const int errors_num = int(utf8_invalid_tests[i][2][0]);
328
329 char buff[80];
330 memcpy(buff, tst, sizeof(buff));
331
332 const int errors_found_num = BLI_str_utf8_invalid_strip(buff, sizeof(buff) - 1);
333
334 printf("[%02d] -> [%02d] \"%s\" -> \"%s\"\n", errors_num, errors_found_num, tst, buff);
335 EXPECT_EQ(errors_found_num, errors_num);
336 EXPECT_STREQ(buff, tst_stripped);
337 }
338}
339
342/* -------------------------------------------------------------------- */
346TEST(string, StringNLenUTF8)
347{
348 EXPECT_EQ(BLI_strnlen_utf8("", 0), 0);
349 EXPECT_EQ(BLI_strnlen_utf8("", 1), 0);
351}
352
353TEST(string, StringNLenUTF8_Incomplete)
354{
355 const char *ref_str =
357 char buf[22];
358 const size_t ref_str_len = 21;
359
360#define EXPECT_BYTE_OFFSET(truncate_ofs, expect_nchars) \
361 { \
362 size_t buf_ofs = 0; \
363 strcpy(buf, ref_str); \
364 buf[truncate_ofs] = '\0'; \
365 EXPECT_EQ(BLI_strnlen_utf8_ex(buf, ref_str_len, &buf_ofs), expect_nchars); \
366 EXPECT_EQ(buf_ofs, truncate_ofs); \
367 }
368
369 EXPECT_BYTE_OFFSET(0, 0);
370 EXPECT_BYTE_OFFSET(1, 1);
371
372 EXPECT_BYTE_OFFSET(2, 2);
373 EXPECT_BYTE_OFFSET(3, 2);
374
375 EXPECT_BYTE_OFFSET(4, 3);
376 EXPECT_BYTE_OFFSET(5, 3);
377 EXPECT_BYTE_OFFSET(6, 3);
378
379 EXPECT_BYTE_OFFSET(7, 4);
380 EXPECT_BYTE_OFFSET(8, 4);
381 EXPECT_BYTE_OFFSET(9, 4);
382 EXPECT_BYTE_OFFSET(10, 4);
383
384 EXPECT_BYTE_OFFSET(11, 5);
385 EXPECT_BYTE_OFFSET(12, 5);
386 EXPECT_BYTE_OFFSET(13, 5);
387 EXPECT_BYTE_OFFSET(14, 5);
388 EXPECT_BYTE_OFFSET(15, 5);
389
390 EXPECT_BYTE_OFFSET(16, 6);
391 EXPECT_BYTE_OFFSET(17, 6);
392 EXPECT_BYTE_OFFSET(18, 6);
393 EXPECT_BYTE_OFFSET(19, 6);
394 EXPECT_BYTE_OFFSET(20, 6);
395 EXPECT_BYTE_OFFSET(21, 6);
396
397#undef EXPECT_BYTE_OFFSET
398}
399
402/* -------------------------------------------------------------------- */
406TEST(string, Utf8OffsetFromIndex_ClampedIndex)
407{
408 /* Ensure an index that exceeds the number of multi-byte characters in the
409 * string has the same behavior as an index which is clamped by the number of code-points. */
410 const char *test_strings[] = {
411 "",
412 "TEST",
419 };
420 for (int i = 0; i < ARRAY_SIZE(test_strings); i++) {
421 const char *str = test_strings[i];
422 const size_t str_len = strlen(str);
423 const int str_len_utf8 = BLI_strlen_utf8(str);
424
425 const int str_offset = BLI_str_utf8_offset_from_index(str, str_len, str_len_utf8);
426 EXPECT_EQ(BLI_str_utf8_offset_from_index(str, str_len, str_len_utf8 + 1), str_offset);
427 EXPECT_EQ(BLI_str_utf8_offset_from_index(str, str_len, str_len_utf8 + 10), str_offset);
428 }
429}
430
433/* -------------------------------------------------------------------- */
437static size_t utf8_as_char32(const char *str, const char str_len, char32_t *r_result)
438{
439 size_t i = 0, result_len = 0;
440 while ((i < str_len) && (str[i] != '\0')) {
441 r_result[result_len++] = BLI_str_utf8_as_unicode_step_safe(str, str_len, &i);
442 }
443 return i;
444}
445
446template<size_t Size, size_t SizeWithPadding>
447void utf8_as_char32_test_compare_with_pad_bytes(const char utf8_src[Size])
448{
449 char utf8_src_with_pad[SizeWithPadding] = {0};
450
451 memcpy(utf8_src_with_pad, utf8_src, Size);
452
453 char32_t unicode_dst_a[Size], unicode_dst_b[Size];
454
455 memset(unicode_dst_a, 0xff, sizeof(unicode_dst_a));
456 const size_t index_a = utf8_as_char32(utf8_src, Size, unicode_dst_a);
457
458 /* Test with padded and un-padded size,
459 * to ensure that extra available space doesn't yield a different result. */
460 for (int pass = 0; pass < 2; pass++) {
461 memset(unicode_dst_b, 0xff, sizeof(unicode_dst_b));
462 const size_t index_b = utf8_as_char32(
463 utf8_src_with_pad, pass ? Size : SizeWithPadding, unicode_dst_b);
464
465 /* Check the resulting content matches. */
466 EXPECT_EQ_ARRAY(unicode_dst_a, unicode_dst_b, Size);
467 /* Check the index of the source strings match. */
468 EXPECT_EQ(index_a, index_b);
469 }
470}
471
472template<size_t Size> void utf8_as_char32_test_compare(const char utf8_src[Size])
473{
474 /* Note that 7 is a little arbitrary,
475 * chosen since it's the maximum length of multi-byte character + 1
476 * to account for any errors that read past null bytes. */
479}
480
481template<size_t Size> void utf8_as_char32_test_at_buffer_size()
482{
483 char utf8_src[Size];
484
485 /* Test uniform bytes, also with offsets ascending & descending. */
486 for (int i = 0; i <= 0xff; i++) {
487 memset(utf8_src, i, sizeof(utf8_src));
489
490 /* Offset trailing bytes up and down in steps of 1, 2, 4 .. etc. */
491 if (Size > 1) {
492 for (int mul = 1; mul < 256; mul *= 2) {
493 for (int ofs = 1; ofs < int(Size); ofs++) {
494 utf8_src[ofs] = char(i + (ofs * mul));
495 }
497
498 for (int ofs = 1; ofs < int(Size); ofs++) {
499 utf8_src[ofs] = char(i - (ofs * mul));
500 }
502 }
503 }
504 }
505
506 /* Random bytes. */
507 RNG *rng = BLI_rng_new(1);
508 for (int i = 0; i < 256; i++) {
509 BLI_rng_get_char_n(rng, utf8_src, sizeof(utf8_src));
511 }
512 BLI_rng_free(rng);
513}
514
532
535/* -------------------------------------------------------------------- */
539TEST(string, StrCursorStepNextUtf32Empty)
540{
541 const char32_t empty[] = U"";
542 const size_t len = 0;
543 int pos = 0;
544 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(empty, len, &pos));
545 pos = 1;
546 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(empty, len, &pos));
547}
548
551/* -------------------------------------------------------------------- */
555TEST(string, StrCursorStepNextUtf32Single)
556
557{
558 const char32_t single[] = U"0";
559 const size_t len = 1;
560 int pos = 0;
561 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(single, len, &pos) && pos == 1);
562 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(single, len, &pos));
563}
564
567/* -------------------------------------------------------------------- */
571TEST(string, StrCursorStepNextUtf32Simple)
572{
573 const char32_t simple[] = U"012";
574 const size_t len = 3;
575 int pos = 0;
576 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(simple, len, &pos) && pos == 1);
577 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(simple, len, &pos) && pos == 2);
578 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(simple, len - 1, &pos));
579 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(simple, len, &pos) && pos == 3);
580 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(simple, len, &pos));
581}
582
585/* -------------------------------------------------------------------- */
589TEST(string, StrCursorStepNextUtf32AllCombining)
590{
591 const char32_t allcombining[] = U"\u0300\u0300\u0300";
592 const size_t len = 3;
593 int pos = 0;
594 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(allcombining, len, &pos) && pos == 3);
595 pos = 1;
596 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(allcombining, len, &pos) && pos == 3);
597 pos = 2;
598 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(allcombining, len, &pos) && pos == 3);
599 pos = 3;
600 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(allcombining, len, &pos));
601}
602
605/* -------------------------------------------------------------------- */
609TEST(string, StrCursorStepNextUtf32Complex)
610{
611 /* Combining character, "A", two combining characters, "B". */
612 const char32_t complex[] = U"\u0300\u0041\u0300\u0320\u0042";
613 const size_t len = 5;
614 int pos = 0;
615 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(complex, len, &pos) && pos == 1);
616 pos = 1;
617 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(complex, len, &pos) && pos == 4);
618 pos = 2;
619 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(complex, len, &pos) && pos == 4);
620 pos = 3;
621 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(complex, len, &pos) && pos == 4);
622 pos = 4;
623 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(complex, len, &pos) && pos == 5);
624 pos = 5;
625 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(complex, len, &pos));
626}
627
630/* -------------------------------------------------------------------- */
634TEST(string, StrCursorStepNextUtf32Invalid)
635{
636 /* Latin1 "À", tab, carriage return, linefeed, separated by combining characters. */
637 const char32_t invalid[] = U"\u00C0\u0300\u0009\u0300\u000D\u0300\u000A\u0300";
638 const size_t len = 8;
639 int pos = 0;
640 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 2);
641 pos = 1;
642 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 2);
643 pos = 2;
644 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 4);
645 pos = 3;
646 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 4);
647 pos = 4;
648 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 6);
649 pos = 5;
650 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 6);
651 pos = 6;
652 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 8);
653 pos = 7;
654 EXPECT_TRUE(BLI_str_cursor_step_next_utf32(invalid, len, &pos) && pos == 8);
655 pos = 8;
656 EXPECT_FALSE(BLI_str_cursor_step_next_utf32(invalid, len, &pos));
657}
658
661/* -------------------------------------------------------------------- */
665TEST(string, StrCursorStepPrevUtf32Empty)
666{
667 const char32_t emtpy[] = U"";
668 const size_t len = 0;
669 int pos = 0;
670 EXPECT_FALSE(BLI_str_cursor_step_prev_utf32(emtpy, len, &pos));
671}
672
675/* -------------------------------------------------------------------- */
679TEST(string, StrCursorStepPrevUtf32Single)
680{
681 const char32_t single[] = U"0";
682 const size_t len = 1;
683 int pos = 1;
684 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(single, len, &pos) && pos == 0);
685 EXPECT_FALSE(BLI_str_cursor_step_prev_utf32(single, len, &pos));
686}
687
690/* -------------------------------------------------------------------- */
694TEST(string, StrCursorStepPrevUtf32Simple)
695{
696 const char32_t simple[] = U"012";
697 const size_t len = 3;
698 int pos = 3;
699 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(simple, len, &pos) && pos == 2);
700 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(simple, len, &pos) && pos == 1);
701 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(simple, len, &pos) && pos == 0);
702 EXPECT_FALSE(BLI_str_cursor_step_prev_utf32(simple, len, &pos));
703}
704
707/* -------------------------------------------------------------------- */
711TEST(string, StrCursorStepPrevUtf32AllCombining)
712{
713 const char32_t allcombining[] = U"\u0300\u0300\u0300";
714 const size_t len = 3;
715 int pos = 3;
716 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(allcombining, len, &pos) && pos == 0);
717 pos = 2;
718 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(allcombining, len, &pos) && pos == 0);
719 pos = 1;
720 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(allcombining, len, &pos) && pos == 0);
721 pos = 0;
722 EXPECT_FALSE(BLI_str_cursor_step_prev_utf32(allcombining, len, &pos));
723}
724
727/* -------------------------------------------------------------------- */
731TEST(string, StrCursorStepPrevUtf32Complex)
732{
733 /* Combining character, "A", two combining characters, "B". */
734 const char32_t complex[] = U"\u0300\u0041\u0300\u0320\u0042";
735 const size_t len = 5;
736 int pos = 5;
737 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(complex, len, &pos) && pos == 4);
738 pos = 4;
739 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(complex, len, &pos) && pos == 1);
740 pos = 3;
741 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(complex, len, &pos) && pos == 1);
742 pos = 2;
743 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(complex, len, &pos) && pos == 1);
744 pos = 1;
745 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(complex, len, &pos) && pos == 0);
746 pos = 0;
747 EXPECT_FALSE(BLI_str_cursor_step_prev_utf32(complex, len, &pos));
748}
749
752/* -------------------------------------------------------------------- */
756TEST(string, StrCursorStepPrevUtf32Invalid)
757{
758 /* Latin1 "À", tab, carriage return, linefeed, separated by combining characters. */
759 const char32_t invalid[] = U"\u00C0\u0300\u0009\u0300\u000D\u0300\u000A\u0300";
760 const size_t len = 8;
761 int pos = 8;
762 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 6);
763 pos = 7;
764 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 6);
765 pos = 6;
766 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 4);
767 pos = 5;
768 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 4);
769 pos = 4;
770 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 2);
771 pos = 3;
772 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 2);
773 pos = 2;
774 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 0);
775 pos = 1;
776 EXPECT_TRUE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos) && pos == 0);
777 pos = 0;
778 EXPECT_FALSE(BLI_str_cursor_step_prev_utf32(invalid, len, &pos));
779}
780
783/* -------------------------------------------------------------------- */
786TEST(string, StrCursorStepNextUtf8Empty)
787{
788 const char empty[] = "";
789 const size_t len = 0;
790 int pos = 0;
791 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(empty, len, &pos));
792 pos = 1;
793 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(empty, len, &pos));
794}
795
798/* -------------------------------------------------------------------- */
801TEST(string, StrCursorStepNextUtf8Single)
802{
803 const char single[] = "0";
804 const size_t len = 1;
805 int pos = 0;
806 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(single, len, &pos) && pos == 1);
807 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(single, len, &pos));
808}
809
812/* -------------------------------------------------------------------- */
816TEST(string, StrCursorStepNextUtf8Simple)
817{
818 const char simple[] = "012";
819 const size_t len = 3;
820 int pos = 0;
821 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(simple, len, &pos) && pos == 1);
822 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(simple, len, &pos) && pos == 2);
823 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(simple, len - 1, &pos));
824 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(simple, len, &pos) && pos == 3);
825 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(simple, len, &pos));
826}
827
830/* -------------------------------------------------------------------- */
834TEST(string, StrCursorStepNextUtf8AllCombining)
835{
836 const char allcombining[] = "\xCC\x80\xCC\x80\xCC\x80";
837 const size_t len = 6;
838 int pos = 0;
839 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos) && pos == 6);
840 pos = 1;
841 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos) && pos == 6);
842 pos = 2;
843 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos) && pos == 6);
844 pos = 3;
845 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos) && pos == 6);
846 pos = 4;
847 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos) && pos == 6);
848 pos = 5;
849 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos) && pos == 6);
850 pos = 6;
851 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(allcombining, len, &pos));
852}
853
856/* -------------------------------------------------------------------- */
860TEST(string, StrCursorStepNextUtf8AllComplex)
861{
862 /* Combining character, "A", "©", two combining characters, "B". */
863 const char complex[] = "\xCC\x80\x41\xC2\xA9\xCC\x80\xCC\xA0\x42";
864 const size_t len = 10;
865 int pos = 0;
866 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 2);
867 pos = 1;
868 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 2);
869 pos = 2;
870 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 3);
871 pos = 3;
872 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 9);
873 pos = 4;
874 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 9);
875 pos = 5;
876 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 9);
877 pos = 6;
878 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 9);
879 pos = 7;
880 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 9);
881 pos = 8;
882 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 9);
883 pos = 9;
884 EXPECT_TRUE(BLI_str_cursor_step_next_utf8(complex, len, &pos) && pos == 10);
885 pos = 10;
886 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(complex, len, &pos));
887}
888
891/* -------------------------------------------------------------------- */
895TEST(string, StrCursorStepNextUtf8Invalid)
896{
897 /* Latin1 "À", combining, tab, carriage return, linefeed, combining. */
898 const char invalid[] = "\xC0\xCC\x80\x09\x0D\x0A\xCC\x80";
899 const size_t len = 8;
900 int pos = 0;
901 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 3);
902 pos = 1;
903 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 3);
904 pos = 2;
905 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 3);
906 pos = 3;
907 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 4);
908 pos = 4;
909 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 5);
910 pos = 5;
911 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 8);
912 pos = 6;
913 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 8);
914 pos = 7;
915 EXPECT_EQ(BLI_str_cursor_step_next_utf8(invalid, len, &pos) ? pos : -1, 8);
916 pos = 8;
917 EXPECT_FALSE(BLI_str_cursor_step_next_utf8(invalid, len, &pos));
918}
919
922/* -------------------------------------------------------------------- */
926TEST(string, StrCursorStepPrevUtf8Empty)
927{
928 const char empty[] = "";
929 const size_t len = 0;
930 int pos = 0;
931 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(empty, len, &pos));
932 pos = 1;
933 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(empty, len, &pos));
934}
935
938/* -------------------------------------------------------------------- */
942TEST(string, StrCursorStepPrevUtf8Single)
943{
944 const char single[] = "0";
945 const size_t len = 1;
946 int pos = 1;
947 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(single, len, &pos) && pos == 0);
948 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(single, len, &pos));
949}
950
953/* -------------------------------------------------------------------- */
957TEST(string, StrCursorStepPrevUtf8Simple)
958{
959 const char simple[] = "012";
960 const size_t len = 3;
961 int pos = 3;
962 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(simple, len, &pos) && pos == 2);
963 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(simple, len, &pos) && pos == 1);
964 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(simple, len, &pos) && pos == 0);
965 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(simple, len, &pos));
966}
967
970/* -------------------------------------------------------------------- */
974TEST(string, StrCursorStepPrevUtf8AllCombining)
975{
976 const char allcombining[] = "\xCC\x80\xCC\x80\xCC\x80";
977 const size_t len = 6;
978 int pos = 6;
979 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos) && pos == 0);
980 pos = 5;
981 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos) && pos == 0);
982 pos = 4;
983 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos) && pos == 0);
984 pos = 3;
985 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos) && pos == 0);
986 pos = 2;
987 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos) && pos == 0);
988 pos = 1;
989 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos) && pos == 0);
990 pos = 0;
991 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(allcombining, len, &pos));
992}
993
996/* -------------------------------------------------------------------- */
1000TEST(string, StrCursorStepPrevUtf8Complex)
1001{
1002 /* Combining character, "A", "©", two combining characters, "B". */
1003 const char complex[] = "\xCC\x80\x41\xC2\xA9\xCC\x80\xCC\xA0\x42";
1004 const size_t len = 10;
1005 int pos = 10;
1006 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 9);
1007 pos = 9;
1008 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 3);
1009 pos = 8;
1010 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 3);
1011 pos = 7;
1012 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 3);
1013 pos = 6;
1014 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 3);
1015 pos = 5;
1016 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 3);
1017 pos = 4;
1018 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 3);
1019 pos = 3;
1020 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 2);
1021 pos = 2;
1022 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 0);
1023 pos = 1;
1024 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(complex, len, &pos) && pos == 0);
1025 pos = 0;
1026 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(complex, len, &pos));
1027}
1028
1031/* -------------------------------------------------------------------- */
1035TEST(string, StrCursorStepPrevUtf8Invalid)
1036{
1037 /* Latin1 "À", combining, tab, carriage return, linefeed, combining. */
1038 const char invalid[] = "\xC0\xCC\x80\x09\x0D\x0A\xCC\x80";
1039 const size_t len = 8;
1040 int pos = 8;
1041 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 5);
1042 pos = 7;
1043 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 5);
1044 pos = 6;
1045 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 5);
1046 pos = 5;
1047 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 4);
1048 pos = 4;
1049 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 3);
1050 pos = 3;
1051 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 0);
1052 pos = 2;
1053 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 0);
1054 pos = 1;
1055 EXPECT_TRUE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos) && pos == 0);
1056 pos = 0;
1057 EXPECT_FALSE(BLI_str_cursor_step_prev_utf8(invalid, len, &pos));
1058}
1059
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Random number functions.
struct RNG * BLI_rng_new(unsigned int seed)
Definition rand.cc:39
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:58
void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len) ATTR_NONNULL(1
bool BLI_str_cursor_step_prev_utf32(const char32_t *str, int str_maxlen, int *pos)
bool BLI_str_cursor_step_next_utf8(const char *str, int str_maxlen, int *pos)
bool BLI_str_cursor_step_prev_utf8(const char *str, int str_maxlen, int *pos)
bool BLI_str_cursor_step_next_utf32(const char32_t *str, int str_maxlen, int *pos)
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_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
int BLI_str_utf8_invalid_strip(char *str, size_t length) ATTR_NONNULL(1)
#define STR_MB_ALPHA_1
void utf8_as_char32_test_at_buffer_size()
#define STR_MB_ALPHA_5
#define EXPECT_BYTE_OFFSET(truncate_ofs, expect_nchars)
#define STR_MB_ALPHA_3
static size_t utf8_as_char32(const char *str, const char str_len, char32_t *r_result)
#define STR_MB_ALPHA_2
void utf8_as_char32_test_compare_with_pad_bytes(const char utf8_src[Size])
void utf8_as_char32_test_compare(const char utf8_src[Size])
#define STR_MB_ALPHA_6
TEST(string, Utf8InvalidBytes)
static const char * utf8_invalid_tests[][3]
#define STR_MB_ALPHA_4
#define ARRAY_SIZE(arr)
unsigned int U
Definition btGjkEpa3.h:78
static void mul(btAlignedObjectArray< T > &items, const Q &value)
#define printf
int len
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
#define str(s)
Definition rand.cc:33