1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fxcrt/widestring.h"
6
7 #include <algorithm>
8 #include <iterator>
9 #include <vector>
10
11 #include "build/build_config.h"
12 #include "core/fxcrt/fx_string.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/base/containers/contains.h"
15 #include "third_party/base/containers/span.h"
16
17 namespace fxcrt {
18
TEST(WideString,ElementAccess)19 TEST(WideString, ElementAccess) {
20 const WideString abc(L"abc");
21 EXPECT_EQ(L'a', abc[0]);
22 EXPECT_EQ(L'b', abc[1]);
23 EXPECT_EQ(L'c', abc[2]);
24 #ifndef NDEBUG
25 EXPECT_DEATH({ abc[4]; }, ".*");
26 #endif
27
28 pdfium::span<const wchar_t> abc_span = abc.span();
29 EXPECT_EQ(3u, abc_span.size());
30 EXPECT_EQ(0, wmemcmp(abc_span.data(), L"abc", 3));
31
32 WideString mutable_abc = abc;
33 EXPECT_EQ(abc.c_str(), mutable_abc.c_str());
34 EXPECT_EQ(L'a', mutable_abc[0]);
35 EXPECT_EQ(L'b', mutable_abc[1]);
36 EXPECT_EQ(L'c', mutable_abc[2]);
37 EXPECT_EQ(abc.c_str(), mutable_abc.c_str());
38 EXPECT_EQ(L"abc", abc);
39
40 const wchar_t* c_str = abc.c_str();
41 mutable_abc.SetAt(0, L'd');
42 EXPECT_EQ(c_str, abc.c_str());
43 EXPECT_NE(c_str, mutable_abc.c_str());
44 EXPECT_EQ(L"abc", abc);
45 EXPECT_EQ(L"dbc", mutable_abc);
46
47 mutable_abc.SetAt(1, L'e');
48 EXPECT_EQ(L"abc", abc);
49 EXPECT_EQ(L"dec", mutable_abc);
50
51 mutable_abc.SetAt(2, L'f');
52 EXPECT_EQ(L"abc", abc);
53 EXPECT_EQ(L"def", mutable_abc);
54 #ifndef NDEBUG
55 EXPECT_DEATH({ mutable_abc.SetAt(3, L'g'); }, ".*");
56 EXPECT_EQ(L"abc", abc);
57 #endif
58 }
59
TEST(WideString,Construct)60 TEST(WideString, Construct) {
61 {
62 // Copy-construct.
63 WideString string1(L"abc");
64 WideString string2(string1);
65 EXPECT_EQ(L"abc", string1);
66 EXPECT_EQ(L"abc", string2);
67 EXPECT_EQ(2, string1.ReferenceCountForTesting());
68 EXPECT_EQ(2, string2.ReferenceCountForTesting());
69 }
70 {
71 // Move-construct.
72 WideString string1(L"abc");
73 WideString string2(std::move(string1));
74 EXPECT_TRUE(string1.IsEmpty());
75 EXPECT_EQ(L"abc", string2);
76 EXPECT_EQ(0, string1.ReferenceCountForTesting());
77 EXPECT_EQ(1, string2.ReferenceCountForTesting());
78 }
79 }
80
TEST(WideString,Assign)81 TEST(WideString, Assign) {
82 {
83 // Copy-assign.
84 WideString string1;
85 EXPECT_EQ(0, string1.ReferenceCountForTesting());
86 {
87 WideString string2(L"abc");
88 EXPECT_EQ(1, string2.ReferenceCountForTesting());
89
90 string1 = string2;
91 EXPECT_EQ(2, string1.ReferenceCountForTesting());
92 EXPECT_EQ(2, string2.ReferenceCountForTesting());
93 }
94 EXPECT_EQ(1, string1.ReferenceCountForTesting());
95 }
96 {
97 // Move-assign.
98 WideString string1;
99 EXPECT_EQ(0, string1.ReferenceCountForTesting());
100 {
101 WideString string2(L"abc");
102 EXPECT_EQ(1, string2.ReferenceCountForTesting());
103
104 string1 = std::move(string2);
105 EXPECT_EQ(L"abc", string1);
106 EXPECT_TRUE(string2.IsEmpty());
107 EXPECT_EQ(1, string1.ReferenceCountForTesting());
108 EXPECT_EQ(0, string2.ReferenceCountForTesting());
109 }
110 EXPECT_EQ(1, string1.ReferenceCountForTesting());
111 }
112 {
113 // From wchar_t*.
114 WideString string1 = L"abc";
115 EXPECT_EQ(L"abc", string1);
116 string1 = nullptr;
117 EXPECT_TRUE(string1.IsEmpty());
118 string1 = L"def";
119 EXPECT_EQ(L"def", string1);
120 string1 = L"";
121 EXPECT_TRUE(string1.IsEmpty());
122 }
123 {
124 // From WideStringView.
125 WideString string1(WideStringView(L"abc"));
126 EXPECT_EQ(L"abc", string1);
127 string1 = WideStringView(L"");
128 EXPECT_TRUE(string1.IsEmpty());
129 string1 = WideStringView(L"def");
130 EXPECT_EQ(L"def", string1);
131 }
132 }
133
TEST(WideString,OperatorLT)134 TEST(WideString, OperatorLT) {
135 WideString empty;
136 WideString a(L"a");
137 WideString ab(L"ab");
138 WideString abc(L"\x0110qq"); // Comes before despite endianness.
139 WideString def(L"\x1001qq"); // Comes after despite endianness.
140 WideStringView v_empty;
141 WideStringView v_a(L"a");
142 WideStringView v_ab(L"ab");
143 WideStringView v_abc(L"\x0110qq");
144 WideStringView v_def(L"\x1001qq");
145 const wchar_t* const c_null = nullptr;
146 const wchar_t* const c_empty = L"";
147 const wchar_t* const c_a = L"a";
148 const wchar_t* const c_ab = L"ab";
149 const wchar_t* const c_abc = L"\x0110qq";
150 const wchar_t* const c_def = L"\x1001qq";
151
152 EXPECT_FALSE(empty < empty);
153 EXPECT_FALSE(a < a);
154 EXPECT_FALSE(abc < abc);
155 EXPECT_FALSE(def < def);
156 EXPECT_FALSE(c_null < empty);
157 EXPECT_FALSE(c_empty < empty);
158 EXPECT_FALSE(c_a < a);
159 EXPECT_FALSE(c_abc < abc);
160 EXPECT_FALSE(c_def < def);
161 EXPECT_FALSE(empty < c_null);
162 EXPECT_FALSE(empty < c_empty);
163 EXPECT_FALSE(a < c_a);
164 EXPECT_FALSE(abc < c_abc);
165 EXPECT_FALSE(def < c_def);
166 EXPECT_FALSE(empty < v_empty);
167 EXPECT_FALSE(a < v_a);
168 EXPECT_FALSE(abc < v_abc);
169 EXPECT_FALSE(def < v_def);
170
171 EXPECT_TRUE(empty < a);
172 EXPECT_FALSE(a < empty);
173 EXPECT_TRUE(c_null < a);
174 EXPECT_TRUE(c_empty < a);
175 EXPECT_FALSE(c_a < empty);
176 EXPECT_TRUE(empty < c_a);
177 EXPECT_FALSE(a < c_null);
178 EXPECT_FALSE(a < c_empty);
179 EXPECT_TRUE(empty < v_a);
180 EXPECT_FALSE(a < v_empty);
181
182 EXPECT_TRUE(empty < abc);
183 EXPECT_FALSE(abc < empty);
184 EXPECT_TRUE(c_null < abc);
185 EXPECT_TRUE(c_empty < abc);
186 EXPECT_FALSE(c_abc < empty);
187 EXPECT_TRUE(empty < c_abc);
188 EXPECT_FALSE(abc < c_null);
189 EXPECT_FALSE(abc < c_empty);
190 EXPECT_TRUE(empty < v_abc);
191 EXPECT_FALSE(abc < v_empty);
192
193 EXPECT_TRUE(empty < def);
194 EXPECT_FALSE(def < empty);
195 EXPECT_TRUE(c_null < def);
196 EXPECT_TRUE(c_empty < def);
197 EXPECT_FALSE(c_def < empty);
198 EXPECT_TRUE(empty < c_def);
199 EXPECT_FALSE(def < c_null);
200 EXPECT_FALSE(def < c_empty);
201 EXPECT_TRUE(empty < v_def);
202 EXPECT_FALSE(def < v_empty);
203
204 EXPECT_TRUE(a < abc);
205 EXPECT_FALSE(abc < a);
206 EXPECT_TRUE(c_a < abc);
207 EXPECT_FALSE(c_abc < a);
208 EXPECT_TRUE(a < c_abc);
209 EXPECT_FALSE(abc < c_a);
210 EXPECT_TRUE(a < v_abc);
211 EXPECT_FALSE(abc < v_a);
212
213 EXPECT_TRUE(a < def);
214 EXPECT_FALSE(def < a);
215 EXPECT_TRUE(c_a < def);
216 EXPECT_FALSE(c_def < a);
217 EXPECT_TRUE(a < c_def);
218 EXPECT_FALSE(def < c_a);
219 EXPECT_TRUE(a < v_def);
220 EXPECT_FALSE(def < v_a);
221
222 EXPECT_TRUE(abc < def);
223 EXPECT_FALSE(def < abc);
224 EXPECT_TRUE(c_abc < def);
225 EXPECT_FALSE(c_def < abc);
226 EXPECT_TRUE(abc < c_def);
227 EXPECT_FALSE(def < c_abc);
228 EXPECT_TRUE(abc < v_def);
229 EXPECT_FALSE(def < v_abc);
230
231 EXPECT_TRUE(a < ab);
232 EXPECT_TRUE(a < c_ab);
233 EXPECT_TRUE(a < v_ab);
234 EXPECT_TRUE(c_a < ab);
235 EXPECT_TRUE(c_a < v_ab);
236 EXPECT_TRUE(v_a < c_ab);
237 EXPECT_TRUE(v_a < v_ab);
238 }
239
TEST(WideString,OperatorEQ)240 TEST(WideString, OperatorEQ) {
241 WideString null_string;
242 EXPECT_TRUE(null_string == null_string);
243
244 WideString empty_string(L"");
245 EXPECT_TRUE(empty_string == empty_string);
246 EXPECT_TRUE(empty_string == null_string);
247 EXPECT_TRUE(null_string == empty_string);
248
249 WideString deleted_string(L"hello");
250 deleted_string.Delete(0, 5);
251 EXPECT_TRUE(deleted_string == deleted_string);
252 EXPECT_TRUE(deleted_string == null_string);
253 EXPECT_TRUE(deleted_string == empty_string);
254 EXPECT_TRUE(null_string == deleted_string);
255 EXPECT_TRUE(null_string == empty_string);
256
257 WideString wide_string(L"hello");
258 EXPECT_TRUE(wide_string == wide_string);
259 EXPECT_FALSE(wide_string == null_string);
260 EXPECT_FALSE(wide_string == empty_string);
261 EXPECT_FALSE(wide_string == deleted_string);
262 EXPECT_FALSE(null_string == wide_string);
263 EXPECT_FALSE(empty_string == wide_string);
264 EXPECT_FALSE(deleted_string == wide_string);
265
266 WideString wide_string_same1(L"hello");
267 EXPECT_TRUE(wide_string == wide_string_same1);
268 EXPECT_TRUE(wide_string_same1 == wide_string);
269
270 WideString wide_string_same2(wide_string);
271 EXPECT_TRUE(wide_string == wide_string_same2);
272 EXPECT_TRUE(wide_string_same2 == wide_string);
273
274 WideString wide_string1(L"he");
275 WideString wide_string2(L"hellp");
276 WideString wide_string3(L"hellod");
277 EXPECT_FALSE(wide_string == wide_string1);
278 EXPECT_FALSE(wide_string == wide_string2);
279 EXPECT_FALSE(wide_string == wide_string3);
280 EXPECT_FALSE(wide_string1 == wide_string);
281 EXPECT_FALSE(wide_string2 == wide_string);
282 EXPECT_FALSE(wide_string3 == wide_string);
283
284 WideStringView null_string_c;
285 WideStringView empty_string_c(L"");
286 EXPECT_TRUE(null_string == null_string_c);
287 EXPECT_TRUE(null_string == empty_string_c);
288 EXPECT_TRUE(empty_string == null_string_c);
289 EXPECT_TRUE(empty_string == empty_string_c);
290 EXPECT_TRUE(deleted_string == null_string_c);
291 EXPECT_TRUE(deleted_string == empty_string_c);
292 EXPECT_TRUE(null_string_c == null_string);
293 EXPECT_TRUE(empty_string_c == null_string);
294 EXPECT_TRUE(null_string_c == empty_string);
295 EXPECT_TRUE(empty_string_c == empty_string);
296 EXPECT_TRUE(null_string_c == deleted_string);
297 EXPECT_TRUE(empty_string_c == deleted_string);
298
299 WideStringView wide_string_c_same1(L"hello");
300 EXPECT_TRUE(wide_string == wide_string_c_same1);
301 EXPECT_TRUE(wide_string_c_same1 == wide_string);
302
303 WideStringView wide_string_c1(L"he");
304 WideStringView wide_string_c2(L"hellp");
305 WideStringView wide_string_c3(L"hellod");
306 EXPECT_FALSE(wide_string == wide_string_c1);
307 EXPECT_FALSE(wide_string == wide_string_c2);
308 EXPECT_FALSE(wide_string == wide_string_c3);
309 EXPECT_FALSE(wide_string_c1 == wide_string);
310 EXPECT_FALSE(wide_string_c2 == wide_string);
311 EXPECT_FALSE(wide_string_c3 == wide_string);
312
313 const wchar_t* const c_null_string = nullptr;
314 const wchar_t* const c_empty_string = L"";
315 EXPECT_TRUE(null_string == c_null_string);
316 EXPECT_TRUE(null_string == c_empty_string);
317 EXPECT_TRUE(empty_string == c_null_string);
318 EXPECT_TRUE(empty_string == c_empty_string);
319 EXPECT_TRUE(deleted_string == c_null_string);
320 EXPECT_TRUE(deleted_string == c_empty_string);
321 EXPECT_TRUE(c_null_string == null_string);
322 EXPECT_TRUE(c_empty_string == null_string);
323 EXPECT_TRUE(c_null_string == empty_string);
324 EXPECT_TRUE(c_empty_string == empty_string);
325 EXPECT_TRUE(c_null_string == deleted_string);
326 EXPECT_TRUE(c_empty_string == deleted_string);
327
328 const wchar_t* const c_string_same1 = L"hello";
329 EXPECT_TRUE(wide_string == c_string_same1);
330 EXPECT_TRUE(c_string_same1 == wide_string);
331
332 const wchar_t* const c_string1 = L"he";
333 const wchar_t* const c_string2 = L"hellp";
334 const wchar_t* const c_string3 = L"hellod";
335 EXPECT_FALSE(wide_string == c_string1);
336 EXPECT_FALSE(wide_string == c_string2);
337 EXPECT_FALSE(wide_string == c_string3);
338 EXPECT_FALSE(c_string1 == wide_string);
339 EXPECT_FALSE(c_string2 == wide_string);
340 EXPECT_FALSE(c_string3 == wide_string);
341 }
342
TEST(WideString,OperatorNE)343 TEST(WideString, OperatorNE) {
344 WideString null_string;
345 EXPECT_FALSE(null_string != null_string);
346
347 WideString empty_string(L"");
348 EXPECT_FALSE(empty_string != empty_string);
349 EXPECT_FALSE(empty_string != null_string);
350 EXPECT_FALSE(null_string != empty_string);
351
352 WideString deleted_string(L"hello");
353 deleted_string.Delete(0, 5);
354 EXPECT_FALSE(deleted_string != deleted_string);
355 EXPECT_FALSE(deleted_string != null_string);
356 EXPECT_FALSE(deleted_string != empty_string);
357 EXPECT_FALSE(null_string != deleted_string);
358 EXPECT_FALSE(null_string != empty_string);
359
360 WideString wide_string(L"hello");
361 EXPECT_FALSE(wide_string != wide_string);
362 EXPECT_TRUE(wide_string != null_string);
363 EXPECT_TRUE(wide_string != empty_string);
364 EXPECT_TRUE(wide_string != deleted_string);
365 EXPECT_TRUE(null_string != wide_string);
366 EXPECT_TRUE(empty_string != wide_string);
367 EXPECT_TRUE(deleted_string != wide_string);
368
369 WideString wide_string_same1(L"hello");
370 EXPECT_FALSE(wide_string != wide_string_same1);
371 EXPECT_FALSE(wide_string_same1 != wide_string);
372
373 WideString wide_string_same2(wide_string);
374 EXPECT_FALSE(wide_string != wide_string_same2);
375 EXPECT_FALSE(wide_string_same2 != wide_string);
376
377 WideString wide_string1(L"he");
378 WideString wide_string2(L"hellp");
379 WideString wide_string3(L"hellod");
380 EXPECT_TRUE(wide_string != wide_string1);
381 EXPECT_TRUE(wide_string != wide_string2);
382 EXPECT_TRUE(wide_string != wide_string3);
383 EXPECT_TRUE(wide_string1 != wide_string);
384 EXPECT_TRUE(wide_string2 != wide_string);
385 EXPECT_TRUE(wide_string3 != wide_string);
386
387 WideStringView null_string_c;
388 WideStringView empty_string_c(L"");
389 EXPECT_FALSE(null_string != null_string_c);
390 EXPECT_FALSE(null_string != empty_string_c);
391 EXPECT_FALSE(empty_string != null_string_c);
392 EXPECT_FALSE(empty_string != empty_string_c);
393 EXPECT_FALSE(deleted_string != null_string_c);
394 EXPECT_FALSE(deleted_string != empty_string_c);
395 EXPECT_FALSE(null_string_c != null_string);
396 EXPECT_FALSE(empty_string_c != null_string);
397 EXPECT_FALSE(null_string_c != empty_string);
398 EXPECT_FALSE(empty_string_c != empty_string);
399
400 WideStringView wide_string_c_same1(L"hello");
401 EXPECT_FALSE(wide_string != wide_string_c_same1);
402 EXPECT_FALSE(wide_string_c_same1 != wide_string);
403
404 WideStringView wide_string_c1(L"he");
405 WideStringView wide_string_c2(L"hellp");
406 WideStringView wide_string_c3(L"hellod");
407 EXPECT_TRUE(wide_string != wide_string_c1);
408 EXPECT_TRUE(wide_string != wide_string_c2);
409 EXPECT_TRUE(wide_string != wide_string_c3);
410 EXPECT_TRUE(wide_string_c1 != wide_string);
411 EXPECT_TRUE(wide_string_c2 != wide_string);
412 EXPECT_TRUE(wide_string_c3 != wide_string);
413
414 const wchar_t* const c_null_string = nullptr;
415 const wchar_t* const c_empty_string = L"";
416 EXPECT_FALSE(null_string != c_null_string);
417 EXPECT_FALSE(null_string != c_empty_string);
418 EXPECT_FALSE(empty_string != c_null_string);
419 EXPECT_FALSE(empty_string != c_empty_string);
420 EXPECT_FALSE(deleted_string != c_null_string);
421 EXPECT_FALSE(deleted_string != c_empty_string);
422 EXPECT_FALSE(c_null_string != null_string);
423 EXPECT_FALSE(c_empty_string != null_string);
424 EXPECT_FALSE(c_null_string != empty_string);
425 EXPECT_FALSE(c_empty_string != empty_string);
426 EXPECT_FALSE(c_null_string != deleted_string);
427 EXPECT_FALSE(c_empty_string != deleted_string);
428
429 const wchar_t* const c_string_same1 = L"hello";
430 EXPECT_FALSE(wide_string != c_string_same1);
431 EXPECT_FALSE(c_string_same1 != wide_string);
432
433 const wchar_t* const c_string1 = L"he";
434 const wchar_t* const c_string2 = L"hellp";
435 const wchar_t* const c_string3 = L"hellod";
436 EXPECT_TRUE(wide_string != c_string1);
437 EXPECT_TRUE(wide_string != c_string2);
438 EXPECT_TRUE(wide_string != c_string3);
439 EXPECT_TRUE(c_string1 != wide_string);
440 EXPECT_TRUE(c_string2 != wide_string);
441 EXPECT_TRUE(c_string3 != wide_string);
442 }
443
TEST(WideString,OperatorPlus)444 TEST(WideString, OperatorPlus) {
445 EXPECT_EQ(L"I like dogs", L"I like " + WideString(L"dogs"));
446 EXPECT_EQ(L"Dogs like me", WideString(L"Dogs") + L" like me");
447 EXPECT_EQ(L"Oh no, error number 42",
448 L"Oh no, error number " + WideString::Format(L"%d", 42));
449
450 {
451 // Make sure operator+= and Concat() increases string memory allocation
452 // geometrically.
453 int allocations = 0;
454 WideString str(L"ABCDEFGHIJKLMN");
455 const wchar_t* buffer = str.c_str();
456 for (size_t i = 0; i < 10000; ++i) {
457 str += L"!";
458 const wchar_t* new_buffer = str.c_str();
459 if (new_buffer != buffer) {
460 buffer = new_buffer;
461 ++allocations;
462 }
463 }
464 EXPECT_LT(allocations, 25);
465 EXPECT_GT(allocations, 10);
466 }
467 }
468
TEST(WideString,ConcatInPlace)469 TEST(WideString, ConcatInPlace) {
470 WideString fred;
471 fred.Concat(L"FRED", 4);
472 EXPECT_EQ(L"FRED", fred);
473
474 fred.Concat(L"DY", 2);
475 EXPECT_EQ(L"FREDDY", fred);
476
477 fred.Delete(3, 3);
478 EXPECT_EQ(L"FRE", fred);
479
480 fred.Concat(L"D", 1);
481 EXPECT_EQ(L"FRED", fred);
482
483 WideString copy = fred;
484 fred.Concat(L"DY", 2);
485 EXPECT_EQ(L"FREDDY", fred);
486 EXPECT_EQ(L"FRED", copy);
487 }
488
TEST(WideString,Remove)489 TEST(WideString, Remove) {
490 WideString freed(L"FREED");
491 freed.Remove(L'E');
492 EXPECT_EQ(L"FRD", freed);
493 freed.Remove(L'F');
494 EXPECT_EQ(L"RD", freed);
495 freed.Remove(L'D');
496 EXPECT_EQ(L"R", freed);
497 freed.Remove(L'X');
498 EXPECT_EQ(L"R", freed);
499 freed.Remove(L'R');
500 EXPECT_EQ(L"", freed);
501
502 WideString empty;
503 empty.Remove(L'X');
504 EXPECT_EQ(L"", empty);
505 }
506
TEST(WideString,RemoveCopies)507 TEST(WideString, RemoveCopies) {
508 WideString freed(L"FREED");
509 const wchar_t* old_buffer = freed.c_str();
510
511 // No change with single reference - no copy.
512 freed.Remove(L'Q');
513 EXPECT_EQ(L"FREED", freed);
514 EXPECT_EQ(old_buffer, freed.c_str());
515
516 // Change with single reference - no copy.
517 freed.Remove(L'E');
518 EXPECT_EQ(L"FRD", freed);
519 EXPECT_EQ(old_buffer, freed.c_str());
520
521 // No change with multiple references - no copy.
522 WideString shared(freed);
523 freed.Remove(L'Q');
524 EXPECT_EQ(L"FRD", freed);
525 EXPECT_EQ(old_buffer, freed.c_str());
526 EXPECT_EQ(old_buffer, shared.c_str());
527
528 // Change with multiple references -- must copy.
529 freed.Remove(L'D');
530 EXPECT_EQ(L"FR", freed);
531 EXPECT_NE(old_buffer, freed.c_str());
532 EXPECT_EQ(L"FRD", shared);
533 EXPECT_EQ(old_buffer, shared.c_str());
534 }
535
TEST(WideString,Replace)536 TEST(WideString, Replace) {
537 WideString empty;
538 empty.Replace(L"", L"CLAMS");
539 empty.Replace(L"xx", L"CLAMS");
540 EXPECT_EQ(L"", empty);
541
542 WideString fred(L"FRED");
543 fred.Replace(L"", L"");
544 EXPECT_EQ(L"FRED", fred);
545 fred.Replace(L"", L"CLAMS");
546 EXPECT_EQ(L"FRED", fred);
547 fred.Replace(L"FR", L"BL");
548 EXPECT_EQ(L"BLED", fred);
549 fred.Replace(L"D", L"DDY");
550 EXPECT_EQ(L"BLEDDY", fred);
551 fred.Replace(L"LEDD", L"");
552 EXPECT_EQ(L"BY", fred);
553 fred.Replace(L"X", L"CLAMS");
554 EXPECT_EQ(L"BY", fred);
555 fred.Replace(L"BY", L"HI");
556 EXPECT_EQ(L"HI", fred);
557 fred.Replace(L"I", L"IHIHI");
558 EXPECT_EQ(L"HIHIHI", fred);
559 fred.Replace(L"HI", L"HO");
560 EXPECT_EQ(L"HOHOHO", fred);
561 fred.Replace(L"HO", L"");
562 EXPECT_EQ(L"", fred);
563
564 WideString five_xs(L"xxxxx");
565 five_xs.Replace(L"xx", L"xxx");
566 EXPECT_EQ(L"xxxxxxx", five_xs);
567
568 WideString five_ys(L"yyyyy");
569 five_ys.Replace(L"yy", L"y");
570 EXPECT_EQ(L"yyy", five_ys);
571 }
572
TEST(WideString,Insert)573 TEST(WideString, Insert) {
574 WideString fred(L"FRED");
575 EXPECT_EQ(5u, fred.Insert(0, 'S'));
576 EXPECT_EQ(L"SFRED", fred);
577 EXPECT_EQ(6u, fred.Insert(1, 'T'));
578 EXPECT_EQ(L"STFRED", fred);
579 EXPECT_EQ(7u, fred.Insert(4, 'U'));
580 EXPECT_EQ(L"STFRUED", fred);
581 EXPECT_EQ(8u, fred.Insert(7, 'V'));
582 EXPECT_EQ(L"STFRUEDV", fred);
583 EXPECT_EQ(8u, fred.Insert(12, 'P'));
584 EXPECT_EQ(L"STFRUEDV", fred);
585 {
586 WideString empty;
587 EXPECT_EQ(1u, empty.Insert(0, 'X'));
588 EXPECT_EQ(L"X", empty);
589 }
590 {
591 WideString empty;
592 EXPECT_EQ(0u, empty.Insert(5, 'X'));
593 EXPECT_NE(L"X", empty);
594 }
595 }
596
TEST(WideString,InsertAtFrontAndInsertAtBack)597 TEST(WideString, InsertAtFrontAndInsertAtBack) {
598 {
599 WideString empty;
600 EXPECT_EQ(1u, empty.InsertAtFront('D'));
601 EXPECT_EQ(L"D", empty);
602 EXPECT_EQ(2u, empty.InsertAtFront('E'));
603 EXPECT_EQ(L"ED", empty);
604 EXPECT_EQ(3u, empty.InsertAtFront('R'));
605 EXPECT_EQ(L"RED", empty);
606 EXPECT_EQ(4u, empty.InsertAtFront('F'));
607 EXPECT_EQ(L"FRED", empty);
608 }
609 {
610 WideString empty;
611 EXPECT_EQ(1u, empty.InsertAtBack('F'));
612 EXPECT_EQ(L"F", empty);
613 EXPECT_EQ(2u, empty.InsertAtBack('R'));
614 EXPECT_EQ(L"FR", empty);
615 EXPECT_EQ(3u, empty.InsertAtBack('E'));
616 EXPECT_EQ(L"FRE", empty);
617 EXPECT_EQ(4u, empty.InsertAtBack('D'));
618 EXPECT_EQ(L"FRED", empty);
619 }
620 {
621 WideString empty;
622 EXPECT_EQ(1u, empty.InsertAtBack('E'));
623 EXPECT_EQ(L"E", empty);
624 EXPECT_EQ(2u, empty.InsertAtFront('R'));
625 EXPECT_EQ(L"RE", empty);
626 EXPECT_EQ(3u, empty.InsertAtBack('D'));
627 EXPECT_EQ(L"RED", empty);
628 EXPECT_EQ(4u, empty.InsertAtFront('F'));
629 EXPECT_EQ(L"FRED", empty);
630 }
631 }
632
TEST(WideString,Delete)633 TEST(WideString, Delete) {
634 WideString fred(L"FRED");
635 EXPECT_EQ(4u, fred.Delete(0, 0));
636 EXPECT_EQ(L"FRED", fred);
637 EXPECT_EQ(2u, fred.Delete(0, 2));
638 EXPECT_EQ(L"ED", fred);
639 EXPECT_EQ(1u, fred.Delete(1));
640 EXPECT_EQ(L"E", fred);
641 EXPECT_EQ(0u, fred.Delete(0));
642 EXPECT_EQ(L"", fred);
643 EXPECT_EQ(0u, fred.Delete(0));
644 EXPECT_EQ(L"", fred);
645
646 WideString empty;
647 EXPECT_EQ(0u, empty.Delete(0));
648 EXPECT_EQ(L"", empty);
649 EXPECT_EQ(0u, empty.Delete(1));
650 EXPECT_EQ(L"", empty);
651 }
652
TEST(WideString,OneArgSubstr)653 TEST(WideString, OneArgSubstr) {
654 WideString fred(L"FRED");
655 EXPECT_EQ(L"FRED", fred.Substr(0));
656 EXPECT_EQ(L"RED", fred.Substr(1));
657 EXPECT_EQ(L"ED", fred.Substr(2));
658 EXPECT_EQ(L"D", fred.Substr(3));
659 EXPECT_EQ(L"", fred.Substr(4));
660
661 WideString empty;
662 EXPECT_EQ(L"", empty.Substr(0));
663 EXPECT_EQ(L"", empty.Substr(1));
664 }
665
TEST(WideString,TwoArgSubstr)666 TEST(WideString, TwoArgSubstr) {
667 WideString fred(L"FRED");
668 EXPECT_EQ(L"", fred.Substr(0, 0));
669 EXPECT_EQ(L"", fred.Substr(3, 0));
670 EXPECT_EQ(L"FRED", fred.Substr(0, 4));
671 EXPECT_EQ(L"RED", fred.Substr(1, 3));
672 EXPECT_EQ(L"ED", fred.Substr(2, 2));
673 EXPECT_EQ(L"D", fred.Substr(3, 1));
674 EXPECT_EQ(L"F", fred.Substr(0, 1));
675 EXPECT_EQ(L"R", fred.Substr(1, 1));
676 EXPECT_EQ(L"E", fred.Substr(2, 1));
677 EXPECT_EQ(L"D", fred.Substr(3, 1));
678 EXPECT_EQ(L"FR", fred.Substr(0, 2));
679 EXPECT_EQ(L"FRED", fred.Substr(0, 4));
680 EXPECT_EQ(L"", fred.Substr(0, 10));
681
682 EXPECT_EQ(L"", fred.Substr(1, 4));
683 EXPECT_EQ(L"", fred.Substr(4, 1));
684
685 WideString empty;
686 EXPECT_EQ(L"", empty.Substr(0, 0));
687 }
688
TEST(WideString,First)689 TEST(WideString, First) {
690 WideString fred(L"FRED");
691 EXPECT_EQ(L"", fred.First(0));
692 EXPECT_EQ(L"F", fred.First(1));
693 EXPECT_EQ(L"FR", fred.First(2));
694 EXPECT_EQ(L"FRE", fred.First(3));
695 EXPECT_EQ(L"FRED", fred.First(4));
696
697 EXPECT_EQ(L"", fred.First(5));
698
699 WideString empty;
700 EXPECT_EQ(L"", empty.First(0));
701 EXPECT_EQ(L"", empty.First(1));
702 }
703
TEST(WideString,Last)704 TEST(WideString, Last) {
705 WideString fred(L"FRED");
706 EXPECT_EQ(L"", fred.Last(0));
707 EXPECT_EQ(L"D", fred.Last(1));
708 EXPECT_EQ(L"ED", fred.Last(2));
709 EXPECT_EQ(L"RED", fred.Last(3));
710 EXPECT_EQ(L"FRED", fred.Last(4));
711
712 EXPECT_EQ(L"", fred.Last(5));
713
714 WideString empty;
715 EXPECT_EQ(L"", empty.Last(0));
716 EXPECT_EQ(L"", empty.Last(1));
717 }
718
TEST(WideString,Find)719 TEST(WideString, Find) {
720 WideString null_string;
721 EXPECT_FALSE(null_string.Find(L'a').has_value());
722 EXPECT_FALSE(null_string.Find(L'\0').has_value());
723
724 WideString empty_string(L"");
725 EXPECT_FALSE(empty_string.Find(L'a').has_value());
726 EXPECT_FALSE(empty_string.Find(L'\0').has_value());
727
728 WideString single_string(L"a");
729 absl::optional<size_t> result = single_string.Find(L'a');
730 ASSERT_TRUE(result.has_value());
731 EXPECT_EQ(0u, result.value());
732 EXPECT_FALSE(single_string.Find(L'b').has_value());
733 EXPECT_FALSE(single_string.Find(L'\0').has_value());
734
735 WideString longer_string(L"abccc");
736 result = longer_string.Find(L'a');
737 ASSERT_TRUE(result.has_value());
738 EXPECT_EQ(0u, result.value());
739 result = longer_string.Find(L'c');
740 ASSERT_TRUE(result.has_value());
741 EXPECT_EQ(2u, result.value());
742 result = longer_string.Find(L'c', 3);
743 ASSERT_TRUE(result.has_value());
744 EXPECT_EQ(3u, result.value());
745 EXPECT_FALSE(longer_string.Find(L'\0').has_value());
746
747 result = longer_string.Find(L"ab");
748 ASSERT_TRUE(result.has_value());
749 EXPECT_EQ(0u, result.value());
750 result = longer_string.Find(L"ccc");
751 ASSERT_TRUE(result.has_value());
752 EXPECT_EQ(2u, result.value());
753 result = longer_string.Find(L"cc", 3);
754 ASSERT_TRUE(result.has_value());
755 EXPECT_EQ(3u, result.value());
756 EXPECT_FALSE(longer_string.Find(L"d").has_value());
757
758 WideString hibyte_string(
759 L"ab\xff8c"
760 L"def");
761 result = hibyte_string.Find(L'\xff8c');
762 ASSERT_TRUE(result.has_value());
763 EXPECT_EQ(2u, result.value());
764 }
765
TEST(WideString,ReverseFind)766 TEST(WideString, ReverseFind) {
767 WideString null_string;
768 EXPECT_FALSE(null_string.ReverseFind(L'a').has_value());
769 EXPECT_FALSE(null_string.ReverseFind(L'\0').has_value());
770
771 WideString empty_string(L"");
772 EXPECT_FALSE(empty_string.ReverseFind(L'a').has_value());
773 EXPECT_FALSE(empty_string.ReverseFind(L'\0').has_value());
774
775 WideString single_string(L"a");
776 absl::optional<size_t> result = single_string.ReverseFind(L'a');
777 ASSERT_TRUE(result.has_value());
778 EXPECT_EQ(0u, result.value());
779 EXPECT_FALSE(single_string.ReverseFind(L'b').has_value());
780 EXPECT_FALSE(single_string.ReverseFind(L'\0').has_value());
781
782 WideString longer_string(L"abccc");
783 result = longer_string.ReverseFind(L'a');
784 ASSERT_TRUE(result.has_value());
785 EXPECT_EQ(0u, result.value());
786 result = longer_string.ReverseFind(L'c');
787 ASSERT_TRUE(result.has_value());
788 EXPECT_EQ(4u, result.value());
789 EXPECT_FALSE(longer_string.ReverseFind(L'\0').has_value());
790
791 WideString hibyte_string(
792 L"ab\xff8c"
793 L"def");
794 result = hibyte_string.ReverseFind(L'\xff8c');
795 ASSERT_TRUE(result.has_value());
796 EXPECT_EQ(2u, result.value());
797 }
798
TEST(WideString,UpperLower)799 TEST(WideString, UpperLower) {
800 WideString fred(L"F-Re.42D");
801 fred.MakeLower();
802 EXPECT_EQ(L"f-re.42d", fred);
803 fred.MakeUpper();
804 EXPECT_EQ(L"F-RE.42D", fred);
805
806 WideString empty;
807 empty.MakeLower();
808 EXPECT_EQ(L"", empty);
809 empty.MakeUpper();
810 EXPECT_EQ(L"", empty);
811
812 WideString empty_with_buffer(L"x");
813 empty_with_buffer.Delete(0);
814
815 WideString additional_empty_with_buffer_ref = empty_with_buffer;
816 additional_empty_with_buffer_ref.MakeLower();
817 EXPECT_EQ(L"", additional_empty_with_buffer_ref);
818
819 additional_empty_with_buffer_ref = empty_with_buffer;
820 additional_empty_with_buffer_ref.MakeUpper();
821 EXPECT_EQ(L"", additional_empty_with_buffer_ref);
822 }
823
TEST(WideString,Trim)824 TEST(WideString, Trim) {
825 WideString fred(L" FRED ");
826 fred.Trim();
827 EXPECT_EQ(L"FRED", fred);
828 fred.Trim(L'E');
829 EXPECT_EQ(L"FRED", fred);
830 fred.Trim(L'F');
831 EXPECT_EQ(L"RED", fred);
832 fred.Trim(L"ERP");
833 EXPECT_EQ(L"D", fred);
834
835 WideString blank(L" ");
836 blank.Trim(L"ERP");
837 EXPECT_EQ(L" ", blank);
838 blank.Trim(L'E');
839 EXPECT_EQ(L" ", blank);
840 blank.Trim();
841 EXPECT_EQ(L"", blank);
842
843 WideString empty;
844 empty.Trim(L"ERP");
845 EXPECT_EQ(L"", empty);
846 empty.Trim(L'E');
847 EXPECT_EQ(L"", empty);
848 empty.Trim();
849 EXPECT_EQ(L"", empty);
850
851 WideString abc(L" ABCCBA ");
852 abc.Trim(L"A");
853 EXPECT_EQ(L" ABCCBA ", abc);
854 abc.Trim(L" A");
855 EXPECT_EQ(L"BCCB", abc);
856 }
857
TEST(WideString,TrimLeft)858 TEST(WideString, TrimLeft) {
859 WideString fred(L" FRED ");
860 fred.TrimLeft();
861 EXPECT_EQ(L"FRED ", fred);
862 fred.TrimLeft(L'E');
863 EXPECT_EQ(L"FRED ", fred);
864 fred.TrimLeft(L'F');
865 EXPECT_EQ(L"RED ", fred);
866 fred.TrimLeft(L"ERP");
867 EXPECT_EQ(L"D ", fred);
868
869 WideString blank(L" ");
870 blank.TrimLeft(L"ERP");
871 EXPECT_EQ(L" ", blank);
872 blank.TrimLeft(L'E');
873 EXPECT_EQ(L" ", blank);
874 blank.TrimLeft();
875 EXPECT_EQ(L"", blank);
876
877 WideString empty;
878 empty.TrimLeft(L"ERP");
879 EXPECT_EQ(L"", empty);
880 empty.TrimLeft(L'E');
881 EXPECT_EQ(L"", empty);
882 empty.TrimLeft();
883 EXPECT_EQ(L"", empty);
884 }
885
TEST(WideString,TrimLeftCopies)886 TEST(WideString, TrimLeftCopies) {
887 {
888 // With a single reference, no copy takes place.
889 WideString fred(L" FRED ");
890 const wchar_t* old_buffer = fred.c_str();
891 fred.TrimLeft();
892 EXPECT_EQ(L"FRED ", fred);
893 EXPECT_EQ(old_buffer, fred.c_str());
894 }
895 {
896 // With multiple references, we must copy.
897 WideString fred(L" FRED ");
898 WideString other_fred = fred;
899 const wchar_t* old_buffer = fred.c_str();
900 fred.TrimLeft();
901 EXPECT_EQ(L"FRED ", fred);
902 EXPECT_EQ(L" FRED ", other_fred);
903 EXPECT_NE(old_buffer, fred.c_str());
904 }
905 {
906 // With multiple references, but no modifications, no copy.
907 WideString fred(L"FRED");
908 WideString other_fred = fred;
909 const wchar_t* old_buffer = fred.c_str();
910 fred.TrimLeft();
911 EXPECT_EQ(L"FRED", fred);
912 EXPECT_EQ(L"FRED", other_fred);
913 EXPECT_EQ(old_buffer, fred.c_str());
914 }
915 }
916
TEST(WideString,TrimRight)917 TEST(WideString, TrimRight) {
918 WideString fred(L" FRED ");
919 fred.TrimRight();
920 EXPECT_EQ(L" FRED", fred);
921 fred.TrimRight(L'E');
922 EXPECT_EQ(L" FRED", fred);
923 fred.TrimRight(L'D');
924 EXPECT_EQ(L" FRE", fred);
925 fred.TrimRight(L"ERP");
926 EXPECT_EQ(L" F", fred);
927
928 WideString blank(L" ");
929 blank.TrimRight(L"ERP");
930 EXPECT_EQ(L" ", blank);
931 blank.TrimRight(L'E');
932 EXPECT_EQ(L" ", blank);
933 blank.TrimRight();
934 EXPECT_EQ(L"", blank);
935
936 WideString empty;
937 empty.TrimRight(L"ERP");
938 EXPECT_EQ(L"", empty);
939 empty.TrimRight(L'E');
940 EXPECT_EQ(L"", empty);
941 empty.TrimRight();
942 EXPECT_EQ(L"", empty);
943 }
944
TEST(WideString,TrimRightCopies)945 TEST(WideString, TrimRightCopies) {
946 {
947 // With a single reference, no copy takes place.
948 WideString fred(L" FRED ");
949 const wchar_t* old_buffer = fred.c_str();
950 fred.TrimRight();
951 EXPECT_EQ(L" FRED", fred);
952 EXPECT_EQ(old_buffer, fred.c_str());
953 }
954 {
955 // With multiple references, we must copy.
956 WideString fred(L" FRED ");
957 WideString other_fred = fred;
958 const wchar_t* old_buffer = fred.c_str();
959 fred.TrimRight();
960 EXPECT_EQ(L" FRED", fred);
961 EXPECT_EQ(L" FRED ", other_fred);
962 EXPECT_NE(old_buffer, fred.c_str());
963 }
964 {
965 // With multiple references, but no modifications, no copy.
966 WideString fred(L"FRED");
967 WideString other_fred = fred;
968 const wchar_t* old_buffer = fred.c_str();
969 fred.TrimRight();
970 EXPECT_EQ(L"FRED", fred);
971 EXPECT_EQ(L"FRED", other_fred);
972 EXPECT_EQ(old_buffer, fred.c_str());
973 }
974 }
975
TEST(WideString,Reserve)976 TEST(WideString, Reserve) {
977 {
978 WideString str;
979 str.Reserve(6);
980 const wchar_t* old_buffer = str.c_str();
981 str += L"ABCDEF";
982 EXPECT_EQ(old_buffer, str.c_str());
983 str += L"Blah Blah Blah Blah Blah Blah";
984 EXPECT_NE(old_buffer, str.c_str());
985 }
986 {
987 WideString str(L"A");
988 str.Reserve(6);
989 const wchar_t* old_buffer = str.c_str();
990 str += L"BCDEF";
991 EXPECT_EQ(old_buffer, str.c_str());
992 str += L"Blah Blah Blah Blah Blah Blah";
993 EXPECT_NE(old_buffer, str.c_str());
994 }
995 }
996
TEST(WideString,GetBuffer)997 TEST(WideString, GetBuffer) {
998 WideString str1;
999 {
1000 pdfium::span<wchar_t> buffer = str1.GetBuffer(12);
1001 wcscpy(buffer.data(), L"clams");
1002 }
1003 str1.ReleaseBuffer(str1.GetStringLength());
1004 EXPECT_EQ(L"clams", str1);
1005
1006 WideString str2(L"cl");
1007 {
1008 pdfium::span<wchar_t> buffer = str2.GetBuffer(12);
1009 wcscpy(buffer.data() + 2, L"ams");
1010 }
1011 str2.ReleaseBuffer(str2.GetStringLength());
1012 EXPECT_EQ(L"clams", str2);
1013 }
1014
TEST(WideString,ReleaseBuffer)1015 TEST(WideString, ReleaseBuffer) {
1016 {
1017 WideString str;
1018 str.Reserve(12);
1019 str += L"clams";
1020 const wchar_t* old_buffer = str.c_str();
1021 str.ReleaseBuffer(4);
1022 EXPECT_EQ(old_buffer, str.c_str());
1023 EXPECT_EQ(L"clam", str);
1024 }
1025 {
1026 WideString str(L"c");
1027 str.Reserve(12);
1028 str += L"lams";
1029 const wchar_t* old_buffer = str.c_str();
1030 str.ReleaseBuffer(4);
1031 EXPECT_EQ(old_buffer, str.c_str());
1032 EXPECT_EQ(L"clam", str);
1033 }
1034 {
1035 WideString str;
1036 str.Reserve(200);
1037 str += L"clams";
1038 const wchar_t* old_buffer = str.c_str();
1039 str.ReleaseBuffer(4);
1040 EXPECT_NE(old_buffer, str.c_str());
1041 EXPECT_EQ(L"clam", str);
1042 }
1043 {
1044 WideString str(L"c");
1045 str.Reserve(200);
1046 str += L"lams";
1047 const wchar_t* old_buffer = str.c_str();
1048 str.ReleaseBuffer(4);
1049 EXPECT_NE(old_buffer, str.c_str());
1050 EXPECT_EQ(L"clam", str);
1051 }
1052 }
1053
TEST(WideString,EmptyReverseIterator)1054 TEST(WideString, EmptyReverseIterator) {
1055 WideString empty;
1056 auto iter = empty.rbegin();
1057 EXPECT_TRUE(iter == empty.rend());
1058 EXPECT_FALSE(iter != empty.rend());
1059 EXPECT_FALSE(iter < empty.rend());
1060 }
1061
TEST(WideString,OneCharReverseIterator)1062 TEST(WideString, OneCharReverseIterator) {
1063 WideString one_str(L"a");
1064 auto iter = one_str.rbegin();
1065 EXPECT_FALSE(iter == one_str.rend());
1066 EXPECT_TRUE(iter != one_str.rend());
1067 EXPECT_TRUE(iter < one_str.rend());
1068
1069 char ch = *iter++;
1070 EXPECT_EQ('a', ch);
1071 EXPECT_TRUE(iter == one_str.rend());
1072 EXPECT_FALSE(iter != one_str.rend());
1073 EXPECT_FALSE(iter < one_str.rend());
1074 }
1075
TEST(WideString,MultiCharReverseIterator)1076 TEST(WideString, MultiCharReverseIterator) {
1077 WideString multi_str(L"abcd");
1078 auto iter = multi_str.rbegin();
1079 EXPECT_NE(iter, multi_str.rend());
1080 EXPECT_EQ(4, multi_str.rend() - iter);
1081 EXPECT_EQ(0, iter - multi_str.rbegin());
1082
1083 char ch = *iter++;
1084 EXPECT_EQ('d', ch);
1085 EXPECT_EQ('c', *iter);
1086 EXPECT_NE(iter, multi_str.rend());
1087 EXPECT_EQ(3, multi_str.rend() - iter);
1088 EXPECT_EQ(1, iter - multi_str.rbegin());
1089
1090 ch = *(++iter);
1091 EXPECT_EQ('b', ch);
1092 EXPECT_EQ('b', *iter);
1093 EXPECT_NE(iter, multi_str.rend());
1094 EXPECT_EQ(2, multi_str.rend() - iter);
1095 EXPECT_EQ(2, iter - multi_str.rbegin());
1096
1097 ch = *iter++;
1098 EXPECT_EQ('b', ch);
1099 EXPECT_EQ('a', *iter);
1100 EXPECT_NE(iter, multi_str.rend());
1101 EXPECT_EQ(1, multi_str.rend() - iter);
1102 EXPECT_EQ(3, iter - multi_str.rbegin());
1103
1104 ch = *iter++;
1105 EXPECT_EQ('a', ch);
1106 EXPECT_EQ(iter, multi_str.rend());
1107 EXPECT_EQ(0, multi_str.rend() - iter);
1108 EXPECT_EQ(4, iter - multi_str.rbegin());
1109
1110 ch = *(--iter);
1111 EXPECT_EQ('a', ch);
1112 EXPECT_EQ('a', *iter);
1113 EXPECT_NE(iter, multi_str.rend());
1114 EXPECT_EQ(1, multi_str.rend() - iter);
1115 EXPECT_EQ(3, iter - multi_str.rbegin());
1116
1117 ch = *iter--;
1118 EXPECT_EQ('a', ch);
1119 EXPECT_EQ('b', *iter);
1120 EXPECT_NE(iter, multi_str.rend());
1121 EXPECT_EQ(2, multi_str.rend() - iter);
1122 EXPECT_EQ(2, iter - multi_str.rbegin());
1123
1124 ch = *iter--;
1125 EXPECT_EQ('b', ch);
1126 EXPECT_EQ('c', *iter);
1127 EXPECT_NE(iter, multi_str.rend());
1128 EXPECT_EQ(3, multi_str.rend() - iter);
1129 EXPECT_EQ(1, iter - multi_str.rbegin());
1130
1131 ch = *(--iter);
1132 EXPECT_EQ('d', ch);
1133 EXPECT_EQ('d', *iter);
1134 EXPECT_EQ(iter, multi_str.rbegin());
1135 EXPECT_EQ(4, multi_str.rend() - iter);
1136 EXPECT_EQ(0, iter - multi_str.rbegin());
1137 }
1138
TEST(WideString,ToUTF16LE)1139 TEST(WideString, ToUTF16LE) {
1140 struct UTF16LEEncodeCase {
1141 WideString ws;
1142 ByteString bs;
1143 } const utf16le_encode_cases[] = {
1144 {L"", ByteString("\0\0", 2)},
1145 {L"abc", ByteString("a\0b\0c\0\0\0", 8)},
1146 {L"abcdef", ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)},
1147 {L"abc\0def", ByteString("a\0b\0c\0\0\0", 8)},
1148 {L"\xaabb\xccdd", ByteString("\xbb\xaa\xdd\xcc\0\0", 6)},
1149 {L"\x3132\x6162", ByteString("\x32\x31\x62\x61\0\0", 6)},
1150 };
1151
1152 for (size_t i = 0; i < std::size(utf16le_encode_cases); ++i) {
1153 EXPECT_EQ(utf16le_encode_cases[i].bs,
1154 utf16le_encode_cases[i].ws.ToUTF16LE())
1155 << " for case number " << i;
1156 }
1157 }
1158
TEST(WideString,EncodeEntities)1159 TEST(WideString, EncodeEntities) {
1160 EXPECT_EQ(WideString(L"Symbols &<>'\".").EncodeEntities(),
1161 L"Symbols &<>'".");
1162 }
1163
TEST(WideString,IsASCII)1164 TEST(WideString, IsASCII) {
1165 EXPECT_TRUE(WideString(L"xy\u007fz").IsASCII());
1166 EXPECT_FALSE(WideString(L"xy\u0080z").IsASCII());
1167 EXPECT_FALSE(WideString(L"xy\u2041z").IsASCII());
1168 }
1169
TEST(WideString,EqualsASCII)1170 TEST(WideString, EqualsASCII) {
1171 EXPECT_TRUE(WideString(L"").EqualsASCII(""));
1172 EXPECT_FALSE(WideString(L"A").EqualsASCII(""));
1173 EXPECT_FALSE(WideString(L"").EqualsASCII("A"));
1174 EXPECT_FALSE(WideString(L"A").EqualsASCII("B"));
1175 EXPECT_TRUE(WideString(L"ABC").EqualsASCII("ABC"));
1176 EXPECT_FALSE(WideString(L"ABC").EqualsASCII("AEC"));
1177 EXPECT_FALSE(WideString(L"\u00c1").EqualsASCII("\x41"));
1178 EXPECT_FALSE(WideString(L"\u0141").EqualsASCII("\x41"));
1179 }
1180
TEST(WideString,EqualsASCIINoCase)1181 TEST(WideString, EqualsASCIINoCase) {
1182 EXPECT_TRUE(WideString(L"").EqualsASCIINoCase(""));
1183 EXPECT_FALSE(WideString(L"A").EqualsASCIINoCase("b"));
1184 EXPECT_TRUE(WideString(L"AbC").EqualsASCIINoCase("aBc"));
1185 EXPECT_FALSE(WideString(L"ABc").EqualsASCIINoCase("AeC"));
1186 EXPECT_FALSE(WideString(L"\u00c1").EqualsASCIINoCase("\x41"));
1187 EXPECT_FALSE(WideString(L"\u0141").EqualsASCIINoCase("\x41"));
1188 }
1189
TEST(WideString,ToASCII)1190 TEST(WideString, ToASCII) {
1191 const char* kResult =
1192 "x"
1193 "\x02"
1194 "\x7f"
1195 "\x22"
1196 "\x0c"
1197 "y";
1198 EXPECT_EQ(kResult, WideString(L"x"
1199 L"\u0082"
1200 L"\u00ff"
1201 L"\u0122"
1202 L"\u208c"
1203 L"y")
1204 .ToASCII());
1205 }
1206
TEST(WideString,ToLatin1)1207 TEST(WideString, ToLatin1) {
1208 const char* kResult =
1209 "x"
1210 "\x82"
1211 "\xff"
1212 "\x22"
1213 "\x8c"
1214 "y";
1215 EXPECT_EQ(kResult, WideString(L"x"
1216 L"\u0082"
1217 L"\u00ff"
1218 L"\u0122"
1219 L"\u208c"
1220 L"y")
1221 .ToLatin1());
1222 }
1223
TEST(WideString,ToDefANSI)1224 TEST(WideString, ToDefANSI) {
1225 EXPECT_EQ("", WideString().ToDefANSI());
1226 #if BUILDFLAG(IS_WIN)
1227 const char* kResult =
1228 "x"
1229 "?"
1230 "\xff"
1231 "A"
1232 "?"
1233 "y";
1234 #else
1235 const char* kResult =
1236 "x"
1237 "\x80"
1238 "\xff"
1239 "y";
1240 #endif
1241 EXPECT_EQ(kResult, WideString(L"x"
1242 L"\u0080"
1243 L"\u00ff"
1244 L"\u0100"
1245 L"\u208c"
1246 L"y")
1247 .ToDefANSI());
1248 }
1249
TEST(WideString,FromASCII)1250 TEST(WideString, FromASCII) {
1251 EXPECT_EQ(L"", WideString::FromASCII(ByteStringView()));
1252 const wchar_t* kResult =
1253 L"x"
1254 L"\u0002"
1255 L"\u007f"
1256 L"y";
1257 EXPECT_EQ(kResult, WideString::FromASCII("x"
1258 "\x82"
1259 "\xff"
1260 "y"));
1261 }
1262
TEST(WideString,FromLatin1)1263 TEST(WideString, FromLatin1) {
1264 EXPECT_EQ(L"", WideString::FromLatin1(ByteStringView()));
1265 const wchar_t* kResult =
1266 L"x"
1267 L"\u0082"
1268 L"\u00ff"
1269 L"y";
1270 EXPECT_EQ(kResult, WideString::FromLatin1("x"
1271 "\x82"
1272 "\xff"
1273 "y"));
1274 }
1275
TEST(WideString,FromDefANSI)1276 TEST(WideString, FromDefANSI) {
1277 EXPECT_EQ(L"", WideString::FromDefANSI(ByteStringView()));
1278 #if BUILDFLAG(IS_WIN)
1279 const wchar_t* kResult =
1280 L"x"
1281 L"\u20ac"
1282 L"\u00ff"
1283 L"y";
1284 #else
1285 const wchar_t* kResult =
1286 L"x"
1287 L"\u0080"
1288 L"\u00ff"
1289 L"y";
1290 #endif
1291 EXPECT_EQ(kResult, WideString::FromDefANSI("x"
1292 "\x80"
1293 "\xff"
1294 "y"));
1295 }
1296
TEST(WideStringView,FromVector)1297 TEST(WideStringView, FromVector) {
1298 std::vector<WideStringView::UnsignedType> null_vec;
1299 WideStringView null_string(null_vec);
1300 EXPECT_EQ(0u, null_string.GetLength());
1301
1302 std::vector<WideStringView::UnsignedType> lower_a_vec(
1303 10, static_cast<WideStringView::UnsignedType>(L'a'));
1304 WideStringView lower_a_string(lower_a_vec);
1305 EXPECT_EQ(10u, lower_a_string.GetLength());
1306 EXPECT_EQ(L"aaaaaaaaaa", lower_a_string);
1307
1308 std::vector<WideStringView::UnsignedType> cleared_vec;
1309 cleared_vec.push_back(42);
1310 cleared_vec.pop_back();
1311 WideStringView cleared_string(cleared_vec);
1312 EXPECT_EQ(0u, cleared_string.GetLength());
1313 EXPECT_FALSE(cleared_string.raw_str());
1314 }
1315
TEST(WideStringView,ElementAccess)1316 TEST(WideStringView, ElementAccess) {
1317 WideStringView abc(L"abc");
1318 EXPECT_EQ(L'a', static_cast<wchar_t>(abc[0]));
1319 EXPECT_EQ(L'b', static_cast<wchar_t>(abc[1]));
1320 EXPECT_EQ(L'c', static_cast<wchar_t>(abc[2]));
1321 #ifndef NDEBUG
1322 EXPECT_DEATH({ abc[4]; }, ".*");
1323 #endif
1324 }
1325
TEST(WideStringView,OperatorLT)1326 TEST(WideStringView, OperatorLT) {
1327 WideStringView empty;
1328 WideStringView a(L"a");
1329 WideStringView abc(L"\x0110qq"); // Comes InsertAtFront despite endianness.
1330 WideStringView def(L"\x1001qq"); // Comes InsertAtBack despite endianness.
1331 const wchar_t* const c_null = nullptr;
1332 const wchar_t* const c_empty = L"";
1333 const wchar_t* const c_a = L"a";
1334 const wchar_t* const c_abc = L"\x0110qq";
1335 const wchar_t* const c_def = L"\x1001qq";
1336
1337 EXPECT_FALSE(empty < empty);
1338 EXPECT_FALSE(a < a);
1339 EXPECT_FALSE(abc < abc);
1340 EXPECT_FALSE(def < def);
1341 EXPECT_FALSE(c_null < empty);
1342 EXPECT_FALSE(c_empty < empty);
1343 EXPECT_FALSE(c_a < a);
1344 EXPECT_FALSE(c_abc < abc);
1345 EXPECT_FALSE(c_def < def);
1346 EXPECT_FALSE(empty < c_null);
1347 EXPECT_FALSE(empty < c_empty);
1348 EXPECT_FALSE(a < c_a);
1349 EXPECT_FALSE(abc < c_abc);
1350 EXPECT_FALSE(def < c_def);
1351
1352 EXPECT_TRUE(empty < a);
1353 EXPECT_FALSE(a < empty);
1354 EXPECT_TRUE(empty < c_a);
1355 EXPECT_FALSE(a < c_null);
1356 EXPECT_FALSE(a < c_empty);
1357
1358 EXPECT_TRUE(empty < abc);
1359 EXPECT_FALSE(abc < empty);
1360 EXPECT_TRUE(empty < c_abc);
1361 EXPECT_FALSE(abc < c_null);
1362 EXPECT_FALSE(abc < c_empty);
1363
1364 EXPECT_TRUE(empty < def);
1365 EXPECT_FALSE(def < empty);
1366 EXPECT_TRUE(empty < c_def);
1367 EXPECT_FALSE(def < c_null);
1368 EXPECT_FALSE(def < c_empty);
1369
1370 EXPECT_TRUE(a < abc);
1371 EXPECT_FALSE(abc < a);
1372 EXPECT_TRUE(a < c_abc);
1373 EXPECT_FALSE(abc < c_a);
1374
1375 EXPECT_TRUE(a < def);
1376 EXPECT_FALSE(def < a);
1377 EXPECT_TRUE(a < c_def);
1378 EXPECT_FALSE(def < c_a);
1379
1380 EXPECT_TRUE(abc < def);
1381 EXPECT_FALSE(def < abc);
1382 EXPECT_TRUE(abc < c_def);
1383 EXPECT_FALSE(def < c_abc);
1384 }
1385
TEST(WideStringView,OperatorEQ)1386 TEST(WideStringView, OperatorEQ) {
1387 WideStringView wide_string_c(L"hello");
1388 EXPECT_TRUE(wide_string_c == wide_string_c);
1389
1390 WideStringView wide_string_c_same1(L"hello");
1391 EXPECT_TRUE(wide_string_c == wide_string_c_same1);
1392 EXPECT_TRUE(wide_string_c_same1 == wide_string_c);
1393
1394 WideStringView wide_string_c_same2(wide_string_c);
1395 EXPECT_TRUE(wide_string_c == wide_string_c_same2);
1396 EXPECT_TRUE(wide_string_c_same2 == wide_string_c);
1397
1398 WideStringView wide_string_c1(L"he");
1399 WideStringView wide_string_c2(L"hellp");
1400 WideStringView wide_string_c3(L"hellod");
1401 EXPECT_FALSE(wide_string_c == wide_string_c1);
1402 EXPECT_FALSE(wide_string_c == wide_string_c2);
1403 EXPECT_FALSE(wide_string_c == wide_string_c3);
1404 EXPECT_FALSE(wide_string_c1 == wide_string_c);
1405 EXPECT_FALSE(wide_string_c2 == wide_string_c);
1406 EXPECT_FALSE(wide_string_c3 == wide_string_c);
1407
1408 WideString wide_string_same1(L"hello");
1409 EXPECT_TRUE(wide_string_c == wide_string_same1);
1410 EXPECT_TRUE(wide_string_same1 == wide_string_c);
1411
1412 WideString wide_string1(L"he");
1413 WideString wide_string2(L"hellp");
1414 WideString wide_string3(L"hellod");
1415 EXPECT_FALSE(wide_string_c == wide_string1);
1416 EXPECT_FALSE(wide_string_c == wide_string2);
1417 EXPECT_FALSE(wide_string_c == wide_string3);
1418 EXPECT_FALSE(wide_string1 == wide_string_c);
1419 EXPECT_FALSE(wide_string2 == wide_string_c);
1420 EXPECT_FALSE(wide_string3 == wide_string_c);
1421
1422 const wchar_t* const c_string_same1 = L"hello";
1423 EXPECT_TRUE(wide_string_c == c_string_same1);
1424 EXPECT_TRUE(c_string_same1 == wide_string_c);
1425
1426 const wchar_t* const c_string1 = L"he";
1427 const wchar_t* const c_string2 = L"hellp";
1428 const wchar_t* const c_string3 = L"hellod";
1429 EXPECT_FALSE(wide_string_c == c_string1);
1430 EXPECT_FALSE(wide_string_c == c_string2);
1431 EXPECT_FALSE(wide_string_c == c_string3);
1432
1433 EXPECT_FALSE(c_string1 == wide_string_c);
1434 EXPECT_FALSE(c_string2 == wide_string_c);
1435 EXPECT_FALSE(c_string3 == wide_string_c);
1436 }
1437
TEST(WideStringView,OperatorNE)1438 TEST(WideStringView, OperatorNE) {
1439 WideStringView wide_string_c(L"hello");
1440 EXPECT_FALSE(wide_string_c != wide_string_c);
1441
1442 WideStringView wide_string_c_same1(L"hello");
1443 EXPECT_FALSE(wide_string_c != wide_string_c_same1);
1444 EXPECT_FALSE(wide_string_c_same1 != wide_string_c);
1445
1446 WideStringView wide_string_c_same2(wide_string_c);
1447 EXPECT_FALSE(wide_string_c != wide_string_c_same2);
1448 EXPECT_FALSE(wide_string_c_same2 != wide_string_c);
1449
1450 WideStringView wide_string_c1(L"he");
1451 WideStringView wide_string_c2(L"hellp");
1452 WideStringView wide_string_c3(L"hellod");
1453 EXPECT_TRUE(wide_string_c != wide_string_c1);
1454 EXPECT_TRUE(wide_string_c != wide_string_c2);
1455 EXPECT_TRUE(wide_string_c != wide_string_c3);
1456 EXPECT_TRUE(wide_string_c1 != wide_string_c);
1457 EXPECT_TRUE(wide_string_c2 != wide_string_c);
1458 EXPECT_TRUE(wide_string_c3 != wide_string_c);
1459
1460 WideString wide_string_same1(L"hello");
1461 EXPECT_FALSE(wide_string_c != wide_string_same1);
1462 EXPECT_FALSE(wide_string_same1 != wide_string_c);
1463
1464 WideString wide_string1(L"he");
1465 WideString wide_string2(L"hellp");
1466 WideString wide_string3(L"hellod");
1467 EXPECT_TRUE(wide_string_c != wide_string1);
1468 EXPECT_TRUE(wide_string_c != wide_string2);
1469 EXPECT_TRUE(wide_string_c != wide_string3);
1470 EXPECT_TRUE(wide_string1 != wide_string_c);
1471 EXPECT_TRUE(wide_string2 != wide_string_c);
1472 EXPECT_TRUE(wide_string3 != wide_string_c);
1473
1474 const wchar_t* const c_string_same1 = L"hello";
1475 EXPECT_FALSE(wide_string_c != c_string_same1);
1476 EXPECT_FALSE(c_string_same1 != wide_string_c);
1477
1478 const wchar_t* const c_string1 = L"he";
1479 const wchar_t* const c_string2 = L"hellp";
1480 const wchar_t* const c_string3 = L"hellod";
1481 EXPECT_TRUE(wide_string_c != c_string1);
1482 EXPECT_TRUE(wide_string_c != c_string2);
1483 EXPECT_TRUE(wide_string_c != c_string3);
1484
1485 EXPECT_TRUE(c_string1 != wide_string_c);
1486 EXPECT_TRUE(c_string2 != wide_string_c);
1487 EXPECT_TRUE(c_string3 != wide_string_c);
1488 }
1489
TEST(WideStringView,Find)1490 TEST(WideStringView, Find) {
1491 WideStringView null_string;
1492 EXPECT_FALSE(null_string.Find(L'a').has_value());
1493 EXPECT_FALSE(null_string.Find(L'\0').has_value());
1494
1495 WideStringView empty_string(L"");
1496 EXPECT_FALSE(empty_string.Find(L'a').has_value());
1497 EXPECT_FALSE(empty_string.Find(L'\0').has_value());
1498
1499 WideStringView single_string(L"a");
1500 absl::optional<size_t> result = single_string.Find(L'a');
1501 ASSERT_TRUE(result.has_value());
1502 EXPECT_EQ(0u, result.value());
1503 EXPECT_FALSE(single_string.Find(L'b').has_value());
1504 EXPECT_FALSE(single_string.Find(L'\0').has_value());
1505
1506 WideStringView longer_string(L"abccc");
1507 result = longer_string.Find(L'a');
1508 ASSERT_TRUE(result.has_value());
1509 EXPECT_EQ(0u, result.value());
1510 result = longer_string.Find(L'c');
1511 ASSERT_TRUE(result.has_value());
1512 EXPECT_EQ(2u, result.value());
1513 EXPECT_FALSE(longer_string.Find(L'd').has_value());
1514 EXPECT_FALSE(longer_string.Find(L'\0').has_value());
1515
1516 WideStringView hibyte_string(
1517 L"ab\xFF8c"
1518 L"def");
1519 result = hibyte_string.Find(L'\xFF8c');
1520 ASSERT_TRUE(result.has_value());
1521 EXPECT_EQ(2u, result.value());
1522 }
1523
TEST(WideStringView,NullIterator)1524 TEST(WideStringView, NullIterator) {
1525 WideStringView null_str;
1526 int32_t sum = 0;
1527 bool any_present = false;
1528 for (const auto& c : null_str) {
1529 sum += c; // Avoid unused arg warnings.
1530 any_present = true;
1531 }
1532 EXPECT_FALSE(any_present);
1533 EXPECT_EQ(0, sum);
1534 }
1535
TEST(WideStringView,EmptyIterator)1536 TEST(WideStringView, EmptyIterator) {
1537 WideStringView empty_str(L"");
1538 int32_t sum = 0;
1539 bool any_present = false;
1540 for (const auto& c : empty_str) {
1541 any_present = true;
1542 sum += c; // Avoid unused arg warnings.
1543 }
1544 EXPECT_FALSE(any_present);
1545 EXPECT_EQ(0, sum);
1546 }
1547
TEST(WideStringView,OneCharIterator)1548 TEST(WideStringView, OneCharIterator) {
1549 WideStringView one_str(L"a");
1550 int32_t sum = 0;
1551 bool any_present = false;
1552 for (const auto& c : one_str) {
1553 any_present = true;
1554 sum += c; // Avoid unused arg warnings.
1555 }
1556 EXPECT_TRUE(any_present);
1557 EXPECT_EQ(static_cast<int32_t>(L'a'), sum);
1558 }
1559
TEST(WideStringView,MultiCharIterator)1560 TEST(WideStringView, MultiCharIterator) {
1561 WideStringView one_str(L"abc");
1562 int32_t sum = 0;
1563 bool any_present = false;
1564 for (const auto& c : one_str) {
1565 any_present = true;
1566 sum += c; // Avoid unused arg warnings.
1567 }
1568 EXPECT_TRUE(any_present);
1569 EXPECT_EQ(static_cast<int32_t>(L'a' + L'b' + L'c'), sum);
1570 }
1571
TEST(WideStringView,EmptyReverseIterator)1572 TEST(WideStringView, EmptyReverseIterator) {
1573 WideStringView empty;
1574 auto iter = empty.rbegin();
1575 EXPECT_TRUE(iter == empty.rend());
1576 EXPECT_FALSE(iter != empty.rend());
1577 EXPECT_FALSE(iter < empty.rend());
1578 }
1579
TEST(WideStringView,OneCharReverseIterator)1580 TEST(WideStringView, OneCharReverseIterator) {
1581 WideStringView one_str(L"a");
1582 auto iter = one_str.rbegin();
1583 EXPECT_FALSE(iter == one_str.rend());
1584 EXPECT_TRUE(iter != one_str.rend());
1585 EXPECT_TRUE(iter < one_str.rend());
1586
1587 char ch = *iter++;
1588 EXPECT_EQ('a', ch);
1589 EXPECT_TRUE(iter == one_str.rend());
1590 EXPECT_FALSE(iter != one_str.rend());
1591 EXPECT_FALSE(iter < one_str.rend());
1592 }
1593
TEST(WideStringView,MultiCharReverseIterator)1594 TEST(WideStringView, MultiCharReverseIterator) {
1595 WideStringView multi_str(L"abcd");
1596 auto iter = multi_str.rbegin();
1597 EXPECT_FALSE(iter == multi_str.rend());
1598
1599 char ch = *iter++;
1600 EXPECT_EQ('d', ch);
1601 EXPECT_EQ('c', *iter);
1602 EXPECT_FALSE(iter == multi_str.rend());
1603
1604 ch = *(++iter);
1605 EXPECT_EQ('b', ch);
1606 EXPECT_EQ('b', *iter);
1607 EXPECT_FALSE(iter == multi_str.rend());
1608
1609 ch = *iter++;
1610 EXPECT_EQ('b', ch);
1611 EXPECT_EQ('a', *iter);
1612 EXPECT_FALSE(iter == multi_str.rend());
1613
1614 ch = *iter++;
1615 EXPECT_EQ('a', ch);
1616 EXPECT_TRUE(iter == multi_str.rend());
1617
1618 ch = *(--iter);
1619 EXPECT_EQ('a', ch);
1620 EXPECT_EQ('a', *iter);
1621 EXPECT_FALSE(iter == multi_str.rend());
1622
1623 ch = *iter--;
1624 EXPECT_EQ('a', ch);
1625 EXPECT_EQ('b', *iter);
1626 EXPECT_FALSE(iter == multi_str.rend());
1627
1628 ch = *iter--;
1629 EXPECT_EQ('b', ch);
1630 EXPECT_EQ('c', *iter);
1631 EXPECT_FALSE(iter == multi_str.rend());
1632
1633 ch = *(--iter);
1634 EXPECT_EQ('d', ch);
1635 EXPECT_EQ('d', *iter);
1636 EXPECT_TRUE(iter == multi_str.rbegin());
1637 }
1638
TEST(WideStringView,AnyAllNoneOf)1639 TEST(WideStringView, AnyAllNoneOf) {
1640 WideStringView str(L"aaaaaaaaaaaaaaaaab");
1641 EXPECT_FALSE(std::all_of(str.begin(), str.end(),
1642 [](const wchar_t& c) { return c == L'a'; }));
1643
1644 EXPECT_FALSE(std::none_of(str.begin(), str.end(),
1645 [](const wchar_t& c) { return c == L'a'; }));
1646
1647 EXPECT_TRUE(std::any_of(str.begin(), str.end(),
1648 [](const wchar_t& c) { return c == L'a'; }));
1649
1650 EXPECT_TRUE(pdfium::Contains(str, L'a'));
1651 EXPECT_TRUE(pdfium::Contains(str, L'b'));
1652 EXPECT_FALSE(pdfium::Contains(str, L'z'));
1653 }
1654
TEST(WideStringView,TrimmedRight)1655 TEST(WideStringView, TrimmedRight) {
1656 WideStringView fred(L"FRED");
1657 EXPECT_EQ(L"FRED", fred.TrimmedRight(L'E'));
1658 EXPECT_EQ(L"FRE", fred.TrimmedRight(L'D'));
1659 WideStringView fredd(L"FREDD");
1660 EXPECT_EQ(L"FRE", fred.TrimmedRight(L'D'));
1661 }
1662
TEST(WideString,FormatWidth)1663 TEST(WideString, FormatWidth) {
1664 EXPECT_EQ(L" 1", WideString::Format(L"%5d", 1));
1665 EXPECT_EQ(L"1", WideString::Format(L"%d", 1));
1666 EXPECT_EQ(L" 1", WideString::Format(L"%*d", 5, 1));
1667 EXPECT_EQ(L"1", WideString::Format(L"%-1d", 1));
1668 EXPECT_EQ(L"1", WideString::Format(L"%0d", 1));
1669 EXPECT_EQ(L"", WideString::Format(L"%1048576d", 1));
1670 }
1671
TEST(WideString,FormatPrecision)1672 TEST(WideString, FormatPrecision) {
1673 EXPECT_EQ(L"1.12", WideString::Format(L"%.2f", 1.12345));
1674 EXPECT_EQ(L"1.123", WideString::Format(L"%.*f", 3, 1.12345));
1675 EXPECT_EQ(L"1.123450", WideString::Format(L"%f", 1.12345));
1676 EXPECT_EQ(L"1.123450", WideString::Format(L"%-1f", 1.12345));
1677 EXPECT_EQ(L"1.123450", WideString::Format(L"%0f", 1.12345));
1678 EXPECT_EQ(L"", WideString::Format(L"%.1048576f", 1.2));
1679 }
1680
TEST(WideString,FormatOutOfRangeChar)1681 TEST(WideString, FormatOutOfRangeChar) {
1682 EXPECT_NE(L"", WideString::Format(L"unsupported char '%c'", 0x00FF00FF));
1683 }
1684
TEST(WideString,FormatString)1685 TEST(WideString, FormatString) {
1686 // %ls and wide characters are the reliable combination across platforms.
1687 EXPECT_EQ(L"", WideString::Format(L"%ls", L""));
1688 EXPECT_EQ(L"", WideString::Format(L"%ls", WideString().c_str()));
1689 EXPECT_EQ(L"clams", WideString::Format(L"%ls", L"clams"));
1690 EXPECT_EQ(L"cla", WideString::Format(L"%.3ls", L"clams"));
1691 EXPECT_EQ(L"\u043e\u043f", WideString(L"\u043e\u043f"));
1692
1693 #if !BUILDFLAG(IS_APPLE)
1694 // See https://bugs.chromium.org/p/pdfium/issues/detail?id=1132
1695 EXPECT_EQ(L"\u043e\u043f", WideString::Format(L"\u043e\u043f"));
1696 EXPECT_EQ(L"\u043e\u043f", WideString::Format(L"%ls", L"\u043e\u043f"));
1697 EXPECT_EQ(L"\u043e", WideString::Format(L"%.1ls", L"\u043e\u043f"));
1698 #endif
1699 }
1700
TEST(WideString,Empty)1701 TEST(WideString, Empty) {
1702 WideString empty_str;
1703 EXPECT_TRUE(empty_str.IsEmpty());
1704 EXPECT_EQ(0u, empty_str.GetLength());
1705
1706 const wchar_t* cstr = empty_str.c_str();
1707 EXPECT_TRUE(cstr);
1708 EXPECT_EQ(0u, wcslen(cstr));
1709
1710 pdfium::span<const wchar_t> cspan = empty_str.span();
1711 EXPECT_TRUE(cspan.empty());
1712 EXPECT_FALSE(cspan.data());
1713 }
1714
TEST(CFX_WidString,InitializerList)1715 TEST(CFX_WidString, InitializerList) {
1716 WideString many_str({L"clams", L" and ", L"oysters"});
1717 EXPECT_EQ(L"clams and oysters", many_str);
1718 many_str = {L"fish", L" and ", L"chips", L" and ", L"soda"};
1719 EXPECT_EQ(L"fish and chips and soda", many_str);
1720 }
1721
TEST(WideString,NullIterator)1722 TEST(WideString, NullIterator) {
1723 WideString null_str;
1724 int32_t sum = 0;
1725 bool any_present = false;
1726 for (const auto& c : null_str) {
1727 sum += c; // Avoid unused arg warnings.
1728 any_present = true;
1729 }
1730 EXPECT_FALSE(any_present);
1731 EXPECT_EQ(0, sum);
1732 }
1733
TEST(WideString,EmptyIterator)1734 TEST(WideString, EmptyIterator) {
1735 WideString empty_str(L"");
1736 int32_t sum = 0;
1737 bool any_present = false;
1738 for (const auto& c : empty_str) {
1739 any_present = true;
1740 sum += c; // Avoid unused arg warnings.
1741 }
1742 EXPECT_FALSE(any_present);
1743 EXPECT_EQ(0, sum);
1744 }
1745
TEST(WideString,OneCharIterator)1746 TEST(WideString, OneCharIterator) {
1747 WideString one_str(L"a");
1748 int32_t sum = 0;
1749 bool any_present = false;
1750 for (const auto& c : one_str) {
1751 any_present = true;
1752 sum += c; // Avoid unused arg warnings.
1753 }
1754 EXPECT_TRUE(any_present);
1755 EXPECT_EQ(static_cast<int32_t>(L'a'), sum);
1756 }
1757
TEST(WideString,MultiCharIterator)1758 TEST(WideString, MultiCharIterator) {
1759 WideString one_str(L"abc");
1760 int32_t sum = 0;
1761 bool any_present = false;
1762 for (const auto& c : one_str) {
1763 any_present = true;
1764 sum += c; // Avoid unused arg warnings.
1765 }
1766 EXPECT_TRUE(any_present);
1767 EXPECT_EQ(static_cast<int32_t>(L'a' + L'b' + L'c'), sum);
1768 }
1769
TEST(WideString,StdBegin)1770 TEST(WideString, StdBegin) {
1771 WideString one_str(L"abc");
1772 std::vector<wchar_t> vec(std::begin(one_str), std::end(one_str));
1773 ASSERT_EQ(3u, vec.size());
1774 EXPECT_EQ(L'a', vec[0]);
1775 EXPECT_EQ(L'b', vec[1]);
1776 EXPECT_EQ(L'c', vec[2]);
1777 }
1778
TEST(WideString,AnyAllNoneOf)1779 TEST(WideString, AnyAllNoneOf) {
1780 WideString str(L"aaaaaaaaaaaaaaaaab");
1781 EXPECT_FALSE(std::all_of(str.begin(), str.end(),
1782 [](const wchar_t& c) { return c == L'a'; }));
1783
1784 EXPECT_FALSE(std::none_of(str.begin(), str.end(),
1785 [](const wchar_t& c) { return c == L'a'; }));
1786
1787 EXPECT_TRUE(std::any_of(str.begin(), str.end(),
1788 [](const wchar_t& c) { return c == L'a'; }));
1789
1790 EXPECT_TRUE(pdfium::Contains(str, L'a'));
1791 EXPECT_TRUE(pdfium::Contains(str, L'b'));
1792 EXPECT_FALSE(pdfium::Contains(str, L'z'));
1793 }
1794
TEST(WideString,OStreamOverload)1795 TEST(WideString, OStreamOverload) {
1796 std::ostringstream stream;
1797
1798 // Basic case, empty string
1799 WideString str;
1800 stream << str;
1801 EXPECT_EQ("", stream.str());
1802
1803 // Basic case, wide character
1804 str = L"\u20AC";
1805 stream << str;
1806 EXPECT_EQ("\u20AC", stream.str());
1807
1808 // Basic case, non-empty string
1809 str = L"def";
1810 stream.str("");
1811 stream << "abc" << str << "ghi";
1812 EXPECT_EQ("abcdefghi", stream.str());
1813
1814 // Changing the WideString does not change the stream it was written to.
1815 str = L"123";
1816 EXPECT_EQ("abcdefghi", stream.str());
1817
1818 // Writing it again to the stream will use the latest value.
1819 stream.str("");
1820 stream << "abc" << str << "ghi";
1821 EXPECT_EQ("abc123ghi", stream.str());
1822
1823 wchar_t stringWithNulls[]{'x', 'y', '\0', 'z'};
1824
1825 // Writing a WideString with nulls and no specified length treats it as
1826 // a C-style null-terminated string.
1827 str = WideString(stringWithNulls);
1828 EXPECT_EQ(2u, str.GetLength());
1829 stream.str("");
1830 stream << str;
1831 EXPECT_EQ(2u, stream.tellp());
1832
1833 // Writing a WideString with nulls but specifying its length treats it as
1834 // a C++-style string.
1835 str = WideString(stringWithNulls, 4);
1836 EXPECT_EQ(4u, str.GetLength());
1837 stream.str("");
1838 stream << str;
1839 EXPECT_EQ(4u, stream.tellp());
1840
1841 // << operators can be chained.
1842 WideString str1(L"abc");
1843 WideString str2(L"def");
1844 stream.str("");
1845 stream << str1 << str2;
1846 EXPECT_EQ("abcdef", stream.str());
1847 }
1848
TEST(WideString,WideOStreamOverload)1849 TEST(WideString, WideOStreamOverload) {
1850 std::wostringstream stream;
1851
1852 // Basic case, empty string
1853 WideString str;
1854 stream << str;
1855 EXPECT_EQ(L"", stream.str());
1856
1857 // Basic case, wide character
1858 str = L"\u20AC";
1859 stream << str;
1860 EXPECT_EQ(L"\u20AC", stream.str());
1861
1862 // Basic case, non-empty string
1863 str = L"def";
1864 stream.str(L"");
1865 stream << L"abc" << str << L"ghi";
1866 EXPECT_EQ(L"abcdefghi", stream.str());
1867
1868 // Changing the WideString does not change the stream it was written to.
1869 str = L"123";
1870 EXPECT_EQ(L"abcdefghi", stream.str());
1871
1872 // Writing it again to the stream will use the latest value.
1873 stream.str(L"");
1874 stream << L"abc" << str << L"ghi";
1875 EXPECT_EQ(L"abc123ghi", stream.str());
1876
1877 wchar_t stringWithNulls[]{'x', 'y', '\0', 'z'};
1878
1879 // Writing a WideString with nulls and no specified length treats it as
1880 // a C-style null-terminated string.
1881 str = WideString(stringWithNulls);
1882 EXPECT_EQ(2u, str.GetLength());
1883 stream.str(L"");
1884 stream << str;
1885 EXPECT_EQ(2u, stream.tellp());
1886
1887 // Writing a WideString with nulls but specifying its length treats it as
1888 // a C++-style string.
1889 str = WideString(stringWithNulls, 4);
1890 EXPECT_EQ(4u, str.GetLength());
1891 stream.str(L"");
1892 stream << str;
1893 EXPECT_EQ(4u, stream.tellp());
1894
1895 // << operators can be chained.
1896 WideString str1(L"abc");
1897 WideString str2(L"def");
1898 stream.str(L"");
1899 stream << str1 << str2;
1900 EXPECT_EQ(L"abcdef", stream.str());
1901 }
1902
TEST(WideStringView,OStreamOverload)1903 TEST(WideStringView, OStreamOverload) {
1904 // Basic case, empty string
1905 {
1906 std::ostringstream stream;
1907 WideStringView str;
1908 stream << str;
1909 EXPECT_EQ("", stream.str());
1910 }
1911
1912 // Basic case, non-empty string
1913 {
1914 std::ostringstream stream;
1915 WideStringView str(L"def");
1916 stream << "abc" << str << "ghi";
1917 EXPECT_EQ("abcdefghi", stream.str());
1918 }
1919
1920 // Basic case, wide character
1921 {
1922 std::ostringstream stream;
1923 WideStringView str(L"\u20AC");
1924 stream << str;
1925 EXPECT_EQ("\u20AC", stream.str());
1926 }
1927
1928 // Changing the WideStringView does not change the stream it was written to.
1929 {
1930 std::ostringstream stream;
1931 WideStringView str(L"abc");
1932 stream << str;
1933 str = L"123";
1934 EXPECT_EQ("abc", stream.str());
1935 }
1936
1937 // Writing it again to the stream will use the latest value.
1938 {
1939 std::ostringstream stream;
1940 WideStringView str(L"abc");
1941 stream << str;
1942 stream.str("");
1943 str = L"123";
1944 stream << str;
1945 EXPECT_EQ("123", stream.str());
1946 }
1947
1948 // Writing a WideStringView with nulls and no specified length treats it as
1949 // a C-style null-terminated string.
1950 {
1951 wchar_t stringWithNulls[]{'x', 'y', '\0', 'z'};
1952 std::ostringstream stream;
1953 WideStringView str(stringWithNulls);
1954 EXPECT_EQ(2u, str.GetLength());
1955 stream << str;
1956 EXPECT_EQ(2u, stream.tellp());
1957 str = L"";
1958 }
1959
1960 // Writing a WideStringView with nulls but specifying its length treats it as
1961 // a C++-style string.
1962 {
1963 wchar_t stringWithNulls[]{'x', 'y', '\0', 'z'};
1964 std::ostringstream stream;
1965 WideStringView str(stringWithNulls, 4);
1966 EXPECT_EQ(4u, str.GetLength());
1967 stream << str;
1968 EXPECT_EQ(4u, stream.tellp());
1969 str = L"";
1970 }
1971
1972 // << operators can be chained.
1973 {
1974 std::ostringstream stream;
1975 WideStringView str1(L"abc");
1976 WideStringView str2(L"def");
1977 stream << str1 << str2;
1978 EXPECT_EQ("abcdef", stream.str());
1979 }
1980 }
1981
TEST(WideStringView,WideOStreamOverload)1982 TEST(WideStringView, WideOStreamOverload) {
1983 // Basic case, empty string
1984 {
1985 std::wostringstream stream;
1986 WideStringView str;
1987 stream << str;
1988 EXPECT_EQ(L"", stream.str());
1989 }
1990
1991 // Basic case, non-empty string
1992 {
1993 std::wostringstream stream;
1994 WideStringView str(L"def");
1995 stream << "abc" << str << "ghi";
1996 EXPECT_EQ(L"abcdefghi", stream.str());
1997 }
1998
1999 // Basic case, wide character
2000 {
2001 std::wostringstream stream;
2002 WideStringView str(L"\u20AC");
2003 stream << str;
2004 EXPECT_EQ(L"\u20AC", stream.str());
2005 }
2006
2007 // Changing the WideStringView does not change the stream it was written to.
2008 {
2009 std::wostringstream stream;
2010 WideStringView str(L"abc");
2011 stream << str;
2012 str = L"123";
2013 EXPECT_EQ(L"abc", stream.str());
2014 }
2015
2016 // Writing it again to the stream will use the latest value.
2017 {
2018 std::wostringstream stream;
2019 WideStringView str(L"abc");
2020 stream << str;
2021 stream.str(L"");
2022 str = L"123";
2023 stream << str;
2024 EXPECT_EQ(L"123", stream.str());
2025 }
2026
2027 // Writing a WideStringView with nulls and no specified length treats it as
2028 // a C-style null-terminated string.
2029 {
2030 wchar_t stringWithNulls[]{'x', 'y', '\0', 'z'};
2031 std::wostringstream stream;
2032 WideStringView str(stringWithNulls);
2033 EXPECT_EQ(2u, str.GetLength());
2034 stream << str;
2035 EXPECT_EQ(2u, stream.tellp());
2036 }
2037
2038 // Writing a WideStringView with nulls but specifying its length treats it as
2039 // a C++-style string.
2040 {
2041 wchar_t stringWithNulls[]{'x', 'y', '\0', 'z'};
2042 std::wostringstream stream;
2043 WideStringView str(stringWithNulls, 4);
2044 EXPECT_EQ(4u, str.GetLength());
2045 stream << str;
2046 EXPECT_EQ(4u, stream.tellp());
2047 }
2048
2049 // << operators can be chained.
2050 {
2051 std::wostringstream stream;
2052 WideStringView str1(L"abc");
2053 WideStringView str2(L"def");
2054 stream << str1 << str2;
2055 EXPECT_EQ(L"abcdef", stream.str());
2056 }
2057 }
2058
TEST(WideString,FormatInteger)2059 TEST(WideString, FormatInteger) {
2060 // Base case of 0.
2061 EXPECT_EQ(L"0", WideString::FormatInteger(0));
2062
2063 // Positive ordinary number.
2064 EXPECT_EQ(L"123456", WideString::FormatInteger(123456));
2065
2066 // Negative ordinary number.
2067 EXPECT_EQ(L"-123456", WideString::FormatInteger(-123456));
2068
2069 // int limits.
2070 EXPECT_EQ(L"2147483647", WideString::FormatInteger(INT_MAX));
2071 EXPECT_EQ(L"-2147483648", WideString::FormatInteger(INT_MIN));
2072 }
2073
TEST(WideString,FX_HashCode_Wide)2074 TEST(WideString, FX_HashCode_Wide) {
2075 EXPECT_EQ(0u, FX_HashCode_GetW(L""));
2076 EXPECT_EQ(65u, FX_HashCode_GetW(L"A"));
2077 EXPECT_EQ(97u, FX_HashCode_GetLoweredW(L"A"));
2078 EXPECT_EQ(1313 * 65u + 66u, FX_HashCode_GetW(L"AB"));
2079 EXPECT_EQ(FX_HashCode_GetAsIfW("AB\xff"), FX_HashCode_GetW(L"AB\xff"));
2080 EXPECT_EQ(FX_HashCode_GetLoweredAsIfW("AB\xff"),
2081 FX_HashCode_GetLoweredW(L"AB\xff"));
2082 }
2083
2084 } // namespace fxcrt
2085