xref: /llvm-project/llvm/unittests/ADT/StringRefTest.cpp (revision 931d04be2fc8f3f0505b43e64297f75d526cb42a)
1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/Hashing.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "gtest/gtest.h"
17 using namespace llvm;
18 
19 namespace llvm {
20 
21 std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
22   OS << S.str();
23   return OS;
24 }
25 
26 std::ostream &operator<<(std::ostream &OS,
27                          const std::pair<StringRef, StringRef> &P) {
28   OS << "(" << P.first << ", " << P.second << ")";
29   return OS;
30 }
31 
32 }
33 
34 // Check that we can't accidentally assign a temporary std::string to a
35 // StringRef. (Unfortunately we can't make use of the same thing with
36 // constructors.)
37 static_assert(!std::is_assignable_v<StringRef &, std::string>,
38               "Assigning from prvalue std::string");
39 static_assert(!std::is_assignable_v<StringRef &, std::string &&>,
40               "Assigning from xvalue std::string");
41 static_assert(std::is_assignable_v<StringRef &, std::string &>,
42               "Assigning from lvalue std::string");
43 static_assert(std::is_assignable_v<StringRef &, const char *>,
44               "Assigning from prvalue C string");
45 static_assert(std::is_assignable_v<StringRef &, const char *&&>,
46               "Assigning from xvalue C string");
47 static_assert(std::is_assignable_v<StringRef &, const char *&>,
48               "Assigning from lvalue C string");
49 
50 namespace {
51 TEST(StringRefTest, Construction) {
52   EXPECT_EQ("", StringRef());
53   EXPECT_EQ("hello", StringRef("hello"));
54   EXPECT_EQ("hello", StringRef("hello world", 5));
55   EXPECT_EQ("hello", StringRef(std::string("hello")));
56   EXPECT_EQ("hello", StringRef(std::string_view("hello")));
57 }
58 
59 TEST(StringRefTest, Conversion) {
60   EXPECT_EQ("hello", std::string(StringRef("hello")));
61   EXPECT_EQ("hello", std::string_view(StringRef("hello")));
62 }
63 
64 TEST(StringRefTest, EmptyInitializerList) {
65   StringRef S = {};
66   EXPECT_TRUE(S.empty());
67 
68   S = {};
69   EXPECT_TRUE(S.empty());
70 }
71 
72 TEST(StringRefTest, Iteration) {
73   StringRef S("hello");
74   const char *p = "hello";
75   for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
76     EXPECT_EQ(*it, *p);
77 }
78 
79 TEST(StringRefTest, StringOps) {
80   const char *p = "hello";
81   EXPECT_EQ(p, StringRef(p, 0).data());
82   EXPECT_TRUE(StringRef().empty());
83   EXPECT_EQ((size_t) 5, StringRef("hello").size());
84   EXPECT_GT( 0, StringRef("aab").compare("aad"));
85   EXPECT_EQ( 0, StringRef("aab").compare("aab"));
86   EXPECT_LT( 0, StringRef("aab").compare("aaa"));
87   EXPECT_GT( 0, StringRef("aab").compare("aabb"));
88   EXPECT_LT( 0, StringRef("aab").compare("aa"));
89   EXPECT_LT( 0, StringRef("\xFF").compare("\1"));
90 
91   EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aAd"));
92   EXPECT_EQ( 0, StringRef("AaB").compare_insensitive("aab"));
93   EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("AAA"));
94   EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aaBb"));
95   EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("bb"));
96   EXPECT_EQ( 1, StringRef("aaBb").compare_insensitive("AaB"));
97   EXPECT_EQ( 1, StringRef("bb").compare_insensitive("AaB"));
98   EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("aA"));
99   EXPECT_EQ( 1, StringRef("\xFF").compare_insensitive("\1"));
100 
101   EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
102   EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
103   EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
104   EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
105   EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
106   EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
107   EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
108   EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
109   EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
110   EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
111   EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
112   EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
113   EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
114   EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
115   EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
116   EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
117   EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
118 }
119 
120 TEST(StringRefTest, Operators) {
121   EXPECT_EQ("", StringRef());
122   EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
123   EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
124   EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
125   EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
126   EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
127   EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
128   EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
129   EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
130   EXPECT_EQ(StringRef("aab"), StringRef("aab"));
131   EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
132   EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
133   EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
134   EXPECT_EQ('a', StringRef("aab")[1]);
135 }
136 
137 TEST(StringRefTest, Substr) {
138   StringRef Str("hello");
139   EXPECT_EQ("lo", Str.substr(3));
140   EXPECT_EQ("", Str.substr(100));
141   EXPECT_EQ("hello", Str.substr(0, 100));
142   EXPECT_EQ("o", Str.substr(4, 10));
143 }
144 
145 TEST(StringRefTest, Slice) {
146   StringRef Str("hello");
147   EXPECT_EQ("l", Str.slice(2, 3));
148   EXPECT_EQ("ell", Str.slice(1, 4));
149   EXPECT_EQ("llo", Str.slice(2, 100));
150   EXPECT_EQ("", Str.slice(2, 1));
151   EXPECT_EQ("", Str.slice(10, 20));
152 }
153 
154 TEST(StringRefTest, Split) {
155   StringRef Str("hello");
156   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
157             Str.split('X'));
158   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
159             Str.split('e'));
160   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
161             Str.split('h'));
162   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
163             Str.split('l'));
164   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
165             Str.split('o'));
166 
167   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
168             Str.rsplit('X'));
169   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
170             Str.rsplit('e'));
171   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
172             Str.rsplit('h'));
173   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
174             Str.rsplit('l'));
175   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
176             Str.rsplit('o'));
177 
178   EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
179 		    Str.rsplit("ll"));
180   EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
181 		    Str.rsplit("h"));
182   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
183 	      Str.rsplit("o"));
184   EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
185 		    Str.rsplit("::"));
186   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
187 		    Str.rsplit("l"));
188 }
189 
190 TEST(StringRefTest, Split2) {
191   SmallVector<StringRef, 5> parts;
192   SmallVector<StringRef, 5> expected;
193 
194   expected.push_back("ab"); expected.push_back("c");
195   StringRef(",ab,,c,").split(parts, ",", -1, false);
196   EXPECT_TRUE(parts == expected);
197 
198   expected.clear(); parts.clear();
199   expected.push_back(""); expected.push_back("ab"); expected.push_back("");
200   expected.push_back("c"); expected.push_back("");
201   StringRef(",ab,,c,").split(parts, ",", -1, true);
202   EXPECT_TRUE(parts == expected);
203 
204   expected.clear(); parts.clear();
205   expected.push_back("");
206   StringRef("").split(parts, ",", -1, true);
207   EXPECT_TRUE(parts == expected);
208 
209   expected.clear(); parts.clear();
210   StringRef("").split(parts, ",", -1, false);
211   EXPECT_TRUE(parts == expected);
212 
213   expected.clear(); parts.clear();
214   StringRef(",").split(parts, ",", -1, false);
215   EXPECT_TRUE(parts == expected);
216 
217   expected.clear(); parts.clear();
218   expected.push_back(""); expected.push_back("");
219   StringRef(",").split(parts, ",", -1, true);
220   EXPECT_TRUE(parts == expected);
221 
222   expected.clear(); parts.clear();
223   expected.push_back("a"); expected.push_back("b");
224   StringRef("a,b").split(parts, ",", -1, true);
225   EXPECT_TRUE(parts == expected);
226 
227   // Test MaxSplit
228   expected.clear(); parts.clear();
229   expected.push_back("a,,b,c");
230   StringRef("a,,b,c").split(parts, ",", 0, true);
231   EXPECT_TRUE(parts == expected);
232 
233   expected.clear(); parts.clear();
234   expected.push_back("a,,b,c");
235   StringRef("a,,b,c").split(parts, ",", 0, false);
236   EXPECT_TRUE(parts == expected);
237 
238   expected.clear(); parts.clear();
239   expected.push_back("a"); expected.push_back(",b,c");
240   StringRef("a,,b,c").split(parts, ",", 1, true);
241   EXPECT_TRUE(parts == expected);
242 
243   expected.clear(); parts.clear();
244   expected.push_back("a"); expected.push_back(",b,c");
245   StringRef("a,,b,c").split(parts, ",", 1, false);
246   EXPECT_TRUE(parts == expected);
247 
248   expected.clear(); parts.clear();
249   expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
250   StringRef("a,,b,c").split(parts, ",", 2, true);
251   EXPECT_TRUE(parts == expected);
252 
253   expected.clear(); parts.clear();
254   expected.push_back("a"); expected.push_back("b,c");
255   StringRef("a,,b,c").split(parts, ",", 2, false);
256   EXPECT_TRUE(parts == expected);
257 
258   expected.clear(); parts.clear();
259   expected.push_back("a"); expected.push_back(""); expected.push_back("b");
260   expected.push_back("c");
261   StringRef("a,,b,c").split(parts, ",", 3, true);
262   EXPECT_TRUE(parts == expected);
263 
264   expected.clear(); parts.clear();
265   expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
266   StringRef("a,,b,c").split(parts, ",", 3, false);
267   EXPECT_TRUE(parts == expected);
268 
269   expected.clear(); parts.clear();
270   expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
271   StringRef("a,,b,c").split(parts, ',', 3, false);
272   EXPECT_TRUE(parts == expected);
273 
274   expected.clear(); parts.clear();
275   expected.push_back("");
276   StringRef().split(parts, ",", 0, true);
277   EXPECT_TRUE(parts == expected);
278 
279   expected.clear(); parts.clear();
280   expected.push_back(StringRef());
281   StringRef("").split(parts, ",", 0, true);
282   EXPECT_TRUE(parts == expected);
283 
284   expected.clear(); parts.clear();
285   StringRef("").split(parts, ",", 0, false);
286   EXPECT_TRUE(parts == expected);
287   StringRef().split(parts, ",", 0, false);
288   EXPECT_TRUE(parts == expected);
289 
290   expected.clear(); parts.clear();
291   expected.push_back("a");
292   expected.push_back("");
293   expected.push_back("b");
294   expected.push_back("c,d");
295   StringRef("a,,b,c,d").split(parts, ",", 3, true);
296   EXPECT_TRUE(parts == expected);
297 
298   expected.clear(); parts.clear();
299   expected.push_back("");
300   StringRef().split(parts, ',', 0, true);
301   EXPECT_TRUE(parts == expected);
302 
303   expected.clear(); parts.clear();
304   expected.push_back(StringRef());
305   StringRef("").split(parts, ',', 0, true);
306   EXPECT_TRUE(parts == expected);
307 
308   expected.clear(); parts.clear();
309   StringRef("").split(parts, ',', 0, false);
310   EXPECT_TRUE(parts == expected);
311   StringRef().split(parts, ',', 0, false);
312   EXPECT_TRUE(parts == expected);
313 
314   expected.clear(); parts.clear();
315   expected.push_back("a");
316   expected.push_back("");
317   expected.push_back("b");
318   expected.push_back("c,d");
319   StringRef("a,,b,c,d").split(parts, ',', 3, true);
320   EXPECT_TRUE(parts == expected);
321 }
322 
323 TEST(StringRefTest, Trim) {
324   StringRef Str0("hello");
325   StringRef Str1(" hello ");
326   StringRef Str2("  hello  ");
327   StringRef Str3("\t\n\v\f\r  hello  \t\n\v\f\r");
328 
329   EXPECT_EQ(StringRef("hello"), Str0.rtrim());
330   EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
331   EXPECT_EQ(StringRef("  hello"), Str2.rtrim());
332   EXPECT_EQ(StringRef("\t\n\v\f\r  hello"), Str3.rtrim());
333   EXPECT_EQ(StringRef("hello"), Str0.ltrim());
334   EXPECT_EQ(StringRef("hello "), Str1.ltrim());
335   EXPECT_EQ(StringRef("hello  "), Str2.ltrim());
336   EXPECT_EQ(StringRef("hello  \t\n\v\f\r"), Str3.ltrim());
337   EXPECT_EQ(StringRef("hello"), Str0.trim());
338   EXPECT_EQ(StringRef("hello"), Str1.trim());
339   EXPECT_EQ(StringRef("hello"), Str2.trim());
340   EXPECT_EQ(StringRef("hello"), Str3.trim());
341 
342   EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
343 
344   EXPECT_EQ(StringRef(""), StringRef("").trim());
345   EXPECT_EQ(StringRef(""), StringRef(" ").trim());
346   EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
347   EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
348   EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
349 }
350 
351 TEST(StringRefTest, StartsWith) {
352   StringRef Str("hello");
353   EXPECT_TRUE(Str.starts_with(""));
354   EXPECT_TRUE(Str.starts_with("he"));
355   EXPECT_FALSE(Str.starts_with("helloworld"));
356   EXPECT_FALSE(Str.starts_with("hi"));
357 }
358 
359 TEST(StringRefTest, StartsWithInsensitive) {
360   StringRef Str("heLLo");
361   EXPECT_TRUE(Str.starts_with_insensitive(""));
362   EXPECT_TRUE(Str.starts_with_insensitive("he"));
363   EXPECT_TRUE(Str.starts_with_insensitive("hell"));
364   EXPECT_TRUE(Str.starts_with_insensitive("HELlo"));
365   EXPECT_FALSE(Str.starts_with_insensitive("helloworld"));
366   EXPECT_FALSE(Str.starts_with_insensitive("hi"));
367 }
368 
369 TEST(StringRefTest, ConsumeFront) {
370   StringRef Str("hello");
371   EXPECT_TRUE(Str.consume_front(""));
372   EXPECT_EQ("hello", Str);
373   EXPECT_TRUE(Str.consume_front("he"));
374   EXPECT_EQ("llo", Str);
375   EXPECT_FALSE(Str.consume_front("lloworld"));
376   EXPECT_EQ("llo", Str);
377   EXPECT_FALSE(Str.consume_front("lol"));
378   EXPECT_EQ("llo", Str);
379   EXPECT_TRUE(Str.consume_front("llo"));
380   EXPECT_EQ("", Str);
381   EXPECT_FALSE(Str.consume_front("o"));
382   EXPECT_TRUE(Str.consume_front(""));
383 }
384 
385 TEST(StringRefTest, ConsumeFrontInsensitive) {
386   StringRef Str("heLLo");
387   EXPECT_TRUE(Str.consume_front_insensitive(""));
388   EXPECT_EQ("heLLo", Str);
389   EXPECT_FALSE(Str.consume_front("HEl"));
390   EXPECT_EQ("heLLo", Str);
391   EXPECT_TRUE(Str.consume_front_insensitive("HEl"));
392   EXPECT_EQ("Lo", Str);
393   EXPECT_FALSE(Str.consume_front_insensitive("loworld"));
394   EXPECT_EQ("Lo", Str);
395   EXPECT_FALSE(Str.consume_front_insensitive("ol"));
396   EXPECT_EQ("Lo", Str);
397   EXPECT_TRUE(Str.consume_front_insensitive("lo"));
398   EXPECT_EQ("", Str);
399   EXPECT_FALSE(Str.consume_front_insensitive("o"));
400   EXPECT_TRUE(Str.consume_front_insensitive(""));
401 }
402 
403 TEST(StringRefTest, EndsWith) {
404   StringRef Str("hello");
405   EXPECT_TRUE(Str.ends_with(""));
406   EXPECT_TRUE(Str.ends_with("lo"));
407   EXPECT_FALSE(Str.ends_with("helloworld"));
408   EXPECT_FALSE(Str.ends_with("worldhello"));
409   EXPECT_FALSE(Str.ends_with("so"));
410 }
411 
412 TEST(StringRefTest, EndsWithInsensitive) {
413   StringRef Str("heLLo");
414   EXPECT_TRUE(Str.ends_with_insensitive(""));
415   EXPECT_TRUE(Str.ends_with_insensitive("lo"));
416   EXPECT_TRUE(Str.ends_with_insensitive("LO"));
417   EXPECT_TRUE(Str.ends_with_insensitive("ELlo"));
418   EXPECT_FALSE(Str.ends_with_insensitive("helloworld"));
419   EXPECT_FALSE(Str.ends_with_insensitive("hi"));
420 }
421 
422 TEST(StringRefTest, ConsumeBack) {
423   StringRef Str("hello");
424   EXPECT_TRUE(Str.consume_back(""));
425   EXPECT_EQ("hello", Str);
426   EXPECT_TRUE(Str.consume_back("lo"));
427   EXPECT_EQ("hel", Str);
428   EXPECT_FALSE(Str.consume_back("helhel"));
429   EXPECT_EQ("hel", Str);
430   EXPECT_FALSE(Str.consume_back("hle"));
431   EXPECT_EQ("hel", Str);
432   EXPECT_TRUE(Str.consume_back("hel"));
433   EXPECT_EQ("", Str);
434   EXPECT_FALSE(Str.consume_back("h"));
435   EXPECT_TRUE(Str.consume_back(""));
436 }
437 
438 TEST(StringRefTest, ConsumeBackInsensitive) {
439   StringRef Str("heLLo");
440   EXPECT_TRUE(Str.consume_back_insensitive(""));
441   EXPECT_EQ("heLLo", Str);
442   EXPECT_FALSE(Str.consume_back("lO"));
443   EXPECT_EQ("heLLo", Str);
444   EXPECT_TRUE(Str.consume_back_insensitive("lO"));
445   EXPECT_EQ("heL", Str);
446   EXPECT_FALSE(Str.consume_back_insensitive("helhel"));
447   EXPECT_EQ("heL", Str);
448   EXPECT_FALSE(Str.consume_back_insensitive("hle"));
449   EXPECT_EQ("heL", Str);
450   EXPECT_TRUE(Str.consume_back_insensitive("hEl"));
451   EXPECT_EQ("", Str);
452   EXPECT_FALSE(Str.consume_back_insensitive("h"));
453   EXPECT_TRUE(Str.consume_back_insensitive(""));
454 }
455 
456 TEST(StringRefTest, Find) {
457   StringRef Str("helloHELLO");
458   StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
459 
460   struct {
461     StringRef Str;
462     char C;
463     std::size_t From;
464     std::size_t Pos;
465     std::size_t InsensitivePos;
466   } CharExpectations[] = {
467       {Str, 'h', 0U, 0U, 0U},
468       {Str, 'e', 0U, 1U, 1U},
469       {Str, 'l', 0U, 2U, 2U},
470       {Str, 'l', 3U, 3U, 3U},
471       {Str, 'o', 0U, 4U, 4U},
472       {Str, 'L', 0U, 7U, 2U},
473       {Str, 'z', 0U, StringRef::npos, StringRef::npos},
474   };
475 
476   struct {
477     StringRef Str;
478     llvm::StringRef S;
479     std::size_t From;
480     std::size_t Pos;
481     std::size_t InsensitivePos;
482   } StrExpectations[] = {
483       {Str, "helloword", 0, StringRef::npos, StringRef::npos},
484       {Str, "hello", 0, 0U, 0U},
485       {Str, "ello", 0, 1U, 1U},
486       {Str, "zz", 0, StringRef::npos, StringRef::npos},
487       {Str, "ll", 2U, 2U, 2U},
488       {Str, "ll", 3U, StringRef::npos, 7U},
489       {Str, "LL", 2U, 7U, 2U},
490       {Str, "LL", 3U, 7U, 7U},
491       {Str, "", 0U, 0U, 0U},
492       {LongStr, "hello", 0U, 36U, 36U},
493       {LongStr, "foo", 0U, 28U, 28U},
494       {LongStr, "hell", 2U, 12U, 12U},
495       {LongStr, "HELL", 2U, 42U, 12U},
496       {LongStr, "", 0U, 0U, 0U}};
497 
498   for (auto &E : CharExpectations) {
499     EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
500     EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.C, E.From));
501     EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(toupper(E.C), E.From));
502   }
503 
504   for (auto &E : StrExpectations) {
505     EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
506     EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S, E.From));
507     EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S.upper(), E.From));
508   }
509 
510   EXPECT_EQ(3U, Str.rfind('l'));
511   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
512   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
513   EXPECT_EQ(0U, Str.rfind("hello"));
514   EXPECT_EQ(1U, Str.rfind("ello"));
515   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
516 
517   EXPECT_EQ(8U, Str.rfind_insensitive('l'));
518   EXPECT_EQ(8U, Str.rfind_insensitive('L'));
519   EXPECT_EQ(StringRef::npos, Str.rfind_insensitive('z'));
520   EXPECT_EQ(StringRef::npos, Str.rfind_insensitive("HELLOWORLD"));
521   EXPECT_EQ(5U, Str.rfind("HELLO"));
522   EXPECT_EQ(6U, Str.rfind("ELLO"));
523   EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
524 
525   EXPECT_EQ(2U, Str.find_first_of('l'));
526   EXPECT_EQ(1U, Str.find_first_of("el"));
527   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
528 
529   Str = "hello";
530   EXPECT_EQ(1U, Str.find_first_not_of('h'));
531   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
532   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
533 
534   EXPECT_EQ(3U, Str.find_last_not_of('o'));
535   EXPECT_EQ(1U, Str.find_last_not_of("lo"));
536   EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
537 }
538 
539 TEST(StringRefTest, Count) {
540   StringRef Str("hello");
541   EXPECT_EQ(2U, Str.count('l'));
542   EXPECT_EQ(1U, Str.count('o'));
543   EXPECT_EQ(0U, Str.count('z'));
544   EXPECT_EQ(0U, Str.count("helloworld"));
545   EXPECT_EQ(1U, Str.count("hello"));
546   EXPECT_EQ(1U, Str.count("ello"));
547   EXPECT_EQ(0U, Str.count("zz"));
548   EXPECT_EQ(0U, Str.count(""));
549 
550   StringRef OverlappingAbba("abbabba");
551   EXPECT_EQ(1U, OverlappingAbba.count("abba"));
552   StringRef NonOverlappingAbba("abbaabba");
553   EXPECT_EQ(2U, NonOverlappingAbba.count("abba"));
554   StringRef ComplexAbba("abbabbaxyzabbaxyz");
555   EXPECT_EQ(2U, ComplexAbba.count("abba"));
556 }
557 
558 TEST(StringRefTest, EditDistance) {
559   StringRef Hello("hello");
560   EXPECT_EQ(2U, Hello.edit_distance("hill"));
561 
562   StringRef Industry("industry");
563   EXPECT_EQ(6U, Industry.edit_distance("interest"));
564 
565   StringRef Soylent("soylent green is people");
566   EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
567   EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
568                                       /* allow replacements = */ false));
569   EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
570                                       /* allow replacements = */ true,
571                                       /* max edit distance = */ 8));
572   EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
573                                        "people soiled our green "
574                                        "people soiled our green "));
575 }
576 
577 TEST(StringRefTest, EditDistanceInsensitive) {
578   StringRef Hello("HELLO");
579   EXPECT_EQ(2U, Hello.edit_distance_insensitive("hill"));
580   EXPECT_EQ(0U, Hello.edit_distance_insensitive("hello"));
581 
582   StringRef Industry("InDuStRy");
583   EXPECT_EQ(6U, Industry.edit_distance_insensitive("iNtErEsT"));
584 }
585 
586 TEST(StringRefTest, Misc) {
587   std::string Storage;
588   raw_string_ostream OS(Storage);
589   OS << StringRef("hello");
590   EXPECT_EQ("hello", OS.str());
591 }
592 
593 TEST(StringRefTest, Hashing) {
594   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
595   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
596   std::string S = "hello world";
597   hash_code H = hash_value(S);
598   EXPECT_EQ(H, hash_value(StringRef("hello world")));
599   EXPECT_EQ(H, hash_value(StringRef(S)));
600   EXPECT_NE(H, hash_value(StringRef("hello worl")));
601   EXPECT_EQ(hash_value(std::string("hello worl")),
602             hash_value(StringRef("hello worl")));
603   EXPECT_NE(H, hash_value(StringRef("hello world ")));
604   EXPECT_EQ(hash_value(std::string("hello world ")),
605             hash_value(StringRef("hello world ")));
606   EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
607   EXPECT_NE(hash_value(std::string("ello worl")),
608             hash_value(StringRef("hello world").slice(1, -1)));
609 }
610 
611 struct UnsignedPair {
612   const char *Str;
613   uint64_t Expected;
614 } Unsigned[] =
615   { {"0", 0}
616   , {"255", 255}
617   , {"256", 256}
618   , {"65535", 65535}
619   , {"65536", 65536}
620   , {"4294967295", 4294967295ULL}
621   , {"4294967296", 4294967296ULL}
622   , {"18446744073709551615", 18446744073709551615ULL}
623   , {"042", 34}
624   , {"0x42", 66}
625   , {"0b101010", 42}
626   };
627 
628 struct SignedPair {
629   const char *Str;
630   int64_t Expected;
631 } Signed[] =
632   { {"0", 0}
633   , {"-0", 0}
634   , {"127", 127}
635   , {"128", 128}
636   , {"-128", -128}
637   , {"-129", -129}
638   , {"32767", 32767}
639   , {"32768", 32768}
640   , {"-32768", -32768}
641   , {"-32769", -32769}
642   , {"2147483647", 2147483647LL}
643   , {"2147483648", 2147483648LL}
644   , {"-2147483648", -2147483648LL}
645   , {"-2147483649", -2147483649LL}
646   , {"-9223372036854775808", -(9223372036854775807LL) - 1}
647   , {"042", 34}
648   , {"0x42", 66}
649   , {"0b101010", 42}
650   , {"-042", -34}
651   , {"-0x42", -66}
652   , {"-0b101010", -42}
653   };
654 
655 TEST(StringRefTest, getAsInteger) {
656   uint8_t U8;
657   uint16_t U16;
658   uint32_t U32;
659   uint64_t U64;
660 
661   for (size_t i = 0; i < std::size(Unsigned); ++i) {
662     bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
663     if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
664       ASSERT_FALSE(U8Success);
665       EXPECT_EQ(U8, Unsigned[i].Expected);
666     } else {
667       ASSERT_TRUE(U8Success);
668     }
669     bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
670     if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
671       ASSERT_FALSE(U16Success);
672       EXPECT_EQ(U16, Unsigned[i].Expected);
673     } else {
674       ASSERT_TRUE(U16Success);
675     }
676     bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
677     if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
678       ASSERT_FALSE(U32Success);
679       EXPECT_EQ(U32, Unsigned[i].Expected);
680     } else {
681       ASSERT_TRUE(U32Success);
682     }
683     bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
684     ASSERT_FALSE(U64Success);
685     EXPECT_EQ(U64, Unsigned[i].Expected);
686   }
687 
688   int8_t S8;
689   int16_t S16;
690   int32_t S32;
691   int64_t S64;
692 
693   for (size_t i = 0; i < std::size(Signed); ++i) {
694     bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
695     if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
696       ASSERT_FALSE(S8Success);
697       EXPECT_EQ(S8, Signed[i].Expected);
698     } else {
699       ASSERT_TRUE(S8Success);
700     }
701     bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
702     if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
703       ASSERT_FALSE(S16Success);
704       EXPECT_EQ(S16, Signed[i].Expected);
705     } else {
706       ASSERT_TRUE(S16Success);
707     }
708     bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
709     if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
710       ASSERT_FALSE(S32Success);
711       EXPECT_EQ(S32, Signed[i].Expected);
712     } else {
713       ASSERT_TRUE(S32Success);
714     }
715     bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
716     ASSERT_FALSE(S64Success);
717     EXPECT_EQ(S64, Signed[i].Expected);
718   }
719 }
720 
721 
722 static const char* BadStrings[] = {
723     ""                      // empty string
724   , "18446744073709551617"  // value just over max
725   , "123456789012345678901" // value way too large
726   , "4t23v"                 // illegal decimal characters
727   , "0x123W56"              // illegal hex characters
728   , "0b2"                   // illegal bin characters
729   , "08"                    // illegal oct characters
730   , "0o8"                   // illegal oct characters
731   , "-123"                  // negative unsigned value
732   , "0x"
733   , "0b"
734 };
735 
736 
737 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
738   unsigned long long U64;
739   for (size_t i = 0; i < std::size(BadStrings); ++i) {
740     bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
741     ASSERT_TRUE(IsBadNumber);
742   }
743 }
744 
745 struct ConsumeUnsignedPair {
746   const char *Str;
747   uint64_t Expected;
748   const char *Leftover;
749 } ConsumeUnsigned[] = {
750     {"0", 0, ""},
751     {"255", 255, ""},
752     {"256", 256, ""},
753     {"65535", 65535, ""},
754     {"65536", 65536, ""},
755     {"4294967295", 4294967295ULL, ""},
756     {"4294967296", 4294967296ULL, ""},
757     {"255A376", 255, "A376"},
758     {"18446744073709551615", 18446744073709551615ULL, ""},
759     {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
760     {"042", 34, ""},
761     {"0x42", 66, ""},
762     {"0x42-0x34", 66, "-0x34"},
763     {"0b101010", 42, ""},
764     {"0429F", 042, "9F"},            // Auto-sensed octal radix, invalid digit
765     {"0x42G12", 0x42, "G12"},        // Auto-sensed hex radix, invalid digit
766     {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
767 
768 struct ConsumeSignedPair {
769   const char *Str;
770   int64_t Expected;
771   const char *Leftover;
772 } ConsumeSigned[] = {
773     {"0", 0, ""},
774     {"-0", 0, ""},
775     {"0-1", 0, "-1"},
776     {"-0-1", 0, "-1"},
777     {"127", 127, ""},
778     {"128", 128, ""},
779     {"127-1", 127, "-1"},
780     {"128-1", 128, "-1"},
781     {"-128", -128, ""},
782     {"-129", -129, ""},
783     {"-128-1", -128, "-1"},
784     {"-129-1", -129, "-1"},
785     {"32767", 32767, ""},
786     {"32768", 32768, ""},
787     {"32767-1", 32767, "-1"},
788     {"32768-1", 32768, "-1"},
789     {"-32768", -32768, ""},
790     {"-32769", -32769, ""},
791     {"-32768-1", -32768, "-1"},
792     {"-32769-1", -32769, "-1"},
793     {"2147483647", 2147483647LL, ""},
794     {"2147483648", 2147483648LL, ""},
795     {"2147483647-1", 2147483647LL, "-1"},
796     {"2147483648-1", 2147483648LL, "-1"},
797     {"-2147483648", -2147483648LL, ""},
798     {"-2147483649", -2147483649LL, ""},
799     {"-2147483648-1", -2147483648LL, "-1"},
800     {"-2147483649-1", -2147483649LL, "-1"},
801     {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
802     {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
803     {"042", 34, ""},
804     {"042-1", 34, "-1"},
805     {"0x42", 66, ""},
806     {"0x42-1", 66, "-1"},
807     {"0b101010", 42, ""},
808     {"0b101010-1", 42, "-1"},
809     {"-042", -34, ""},
810     {"-042-1", -34, "-1"},
811     {"-0x42", -66, ""},
812     {"-0x42-1", -66, "-1"},
813     {"-0b101010", -42, ""},
814     {"-0b101010-1", -42, "-1"}};
815 
816 TEST(StringRefTest, consumeIntegerUnsigned) {
817   uint8_t U8;
818   uint16_t U16;
819   uint32_t U32;
820   uint64_t U64;
821 
822   for (size_t i = 0; i < std::size(ConsumeUnsigned); ++i) {
823     StringRef Str = ConsumeUnsigned[i].Str;
824     bool U8Success = Str.consumeInteger(0, U8);
825     if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
826         ConsumeUnsigned[i].Expected) {
827       ASSERT_FALSE(U8Success);
828       EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
829       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
830     } else {
831       ASSERT_TRUE(U8Success);
832     }
833 
834     Str = ConsumeUnsigned[i].Str;
835     bool U16Success = Str.consumeInteger(0, U16);
836     if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
837         ConsumeUnsigned[i].Expected) {
838       ASSERT_FALSE(U16Success);
839       EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
840       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
841     } else {
842       ASSERT_TRUE(U16Success);
843     }
844 
845     Str = ConsumeUnsigned[i].Str;
846     bool U32Success = Str.consumeInteger(0, U32);
847     if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
848         ConsumeUnsigned[i].Expected) {
849       ASSERT_FALSE(U32Success);
850       EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
851       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
852     } else {
853       ASSERT_TRUE(U32Success);
854     }
855 
856     Str = ConsumeUnsigned[i].Str;
857     bool U64Success = Str.consumeInteger(0, U64);
858     ASSERT_FALSE(U64Success);
859     EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
860     EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
861   }
862 }
863 
864 TEST(StringRefTest, consumeIntegerSigned) {
865   int8_t S8;
866   int16_t S16;
867   int32_t S32;
868   int64_t S64;
869 
870   for (size_t i = 0; i < std::size(ConsumeSigned); ++i) {
871     StringRef Str = ConsumeSigned[i].Str;
872     bool S8Success = Str.consumeInteger(0, S8);
873     if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
874         ConsumeSigned[i].Expected) {
875       ASSERT_FALSE(S8Success);
876       EXPECT_EQ(S8, ConsumeSigned[i].Expected);
877       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
878     } else {
879       ASSERT_TRUE(S8Success);
880     }
881 
882     Str = ConsumeSigned[i].Str;
883     bool S16Success = Str.consumeInteger(0, S16);
884     if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
885         ConsumeSigned[i].Expected) {
886       ASSERT_FALSE(S16Success);
887       EXPECT_EQ(S16, ConsumeSigned[i].Expected);
888       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
889     } else {
890       ASSERT_TRUE(S16Success);
891     }
892 
893     Str = ConsumeSigned[i].Str;
894     bool S32Success = Str.consumeInteger(0, S32);
895     if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
896         ConsumeSigned[i].Expected) {
897       ASSERT_FALSE(S32Success);
898       EXPECT_EQ(S32, ConsumeSigned[i].Expected);
899       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
900     } else {
901       ASSERT_TRUE(S32Success);
902     }
903 
904     Str = ConsumeSigned[i].Str;
905     bool S64Success = Str.consumeInteger(0, S64);
906     ASSERT_FALSE(S64Success);
907     EXPECT_EQ(S64, ConsumeSigned[i].Expected);
908     EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
909   }
910 }
911 
912 struct GetDoubleStrings {
913   const char *Str;
914   bool AllowInexact;
915   bool ShouldFail;
916   double D;
917 } DoubleStrings[] = {{"0", false, false, 0.0},
918                      {"0.0", false, false, 0.0},
919                      {"-0.0", false, false, -0.0},
920                      {"123.45", false, true, 123.45},
921                      {"123.45", true, false, 123.45},
922                      {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
923                      {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
924                      {"0x0.0000000000001P-1023", false, true, 0.0},
925                      {"0x0.0000000000001P-1023", true, false, 0.0},
926                     };
927 
928 TEST(StringRefTest, getAsDouble) {
929   for (const auto &Entry : DoubleStrings) {
930     double Result;
931     StringRef S(Entry.Str);
932     EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
933     if (!Entry.ShouldFail) {
934       EXPECT_EQ(Result, Entry.D);
935     }
936   }
937 }
938 
939 static const char *join_input[] = { "a", "b", "c" };
940 static const char join_result1[] = "a";
941 static const char join_result2[] = "a:b:c";
942 static const char join_result3[] = "a::b::c";
943 
944 TEST(StringRefTest, joinStrings) {
945   std::vector<StringRef> v1;
946   std::vector<std::string> v2;
947   for (size_t i = 0; i < std::size(join_input); ++i) {
948     v1.push_back(join_input[i]);
949     v2.push_back(join_input[i]);
950   }
951 
952   bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
953   EXPECT_TRUE(v1_join1);
954   bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
955   EXPECT_TRUE(v1_join2);
956   bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
957   EXPECT_TRUE(v1_join3);
958 
959   bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
960   EXPECT_TRUE(v2_join1);
961   bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
962   EXPECT_TRUE(v2_join2);
963   bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
964   EXPECT_TRUE(v2_join3);
965   v2_join3 = join(v2, "::") == join_result3;
966   EXPECT_TRUE(v2_join3);
967 }
968 
969 
970 TEST(StringRefTest, AllocatorCopy) {
971   BumpPtrAllocator Alloc;
972   // First test empty strings.  We don't want these to allocate anything on the
973   // allocator.
974   StringRef StrEmpty = "";
975   StringRef StrEmptyc = StrEmpty.copy(Alloc);
976   EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
977   EXPECT_EQ(StrEmptyc.data(), nullptr);
978   EXPECT_EQ(StrEmptyc.size(), 0u);
979   EXPECT_EQ(Alloc.getTotalMemory(), 0u);
980 
981   StringRef Str1 = "hello";
982   StringRef Str2 = "bye";
983   StringRef Str1c = Str1.copy(Alloc);
984   StringRef Str2c = Str2.copy(Alloc);
985   EXPECT_TRUE(Str1.equals(Str1c));
986   EXPECT_NE(Str1.data(), Str1c.data());
987   EXPECT_TRUE(Str2.equals(Str2c));
988   EXPECT_NE(Str2.data(), Str2c.data());
989 }
990 
991 TEST(StringRefTest, Drop) {
992   StringRef Test("StringRefTest::Drop");
993 
994   StringRef Dropped = Test.drop_front(5);
995   EXPECT_EQ(Dropped, "gRefTest::Drop");
996 
997   Dropped = Test.drop_back(5);
998   EXPECT_EQ(Dropped, "StringRefTest:");
999 
1000   Dropped = Test.drop_front(0);
1001   EXPECT_EQ(Dropped, Test);
1002 
1003   Dropped = Test.drop_back(0);
1004   EXPECT_EQ(Dropped, Test);
1005 
1006   Dropped = Test.drop_front(Test.size());
1007   EXPECT_TRUE(Dropped.empty());
1008 
1009   Dropped = Test.drop_back(Test.size());
1010   EXPECT_TRUE(Dropped.empty());
1011 }
1012 
1013 TEST(StringRefTest, Take) {
1014   StringRef Test("StringRefTest::Take");
1015 
1016   StringRef Taken = Test.take_front(5);
1017   EXPECT_EQ(Taken, "Strin");
1018 
1019   Taken = Test.take_back(5);
1020   EXPECT_EQ(Taken, ":Take");
1021 
1022   Taken = Test.take_front(Test.size());
1023   EXPECT_EQ(Taken, Test);
1024 
1025   Taken = Test.take_back(Test.size());
1026   EXPECT_EQ(Taken, Test);
1027 
1028   Taken = Test.take_front(0);
1029   EXPECT_TRUE(Taken.empty());
1030 
1031   Taken = Test.take_back(0);
1032   EXPECT_TRUE(Taken.empty());
1033 }
1034 
1035 TEST(StringRefTest, FindIf) {
1036   StringRef Punct("Test.String");
1037   StringRef NoPunct("ABCDEFG");
1038   StringRef Empty;
1039 
1040   auto IsPunct = [](char c) { return ::ispunct(c); };
1041   auto IsAlpha = [](char c) { return ::isalpha(c); };
1042   EXPECT_EQ(4U, Punct.find_if(IsPunct));
1043   EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
1044   EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
1045 
1046   EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
1047   EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1048   EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1049 }
1050 
1051 TEST(StringRefTest, TakeWhileUntil) {
1052   StringRef Test("String With 1 Number");
1053 
1054   StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1055   EXPECT_EQ("", Taken);
1056 
1057   Taken = Test.take_until([](char c) { return ::isdigit(c); });
1058   EXPECT_EQ("String With ", Taken);
1059 
1060   Taken = Test.take_while([](char c) { return true; });
1061   EXPECT_EQ(Test, Taken);
1062 
1063   Taken = Test.take_until([](char c) { return true; });
1064   EXPECT_EQ("", Taken);
1065 
1066   Test = "";
1067   Taken = Test.take_while([](char c) { return true; });
1068   EXPECT_EQ("", Taken);
1069 }
1070 
1071 TEST(StringRefTest, DropWhileUntil) {
1072   StringRef Test("String With 1 Number");
1073 
1074   StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1075   EXPECT_EQ(Test, Taken);
1076 
1077   Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1078   EXPECT_EQ("1 Number", Taken);
1079 
1080   Taken = Test.drop_while([](char c) { return true; });
1081   EXPECT_EQ("", Taken);
1082 
1083   Taken = Test.drop_until([](char c) { return true; });
1084   EXPECT_EQ(Test, Taken);
1085 
1086   StringRef EmptyString = "";
1087   Taken = EmptyString.drop_while([](char c) { return true; });
1088   EXPECT_EQ("", Taken);
1089 }
1090 
1091 TEST(StringRefTest, StringLiteral) {
1092   constexpr StringRef StringRefs[] = {"Foo", "Bar"};
1093   EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
1094   EXPECT_EQ(3u, (std::integral_constant<size_t, StringRefs[0].size()>::value));
1095   EXPECT_EQ(false, (std::integral_constant<bool, StringRefs[0].empty()>::value));
1096   EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
1097 
1098   constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1099   EXPECT_EQ(StringRef("Foo"), Strings[0]);
1100   EXPECT_EQ(3u, (std::integral_constant<size_t, Strings[0].size()>::value));
1101   EXPECT_EQ(false, (std::integral_constant<bool, Strings[0].empty()>::value));
1102   EXPECT_EQ(StringRef("Bar"), Strings[1]);
1103 }
1104 
1105 // Check gtest prints StringRef as a string instead of a container of chars.
1106 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
1107 TEST(StringRefTest, GTestPrinter) {
1108   EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
1109 }
1110 
1111 TEST(StringRefTest, LFLineEnding) {
1112   constexpr StringRef Cases[] = {"\nDoggo\nPupper", "Floofer\n", "Woofer"};
1113   EXPECT_EQ(StringRef("\n"), Cases[0].detectEOL());
1114   EXPECT_EQ(StringRef("\n"), Cases[1].detectEOL());
1115   EXPECT_EQ(StringRef("\n"), Cases[2].detectEOL());
1116 }
1117 
1118 TEST(StringRefTest, CRLineEnding) {
1119   constexpr StringRef Cases[] = {"\rDoggo\rPupper", "Floofer\r", "Woo\rfer\n"};
1120   EXPECT_EQ(StringRef("\r"), Cases[0].detectEOL());
1121   EXPECT_EQ(StringRef("\r"), Cases[1].detectEOL());
1122   EXPECT_EQ(StringRef("\r"), Cases[2].detectEOL());
1123 }
1124 
1125 TEST(StringRefTest, CRLFLineEnding) {
1126   constexpr StringRef Cases[] = {"\r\nDoggo\r\nPupper", "Floofer\r\n",
1127                                  "Woofer\r\nSubWoofer\n"};
1128   EXPECT_EQ(StringRef("\r\n"), Cases[0].detectEOL());
1129   EXPECT_EQ(StringRef("\r\n"), Cases[1].detectEOL());
1130   EXPECT_EQ(StringRef("\r\n"), Cases[2].detectEOL());
1131 }
1132 
1133 TEST(StringRefTest, LFCRLineEnding) {
1134   constexpr StringRef Cases[] = {"\n\rDoggo\n\rPupper", "Floofer\n\r",
1135                                  "Woofer\n\rSubWoofer\n"};
1136   EXPECT_EQ(StringRef("\n\r"), Cases[0].detectEOL());
1137   EXPECT_EQ(StringRef("\n\r"), Cases[1].detectEOL());
1138   EXPECT_EQ(StringRef("\n\r"), Cases[2].detectEOL());
1139 }
1140 
1141 static_assert(std::is_trivially_copyable_v<StringRef>, "trivially copyable");
1142 
1143 } // end anonymous namespace
1144