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 }
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_EQ(-1, StringRef("aab").compare("aad"));
85   EXPECT_EQ( 0, StringRef("aab").compare("aab"));
86   EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
87   EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
88   EXPECT_EQ( 1, StringRef("aab").compare("aa"));
89   EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
90 
91   EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
92   EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
93   EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
94   EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
95   EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
96   EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
97   EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
98   EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
99   EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\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.startswith(""));
354   EXPECT_TRUE(Str.startswith("he"));
355   EXPECT_FALSE(Str.startswith("helloworld"));
356   EXPECT_FALSE(Str.startswith("hi"));
357 }
358 
359 TEST(StringRefTest, StartsWithLower) {
360   StringRef Str("heLLo");
361   EXPECT_TRUE(Str.startswith_lower(""));
362   EXPECT_TRUE(Str.startswith_lower("he"));
363   EXPECT_TRUE(Str.startswith_lower("hell"));
364   EXPECT_TRUE(Str.startswith_lower("HELlo"));
365   EXPECT_FALSE(Str.startswith_lower("helloworld"));
366   EXPECT_FALSE(Str.startswith_lower("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, EndsWith) {
386   StringRef Str("hello");
387   EXPECT_TRUE(Str.endswith(""));
388   EXPECT_TRUE(Str.endswith("lo"));
389   EXPECT_FALSE(Str.endswith("helloworld"));
390   EXPECT_FALSE(Str.endswith("worldhello"));
391   EXPECT_FALSE(Str.endswith("so"));
392 }
393 
394 TEST(StringRefTest, EndsWithLower) {
395   StringRef Str("heLLo");
396   EXPECT_TRUE(Str.endswith_lower(""));
397   EXPECT_TRUE(Str.endswith_lower("lo"));
398   EXPECT_TRUE(Str.endswith_lower("LO"));
399   EXPECT_TRUE(Str.endswith_lower("ELlo"));
400   EXPECT_FALSE(Str.endswith_lower("helloworld"));
401   EXPECT_FALSE(Str.endswith_lower("hi"));
402 }
403 
404 TEST(StringRefTest, ConsumeBack) {
405   StringRef Str("hello");
406   EXPECT_TRUE(Str.consume_back(""));
407   EXPECT_EQ("hello", Str);
408   EXPECT_TRUE(Str.consume_back("lo"));
409   EXPECT_EQ("hel", Str);
410   EXPECT_FALSE(Str.consume_back("helhel"));
411   EXPECT_EQ("hel", Str);
412   EXPECT_FALSE(Str.consume_back("hle"));
413   EXPECT_EQ("hel", Str);
414   EXPECT_TRUE(Str.consume_back("hel"));
415   EXPECT_EQ("", Str);
416   EXPECT_FALSE(Str.consume_back("h"));
417   EXPECT_TRUE(Str.consume_back(""));
418 }
419 
420 TEST(StringRefTest, Find) {
421   StringRef Str("helloHELLO");
422   StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
423 
424   struct {
425     StringRef Str;
426     char C;
427     std::size_t From;
428     std::size_t Pos;
429     std::size_t LowerPos;
430   } CharExpectations[] = {
431       {Str, 'h', 0U, 0U, 0U},
432       {Str, 'e', 0U, 1U, 1U},
433       {Str, 'l', 0U, 2U, 2U},
434       {Str, 'l', 3U, 3U, 3U},
435       {Str, 'o', 0U, 4U, 4U},
436       {Str, 'L', 0U, 7U, 2U},
437       {Str, 'z', 0U, StringRef::npos, StringRef::npos},
438   };
439 
440   struct {
441     StringRef Str;
442     llvm::StringRef S;
443     std::size_t From;
444     std::size_t Pos;
445     std::size_t LowerPos;
446   } StrExpectations[] = {
447       {Str, "helloword", 0, StringRef::npos, StringRef::npos},
448       {Str, "hello", 0, 0U, 0U},
449       {Str, "ello", 0, 1U, 1U},
450       {Str, "zz", 0, StringRef::npos, StringRef::npos},
451       {Str, "ll", 2U, 2U, 2U},
452       {Str, "ll", 3U, StringRef::npos, 7U},
453       {Str, "LL", 2U, 7U, 2U},
454       {Str, "LL", 3U, 7U, 7U},
455       {Str, "", 0U, 0U, 0U},
456       {LongStr, "hello", 0U, 36U, 36U},
457       {LongStr, "foo", 0U, 28U, 28U},
458       {LongStr, "hell", 2U, 12U, 12U},
459       {LongStr, "HELL", 2U, 42U, 12U},
460       {LongStr, "", 0U, 0U, 0U}};
461 
462   for (auto &E : CharExpectations) {
463     EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
464     EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From));
465     EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From));
466   }
467 
468   for (auto &E : StrExpectations) {
469     EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
470     EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From));
471     EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From));
472   }
473 
474   EXPECT_EQ(3U, Str.rfind('l'));
475   EXPECT_EQ(StringRef::npos, Str.rfind('z'));
476   EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
477   EXPECT_EQ(0U, Str.rfind("hello"));
478   EXPECT_EQ(1U, Str.rfind("ello"));
479   EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
480 
481   EXPECT_EQ(8U, Str.rfind_lower('l'));
482   EXPECT_EQ(8U, Str.rfind_lower('L'));
483   EXPECT_EQ(StringRef::npos, Str.rfind_lower('z'));
484   EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD"));
485   EXPECT_EQ(5U, Str.rfind("HELLO"));
486   EXPECT_EQ(6U, Str.rfind("ELLO"));
487   EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
488 
489   EXPECT_EQ(2U, Str.find_first_of('l'));
490   EXPECT_EQ(1U, Str.find_first_of("el"));
491   EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
492 
493   Str = "hello";
494   EXPECT_EQ(1U, Str.find_first_not_of('h'));
495   EXPECT_EQ(4U, Str.find_first_not_of("hel"));
496   EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
497 
498   EXPECT_EQ(3U, Str.find_last_not_of('o'));
499   EXPECT_EQ(1U, Str.find_last_not_of("lo"));
500   EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
501 }
502 
503 TEST(StringRefTest, Count) {
504   StringRef Str("hello");
505   EXPECT_EQ(2U, Str.count('l'));
506   EXPECT_EQ(1U, Str.count('o'));
507   EXPECT_EQ(0U, Str.count('z'));
508   EXPECT_EQ(0U, Str.count("helloworld"));
509   EXPECT_EQ(1U, Str.count("hello"));
510   EXPECT_EQ(1U, Str.count("ello"));
511   EXPECT_EQ(0U, Str.count("zz"));
512 
513   StringRef OverlappingAbba("abbabba");
514   EXPECT_EQ(1U, OverlappingAbba.count("abba"));
515   StringRef NonOverlappingAbba("abbaabba");
516   EXPECT_EQ(2U, NonOverlappingAbba.count("abba"));
517   StringRef ComplexAbba("abbabbaxyzabbaxyz");
518   EXPECT_EQ(2U, ComplexAbba.count("abba"));
519 }
520 
521 TEST(StringRefTest, EditDistance) {
522   StringRef Hello("hello");
523   EXPECT_EQ(2U, Hello.edit_distance("hill"));
524 
525   StringRef Industry("industry");
526   EXPECT_EQ(6U, Industry.edit_distance("interest"));
527 
528   StringRef Soylent("soylent green is people");
529   EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
530   EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
531                                       /* allow replacements = */ false));
532   EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
533                                       /* allow replacements = */ true,
534                                       /* max edit distance = */ 8));
535   EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
536                                        "people soiled our green "
537                                        "people soiled our green "));
538 }
539 
540 TEST(StringRefTest, Misc) {
541   std::string Storage;
542   raw_string_ostream OS(Storage);
543   OS << StringRef("hello");
544   EXPECT_EQ("hello", OS.str());
545 }
546 
547 TEST(StringRefTest, Hashing) {
548   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
549   EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
550   std::string S = "hello world";
551   hash_code H = hash_value(S);
552   EXPECT_EQ(H, hash_value(StringRef("hello world")));
553   EXPECT_EQ(H, hash_value(StringRef(S)));
554   EXPECT_NE(H, hash_value(StringRef("hello worl")));
555   EXPECT_EQ(hash_value(std::string("hello worl")),
556             hash_value(StringRef("hello worl")));
557   EXPECT_NE(H, hash_value(StringRef("hello world ")));
558   EXPECT_EQ(hash_value(std::string("hello world ")),
559             hash_value(StringRef("hello world ")));
560   EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
561   EXPECT_NE(hash_value(std::string("ello worl")),
562             hash_value(StringRef("hello world").slice(1, -1)));
563 }
564 
565 struct UnsignedPair {
566   const char *Str;
567   uint64_t Expected;
568 } Unsigned[] =
569   { {"0", 0}
570   , {"255", 255}
571   , {"256", 256}
572   , {"65535", 65535}
573   , {"65536", 65536}
574   , {"4294967295", 4294967295ULL}
575   , {"4294967296", 4294967296ULL}
576   , {"18446744073709551615", 18446744073709551615ULL}
577   , {"042", 34}
578   , {"0x42", 66}
579   , {"0b101010", 42}
580   };
581 
582 struct SignedPair {
583   const char *Str;
584   int64_t Expected;
585 } Signed[] =
586   { {"0", 0}
587   , {"-0", 0}
588   , {"127", 127}
589   , {"128", 128}
590   , {"-128", -128}
591   , {"-129", -129}
592   , {"32767", 32767}
593   , {"32768", 32768}
594   , {"-32768", -32768}
595   , {"-32769", -32769}
596   , {"2147483647", 2147483647LL}
597   , {"2147483648", 2147483648LL}
598   , {"-2147483648", -2147483648LL}
599   , {"-2147483649", -2147483649LL}
600   , {"-9223372036854775808", -(9223372036854775807LL) - 1}
601   , {"042", 34}
602   , {"0x42", 66}
603   , {"0b101010", 42}
604   , {"-042", -34}
605   , {"-0x42", -66}
606   , {"-0b101010", -42}
607   };
608 
609 TEST(StringRefTest, getAsInteger) {
610   uint8_t U8;
611   uint16_t U16;
612   uint32_t U32;
613   uint64_t U64;
614 
615   for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
616     bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
617     if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
618       ASSERT_FALSE(U8Success);
619       EXPECT_EQ(U8, Unsigned[i].Expected);
620     } else {
621       ASSERT_TRUE(U8Success);
622     }
623     bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
624     if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
625       ASSERT_FALSE(U16Success);
626       EXPECT_EQ(U16, Unsigned[i].Expected);
627     } else {
628       ASSERT_TRUE(U16Success);
629     }
630     bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
631     if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
632       ASSERT_FALSE(U32Success);
633       EXPECT_EQ(U32, Unsigned[i].Expected);
634     } else {
635       ASSERT_TRUE(U32Success);
636     }
637     bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
638     if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
639       ASSERT_FALSE(U64Success);
640       EXPECT_EQ(U64, Unsigned[i].Expected);
641     } else {
642       ASSERT_TRUE(U64Success);
643     }
644   }
645 
646   int8_t S8;
647   int16_t S16;
648   int32_t S32;
649   int64_t S64;
650 
651   for (size_t i = 0; i < array_lengthof(Signed); ++i) {
652     bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
653     if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
654       ASSERT_FALSE(S8Success);
655       EXPECT_EQ(S8, Signed[i].Expected);
656     } else {
657       ASSERT_TRUE(S8Success);
658     }
659     bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
660     if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
661       ASSERT_FALSE(S16Success);
662       EXPECT_EQ(S16, Signed[i].Expected);
663     } else {
664       ASSERT_TRUE(S16Success);
665     }
666     bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
667     if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
668       ASSERT_FALSE(S32Success);
669       EXPECT_EQ(S32, Signed[i].Expected);
670     } else {
671       ASSERT_TRUE(S32Success);
672     }
673     bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
674     if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
675       ASSERT_FALSE(S64Success);
676       EXPECT_EQ(S64, Signed[i].Expected);
677     } else {
678       ASSERT_TRUE(S64Success);
679     }
680   }
681 }
682 
683 
684 static const char* BadStrings[] = {
685     ""                      // empty string
686   , "18446744073709551617"  // value just over max
687   , "123456789012345678901" // value way too large
688   , "4t23v"                 // illegal decimal characters
689   , "0x123W56"              // illegal hex characters
690   , "0b2"                   // illegal bin characters
691   , "08"                    // illegal oct characters
692   , "0o8"                   // illegal oct characters
693   , "-123"                  // negative unsigned value
694   , "0x"
695   , "0b"
696 };
697 
698 
699 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
700   unsigned long long U64;
701   for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
702     bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
703     ASSERT_TRUE(IsBadNumber);
704   }
705 }
706 
707 struct ConsumeUnsignedPair {
708   const char *Str;
709   uint64_t Expected;
710   const char *Leftover;
711 } ConsumeUnsigned[] = {
712     {"0", 0, ""},
713     {"255", 255, ""},
714     {"256", 256, ""},
715     {"65535", 65535, ""},
716     {"65536", 65536, ""},
717     {"4294967295", 4294967295ULL, ""},
718     {"4294967296", 4294967296ULL, ""},
719     {"255A376", 255, "A376"},
720     {"18446744073709551615", 18446744073709551615ULL, ""},
721     {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
722     {"042", 34, ""},
723     {"0x42", 66, ""},
724     {"0x42-0x34", 66, "-0x34"},
725     {"0b101010", 42, ""},
726     {"0429F", 042, "9F"},            // Auto-sensed octal radix, invalid digit
727     {"0x42G12", 0x42, "G12"},        // Auto-sensed hex radix, invalid digit
728     {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
729 
730 struct ConsumeSignedPair {
731   const char *Str;
732   int64_t Expected;
733   const char *Leftover;
734 } ConsumeSigned[] = {
735     {"0", 0, ""},
736     {"-0", 0, ""},
737     {"0-1", 0, "-1"},
738     {"-0-1", 0, "-1"},
739     {"127", 127, ""},
740     {"128", 128, ""},
741     {"127-1", 127, "-1"},
742     {"128-1", 128, "-1"},
743     {"-128", -128, ""},
744     {"-129", -129, ""},
745     {"-128-1", -128, "-1"},
746     {"-129-1", -129, "-1"},
747     {"32767", 32767, ""},
748     {"32768", 32768, ""},
749     {"32767-1", 32767, "-1"},
750     {"32768-1", 32768, "-1"},
751     {"-32768", -32768, ""},
752     {"-32769", -32769, ""},
753     {"-32768-1", -32768, "-1"},
754     {"-32769-1", -32769, "-1"},
755     {"2147483647", 2147483647LL, ""},
756     {"2147483648", 2147483648LL, ""},
757     {"2147483647-1", 2147483647LL, "-1"},
758     {"2147483648-1", 2147483648LL, "-1"},
759     {"-2147483648", -2147483648LL, ""},
760     {"-2147483649", -2147483649LL, ""},
761     {"-2147483648-1", -2147483648LL, "-1"},
762     {"-2147483649-1", -2147483649LL, "-1"},
763     {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
764     {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
765     {"042", 34, ""},
766     {"042-1", 34, "-1"},
767     {"0x42", 66, ""},
768     {"0x42-1", 66, "-1"},
769     {"0b101010", 42, ""},
770     {"0b101010-1", 42, "-1"},
771     {"-042", -34, ""},
772     {"-042-1", -34, "-1"},
773     {"-0x42", -66, ""},
774     {"-0x42-1", -66, "-1"},
775     {"-0b101010", -42, ""},
776     {"-0b101010-1", -42, "-1"}};
777 
778 TEST(StringRefTest, consumeIntegerUnsigned) {
779   uint8_t U8;
780   uint16_t U16;
781   uint32_t U32;
782   uint64_t U64;
783 
784   for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
785     StringRef Str = ConsumeUnsigned[i].Str;
786     bool U8Success = Str.consumeInteger(0, U8);
787     if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
788         ConsumeUnsigned[i].Expected) {
789       ASSERT_FALSE(U8Success);
790       EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
791       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
792     } else {
793       ASSERT_TRUE(U8Success);
794     }
795 
796     Str = ConsumeUnsigned[i].Str;
797     bool U16Success = Str.consumeInteger(0, U16);
798     if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
799         ConsumeUnsigned[i].Expected) {
800       ASSERT_FALSE(U16Success);
801       EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
802       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
803     } else {
804       ASSERT_TRUE(U16Success);
805     }
806 
807     Str = ConsumeUnsigned[i].Str;
808     bool U32Success = Str.consumeInteger(0, U32);
809     if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
810         ConsumeUnsigned[i].Expected) {
811       ASSERT_FALSE(U32Success);
812       EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
813       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
814     } else {
815       ASSERT_TRUE(U32Success);
816     }
817 
818     Str = ConsumeUnsigned[i].Str;
819     bool U64Success = Str.consumeInteger(0, U64);
820     if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
821         ConsumeUnsigned[i].Expected) {
822       ASSERT_FALSE(U64Success);
823       EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
824       EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
825     } else {
826       ASSERT_TRUE(U64Success);
827     }
828   }
829 }
830 
831 TEST(StringRefTest, consumeIntegerSigned) {
832   int8_t S8;
833   int16_t S16;
834   int32_t S32;
835   int64_t S64;
836 
837   for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
838     StringRef Str = ConsumeSigned[i].Str;
839     bool S8Success = Str.consumeInteger(0, S8);
840     if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
841         ConsumeSigned[i].Expected) {
842       ASSERT_FALSE(S8Success);
843       EXPECT_EQ(S8, ConsumeSigned[i].Expected);
844       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
845     } else {
846       ASSERT_TRUE(S8Success);
847     }
848 
849     Str = ConsumeSigned[i].Str;
850     bool S16Success = Str.consumeInteger(0, S16);
851     if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
852         ConsumeSigned[i].Expected) {
853       ASSERT_FALSE(S16Success);
854       EXPECT_EQ(S16, ConsumeSigned[i].Expected);
855       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
856     } else {
857       ASSERT_TRUE(S16Success);
858     }
859 
860     Str = ConsumeSigned[i].Str;
861     bool S32Success = Str.consumeInteger(0, S32);
862     if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
863         ConsumeSigned[i].Expected) {
864       ASSERT_FALSE(S32Success);
865       EXPECT_EQ(S32, ConsumeSigned[i].Expected);
866       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
867     } else {
868       ASSERT_TRUE(S32Success);
869     }
870 
871     Str = ConsumeSigned[i].Str;
872     bool S64Success = Str.consumeInteger(0, S64);
873     if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
874         ConsumeSigned[i].Expected) {
875       ASSERT_FALSE(S64Success);
876       EXPECT_EQ(S64, ConsumeSigned[i].Expected);
877       EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
878     } else {
879       ASSERT_TRUE(S64Success);
880     }
881   }
882 }
883 
884 struct GetDoubleStrings {
885   const char *Str;
886   bool AllowInexact;
887   bool ShouldFail;
888   double D;
889 } DoubleStrings[] = {{"0", false, false, 0.0},
890                      {"0.0", false, false, 0.0},
891                      {"-0.0", false, false, -0.0},
892                      {"123.45", false, true, 123.45},
893                      {"123.45", true, false, 123.45},
894                      {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
895                      {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
896                      {"0x0.0000000000001P-1023", false, true, 0.0},
897                      {"0x0.0000000000001P-1023", true, false, 0.0},
898                     };
899 
900 TEST(StringRefTest, getAsDouble) {
901   for (const auto &Entry : DoubleStrings) {
902     double Result;
903     StringRef S(Entry.Str);
904     EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
905     if (!Entry.ShouldFail) {
906       EXPECT_EQ(Result, Entry.D);
907     }
908   }
909 }
910 
911 static const char *join_input[] = { "a", "b", "c" };
912 static const char join_result1[] = "a";
913 static const char join_result2[] = "a:b:c";
914 static const char join_result3[] = "a::b::c";
915 
916 TEST(StringRefTest, joinStrings) {
917   std::vector<StringRef> v1;
918   std::vector<std::string> v2;
919   for (size_t i = 0; i < array_lengthof(join_input); ++i) {
920     v1.push_back(join_input[i]);
921     v2.push_back(join_input[i]);
922   }
923 
924   bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
925   EXPECT_TRUE(v1_join1);
926   bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
927   EXPECT_TRUE(v1_join2);
928   bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
929   EXPECT_TRUE(v1_join3);
930 
931   bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
932   EXPECT_TRUE(v2_join1);
933   bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
934   EXPECT_TRUE(v2_join2);
935   bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
936   EXPECT_TRUE(v2_join3);
937   v2_join3 = join(v2, "::") == join_result3;
938   EXPECT_TRUE(v2_join3);
939 }
940 
941 
942 TEST(StringRefTest, AllocatorCopy) {
943   BumpPtrAllocator Alloc;
944   // First test empty strings.  We don't want these to allocate anything on the
945   // allocator.
946   StringRef StrEmpty = "";
947   StringRef StrEmptyc = StrEmpty.copy(Alloc);
948   EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
949   EXPECT_EQ(StrEmptyc.data(), nullptr);
950   EXPECT_EQ(StrEmptyc.size(), 0u);
951   EXPECT_EQ(Alloc.getTotalMemory(), 0u);
952 
953   StringRef Str1 = "hello";
954   StringRef Str2 = "bye";
955   StringRef Str1c = Str1.copy(Alloc);
956   StringRef Str2c = Str2.copy(Alloc);
957   EXPECT_TRUE(Str1.equals(Str1c));
958   EXPECT_NE(Str1.data(), Str1c.data());
959   EXPECT_TRUE(Str2.equals(Str2c));
960   EXPECT_NE(Str2.data(), Str2c.data());
961 }
962 
963 TEST(StringRefTest, Drop) {
964   StringRef Test("StringRefTest::Drop");
965 
966   StringRef Dropped = Test.drop_front(5);
967   EXPECT_EQ(Dropped, "gRefTest::Drop");
968 
969   Dropped = Test.drop_back(5);
970   EXPECT_EQ(Dropped, "StringRefTest:");
971 
972   Dropped = Test.drop_front(0);
973   EXPECT_EQ(Dropped, Test);
974 
975   Dropped = Test.drop_back(0);
976   EXPECT_EQ(Dropped, Test);
977 
978   Dropped = Test.drop_front(Test.size());
979   EXPECT_TRUE(Dropped.empty());
980 
981   Dropped = Test.drop_back(Test.size());
982   EXPECT_TRUE(Dropped.empty());
983 }
984 
985 TEST(StringRefTest, Take) {
986   StringRef Test("StringRefTest::Take");
987 
988   StringRef Taken = Test.take_front(5);
989   EXPECT_EQ(Taken, "Strin");
990 
991   Taken = Test.take_back(5);
992   EXPECT_EQ(Taken, ":Take");
993 
994   Taken = Test.take_front(Test.size());
995   EXPECT_EQ(Taken, Test);
996 
997   Taken = Test.take_back(Test.size());
998   EXPECT_EQ(Taken, Test);
999 
1000   Taken = Test.take_front(0);
1001   EXPECT_TRUE(Taken.empty());
1002 
1003   Taken = Test.take_back(0);
1004   EXPECT_TRUE(Taken.empty());
1005 }
1006 
1007 TEST(StringRefTest, FindIf) {
1008   StringRef Punct("Test.String");
1009   StringRef NoPunct("ABCDEFG");
1010   StringRef Empty;
1011 
1012   auto IsPunct = [](char c) { return ::ispunct(c); };
1013   auto IsAlpha = [](char c) { return ::isalpha(c); };
1014   EXPECT_EQ(4U, Punct.find_if(IsPunct));
1015   EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
1016   EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
1017 
1018   EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
1019   EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
1020   EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
1021 }
1022 
1023 TEST(StringRefTest, TakeWhileUntil) {
1024   StringRef Test("String With 1 Number");
1025 
1026   StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
1027   EXPECT_EQ("", Taken);
1028 
1029   Taken = Test.take_until([](char c) { return ::isdigit(c); });
1030   EXPECT_EQ("String With ", Taken);
1031 
1032   Taken = Test.take_while([](char c) { return true; });
1033   EXPECT_EQ(Test, Taken);
1034 
1035   Taken = Test.take_until([](char c) { return true; });
1036   EXPECT_EQ("", Taken);
1037 
1038   Test = "";
1039   Taken = Test.take_while([](char c) { return true; });
1040   EXPECT_EQ("", Taken);
1041 }
1042 
1043 TEST(StringRefTest, DropWhileUntil) {
1044   StringRef Test("String With 1 Number");
1045 
1046   StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
1047   EXPECT_EQ(Test, Taken);
1048 
1049   Taken = Test.drop_until([](char c) { return ::isdigit(c); });
1050   EXPECT_EQ("1 Number", Taken);
1051 
1052   Taken = Test.drop_while([](char c) { return true; });
1053   EXPECT_EQ("", Taken);
1054 
1055   Taken = Test.drop_until([](char c) { return true; });
1056   EXPECT_EQ(Test, Taken);
1057 
1058   StringRef EmptyString = "";
1059   Taken = EmptyString.drop_while([](char c) { return true; });
1060   EXPECT_EQ("", Taken);
1061 }
1062 
1063 TEST(StringRefTest, StringLiteral) {
1064   constexpr StringRef StringRefs[] = {"Foo", "Bar"};
1065   EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
1066   EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
1067 
1068   constexpr StringLiteral Strings[] = {"Foo", "Bar"};
1069   EXPECT_EQ(StringRef("Foo"), Strings[0]);
1070   EXPECT_EQ(StringRef("Bar"), Strings[1]);
1071 }
1072 
1073 // Check gtest prints StringRef as a string instead of a container of chars.
1074 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
1075 TEST(StringRefTest, GTestPrinter) {
1076   EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));
1077 }
1078 
1079 static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable");
1080 
1081 } // end anonymous namespace
1082