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