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