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