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