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