Blender V5.3
BLI_string_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 <array>
8#include <initializer_list>
9#include <ostream> /* NOLINT */
10#include <string>
11#include <utility>
12#include <vector>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_string.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_string_utils.hh"
19#include "BLI_utildefines.hh"
20
21namespace blender {
22
23using std::initializer_list;
24using std::pair;
25using std::string;
26using std::vector;
27
28/* -------------------------------------------------------------------- */
31
32TEST(string, StrCat)
33{
34#define STR_N_CAT(dst_init, dst_size, src, result_expect) \
35 { \
36 char dst[dst_size + 1] = dst_init; \
37 dst[dst_size] = 0xff; \
38 BLI_strncat(dst, src, dst_size); \
39 EXPECT_STREQ(dst, result_expect); \
40 EXPECT_EQ(dst[dst_size], 0xff); \
41 }
42
43 STR_N_CAT("", 1, "", "");
44 STR_N_CAT("", 1, "Y", "");
45 STR_N_CAT("", 2, "Y", "Y");
46 STR_N_CAT("", 2, "YZ", "Y");
47 STR_N_CAT("X", 2, "YZ", "X");
48 STR_N_CAT("ABC", 4, "XYZ", "ABC");
49 STR_N_CAT("ABC", 7, "XYZ", "ABCXYZ");
50#undef STR_N_CAT
51}
52
54
55/* -------------------------------------------------------------------- */
58
59TEST(string, StrCopyEnsurePad)
60{
61#define STRNCPY_ENSURE_PAD_EXPECT(src, dst_expect, dst_maxncpy) \
62 { \
63 char dst[dst_maxncpy + 5]; \
64 memset(dst, 0xff, sizeof(dst)); \
65 dst[sizeof(dst) - 1] = '\0'; \
66 BLI_strncpy_ensure_pad(dst, src, '*', dst_maxncpy); \
67 EXPECT_STREQ(dst, dst_expect); \
68 EXPECT_STREQ(dst + dst_maxncpy, "\xff\xff\xff\xff"); \
69 }
70
72 STRNCPY_ENSURE_PAD_EXPECT("A", "*A*", 4);
73 STRNCPY_ENSURE_PAD_EXPECT("*A*", "*A*", 4);
74 STRNCPY_ENSURE_PAD_EXPECT("*ABC", "*A*", 4);
75 STRNCPY_ENSURE_PAD_EXPECT("ABC", "*ABC*", 6);
76 STRNCPY_ENSURE_PAD_EXPECT("ABC", "*AB*", 5);
77 STRNCPY_ENSURE_PAD_EXPECT("ABC", "*A*", 4);
78 STRNCPY_ENSURE_PAD_EXPECT("A", "*", 3);
79 STRNCPY_ENSURE_PAD_EXPECT("A", "*", 2);
80 STRNCPY_ENSURE_PAD_EXPECT("A", "", 1);
81 STRNCPY_ENSURE_PAD_EXPECT("*", "*", 2);
82 STRNCPY_ENSURE_PAD_EXPECT("*", "", 1);
83#undef STRNCPY_ENSURE_PAD_EXPECT
84}
85
87
88/* -------------------------------------------------------------------- */
91
92TEST(string, StrReplaceRange)
93{
94#define STR_REPLACE_RANGE(src, size, beg, end, dst, result_expect) \
95 { \
96 char string[size] = src; \
97 BLI_string_replace_range(string, sizeof(string), beg, end, dst); \
98 EXPECT_STREQ(string, result_expect); \
99 }
100
101 STR_REPLACE_RANGE("a ", 5, 2, 2, "b!", "a b!");
102 STR_REPLACE_RANGE("a ", 4, 2, 2, "b!", "a b");
103 STR_REPLACE_RANGE("a ", 5, 1, 2, "b!", "ab!");
104 STR_REPLACE_RANGE("XYZ", 5, 1, 1, "A", "XAYZ");
105 STR_REPLACE_RANGE("XYZ", 5, 1, 1, "AB", "XABY");
106 STR_REPLACE_RANGE("XYZ", 5, 1, 1, "ABC", "XABC");
107
108 /* Add at the end when there is no room (no-op). */
109 STR_REPLACE_RANGE("XYZA", 5, 4, 4, "?", "XYZA");
110 /* Add at the start, replace all contents. */
111 STR_REPLACE_RANGE("XYZ", 4, 0, 0, "ABC", "ABC");
112 STR_REPLACE_RANGE("XYZ", 7, 0, 0, "ABC", "ABCXYZ");
113 /* Only remove. */
114 STR_REPLACE_RANGE("XYZ", 4, 1, 3, "", "X");
115 STR_REPLACE_RANGE("XYZ", 4, 0, 2, "", "Z");
116 STR_REPLACE_RANGE("XYZ", 4, 0, 3, "", "");
117 /* Only Add. */
118 STR_REPLACE_RANGE("", 4, 0, 0, "XYZ", "XYZ");
119 STR_REPLACE_RANGE("", 4, 0, 0, "XYZ?", "XYZ");
120 /* Do nothing. */
121 STR_REPLACE_RANGE("", 1, 0, 0, "?", "");
122 STR_REPLACE_RANGE("", 1, 0, 0, "", "");
123
124#undef STR_REPLACE_RANGE
125}
126
128
129/* -------------------------------------------------------------------- */
132
133/* BLI_str_partition */
134TEST(string, StrPartition)
135{
136 const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
137 const char *sep, *suf;
138 size_t pre_len;
139
140 {
141 const char *str = "mat.e-r_ial";
142
143 /* "mat.e-r_ial" -> "mat", '.', "e-r_ial", 3 */
144 pre_len = BLI_str_partition(str, delim, &sep, &suf);
145 EXPECT_EQ(pre_len, 3);
146 EXPECT_EQ(&str[3], sep);
147 EXPECT_STREQ("e-r_ial", suf);
148 }
149
150 /* Corner cases. */
151 {
152 const char *str = ".mate-rial--";
153
154 /* ".mate-rial--" -> "", '.', "mate-rial--", 0 */
155 pre_len = BLI_str_partition(str, delim, &sep, &suf);
156 EXPECT_EQ(pre_len, 0);
157 EXPECT_EQ(&str[0], sep);
158 EXPECT_STREQ("mate-rial--", suf);
159 }
160
161 {
162 const char *str = ".__.--_";
163
164 /* ".__.--_" -> "", '.', "__.--_", 0 */
165 pre_len = BLI_str_partition(str, delim, &sep, &suf);
166 EXPECT_EQ(pre_len, 0);
167 EXPECT_EQ(&str[0], sep);
168 EXPECT_STREQ("__.--_", suf);
169 }
170
171 {
172 const char *str = "";
173
174 /* "" -> "", nullptr, nullptr, 0 */
175 pre_len = BLI_str_partition(str, delim, &sep, &suf);
176 EXPECT_EQ(pre_len, 0);
177 EXPECT_EQ(sep, static_cast<void *>(nullptr));
178 EXPECT_EQ(suf, static_cast<void *>(nullptr));
179 }
180
181 {
182 const char *str = "material";
183
184 /* "material" -> "material", nullptr, nullptr, 8 */
185 pre_len = BLI_str_partition(str, delim, &sep, &suf);
186 EXPECT_EQ(pre_len, 8);
187 EXPECT_EQ(sep, static_cast<void *>(nullptr));
188 EXPECT_EQ(suf, static_cast<void *>(nullptr));
189 }
190}
191
192/* BLI_str_rpartition */
193TEST(string, StrRPartition)
194{
195 const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
196 const char *sep, *suf;
197 size_t pre_len;
198
199 {
200 const char *str = "mat.e-r_ial";
201
202 /* "mat.e-r_ial" -> "mat.e-r", '_', "ial", 7 */
203 pre_len = BLI_str_rpartition(str, delim, &sep, &suf);
204 EXPECT_EQ(pre_len, 7);
205 EXPECT_EQ(&str[7], sep);
206 EXPECT_STREQ("ial", suf);
207 }
208
209 /* Corner cases. */
210 {
211 const char *str = ".mate-rial--";
212
213 /* ".mate-rial--" -> ".mate-rial-", '-', "", 11 */
214 pre_len = BLI_str_rpartition(str, delim, &sep, &suf);
215 EXPECT_EQ(pre_len, 11);
216 EXPECT_EQ(&str[11], sep);
217 EXPECT_STREQ("", suf);
218 }
219
220 {
221 const char *str = ".__.--_";
222
223 /* ".__.--_" -> ".__.--", '_', "", 6 */
224 pre_len = BLI_str_rpartition(str, delim, &sep, &suf);
225 EXPECT_EQ(pre_len, 6);
226 EXPECT_EQ(&str[6], sep);
227 EXPECT_STREQ("", suf);
228 }
229
230 {
231 const char *str = ".material";
232
233 /* ".material" -> "", '.', "material", 0 */
234 pre_len = BLI_str_rpartition(str, delim, &sep, &suf);
235 EXPECT_EQ(pre_len, 0);
236 EXPECT_EQ(&str[0], sep);
237 EXPECT_STREQ("material", suf);
238 }
239
240 {
241 const char *str = "";
242
243 /* "" -> "", nullptr, nullptr, 0 */
244 pre_len = BLI_str_rpartition(str, delim, &sep, &suf);
245 EXPECT_EQ(pre_len, 0);
246 EXPECT_EQ(sep, static_cast<void *>(nullptr));
247 EXPECT_EQ(suf, static_cast<void *>(nullptr));
248 }
249
250 {
251 const char *str = "material";
252
253 /* "material" -> "material", nullptr, nullptr, 8 */
254 pre_len = BLI_str_rpartition(str, delim, &sep, &suf);
255 EXPECT_EQ(pre_len, 8);
256 EXPECT_EQ(sep, static_cast<void *>(nullptr));
257 EXPECT_EQ(suf, static_cast<void *>(nullptr));
258 }
259}
260
261/* BLI_str_partition_ex */
262TEST(string, StrPartitionEx)
263{
264 const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
265 const char *sep, *suf;
266 size_t pre_len;
267
268 /* Only considering 'from_right' cases here. */
269
270 {
271 const char *str = "mat.e-r_ia.l";
272
273 /* "mat.e-r_ia.l" over "mat.e-r" -> "mat.e", '.', "r_ia.l", 3 */
274 pre_len = BLI_str_partition_ex(str, str + 6, delim, &sep, &suf, true);
275 EXPECT_EQ(pre_len, 5);
276 EXPECT_EQ(&str[5], sep);
277 EXPECT_STREQ("r_ia.l", suf);
278 }
279
280 /* Corner cases. */
281 {
282 const char *str = "mate.rial";
283
284 /* "mate.rial" over "mate" -> "mate.rial", nullptr, nullptr, 4 */
285 pre_len = BLI_str_partition_ex(str, str + 4, delim, &sep, &suf, true);
286 EXPECT_EQ(pre_len, 4);
287 EXPECT_EQ(sep, static_cast<void *>(nullptr));
288 EXPECT_EQ(suf, static_cast<void *>(nullptr));
289 }
290}
291
292/* BLI_str_partition_utf8 */
293TEST(string, StrPartitionUtf8)
294{
295 const uint delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* yin-yang */, '\0'};
296 const char *sep, *suf;
297 size_t pre_len;
298
299 {
300 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
301
302 /* "ma\xc3\xb1te-r\xe2\x98\xafial" -> "ma", '\xc3\xb1', "te-r\xe2\x98\xafial", 2 */
303 pre_len = BLI_str_partition_utf8(str, delim, &sep, &suf);
304 EXPECT_EQ(pre_len, 2);
305 EXPECT_EQ(&str[2], sep);
306 EXPECT_STREQ("te-r\xe2\x98\xafial", suf);
307 }
308
309 /* Corner cases. */
310 {
311 const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
312
313 /* "\xe2\x98\xafmate-rial-\xc3\xb1" -> "", '\xe2\x98\xaf', "mate-rial-\xc3\xb1", 0 */
314 pre_len = BLI_str_partition_utf8(str, delim, &sep, &suf);
315 EXPECT_EQ(pre_len, 0);
316 EXPECT_EQ(&str[0], sep);
317 EXPECT_STREQ("mate-rial-\xc3\xb1", suf);
318 }
319
320 {
321 const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
322
323 /* "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1" -> "", '\xe2\x98\xaf', ".\xc3\xb1_.--\xc3\xb1", 0 */
324 pre_len = BLI_str_partition_utf8(str, delim, &sep, &suf);
325 EXPECT_EQ(pre_len, 0);
326 EXPECT_EQ(&str[0], sep);
327 EXPECT_STREQ(".\xc3\xb1_.--\xc3\xb1", suf);
328 }
329
330 {
331 const char *str = "";
332
333 /* "" -> "", nullptr, nullptr, 0 */
334 pre_len = BLI_str_partition_utf8(str, delim, &sep, &suf);
335 EXPECT_EQ(pre_len, 0);
336 EXPECT_EQ(sep, static_cast<void *>(nullptr));
337 EXPECT_EQ(suf, static_cast<void *>(nullptr));
338 }
339
340 {
341 const char *str = "material";
342
343 /* "material" -> "material", nullptr, nullptr, 8 */
344 pre_len = BLI_str_partition_utf8(str, delim, &sep, &suf);
345 EXPECT_EQ(pre_len, 8);
346 EXPECT_EQ(sep, static_cast<void *>(nullptr));
347 EXPECT_EQ(suf, static_cast<void *>(nullptr));
348 }
349}
350
351/* BLI_str_rpartition_utf8 */
352TEST(string, StrRPartitionUtf8)
353{
354 const uint delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* yin-yang */, '\0'};
355 const char *sep, *suf;
356 size_t pre_len;
357
358 {
359 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
360
361 /* "ma\xc3\xb1te-r\xe2\x98\xafial" -> "mat\xc3\xb1te-r", '\xe2\x98\xaf', "ial", 8 */
362 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
363 EXPECT_EQ(pre_len, 8);
364 EXPECT_EQ(&str[8], sep);
365 EXPECT_STREQ("ial", suf);
366 }
367
368 /* Corner cases. */
369 {
370 const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
371
372 /* "\xe2\x98\xafmate-rial-\xc3\xb1" -> "\xe2\x98\xafmate-rial-", '\xc3\xb1', "", 13 */
373 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
374 EXPECT_EQ(pre_len, 13);
375 EXPECT_EQ(&str[13], sep);
376 EXPECT_STREQ("", suf);
377 }
378
379 {
380 const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
381
382 /* "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1" -> "\xe2\x98\xaf.\xc3\xb1_.--", '\xc3\xb1', "", 10 */
383 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
384 EXPECT_EQ(pre_len, 10);
385 EXPECT_EQ(&str[10], sep);
386 EXPECT_STREQ("", suf);
387 }
388
389 {
390 const char *str = ".material";
391
392 /* ".material" -> "", '.', "material", 0 */
393 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
394 EXPECT_EQ(pre_len, 0);
395 EXPECT_EQ(&str[0], sep);
396 EXPECT_STREQ("material", suf);
397 }
398
399 {
400 const char *str = "\xc3\xb1material";
401
402 /* "\xc3\xb1material" -> "", '\xc3\xb1', "material", 0 */
403 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
404 EXPECT_EQ(pre_len, 0);
405 EXPECT_EQ(&str[0], sep);
406 EXPECT_STREQ("material", suf);
407 }
408
409 {
410 const char *str = "";
411
412 /* "" -> "", nullptr, nullptr, 0 */
413 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
414 EXPECT_EQ(pre_len, 0);
415 EXPECT_EQ(sep, static_cast<void *>(nullptr));
416 EXPECT_EQ(suf, static_cast<void *>(nullptr));
417 }
418
419 {
420 const char *str = "material";
421
422 /* "material" -> "material", nullptr, nullptr, 8 */
423 pre_len = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
424 EXPECT_EQ(pre_len, 8);
425 EXPECT_EQ(sep, static_cast<void *>(nullptr));
426 EXPECT_EQ(suf, static_cast<void *>(nullptr));
427 }
428}
429
430/* BLI_str_partition_ex_utf8 */
431TEST(string, StrPartitionExUtf8)
432{
433 const uint delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* yin-yang */, '\0'};
434 const char *sep, *suf;
435 size_t pre_len;
436
437 /* Only considering 'from_right' cases here. */
438
439 {
440 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
441
442 /* "ma\xc3\xb1te-r\xe2\x98\xafial" over
443 * "ma\xc3\xb1te" -> "ma", '\xc3\xb1', "te-r\xe2\x98\xafial", 2 */
444 pre_len = BLI_str_partition_ex_utf8(str, str + 6, delim, &sep, &suf, true);
445 EXPECT_EQ(pre_len, 2);
446 EXPECT_EQ(&str[2], sep);
447 EXPECT_STREQ("te-r\xe2\x98\xafial", suf);
448 }
449
450 /* Corner cases. */
451 {
452 const char *str = "mate\xe2\x98\xafrial";
453
454 /* "mate\xe2\x98\xafrial" over "mate" -> "mate\xe2\x98\xafrial", nullptr, nullptr, 4 */
455 pre_len = BLI_str_partition_ex_utf8(str, str + 4, delim, &sep, &suf, true);
456 EXPECT_EQ(pre_len, 4);
457 EXPECT_EQ(sep, static_cast<void *>(nullptr));
458 EXPECT_EQ(suf, static_cast<void *>(nullptr));
459 }
460}
461
463
464/* -------------------------------------------------------------------- */
467
468/* BLI_str_format_int_grouped */
469TEST(string, StrFormatIntGrouped)
470{
471 char number_str[BLI_STR_FORMAT_INT32_GROUPED_SIZE];
472 int number;
473
474 BLI_str_format_int_grouped(number_str, number = 0);
475 EXPECT_STREQ("0", number_str);
476
477 BLI_str_format_int_grouped(number_str, number = 1);
478 EXPECT_STREQ("1", number_str);
479
480 BLI_str_format_int_grouped(number_str, number = -1);
481 EXPECT_STREQ("-1", number_str);
482
483 BLI_str_format_int_grouped(number_str, number = 1000);
484 EXPECT_STREQ("1,000", number_str);
485
486 BLI_str_format_int_grouped(number_str, number = -1000);
487 EXPECT_STREQ("-1,000", number_str);
488
489 BLI_str_format_int_grouped(number_str, number = 999);
490 EXPECT_STREQ("999", number_str);
491
492 BLI_str_format_int_grouped(number_str, number = -999);
493 EXPECT_STREQ("-999", number_str);
494
495 BLI_str_format_int_grouped(number_str, number = 2147483647);
496 EXPECT_STREQ("2,147,483,647", number_str);
497
498 BLI_str_format_int_grouped(number_str, number = -2147483648);
499 EXPECT_STREQ("-2,147,483,648", number_str);
500 /* Ensure the limit is correct. */
501 EXPECT_EQ(sizeof(number_str), strlen(number_str) + 1);
502}
503
504/* BLI_str_format_uint64_grouped */
505TEST(string, StrFormatUint64Grouped)
506{
507 char number_str[BLI_STR_FORMAT_UINT64_GROUPED_SIZE];
508 uint64_t number;
509
510 BLI_str_format_uint64_grouped(number_str, number = 0);
511 EXPECT_STREQ("0", number_str);
512
513 BLI_str_format_uint64_grouped(number_str, number = 1);
514 EXPECT_STREQ("1", number_str);
515
516 BLI_str_format_uint64_grouped(number_str, number = 999);
517 EXPECT_STREQ("999", number_str);
518
519 BLI_str_format_uint64_grouped(number_str, number = 1000);
520 EXPECT_STREQ("1,000", number_str);
521
522 BLI_str_format_uint64_grouped(number_str, number = 18446744073709551615u);
523 EXPECT_STREQ("18,446,744,073,709,551,615", number_str);
524 /* Ensure the limit is correct. */
525 EXPECT_EQ(sizeof(number_str), strlen(number_str) + 1);
526}
527
529
530/* -------------------------------------------------------------------- */
533
534/* BLI_str_format_byte_unit */
535TEST(string, StrFormatByteUnits)
536{
538 long long int size;
539
540 /* Base 10 */
541 BLI_str_format_byte_unit(size_str, size = 0, true);
542 EXPECT_STREQ("0 B", size_str);
543 BLI_str_format_byte_unit(size_str, size = -0, true);
544 EXPECT_STREQ("0 B", size_str);
545
546 BLI_str_format_byte_unit(size_str, size = 1, true);
547 EXPECT_STREQ("1 B", size_str);
548 BLI_str_format_byte_unit(size_str, size = -1, true);
549 EXPECT_STREQ("-1 B", size_str);
550
551 BLI_str_format_byte_unit(size_str, size = 1000, true);
552 EXPECT_STREQ("1 KB", size_str);
553 BLI_str_format_byte_unit(size_str, size = -1000, true);
554 EXPECT_STREQ("-1 KB", size_str);
555
556 BLI_str_format_byte_unit(size_str, size = 1024, true);
557 EXPECT_STREQ("1 KB", size_str);
558 BLI_str_format_byte_unit(size_str, size = -1024, true);
559 EXPECT_STREQ("-1 KB", size_str);
560
561 /* LLONG_MAX - largest possible value */
562 BLI_str_format_byte_unit(size_str, size = 9223372036854775807, true);
563 EXPECT_STREQ("9223.372 PB", size_str);
564 BLI_str_format_byte_unit(size_str, size = -9223372036854775807, true);
565 EXPECT_STREQ("-9223.372 PB", size_str);
566
567 /* Base 2 */
568 BLI_str_format_byte_unit(size_str, size = 0, false);
569 EXPECT_STREQ("0 B", size_str);
570 BLI_str_format_byte_unit(size_str, size = -0, false);
571 EXPECT_STREQ("0 B", size_str);
572
573 BLI_str_format_byte_unit(size_str, size = 1, false);
574 EXPECT_STREQ("1 B", size_str);
575 BLI_str_format_byte_unit(size_str, size = -1, false);
576 EXPECT_STREQ("-1 B", size_str);
577
578 BLI_str_format_byte_unit(size_str, size = 1000, false);
579 EXPECT_STREQ("1000 B", size_str);
580 BLI_str_format_byte_unit(size_str, size = -1000, false);
581 EXPECT_STREQ("-1000 B", size_str);
582
583 BLI_str_format_byte_unit(size_str, size = 1024, false);
584 EXPECT_STREQ("1 KiB", size_str);
585 BLI_str_format_byte_unit(size_str, size = -1024, false);
586 EXPECT_STREQ("-1 KiB", size_str);
587
588 /* LLONG_MAX - largest possible value */
589 BLI_str_format_byte_unit(size_str, size = 9223372036854775807, false);
590 EXPECT_STREQ("8192.0 PiB", size_str);
591 BLI_str_format_byte_unit(size_str, size = -9223372036854775807, false);
592 EXPECT_STREQ("-8192.0 PiB", size_str);
593
594 /* Test maximum string length. */
595 BLI_str_format_byte_unit(size_str, size = -9223200000000000000, false);
596 EXPECT_STREQ("-8191.8472 PiB", size_str);
597 /* Ensure the limit is correct. */
598 EXPECT_EQ(sizeof(size_str), strlen(size_str) + 1);
599}
600
601/* BLI_str_format_byte_unit_compact */
602TEST(string, StrFormatByteUnitsCompact)
603{
605 long long int size;
606
607 /* Base 10 */
608 BLI_str_format_byte_unit_compact(size_str, size = 0, true);
609 EXPECT_STREQ("0B", size_str);
610 BLI_str_format_byte_unit_compact(size_str, size = 155, true);
611 EXPECT_STREQ("155B", size_str);
612 BLI_str_format_byte_unit_compact(size_str, size = 1'555, true);
613 EXPECT_STREQ("1K", size_str);
614 BLI_str_format_byte_unit_compact(size_str, size = 155'555, true);
615 EXPECT_STREQ(".1M", size_str);
616 BLI_str_format_byte_unit_compact(size_str, size = 1'000'000'000, true);
617 EXPECT_STREQ("1G", size_str);
618 BLI_str_format_byte_unit_compact(size_str, size = 550'000'000'000'000'000, true);
619 EXPECT_STREQ(".5E", size_str);
620
621 /* Largest possible value. */
622 BLI_str_format_byte_unit_compact(size_str, size = (1ULL << 63) - 1, true);
623 EXPECT_STREQ("9E", size_str);
624
625 /* Base 2 */
626 BLI_str_format_byte_unit_compact(size_str, size = 0, false);
627 EXPECT_STREQ("0B", size_str);
628 BLI_str_format_byte_unit_compact(size_str, size = 1024, false);
629 EXPECT_STREQ("1K", size_str);
630
631 /* Largest possible value. */
632 BLI_str_format_byte_unit_compact(size_str, size = (1ULL << 63) - 1, false);
633 EXPECT_STREQ("8E", size_str);
634}
635
637
638/* -------------------------------------------------------------------- */
641
642/* BLI_str_format_decimal_unit */
643TEST(string, StrFormatDecimalUnits)
644{
646 int size;
647
648 BLI_str_format_decimal_unit(size_str, size = 0);
649 EXPECT_STREQ("0", size_str);
650 BLI_str_format_decimal_unit(size_str, size = 1);
651 EXPECT_STREQ("1", size_str);
652 BLI_str_format_decimal_unit(size_str, size = 10);
653 EXPECT_STREQ("10", size_str);
654 BLI_str_format_decimal_unit(size_str, size = 15);
655 EXPECT_STREQ("15", size_str);
656 BLI_str_format_decimal_unit(size_str, size = 100);
657 EXPECT_STREQ("100", size_str);
658 BLI_str_format_decimal_unit(size_str, size = 155);
659 EXPECT_STREQ("155", size_str);
660 BLI_str_format_decimal_unit(size_str, size = 1000);
661 EXPECT_STREQ("1.0K", size_str);
662 BLI_str_format_decimal_unit(size_str, size = 1555);
663 EXPECT_STREQ("1.6K", size_str);
664 BLI_str_format_decimal_unit(size_str, size = 10000);
665 EXPECT_STREQ("10.0K", size_str);
666 BLI_str_format_decimal_unit(size_str, size = 15555);
667 EXPECT_STREQ("15.6K", size_str);
668 BLI_str_format_decimal_unit(size_str, size = 100000);
669 EXPECT_STREQ("100K", size_str);
670 BLI_str_format_decimal_unit(size_str, size = 100000);
671 EXPECT_STREQ("100K", size_str);
672 BLI_str_format_decimal_unit(size_str, size = 155555);
673 EXPECT_STREQ("156K", size_str);
674 BLI_str_format_decimal_unit(size_str, size = 1000000);
675 EXPECT_STREQ("1.0M", size_str);
676 BLI_str_format_decimal_unit(size_str, size = 1555555);
677 EXPECT_STREQ("1.6M", size_str);
678 BLI_str_format_decimal_unit(size_str, size = 10000000);
679 EXPECT_STREQ("10.0M", size_str);
680 BLI_str_format_decimal_unit(size_str, size = 15555555);
681 EXPECT_STREQ("15.6M", size_str);
682 BLI_str_format_decimal_unit(size_str, size = 100000000);
683 EXPECT_STREQ("100M", size_str);
684 BLI_str_format_decimal_unit(size_str, size = 155555555);
685 EXPECT_STREQ("156M", size_str);
686 BLI_str_format_decimal_unit(size_str, size = 1000000000);
687 EXPECT_STREQ("1.0B", size_str);
688
689 /* Largest possible value. */
690 BLI_str_format_decimal_unit(size_str, size = INT32_MAX);
691 EXPECT_STREQ("2.1B", size_str);
692
693 BLI_str_format_decimal_unit(size_str, size = -0);
694 EXPECT_STREQ("0", size_str);
695 BLI_str_format_decimal_unit(size_str, size = -1);
696 EXPECT_STREQ("-1", size_str);
697 BLI_str_format_decimal_unit(size_str, size = -10);
698 EXPECT_STREQ("-10", size_str);
699 BLI_str_format_decimal_unit(size_str, size = -15);
700 EXPECT_STREQ("-15", size_str);
701 BLI_str_format_decimal_unit(size_str, size = -100);
702 EXPECT_STREQ("-100", size_str);
703 BLI_str_format_decimal_unit(size_str, size = -155);
704 EXPECT_STREQ("-155", size_str);
705 BLI_str_format_decimal_unit(size_str, size = -1000);
706 EXPECT_STREQ("-1.0K", size_str);
707 BLI_str_format_decimal_unit(size_str, size = -1555);
708 EXPECT_STREQ("-1.6K", size_str);
709 BLI_str_format_decimal_unit(size_str, size = -10000);
710 EXPECT_STREQ("-10.0K", size_str);
711 BLI_str_format_decimal_unit(size_str, size = -15555);
712 EXPECT_STREQ("-15.6K", size_str);
713 BLI_str_format_decimal_unit(size_str, size = -100000);
714 EXPECT_STREQ("-100K", size_str);
715 BLI_str_format_decimal_unit(size_str, size = -155555);
716 EXPECT_STREQ("-156K", size_str);
717 BLI_str_format_decimal_unit(size_str, size = -1000000);
718 EXPECT_STREQ("-1.0M", size_str);
719 BLI_str_format_decimal_unit(size_str, size = -1555555);
720 EXPECT_STREQ("-1.6M", size_str);
721 BLI_str_format_decimal_unit(size_str, size = -10000000);
722 EXPECT_STREQ("-10.0M", size_str);
723 BLI_str_format_decimal_unit(size_str, size = -15555555);
724 EXPECT_STREQ("-15.6M", size_str);
725 BLI_str_format_decimal_unit(size_str, size = -100000000);
726 EXPECT_STREQ("-100M", size_str);
727 BLI_str_format_decimal_unit(size_str, size = -155555555);
728 EXPECT_STREQ("-156M", size_str);
729 BLI_str_format_decimal_unit(size_str, size = -1000000000);
730 EXPECT_STREQ("-1.0B", size_str);
731
732 /* Smallest possible value. */
733 BLI_str_format_decimal_unit(size_str, size = INT32_MIN);
734 EXPECT_STREQ("-2.1B", size_str);
735}
736
737/* BLI_str_format_integer_unit */
738TEST(string, StrFormatIntegerUnits)
739{
741 int size;
742
743 BLI_str_format_integer_unit(size_str, size = 0);
744 EXPECT_STREQ("0", size_str);
745 BLI_str_format_integer_unit(size_str, size = 1);
746 EXPECT_STREQ("1", size_str);
747 BLI_str_format_integer_unit(size_str, size = 10);
748 EXPECT_STREQ("10", size_str);
749 BLI_str_format_integer_unit(size_str, size = 15);
750 EXPECT_STREQ("15", size_str);
751 BLI_str_format_integer_unit(size_str, size = 100);
752 EXPECT_STREQ("100", size_str);
753 BLI_str_format_integer_unit(size_str, size = 155);
754 EXPECT_STREQ("155", size_str);
755 BLI_str_format_integer_unit(size_str, size = 1000);
756 EXPECT_STREQ("1K", size_str);
757 BLI_str_format_integer_unit(size_str, size = 1555);
758 EXPECT_STREQ("1K", size_str);
759 BLI_str_format_integer_unit(size_str, size = 10000);
760 EXPECT_STREQ("10K", size_str);
761 BLI_str_format_integer_unit(size_str, size = 15555);
762 EXPECT_STREQ("15K", size_str);
763 BLI_str_format_integer_unit(size_str, size = 100000);
764 EXPECT_STREQ(".1M", size_str);
765 BLI_str_format_integer_unit(size_str, size = 155555);
766 EXPECT_STREQ(".1M", size_str);
767 BLI_str_format_integer_unit(size_str, size = 1000000);
768 EXPECT_STREQ("1M", size_str);
769 BLI_str_format_integer_unit(size_str, size = 1555555);
770 EXPECT_STREQ("1M", size_str);
771 BLI_str_format_integer_unit(size_str, size = 2555555);
772 EXPECT_STREQ("2M", size_str);
773 BLI_str_format_integer_unit(size_str, size = 10000000);
774 EXPECT_STREQ("10M", size_str);
775 BLI_str_format_integer_unit(size_str, size = 15555555);
776 EXPECT_STREQ("15M", size_str);
777 BLI_str_format_integer_unit(size_str, size = 100000000);
778 EXPECT_STREQ(".1B", size_str);
779 BLI_str_format_integer_unit(size_str, size = 155555555);
780 EXPECT_STREQ(".1B", size_str);
781 BLI_str_format_integer_unit(size_str, size = 255555555);
782 EXPECT_STREQ(".2B", size_str);
783 BLI_str_format_integer_unit(size_str, size = 1000000000);
784 EXPECT_STREQ("1B", size_str);
785
786 /* Largest possible value. */
787 BLI_str_format_integer_unit(size_str, size = INT32_MAX);
788 EXPECT_STREQ("2B", size_str);
789
790 BLI_str_format_integer_unit(size_str, size = -0);
791 EXPECT_STREQ("0", size_str);
792 BLI_str_format_integer_unit(size_str, size = -1);
793 EXPECT_STREQ("-1", size_str);
794 BLI_str_format_integer_unit(size_str, size = -10);
795 EXPECT_STREQ("-10", size_str);
796 BLI_str_format_integer_unit(size_str, size = -15);
797 EXPECT_STREQ("-15", size_str);
798 BLI_str_format_integer_unit(size_str, size = -100);
799 EXPECT_STREQ("-100", size_str);
800 BLI_str_format_integer_unit(size_str, size = -155);
801 EXPECT_STREQ("-155", size_str);
802 BLI_str_format_integer_unit(size_str, size = -1000);
803 EXPECT_STREQ("-1K", size_str);
804 BLI_str_format_integer_unit(size_str, size = -1555);
805 EXPECT_STREQ("-1K", size_str);
806 BLI_str_format_integer_unit(size_str, size = -10000);
807 EXPECT_STREQ("-10K", size_str);
808 BLI_str_format_integer_unit(size_str, size = -15555);
809 EXPECT_STREQ("-15K", size_str);
810 BLI_str_format_integer_unit(size_str, size = -100000);
811 EXPECT_STREQ("-.1M", size_str);
812 BLI_str_format_integer_unit(size_str, size = -155555);
813 EXPECT_STREQ("-.1M", size_str);
814 BLI_str_format_integer_unit(size_str, size = -1000000);
815 EXPECT_STREQ("-1M", size_str);
816 BLI_str_format_integer_unit(size_str, size = -1555555);
817 EXPECT_STREQ("-1M", size_str);
818 BLI_str_format_integer_unit(size_str, size = -10000000);
819 EXPECT_STREQ("-10M", size_str);
820 BLI_str_format_integer_unit(size_str, size = -15555555);
821 EXPECT_STREQ("-15M", size_str);
822 BLI_str_format_integer_unit(size_str, size = -100000000);
823 EXPECT_STREQ("-.1B", size_str);
824 BLI_str_format_integer_unit(size_str, size = -155555555);
825 EXPECT_STREQ("-.1B", size_str);
826 BLI_str_format_integer_unit(size_str, size = -1000000000);
827 EXPECT_STREQ("-1B", size_str);
828
829 /* Smallest possible value. */
830 BLI_str_format_integer_unit(size_str, size = INT32_MIN);
831 EXPECT_STREQ("-2B", size_str);
832}
833
835
836/* -------------------------------------------------------------------- */
839
840TEST(string, StringNLen)
841{
842 EXPECT_EQ(0, BLI_strnlen("", 0));
843 EXPECT_EQ(0, BLI_strnlen("", 1));
844 EXPECT_EQ(0, BLI_strnlen("", 100));
845
846 EXPECT_EQ(0, BLI_strnlen("x", 0));
847 EXPECT_EQ(1, BLI_strnlen("x", 1));
848 EXPECT_EQ(1, BLI_strnlen("x", 100));
849
850 /* `ü` is `\xc3\xbc`. */
851 EXPECT_EQ(2, BLI_strnlen("ü", 100));
852
853 EXPECT_EQ(0, BLI_strnlen("this is a longer string", 0));
854 EXPECT_EQ(1, BLI_strnlen("this is a longer string", 1));
855 EXPECT_EQ(5, BLI_strnlen("this is a longer string", 5));
856 EXPECT_EQ(47, BLI_strnlen("This string writes about an agent without name.", 100));
857}
858
860
861/* -------------------------------------------------------------------- */
864
865#define BUFFER_SIZE 128
866
867static void string_join_array_test_truncate(const char *strings[],
868 int strings_num,
869 char buffer[BUFFER_SIZE])
870{
871 const int buffer_len = BLI_string_join_array(buffer, BUFFER_SIZE, strings, strings_num);
872
873 { /* Ensure the allocated version is the same. */
874 char *buffer_alloc = BLI_string_join_arrayN(strings, strings_num);
875 EXPECT_STREQ(buffer_alloc, buffer);
876 MEM_delete(buffer_alloc);
877 }
878
879 for (int dst_size = buffer_len + 1; dst_size > 0; dst_size--) {
880 char dst_tmp[BUFFER_SIZE];
881 int dst_tmp_len = BLI_string_join_array(dst_tmp, dst_size, strings, strings_num);
882 EXPECT_EQ(dst_tmp_len + 1, dst_size);
883 EXPECT_EQ(strncmp(dst_tmp, buffer, dst_tmp_len), 0);
884 }
885}
886
887static void string_join_array_with_sep_char_test_truncate(const char *strings[],
888 int strings_num,
889 char buffer[BUFFER_SIZE])
890{
891 const int buffer_len = BLI_string_join_array_by_sep_char(
892 buffer, BUFFER_SIZE, '|', strings, strings_num);
893
894 { /* Ensure the allocated version is the same. */
895 char *buffer_alloc = BLI_string_join_array_by_sep_charN('|', strings, strings_num);
896 EXPECT_STREQ(buffer_alloc, buffer);
897 MEM_delete(buffer_alloc);
898 }
899
900 for (int dst_size = buffer_len + 1; dst_size > 0; dst_size--) {
901 char dst_tmp[BUFFER_SIZE];
902 int dst_tmp_len = BLI_string_join_array_by_sep_char(
903 dst_tmp, dst_size, '|', strings, strings_num);
904 EXPECT_EQ(dst_tmp_len + 1, dst_size);
905 EXPECT_EQ(strncmp(dst_tmp, buffer, dst_tmp_len), 0);
906 }
907}
908
909TEST(string, StrJoin_Truncate)
910{
911 char buffer[BUFFER_SIZE];
912 { /* Multiple single char words. */
913 const char *strings[] = {"a", "b", "c", "d", "e", "f"};
914 string_join_array_test_truncate(strings, ARRAY_SIZE(strings), buffer);
915 EXPECT_STREQ(buffer, "abcdef");
917 EXPECT_STREQ(buffer, "a|b|c|d|e|f");
918 }
919 { /* Multiple char pair words. */
920 const char *strings[] = {"aa", "bb", "cc", "dd", "ee", "ff"};
921 string_join_array_test_truncate(strings, ARRAY_SIZE(strings), buffer);
922 EXPECT_STREQ(buffer, "aabbccddeeff");
924 EXPECT_STREQ(buffer, "aa|bb|cc|dd|ee|ff");
925 }
926 { /* Multiple empty words. */
927 const char *strings[] = {"", "", "", "", "", ""};
928 string_join_array_test_truncate(strings, ARRAY_SIZE(strings), buffer);
929 EXPECT_STREQ(buffer, "");
931 EXPECT_STREQ(buffer, "|||||");
932 }
933 { /* Single word. */
934 const char *strings[] = {"test"};
935 string_join_array_test_truncate(strings, ARRAY_SIZE(strings), buffer);
936 EXPECT_STREQ(buffer, "test");
938 EXPECT_STREQ(buffer, "test");
939 }
940 { /* Empty item. */
941 const char *strings[] = {""};
942 string_join_array_test_truncate(strings, ARRAY_SIZE(strings), buffer);
943 EXPECT_STREQ(buffer, "");
945 EXPECT_STREQ(buffer, "");
946 }
947 { /* Empty array. */
948 const char *strings[] = {"a"};
949 string_join_array_test_truncate(strings, 0, buffer);
950 EXPECT_STREQ(buffer, "");
952 EXPECT_STREQ(buffer, "");
953 }
954}
955
956#undef BUFFER_SIZE
957
959
960/* -------------------------------------------------------------------- */
963
964struct WordInfo {
965 WordInfo() = default;
966 WordInfo(int start, int end) : start(start), end(end) {}
967 bool operator==(const WordInfo &other) const
968 {
969 return start == other.start && end == other.end;
970 }
971 int start, end;
972};
973static std::ostream &operator<<(std::ostream &os, const WordInfo &word_info)
974{
975 os << "start: " << word_info.start << ", end: " << word_info.end;
976 return os;
977}
978
979class StringFindSplitWords : public testing::Test {
980 protected:
982
983 /* If max_words is -1 it will be initialized from the number of expected
984 * words +1. This way there is no need to pass an explicit number of words,
985 * but is also making it possible to catch situation when too many words
986 * are being returned. */
987 void testStringFindSplitWords(const string &str,
988 const size_t max_length,
989 initializer_list<WordInfo> expected_words_info_init,
990 int max_words = -1)
991 {
992 const vector<WordInfo> expected_words_info = expected_words_info_init;
993 if (max_words != -1) {
994 CHECK_LE(max_words, expected_words_info.size() - 1);
995 }
996 /* Since number of word info is used here, this makes it so we allow one
997 * extra word to be collected from the input. This allows to catch possible
998 * issues with word splitting not doing a correct thing. */
999 const int effective_max_words = (max_words == -1) ? expected_words_info.size() : max_words;
1000 /* One extra element for the {-1, -1}. */
1001 vector<WordInfo> actual_word_info(effective_max_words + 1, WordInfo(-1, -1));
1002 const int actual_word_num = BLI_string_find_split_words(
1003 str.c_str(),
1004 max_length,
1005 ' ',
1006 reinterpret_cast<int (*)[2]>(actual_word_info.data()),
1007 effective_max_words);
1008 /* Schrink actual array to an actual number of words, so we can compare
1009 * vectors as-is. */
1010 EXPECT_LE(actual_word_num, actual_word_info.size() - 1);
1011 actual_word_info.resize(actual_word_num + 1);
1012 /* Perform actual comparison. */
1013 EXPECT_EQ_VECTOR(actual_word_info, expected_words_info);
1014 }
1015
1016 void testStringFindSplitWords(const string &str,
1017 initializer_list<WordInfo> expected_words_info_init)
1018 {
1019 testStringFindSplitWords(str, str.length(), expected_words_info_init);
1020 }
1021};
1022
1023/* BLI_string_find_split_words */
1025{
1026 testStringFindSplitWords("t", {{0, 1}, {-1, -1}});
1027 testStringFindSplitWords("test", {{0, 4}, {-1, -1}});
1028}
1030{
1031 testStringFindSplitWords("f t w", {{0, 1}, {2, 1}, {4, 1}, {-1, -1}});
1032 testStringFindSplitWords("find three words", {{0, 4}, {5, 5}, {11, 5}, {-1, -1}});
1033}
1035{
1036 testStringFindSplitWords("# ## ### ####", {{0, 1}, {2, 2}, {5, 3}, {9, 4}, {-1, -1}});
1037 testStringFindSplitWords("# # # #", {{0, 1}, {3, 1}, {7, 1}, {12, 1}, {-1, -1}});
1038}
1040{
1041 testStringFindSplitWords(" t", {{3, 1}, {-1, -1}});
1042 testStringFindSplitWords(" test", {{3, 4}, {-1, -1}});
1043}
1045{
1046 testStringFindSplitWords("t ", {{0, 1}, {-1, -1}});
1047 testStringFindSplitWords("test ", {{0, 4}, {-1, -1}});
1048}
1049TEST_F(StringFindSplitWords, Trailing_LeftRight)
1050{
1051 testStringFindSplitWords(" surrounding space test 123 ",
1052 {{3, 11}, {15, 5}, {21, 4}, {28, 3}, {-1, -1}});
1053}
1055{
1056 testStringFindSplitWords("", {{-1, -1}});
1057}
1059{
1060 testStringFindSplitWords(" ", {{-1, -1}});
1061 testStringFindSplitWords(" ", {{-1, -1}});
1062}
1064{
1065 const string words = "too many chars";
1066 const int words_len = words.length();
1067 testStringFindSplitWords(words, words_len, {{0, 3}, {4, 4}, {9, 5}, {-1, -1}}, 3);
1068 testStringFindSplitWords(words, words_len, {{0, 3}, {4, 4}, {-1, -1}}, 2);
1069 testStringFindSplitWords(words, words_len, {{0, 3}, {-1, -1}}, 1);
1070 testStringFindSplitWords(words, words_len, {{-1, -1}}, 0);
1071}
1073{
1074 const string words = "too many chars";
1075 const int words_len = words.length();
1076 testStringFindSplitWords(words, words_len, {{0, 3}, {4, 4}, {9, 5}, {-1, -1}});
1077 testStringFindSplitWords(words, words_len - 1, {{0, 3}, {4, 4}, {9, 4}, {-1, -1}});
1078 testStringFindSplitWords(words, words_len - 5, {{0, 3}, {4, 4}, {-1, -1}});
1079 testStringFindSplitWords(words, 1, {{0, 1}, {-1, -1}});
1080 testStringFindSplitWords(words, 0, {{-1, -1}});
1081}
1082
1084
1085/* -------------------------------------------------------------------- */
1088
1089/* #BLI_string_elem_split_by_delim */
1090
1091TEST(string, StringElemEmpty)
1092{
1094
1100}
1101
1102TEST(string, StringElemSingle)
1103{
1106
1109}
1110
1111TEST(string, StringElemComplex)
1112{
1113 EXPECT_TRUE(BLI_string_elem_split_by_delim("TEST", ':', "TEST"));
1114
1115 EXPECT_TRUE(BLI_string_elem_split_by_delim(":TEST", ':', "TEST"));
1116 EXPECT_TRUE(BLI_string_elem_split_by_delim("TEST:", ':', "TEST"));
1117 EXPECT_TRUE(BLI_string_elem_split_by_delim(":TEST:", ':', "TEST"));
1118
1119 EXPECT_TRUE(BLI_string_elem_split_by_delim("::TEST", ':', "TEST"));
1120 EXPECT_TRUE(BLI_string_elem_split_by_delim("TEST::", ':', "TEST"));
1121 EXPECT_TRUE(BLI_string_elem_split_by_delim("::TEST::", ':', "TEST"));
1122
1123 EXPECT_FALSE(BLI_string_elem_split_by_delim(":TEST ", ':', "TEST"));
1124 EXPECT_FALSE(BLI_string_elem_split_by_delim(" TEST:", ':', "TEST"));
1125 EXPECT_FALSE(BLI_string_elem_split_by_delim(": TEST :", ':', "TEST"));
1126
1127 EXPECT_TRUE(BLI_string_elem_split_by_delim("A:B:TEST", ':', "TEST"));
1128 EXPECT_TRUE(BLI_string_elem_split_by_delim("TEST:A:B", ':', "TEST"));
1129 EXPECT_TRUE(BLI_string_elem_split_by_delim("A:TEST:B", ':', "TEST"));
1130 EXPECT_TRUE(BLI_string_elem_split_by_delim(":A:TEST:B:", ':', "TEST"));
1131}
1132
1134
1135/* -------------------------------------------------------------------- */
1138
1139/* BLI_strncasestr */
1140TEST(string, StringStrncasestr)
1141{
1142 const char *str_test0 = "search here";
1143 const char *res;
1144
1145 res = BLI_strncasestr(str_test0, "", 0);
1146 EXPECT_EQ(res, str_test0);
1147
1148 res = BLI_strncasestr(str_test0, " ", 1);
1149 EXPECT_EQ(res, str_test0 + 6);
1150
1151 res = BLI_strncasestr(str_test0, "her", 3);
1152 EXPECT_EQ(res, str_test0 + 7);
1153
1154 res = BLI_strncasestr(str_test0, "ARCh", 4);
1155 EXPECT_EQ(res, str_test0 + 2);
1156
1157 res = BLI_strncasestr(str_test0, "earcq", 4);
1158 EXPECT_EQ(res, str_test0 + 1);
1159
1160 res = BLI_strncasestr(str_test0, "not there", 9);
1161 EXPECT_EQ(res, static_cast<void *>(nullptr));
1162}
1163
1165
1166/* -------------------------------------------------------------------- */
1169
1170/* BLI_string_max_possible_word_count */
1179
1181
1182/* -------------------------------------------------------------------- */
1185
1186/* BLI_string_is_decimal */
1187TEST(string, StrIsDecimal)
1188{
1190 EXPECT_FALSE(BLI_string_is_decimal("je moeder"));
1191 EXPECT_FALSE(BLI_string_is_decimal("je møder"));
1192 EXPECT_FALSE(BLI_string_is_decimal("Agent 327"));
1193 EXPECT_FALSE(BLI_string_is_decimal("Agent\000327"));
1198
1202 EXPECT_TRUE(BLI_string_is_decimal("11342908713948713498745980171334059871345098713405981734"));
1203}
1204
1206
1207/* -------------------------------------------------------------------- */
1210
1211/* BLI_strcasecmp_natural */
1212class StringCasecmpNatural : public testing::Test {
1213 protected:
1215
1216 using CompareWordsArray = vector<std::array<const char *, 2>>;
1217
1219 {
1220 for (const auto &item : items) {
1221 int res = BLI_strcasecmp_natural(item[0], item[1]);
1222 EXPECT_EQ(res, 0);
1223 }
1224 }
1226 {
1227 for (const auto &item : items) {
1228 int res = BLI_strcasecmp_natural(item[0], item[1]);
1229 EXPECT_LT(res, 0);
1230 }
1231 }
1233 {
1234 for (const auto &item : items) {
1235 int res = BLI_strcasecmp_natural(item[0], item[1]);
1236 EXPECT_GT(res, 0);
1237 }
1238 }
1239
1241 {
1242 CompareWordsArray ret_array;
1243
1244 /* E.g. {{"a", "b"}, {"ab", "cd"}} becomes {{"b", "a"}, {"cd", "ab"}} */
1245
1246 ret_array.reserve(items.size());
1247 for (const auto &item : items) {
1248 ret_array.push_back({item[1], item[0]});
1249 }
1250
1251 return ret_array;
1252 }
1253};
1254
1256
1257/* -------------------------------------------------------------------- */
1260
1262{
1263 const CompareWordsArray equal{
1264 {"", ""},
1265 };
1266 const CompareWordsArray negative{
1267 {"", "a"},
1268 {"", "A"},
1269 };
1270 CompareWordsArray positive = copyWithSwappedWords(negative);
1271
1272 testReturnsZeroForAll(equal);
1273 testReturnsLessThanZeroForAll(negative);
1274 testReturnsMoreThanZeroForAll(positive);
1275}
1276
1278{
1279 const CompareWordsArray equal{
1280 {" ", " "},
1281 {" a", " a"},
1282 {" a ", " a "},
1283 };
1284 const CompareWordsArray negative{
1285 {"", " "},
1286 {"", " a"},
1287 {"", " a "},
1288 {" ", " a"},
1289 };
1290 CompareWordsArray positive = copyWithSwappedWords(negative);
1291
1292 testReturnsZeroForAll(equal);
1293 testReturnsLessThanZeroForAll(negative);
1294 testReturnsMoreThanZeroForAll(positive);
1295}
1296
1297TEST_F(StringCasecmpNatural, TextOnlyLowerCase)
1298{
1299 const CompareWordsArray equal{
1300 {"a", "a"},
1301 {"aa", "aa"},
1302 {"ab", "ab"},
1303 {"ba", "ba"},
1304 {"je møder", "je møder"},
1305 };
1306 const CompareWordsArray negative{
1307 {"a", "b"},
1308 {"a", "aa"},
1309 {"a", "ab"},
1310 {"aa", "b"},
1311 {"je møda", "je møder"},
1312 };
1313 CompareWordsArray positive = copyWithSwappedWords(negative);
1314
1315 testReturnsZeroForAll(equal);
1316 testReturnsLessThanZeroForAll(negative);
1317 testReturnsMoreThanZeroForAll(positive);
1318}
1319
1321{
1322 const CompareWordsArray equal{
1323 {"A", "A"},
1324 {"AA", "AA"},
1325 {"AB", "AB"},
1326 {"Ab", "Ab"},
1327 {"aB", "aB"},
1328 };
1329 const CompareWordsArray negative{
1330 {"A", "a"},
1331 {"A", "B"},
1332 {"A", "b"},
1333 {"a", "B"},
1334 {"AA", "aA"},
1335 {"AA", "aA"},
1336 {"Ab", "ab"},
1337 {"AB", "Ab"},
1338 /* Different lengths */
1339 {"A", "ab"},
1340 {"Aa", "b"},
1341 {"aA", "b"},
1342 {"AA", "b"},
1343 {"A", "Ab"},
1344 {"A", "aB"},
1345 {"Aa", "B"},
1346 {"aA", "B"},
1347 {"AA", "B"},
1348 };
1349 CompareWordsArray positive = copyWithSwappedWords(negative);
1350
1351 testReturnsZeroForAll(equal);
1352 testReturnsLessThanZeroForAll(negative);
1353 testReturnsMoreThanZeroForAll(positive);
1354}
1355
1357{
1358 const CompareWordsArray equal{
1359 {".", "."},
1360 {". ", ". "},
1361 {" .", " ."},
1362 {" . ", " . "},
1363 };
1364 const CompareWordsArray negative{
1365 {".", ". "},
1366 {" .", " . "},
1367 {"foo.bar", "foo 1.bar"},
1368 };
1369 CompareWordsArray positive = copyWithSwappedWords(negative);
1370
1371 testReturnsZeroForAll(equal);
1372 testReturnsLessThanZeroForAll(negative);
1373 testReturnsMoreThanZeroForAll(positive);
1374}
1375
1377{
1378 const CompareWordsArray equal{
1379 {"0", "0"},
1380 {"0001", "0001"},
1381 {"42", "42"},
1382 {"0042", "0042"},
1383 };
1384 const CompareWordsArray negative{
1385 /* If numeric values are equal, number of leading zeros is used as tiebreaker. */
1386 {"1", "0001"},
1387 {"01", "001"},
1388 {"0042", "0043"},
1389 {"0042", "43"},
1390 };
1391 const CompareWordsArray positive = copyWithSwappedWords(negative);
1392
1393 testReturnsZeroForAll(equal);
1394 testReturnsLessThanZeroForAll(negative);
1395 testReturnsMoreThanZeroForAll(positive);
1396}
1397
1399{
1400 const CompareWordsArray equal{
1401 {"00je møder1", "00je møder1"},
1402 {".0 ", ".0 "},
1403 {" 1.", " 1."},
1404 {" .0 ", " .0 "},
1405 };
1406 const CompareWordsArray negative{
1407 {"00je møder0", "00je møder1"},
1408 {"05je møder0", "06je møder1"},
1409 {"Cube", "Cube.001"},
1410 {"Cube.001", "Cube.002"},
1411 {"CUbe.001", "Cube.002"},
1412 {"CUbe.002", "Cube.002"},
1413 };
1414 const CompareWordsArray positive = copyWithSwappedWords(negative);
1415
1416 testReturnsZeroForAll(equal);
1417 testReturnsLessThanZeroForAll(negative);
1418 testReturnsMoreThanZeroForAll(positive);
1419}
1420
1422
1423/* -------------------------------------------------------------------- */
1428
1429class StringEscape : public testing::Test {
1430 protected:
1431 StringEscape() = default;
1432
1433 using CompareWordsArray = vector<std::array<const char *, 2>>;
1434
1436 {
1437 size_t dst_test_len;
1438 char dst_test[64]; /* Must be big enough for all input. */
1439 for (const auto &item : items) {
1440 /* Validate the static size is big enough (test the test itself). */
1441 EXPECT_LT((strlen(item[0]) * 2) + 1, sizeof(dst_test));
1442 /* Escape the string. */
1443 dst_test_len = BLI_str_escape(dst_test, item[0], sizeof(dst_test));
1444 EXPECT_STREQ(dst_test, item[1]);
1445 EXPECT_EQ(dst_test_len, strlen(dst_test));
1446 /* Escape back. */
1447 dst_test_len = BLI_str_unescape(dst_test, item[1], strlen(item[1]));
1448 EXPECT_STREQ(dst_test, item[0]);
1449 EXPECT_EQ(dst_test_len, strlen(dst_test));
1450 }
1451 }
1452};
1453
1455{
1456 /* NOTE: clang-tidy `modernize-raw-string-literal` is disabled as it causes errors with MSVC.
1457 * TODO: investigate resolving with `/Zc:preprocessor` flag. */
1458
1459 const CompareWordsArray equal{
1460 {"", ""},
1461 {"/", "/"},
1462 {"'", "'"},
1463 {"?", "?"},
1464 };
1465
1466 const CompareWordsArray escaped{
1467 {"\\", "\\\\"},
1468 {"A\\", "A\\\\"},
1469 {"\\A", "\\\\A"},
1470 {"A\\B", "A\\\\B"},
1471 {"?", "?"},
1472 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1473 {"\"\\", "\\\"\\\\"},
1474 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1475 {"\\\"", "\\\\\\\""},
1476 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1477 {"\"\\\"", "\\\"\\\\\\\""},
1478
1479 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1480 {"\"\"\"", "\\\"\\\"\\\""},
1481 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1482 {"\\\\\\", "\\\\\\\\\\\\"},
1483 };
1484
1485 testEscapeWords(equal);
1486 testEscapeWords(escaped);
1487}
1488
1490{
1491 const CompareWordsArray escaped{
1492 {"\n", "\\n"},
1493 {"\r", "\\r"},
1494 {"\t", "\\t"},
1495 {"\a", "\\a"},
1496 {"\b", "\\b"},
1497 {"\f", "\\f"},
1498 {"A\n", "A\\n"},
1499 {"\nA", "\\nA"},
1500 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1501 {"\n\r\t\a\b\f", "\\n\\r\\t\\a\\b\\f"},
1502 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1503 {"\n_\r_\t_\a_\b_\f", "\\n_\\r_\\t_\\a_\\b_\\f"},
1504 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1505 {"\n\\\r\\\t\\\a\\\b\\\f", "\\n\\\\\\r\\\\\\t\\\\\\a\\\\\\b\\\\\\f"},
1506 };
1507
1508 testEscapeWords(escaped);
1509}
1510
1512{
1513#define STR_ESCAPE_TRUNC_EXPECT(src, dst_expect, dst_maxncpy) \
1514 { \
1515 char dst[dst_maxncpy + 1]; \
1516 dst[dst_maxncpy] = 0xff; \
1517 size_t len = BLI_str_escape(dst, src, dst_maxncpy); \
1518 EXPECT_EQ(len, strlen(dst)); \
1519 EXPECT_STREQ(dst, dst_expect); \
1520 EXPECT_EQ(dst[dst_maxncpy], 0xff); \
1521 }
1522
1523 STR_ESCAPE_TRUNC_EXPECT("A", "", 1);
1524 STR_ESCAPE_TRUNC_EXPECT("AB", "AB", 3);
1525 STR_ESCAPE_TRUNC_EXPECT("AB", "A", 2);
1526
1527 STR_ESCAPE_TRUNC_EXPECT("\n", "\\n", 3);
1528 STR_ESCAPE_TRUNC_EXPECT("\n", "", 2);
1529 STR_ESCAPE_TRUNC_EXPECT("A\n", "A\\n", 4);
1530 STR_ESCAPE_TRUNC_EXPECT("A\n", "A", 3);
1531 STR_ESCAPE_TRUNC_EXPECT("\nA", "\\n", 3);
1532
1533 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1534 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "\\n\\n\\n", 7);
1535 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1536 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "\\n\\n", 6);
1537 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1538 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "\\n\\n", 5);
1539 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "\\n", 4);
1540 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "\\n", 3);
1541 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "", 2);
1542 STR_ESCAPE_TRUNC_EXPECT("\n\n\n", "", 1);
1543
1544 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1545 STR_ESCAPE_TRUNC_EXPECT("\"", "\\\"", 3);
1546 STR_ESCAPE_TRUNC_EXPECT("\"", "", 2);
1547 /* NOLINTNEXTLINE: modernize-raw-string-literal. */
1548 STR_ESCAPE_TRUNC_EXPECT("A\"", "A\\\"", 4);
1549 STR_ESCAPE_TRUNC_EXPECT("A\"", "A", 3);
1550
1551#undef STR_ESCAPE_TRUNC_EXPECT
1552}
1553
1554TEST_F(StringEscape, UnescapeTrailingBackslash)
1555{
1556 /* The back-slash ends the range, the "n" after it must not be read. */
1557 const char *src = "A\\n";
1558 char dst[8];
1559 size_t dst_len;
1560
1561 dst_len = BLI_str_unescape(dst, src, 2);
1562 EXPECT_STREQ(dst, "A\\");
1563 EXPECT_EQ(dst_len, 2);
1564
1565 bool is_complete;
1566 dst_len = BLI_str_unescape_ex(dst, src, 2, sizeof(dst), &is_complete);
1567 EXPECT_STREQ(dst, "A\\");
1568 EXPECT_EQ(dst_len, 2);
1569 EXPECT_TRUE(is_complete);
1570}
1571
1573{
1574#define STR_UNESCAPE_EXPECT(src, dst_expect, src_len) \
1575 { \
1576 char dst[src_len + 2]; \
1577 dst[src_len + 1] = 0xff; \
1578 size_t len = BLI_str_unescape(dst, src, src_len); \
1579 EXPECT_EQ(len, strlen(dst)); \
1580 EXPECT_STREQ(dst, dst_expect); \
1581 EXPECT_EQ(dst[src_len + 1], 0xff); \
1582 }
1583
1584 STR_UNESCAPE_EXPECT("AB", "AB", 2);
1585 STR_UNESCAPE_EXPECT("AB", "A", 1);
1586 STR_UNESCAPE_EXPECT("AB", "", 0);
1587 STR_UNESCAPE_EXPECT("A\\n", "A\n", 3);
1588 STR_UNESCAPE_EXPECT("A\\n", "A\\", 2);
1589
1590#undef STR_UNESCAPE_EXPECT
1591
1592 char dst[8];
1593 bool is_complete;
1594 const size_t len = BLI_str_unescape_ex(dst, "AB", 1, sizeof(dst), &is_complete);
1595 EXPECT_STREQ(dst, "A");
1596 EXPECT_EQ(len, 1);
1597 EXPECT_TRUE(is_complete);
1598}
1599
1601
1602TEST(BLI_string, bounded_strcpy)
1603{
1604 {
1605 char str[8];
1606 STRNCPY(str, "Hello");
1607 EXPECT_STREQ(str, "Hello");
1608 }
1609
1610 {
1611 char str[8];
1612 STRNCPY(str, "Hello, World!");
1613 EXPECT_STREQ(str, "Hello, ");
1614 }
1615}
1616
1617TEST(BLI_string, StartsWith)
1618{
1619 EXPECT_TRUE(BLI_str_startswith("ab", "a"));
1620 EXPECT_FALSE(BLI_str_startswith("ab", "b"));
1621 EXPECT_TRUE(BLI_str_startswith("ab", "ab"));
1624}
1625
1626TEST(BLI_string, EndsWith)
1627{
1628 EXPECT_TRUE(BLI_str_endswith("ab", "b"));
1629 EXPECT_FALSE(BLI_str_endswith("ab", "a"));
1630 EXPECT_TRUE(BLI_str_endswith("ab", "ab"));
1631 EXPECT_TRUE(BLI_str_endswith("ab", ""));
1633}
1634
1635} // namespace blender
#define INT32_MAX
#define INT32_MIN
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE
Definition BLI_string.hh:31
#define BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE
Definition BLI_string.hh:37
#define BLI_STR_FORMAT_INT32_GROUPED_SIZE
Definition BLI_string.hh:28
#define BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE
Definition BLI_string.hh:40
#define BLI_STR_FORMAT_UINT64_GROUPED_SIZE
Definition BLI_string.hh:22
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_COMPACT_SIZE
Definition BLI_string.hh:34
#define STRNCPY_ENSURE_PAD_EXPECT(src, dst_expect, dst_maxncpy)
#define STR_N_CAT(dst_init, dst_size, src, result_expect)
#define STR_UNESCAPE_EXPECT(src, dst_expect, src_len)
#define BUFFER_SIZE
#define STR_ESCAPE_TRUNC_EXPECT(src, dst_expect, dst_maxncpy)
#define STR_REPLACE_RANGE(src, size, beg, end, dst, result_expect)
Read Blender memory allocation and freeing.
CompareWordsArray copyWithSwappedWords(const CompareWordsArray &items)
void testReturnsLessThanZeroForAll(const CompareWordsArray &items)
void testReturnsZeroForAll(const CompareWordsArray &items)
void testReturnsMoreThanZeroForAll(const CompareWordsArray &items)
vector< std::array< const char *, 2 > > CompareWordsArray
vector< std::array< const char *, 2 > > CompareWordsArray
void testEscapeWords(const CompareWordsArray &items)
void testStringFindSplitWords(const string &str, const size_t max_length, initializer_list< WordInfo > expected_words_info_init, int max_words=-1)
void testStringFindSplitWords(const string &str, initializer_list< WordInfo > expected_words_info_init)
#define str(s)
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
#define EXPECT_FALSE(result)
#define EXPECT_GT(result, expect)
#define EXPECT_LT(result, expect)
#define EXPECT_TRUE(result)
#define EXPECT_LE(result, expect)
#define CHECK_LE(a, b)
Definition log.h:123
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::ostream & operator<<(std::ostream &stream, const AssetCatalogPath &path_to_append)
BMesh Iterators.
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
bool BLI_string_is_decimal(const char *string) ATTR_NONNULL(1)
void BLI_str_format_decimal_unit(char dst[BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE], int number_to_format) ATTR_NONNULL(1)
unsigned __int64 uint64_t
Definition BKE_text.h:91
char * STRNCPY(char(&dst)[N], const char *src)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int bool bool bool size_t BLI_str_partition(const char *str, const char delim[], const char **sep, const char **suf) ATTR_NONNULL(1
unsigned int uint
Definition BKE_text.h:55
int bool bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL(1
char * BLI_string_join_arrayN(const char *strings[], uint strings_num) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_str_format_byte_unit_compact(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_COMPACT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
bool bool int BLI_string_find_split_words(const char *str, size_t str_maxlen, char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static void string_join_array_test_truncate(const char *strings[], int strings_num, char buffer[BUFFER_SIZE])
TEST(array_store, Nop)
char * BLI_string_join_array_by_sep_charN(char sep, const char *strings[], uint strings_num) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool bool int bool BLI_string_elem_split_by_delim(const char *haystack, const char delim, const char *needle) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t size_t BLI_str_rpartition_utf8(const char *str, const unsigned int delim[], const char **r_sep, const char **r_suf) ATTR_NONNULL(1
size_t BLI_str_partition_utf8(const char *str, const unsigned int delim[], const char **r_sep, const char **r_suf) ATTR_NONNULL(1
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
int bool bool bool size_t size_t size_t int BLI_string_max_possible_word_count(int str_len) ATTR_WARN_UNUSED_RESULT
TEST_F(BrushTest, deep_copy)
Definition brush_test.cc:52
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t BLI_string_join_array_by_sep_char(char *result, size_t result_maxncpy, char sep, const char *strings[], uint strings_num) ATTR_NONNULL()
size_t BLI_str_unescape_ex(char *__restrict dst, const char *__restrict src, size_t src_len, size_t dst_maxncpy, bool *r_is_complete) ATTR_NONNULL(1
size_t BLI_str_format_int_grouped(char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE], int num) ATTR_NONNULL(1)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_str_format_integer_unit(char dst[BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE], int number_to_format) ATTR_NONNULL(1)
int bool BLI_str_startswith(const char *__restrict str, const char *__restrict start) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t size_t size_t BLI_str_partition_ex_utf8(const char *str, const char *end, const unsigned int delim[], const char **r_sep, const char **r_suf, bool from_right) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int bool bool bool size_t size_t size_t BLI_str_partition_ex(const char *str, const char *end, const char delim[], const char **sep, const char **suf, bool from_right) ATTR_NONNULL(1
int bool bool bool size_t size_t BLI_str_rpartition(const char *str, const char delim[], const char **sep, const char **suf) ATTR_NONNULL(1
size_t size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, size_t src_len) ATTR_NONNULL(1
static void string_join_array_with_sep_char_test_truncate(const char *strings[], int strings_num, char buffer[BUFFER_SIZE])
size_t BLI_string_join_array(char *result, size_t result_maxncpy, const char *strings[], uint strings_num) ATTR_NONNULL()
int char char * BLI_strncasestr(const char *s, const char *find, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t BLI_str_format_uint64_grouped(char dst[BLI_STR_FORMAT_UINT64_GROUPED_SIZE], uint64_t num) ATTR_NONNULL(1)
bool operator==(const WordInfo &other) const
WordInfo()=default
WordInfo(int start, int end)