StringRefTest.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/ADT/Hashing.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/Support/Allocator.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace llvm {
  18. std::ostream &operator<<(std::ostream &OS, const StringRef &S) {
  19. OS << S.str();
  20. return OS;
  21. }
  22. std::ostream &operator<<(std::ostream &OS,
  23. const std::pair<StringRef, StringRef> &P) {
  24. OS << "(" << P.first << ", " << P.second << ")";
  25. return OS;
  26. }
  27. }
  28. // Check that we can't accidentally assign a temporary std::string to a
  29. // StringRef. (Unfortunately we can't make use of the same thing with
  30. // constructors.)
  31. //
  32. // Disable this check under MSVC; even MSVC 2015 isn't consistent between
  33. // std::is_assignable and actually writing such an assignment.
  34. #if !defined(_MSC_VER)
  35. static_assert(
  36. !std::is_assignable<StringRef&, std::string>::value,
  37. "Assigning from prvalue std::string");
  38. static_assert(
  39. !std::is_assignable<StringRef&, std::string &&>::value,
  40. "Assigning from xvalue std::string");
  41. static_assert(
  42. std::is_assignable<StringRef&, std::string &>::value,
  43. "Assigning from lvalue std::string");
  44. static_assert(
  45. std::is_assignable<StringRef&, const char *>::value,
  46. "Assigning from prvalue C string");
  47. static_assert(
  48. std::is_assignable<StringRef&, const char * &&>::value,
  49. "Assigning from xvalue C string");
  50. static_assert(
  51. std::is_assignable<StringRef&, const char * &>::value,
  52. "Assigning from lvalue C string");
  53. #endif
  54. namespace {
  55. TEST(StringRefTest, Construction) {
  56. EXPECT_EQ("", StringRef());
  57. EXPECT_EQ("hello", StringRef("hello"));
  58. EXPECT_EQ("hello", StringRef("hello world", 5));
  59. EXPECT_EQ("hello", StringRef(std::string("hello")));
  60. }
  61. TEST(StringRefTest, EmptyInitializerList) {
  62. StringRef S = {};
  63. EXPECT_TRUE(S.empty());
  64. S = {};
  65. EXPECT_TRUE(S.empty());
  66. }
  67. TEST(StringRefTest, Iteration) {
  68. StringRef S("hello");
  69. const char *p = "hello";
  70. for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
  71. EXPECT_EQ(*it, *p);
  72. }
  73. TEST(StringRefTest, StringOps) {
  74. const char *p = "hello";
  75. EXPECT_EQ(p, StringRef(p, 0).data());
  76. EXPECT_TRUE(StringRef().empty());
  77. EXPECT_EQ((size_t) 5, StringRef("hello").size());
  78. EXPECT_EQ(-1, StringRef("aab").compare("aad"));
  79. EXPECT_EQ( 0, StringRef("aab").compare("aab"));
  80. EXPECT_EQ( 1, StringRef("aab").compare("aaa"));
  81. EXPECT_EQ(-1, StringRef("aab").compare("aabb"));
  82. EXPECT_EQ( 1, StringRef("aab").compare("aa"));
  83. EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
  84. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
  85. EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
  86. EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
  87. EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb"));
  88. EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb"));
  89. EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB"));
  90. EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB"));
  91. EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA"));
  92. EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1"));
  93. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad"));
  94. EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab"));
  95. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa"));
  96. EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb"));
  97. EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa"));
  98. EXPECT_EQ(-1, StringRef("1").compare_numeric("10"));
  99. EXPECT_EQ( 0, StringRef("10").compare_numeric("10"));
  100. EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a"));
  101. EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
  102. EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
  103. EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
  104. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
  105. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
  106. EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
  107. EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
  108. EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
  109. EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
  110. }
  111. TEST(StringRefTest, Operators) {
  112. EXPECT_EQ("", StringRef());
  113. EXPECT_TRUE(StringRef("aab") < StringRef("aad"));
  114. EXPECT_FALSE(StringRef("aab") < StringRef("aab"));
  115. EXPECT_TRUE(StringRef("aab") <= StringRef("aab"));
  116. EXPECT_FALSE(StringRef("aab") <= StringRef("aaa"));
  117. EXPECT_TRUE(StringRef("aad") > StringRef("aab"));
  118. EXPECT_FALSE(StringRef("aab") > StringRef("aab"));
  119. EXPECT_TRUE(StringRef("aab") >= StringRef("aab"));
  120. EXPECT_FALSE(StringRef("aaa") >= StringRef("aab"));
  121. EXPECT_EQ(StringRef("aab"), StringRef("aab"));
  122. EXPECT_FALSE(StringRef("aab") == StringRef("aac"));
  123. EXPECT_FALSE(StringRef("aab") != StringRef("aab"));
  124. EXPECT_TRUE(StringRef("aab") != StringRef("aac"));
  125. EXPECT_EQ('a', StringRef("aab")[1]);
  126. }
  127. TEST(StringRefTest, Substr) {
  128. StringRef Str("hello");
  129. EXPECT_EQ("lo", Str.substr(3));
  130. EXPECT_EQ("", Str.substr(100));
  131. EXPECT_EQ("hello", Str.substr(0, 100));
  132. EXPECT_EQ("o", Str.substr(4, 10));
  133. }
  134. TEST(StringRefTest, Slice) {
  135. StringRef Str("hello");
  136. EXPECT_EQ("l", Str.slice(2, 3));
  137. EXPECT_EQ("ell", Str.slice(1, 4));
  138. EXPECT_EQ("llo", Str.slice(2, 100));
  139. EXPECT_EQ("", Str.slice(2, 1));
  140. EXPECT_EQ("", Str.slice(10, 20));
  141. }
  142. TEST(StringRefTest, Split) {
  143. StringRef Str("hello");
  144. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  145. Str.split('X'));
  146. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  147. Str.split('e'));
  148. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  149. Str.split('h'));
  150. EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")),
  151. Str.split('l'));
  152. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  153. Str.split('o'));
  154. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  155. Str.rsplit('X'));
  156. EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
  157. Str.rsplit('e'));
  158. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  159. Str.rsplit('h'));
  160. EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
  161. Str.rsplit('l'));
  162. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  163. Str.rsplit('o'));
  164. EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")),
  165. Str.rsplit("ll"));
  166. EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")),
  167. Str.rsplit("h"));
  168. EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
  169. Str.rsplit("o"));
  170. EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
  171. Str.rsplit("::"));
  172. EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
  173. Str.rsplit("l"));
  174. }
  175. TEST(StringRefTest, Split2) {
  176. SmallVector<StringRef, 5> parts;
  177. SmallVector<StringRef, 5> expected;
  178. expected.push_back("ab"); expected.push_back("c");
  179. StringRef(",ab,,c,").split(parts, ",", -1, false);
  180. EXPECT_TRUE(parts == expected);
  181. expected.clear(); parts.clear();
  182. expected.push_back(""); expected.push_back("ab"); expected.push_back("");
  183. expected.push_back("c"); expected.push_back("");
  184. StringRef(",ab,,c,").split(parts, ",", -1, true);
  185. EXPECT_TRUE(parts == expected);
  186. expected.clear(); parts.clear();
  187. expected.push_back("");
  188. StringRef("").split(parts, ",", -1, true);
  189. EXPECT_TRUE(parts == expected);
  190. expected.clear(); parts.clear();
  191. StringRef("").split(parts, ",", -1, false);
  192. EXPECT_TRUE(parts == expected);
  193. expected.clear(); parts.clear();
  194. StringRef(",").split(parts, ",", -1, false);
  195. EXPECT_TRUE(parts == expected);
  196. expected.clear(); parts.clear();
  197. expected.push_back(""); expected.push_back("");
  198. StringRef(",").split(parts, ",", -1, true);
  199. EXPECT_TRUE(parts == expected);
  200. expected.clear(); parts.clear();
  201. expected.push_back("a"); expected.push_back("b");
  202. StringRef("a,b").split(parts, ",", -1, true);
  203. EXPECT_TRUE(parts == expected);
  204. // Test MaxSplit
  205. expected.clear(); parts.clear();
  206. expected.push_back("a,,b,c");
  207. StringRef("a,,b,c").split(parts, ",", 0, true);
  208. EXPECT_TRUE(parts == expected);
  209. expected.clear(); parts.clear();
  210. expected.push_back("a,,b,c");
  211. StringRef("a,,b,c").split(parts, ",", 0, false);
  212. EXPECT_TRUE(parts == expected);
  213. expected.clear(); parts.clear();
  214. expected.push_back("a"); expected.push_back(",b,c");
  215. StringRef("a,,b,c").split(parts, ",", 1, true);
  216. EXPECT_TRUE(parts == expected);
  217. expected.clear(); parts.clear();
  218. expected.push_back("a"); expected.push_back(",b,c");
  219. StringRef("a,,b,c").split(parts, ",", 1, false);
  220. EXPECT_TRUE(parts == expected);
  221. expected.clear(); parts.clear();
  222. expected.push_back("a"); expected.push_back(""); expected.push_back("b,c");
  223. StringRef("a,,b,c").split(parts, ",", 2, true);
  224. EXPECT_TRUE(parts == expected);
  225. expected.clear(); parts.clear();
  226. expected.push_back("a"); expected.push_back("b,c");
  227. StringRef("a,,b,c").split(parts, ",", 2, false);
  228. EXPECT_TRUE(parts == expected);
  229. expected.clear(); parts.clear();
  230. expected.push_back("a"); expected.push_back(""); expected.push_back("b");
  231. expected.push_back("c");
  232. StringRef("a,,b,c").split(parts, ",", 3, true);
  233. EXPECT_TRUE(parts == expected);
  234. expected.clear(); parts.clear();
  235. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  236. StringRef("a,,b,c").split(parts, ",", 3, false);
  237. EXPECT_TRUE(parts == expected);
  238. expected.clear(); parts.clear();
  239. expected.push_back("a"); expected.push_back("b"); expected.push_back("c");
  240. StringRef("a,,b,c").split(parts, ',', 3, false);
  241. EXPECT_TRUE(parts == expected);
  242. expected.clear(); parts.clear();
  243. expected.push_back("");
  244. StringRef().split(parts, ",", 0, true);
  245. EXPECT_TRUE(parts == expected);
  246. expected.clear(); parts.clear();
  247. expected.push_back(StringRef());
  248. StringRef("").split(parts, ",", 0, true);
  249. EXPECT_TRUE(parts == expected);
  250. expected.clear(); parts.clear();
  251. StringRef("").split(parts, ",", 0, false);
  252. EXPECT_TRUE(parts == expected);
  253. StringRef().split(parts, ",", 0, false);
  254. EXPECT_TRUE(parts == expected);
  255. expected.clear(); parts.clear();
  256. expected.push_back("a");
  257. expected.push_back("");
  258. expected.push_back("b");
  259. expected.push_back("c,d");
  260. StringRef("a,,b,c,d").split(parts, ",", 3, true);
  261. EXPECT_TRUE(parts == expected);
  262. expected.clear(); parts.clear();
  263. expected.push_back("");
  264. StringRef().split(parts, ',', 0, true);
  265. EXPECT_TRUE(parts == expected);
  266. expected.clear(); parts.clear();
  267. expected.push_back(StringRef());
  268. StringRef("").split(parts, ',', 0, true);
  269. EXPECT_TRUE(parts == expected);
  270. expected.clear(); parts.clear();
  271. StringRef("").split(parts, ',', 0, false);
  272. EXPECT_TRUE(parts == expected);
  273. StringRef().split(parts, ',', 0, false);
  274. EXPECT_TRUE(parts == expected);
  275. expected.clear(); parts.clear();
  276. expected.push_back("a");
  277. expected.push_back("");
  278. expected.push_back("b");
  279. expected.push_back("c,d");
  280. StringRef("a,,b,c,d").split(parts, ',', 3, true);
  281. EXPECT_TRUE(parts == expected);
  282. }
  283. TEST(StringRefTest, Trim) {
  284. StringRef Str0("hello");
  285. StringRef Str1(" hello ");
  286. StringRef Str2(" hello ");
  287. EXPECT_EQ(StringRef("hello"), Str0.rtrim());
  288. EXPECT_EQ(StringRef(" hello"), Str1.rtrim());
  289. EXPECT_EQ(StringRef(" hello"), Str2.rtrim());
  290. EXPECT_EQ(StringRef("hello"), Str0.ltrim());
  291. EXPECT_EQ(StringRef("hello "), Str1.ltrim());
  292. EXPECT_EQ(StringRef("hello "), Str2.ltrim());
  293. EXPECT_EQ(StringRef("hello"), Str0.trim());
  294. EXPECT_EQ(StringRef("hello"), Str1.trim());
  295. EXPECT_EQ(StringRef("hello"), Str2.trim());
  296. EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh"));
  297. EXPECT_EQ(StringRef(""), StringRef("").trim());
  298. EXPECT_EQ(StringRef(""), StringRef(" ").trim());
  299. EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim());
  300. EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim());
  301. EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0'));
  302. }
  303. TEST(StringRefTest, StartsWith) {
  304. StringRef Str("hello");
  305. EXPECT_TRUE(Str.startswith(""));
  306. EXPECT_TRUE(Str.startswith("he"));
  307. EXPECT_FALSE(Str.startswith("helloworld"));
  308. EXPECT_FALSE(Str.startswith("hi"));
  309. }
  310. TEST(StringRefTest, StartsWithLower) {
  311. StringRef Str("heLLo");
  312. EXPECT_TRUE(Str.startswith_lower(""));
  313. EXPECT_TRUE(Str.startswith_lower("he"));
  314. EXPECT_TRUE(Str.startswith_lower("hell"));
  315. EXPECT_TRUE(Str.startswith_lower("HELlo"));
  316. EXPECT_FALSE(Str.startswith_lower("helloworld"));
  317. EXPECT_FALSE(Str.startswith_lower("hi"));
  318. }
  319. TEST(StringRefTest, ConsumeFront) {
  320. StringRef Str("hello");
  321. EXPECT_TRUE(Str.consume_front(""));
  322. EXPECT_EQ("hello", Str);
  323. EXPECT_TRUE(Str.consume_front("he"));
  324. EXPECT_EQ("llo", Str);
  325. EXPECT_FALSE(Str.consume_front("lloworld"));
  326. EXPECT_EQ("llo", Str);
  327. EXPECT_FALSE(Str.consume_front("lol"));
  328. EXPECT_EQ("llo", Str);
  329. EXPECT_TRUE(Str.consume_front("llo"));
  330. EXPECT_EQ("", Str);
  331. EXPECT_FALSE(Str.consume_front("o"));
  332. EXPECT_TRUE(Str.consume_front(""));
  333. }
  334. TEST(StringRefTest, EndsWith) {
  335. StringRef Str("hello");
  336. EXPECT_TRUE(Str.endswith(""));
  337. EXPECT_TRUE(Str.endswith("lo"));
  338. EXPECT_FALSE(Str.endswith("helloworld"));
  339. EXPECT_FALSE(Str.endswith("worldhello"));
  340. EXPECT_FALSE(Str.endswith("so"));
  341. }
  342. TEST(StringRefTest, EndsWithLower) {
  343. StringRef Str("heLLo");
  344. EXPECT_TRUE(Str.endswith_lower(""));
  345. EXPECT_TRUE(Str.endswith_lower("lo"));
  346. EXPECT_TRUE(Str.endswith_lower("LO"));
  347. EXPECT_TRUE(Str.endswith_lower("ELlo"));
  348. EXPECT_FALSE(Str.endswith_lower("helloworld"));
  349. EXPECT_FALSE(Str.endswith_lower("hi"));
  350. }
  351. TEST(StringRefTest, ConsumeBack) {
  352. StringRef Str("hello");
  353. EXPECT_TRUE(Str.consume_back(""));
  354. EXPECT_EQ("hello", Str);
  355. EXPECT_TRUE(Str.consume_back("lo"));
  356. EXPECT_EQ("hel", Str);
  357. EXPECT_FALSE(Str.consume_back("helhel"));
  358. EXPECT_EQ("hel", Str);
  359. EXPECT_FALSE(Str.consume_back("hle"));
  360. EXPECT_EQ("hel", Str);
  361. EXPECT_TRUE(Str.consume_back("hel"));
  362. EXPECT_EQ("", Str);
  363. EXPECT_FALSE(Str.consume_back("h"));
  364. EXPECT_TRUE(Str.consume_back(""));
  365. }
  366. TEST(StringRefTest, Find) {
  367. StringRef Str("helloHELLO");
  368. StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
  369. struct {
  370. StringRef Str;
  371. char C;
  372. std::size_t From;
  373. std::size_t Pos;
  374. std::size_t LowerPos;
  375. } CharExpectations[] = {
  376. {Str, 'h', 0U, 0U, 0U},
  377. {Str, 'e', 0U, 1U, 1U},
  378. {Str, 'l', 0U, 2U, 2U},
  379. {Str, 'l', 3U, 3U, 3U},
  380. {Str, 'o', 0U, 4U, 4U},
  381. {Str, 'L', 0U, 7U, 2U},
  382. {Str, 'z', 0U, StringRef::npos, StringRef::npos},
  383. };
  384. struct {
  385. StringRef Str;
  386. llvm::StringRef S;
  387. std::size_t From;
  388. std::size_t Pos;
  389. std::size_t LowerPos;
  390. } StrExpectations[] = {
  391. {Str, "helloword", 0, StringRef::npos, StringRef::npos},
  392. {Str, "hello", 0, 0U, 0U},
  393. {Str, "ello", 0, 1U, 1U},
  394. {Str, "zz", 0, StringRef::npos, StringRef::npos},
  395. {Str, "ll", 2U, 2U, 2U},
  396. {Str, "ll", 3U, StringRef::npos, 7U},
  397. {Str, "LL", 2U, 7U, 2U},
  398. {Str, "LL", 3U, 7U, 7U},
  399. {Str, "", 0U, 0U, 0U},
  400. {LongStr, "hello", 0U, 36U, 36U},
  401. {LongStr, "foo", 0U, 28U, 28U},
  402. {LongStr, "hell", 2U, 12U, 12U},
  403. {LongStr, "HELL", 2U, 42U, 12U},
  404. {LongStr, "", 0U, 0U, 0U}};
  405. for (auto &E : CharExpectations) {
  406. EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From));
  407. EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From));
  408. EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From));
  409. }
  410. for (auto &E : StrExpectations) {
  411. EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From));
  412. EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From));
  413. EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From));
  414. }
  415. EXPECT_EQ(3U, Str.rfind('l'));
  416. EXPECT_EQ(StringRef::npos, Str.rfind('z'));
  417. EXPECT_EQ(StringRef::npos, Str.rfind("helloworld"));
  418. EXPECT_EQ(0U, Str.rfind("hello"));
  419. EXPECT_EQ(1U, Str.rfind("ello"));
  420. EXPECT_EQ(StringRef::npos, Str.rfind("zz"));
  421. EXPECT_EQ(8U, Str.rfind_lower('l'));
  422. EXPECT_EQ(8U, Str.rfind_lower('L'));
  423. EXPECT_EQ(StringRef::npos, Str.rfind_lower('z'));
  424. EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD"));
  425. EXPECT_EQ(5U, Str.rfind("HELLO"));
  426. EXPECT_EQ(6U, Str.rfind("ELLO"));
  427. EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));
  428. EXPECT_EQ(2U, Str.find_first_of('l'));
  429. EXPECT_EQ(1U, Str.find_first_of("el"));
  430. EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz"));
  431. Str = "hello";
  432. EXPECT_EQ(1U, Str.find_first_not_of('h'));
  433. EXPECT_EQ(4U, Str.find_first_not_of("hel"));
  434. EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello"));
  435. EXPECT_EQ(3U, Str.find_last_not_of('o'));
  436. EXPECT_EQ(1U, Str.find_last_not_of("lo"));
  437. EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo"));
  438. }
  439. TEST(StringRefTest, Count) {
  440. StringRef Str("hello");
  441. EXPECT_EQ(2U, Str.count('l'));
  442. EXPECT_EQ(1U, Str.count('o'));
  443. EXPECT_EQ(0U, Str.count('z'));
  444. EXPECT_EQ(0U, Str.count("helloworld"));
  445. EXPECT_EQ(1U, Str.count("hello"));
  446. EXPECT_EQ(1U, Str.count("ello"));
  447. EXPECT_EQ(0U, Str.count("zz"));
  448. }
  449. TEST(StringRefTest, EditDistance) {
  450. StringRef Hello("hello");
  451. EXPECT_EQ(2U, Hello.edit_distance("hill"));
  452. StringRef Industry("industry");
  453. EXPECT_EQ(6U, Industry.edit_distance("interest"));
  454. StringRef Soylent("soylent green is people");
  455. EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green"));
  456. EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green",
  457. /* allow replacements = */ false));
  458. EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green",
  459. /* allow replacements = */ true,
  460. /* max edit distance = */ 8));
  461. EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green "
  462. "people soiled our green "
  463. "people soiled our green "));
  464. }
  465. TEST(StringRefTest, Misc) {
  466. std::string Storage;
  467. raw_string_ostream OS(Storage);
  468. OS << StringRef("hello");
  469. EXPECT_EQ("hello", OS.str());
  470. }
  471. TEST(StringRefTest, Hashing) {
  472. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
  473. EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
  474. std::string S = "hello world";
  475. hash_code H = hash_value(S);
  476. EXPECT_EQ(H, hash_value(StringRef("hello world")));
  477. EXPECT_EQ(H, hash_value(StringRef(S)));
  478. EXPECT_NE(H, hash_value(StringRef("hello worl")));
  479. EXPECT_EQ(hash_value(std::string("hello worl")),
  480. hash_value(StringRef("hello worl")));
  481. EXPECT_NE(H, hash_value(StringRef("hello world ")));
  482. EXPECT_EQ(hash_value(std::string("hello world ")),
  483. hash_value(StringRef("hello world ")));
  484. EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
  485. EXPECT_NE(hash_value(std::string("ello worl")),
  486. hash_value(StringRef("hello world").slice(1, -1)));
  487. }
  488. struct UnsignedPair {
  489. const char *Str;
  490. uint64_t Expected;
  491. } Unsigned[] =
  492. { {"0", 0}
  493. , {"255", 255}
  494. , {"256", 256}
  495. , {"65535", 65535}
  496. , {"65536", 65536}
  497. , {"4294967295", 4294967295ULL}
  498. , {"4294967296", 4294967296ULL}
  499. , {"18446744073709551615", 18446744073709551615ULL}
  500. , {"042", 34}
  501. , {"0x42", 66}
  502. , {"0b101010", 42}
  503. };
  504. struct SignedPair {
  505. const char *Str;
  506. int64_t Expected;
  507. } Signed[] =
  508. { {"0", 0}
  509. , {"-0", 0}
  510. , {"127", 127}
  511. , {"128", 128}
  512. , {"-128", -128}
  513. , {"-129", -129}
  514. , {"32767", 32767}
  515. , {"32768", 32768}
  516. , {"-32768", -32768}
  517. , {"-32769", -32769}
  518. , {"2147483647", 2147483647LL}
  519. , {"2147483648", 2147483648LL}
  520. , {"-2147483648", -2147483648LL}
  521. , {"-2147483649", -2147483649LL}
  522. , {"-9223372036854775808", -(9223372036854775807LL) - 1}
  523. , {"042", 34}
  524. , {"0x42", 66}
  525. , {"0b101010", 42}
  526. , {"-042", -34}
  527. , {"-0x42", -66}
  528. , {"-0b101010", -42}
  529. };
  530. TEST(StringRefTest, getAsInteger) {
  531. uint8_t U8;
  532. uint16_t U16;
  533. uint32_t U32;
  534. uint64_t U64;
  535. for (size_t i = 0; i < array_lengthof(Unsigned); ++i) {
  536. bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);
  537. if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  538. ASSERT_FALSE(U8Success);
  539. EXPECT_EQ(U8, Unsigned[i].Expected);
  540. } else {
  541. ASSERT_TRUE(U8Success);
  542. }
  543. bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);
  544. if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  545. ASSERT_FALSE(U16Success);
  546. EXPECT_EQ(U16, Unsigned[i].Expected);
  547. } else {
  548. ASSERT_TRUE(U16Success);
  549. }
  550. bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);
  551. if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  552. ASSERT_FALSE(U32Success);
  553. EXPECT_EQ(U32, Unsigned[i].Expected);
  554. } else {
  555. ASSERT_TRUE(U32Success);
  556. }
  557. bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);
  558. if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {
  559. ASSERT_FALSE(U64Success);
  560. EXPECT_EQ(U64, Unsigned[i].Expected);
  561. } else {
  562. ASSERT_TRUE(U64Success);
  563. }
  564. }
  565. int8_t S8;
  566. int16_t S16;
  567. int32_t S32;
  568. int64_t S64;
  569. for (size_t i = 0; i < array_lengthof(Signed); ++i) {
  570. bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);
  571. if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {
  572. ASSERT_FALSE(S8Success);
  573. EXPECT_EQ(S8, Signed[i].Expected);
  574. } else {
  575. ASSERT_TRUE(S8Success);
  576. }
  577. bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);
  578. if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {
  579. ASSERT_FALSE(S16Success);
  580. EXPECT_EQ(S16, Signed[i].Expected);
  581. } else {
  582. ASSERT_TRUE(S16Success);
  583. }
  584. bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);
  585. if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {
  586. ASSERT_FALSE(S32Success);
  587. EXPECT_EQ(S32, Signed[i].Expected);
  588. } else {
  589. ASSERT_TRUE(S32Success);
  590. }
  591. bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);
  592. if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) {
  593. ASSERT_FALSE(S64Success);
  594. EXPECT_EQ(S64, Signed[i].Expected);
  595. } else {
  596. ASSERT_TRUE(S64Success);
  597. }
  598. }
  599. }
  600. static const char* BadStrings[] = {
  601. "" // empty string
  602. , "18446744073709551617" // value just over max
  603. , "123456789012345678901" // value way too large
  604. , "4t23v" // illegal decimal characters
  605. , "0x123W56" // illegal hex characters
  606. , "0b2" // illegal bin characters
  607. , "08" // illegal oct characters
  608. , "0o8" // illegal oct characters
  609. , "-123" // negative unsigned value
  610. , "0x"
  611. , "0b"
  612. };
  613. TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
  614. unsigned long long U64;
  615. for (size_t i = 0; i < array_lengthof(BadStrings); ++i) {
  616. bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);
  617. ASSERT_TRUE(IsBadNumber);
  618. }
  619. }
  620. struct ConsumeUnsignedPair {
  621. const char *Str;
  622. uint64_t Expected;
  623. const char *Leftover;
  624. } ConsumeUnsigned[] = {
  625. {"0", 0, ""},
  626. {"255", 255, ""},
  627. {"256", 256, ""},
  628. {"65535", 65535, ""},
  629. {"65536", 65536, ""},
  630. {"4294967295", 4294967295ULL, ""},
  631. {"4294967296", 4294967296ULL, ""},
  632. {"255A376", 255, "A376"},
  633. {"18446744073709551615", 18446744073709551615ULL, ""},
  634. {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},
  635. {"042", 34, ""},
  636. {"0x42", 66, ""},
  637. {"0x42-0x34", 66, "-0x34"},
  638. {"0b101010", 42, ""},
  639. {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit
  640. {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit
  641. {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.
  642. struct ConsumeSignedPair {
  643. const char *Str;
  644. int64_t Expected;
  645. const char *Leftover;
  646. } ConsumeSigned[] = {
  647. {"0", 0, ""},
  648. {"-0", 0, ""},
  649. {"0-1", 0, "-1"},
  650. {"-0-1", 0, "-1"},
  651. {"127", 127, ""},
  652. {"128", 128, ""},
  653. {"127-1", 127, "-1"},
  654. {"128-1", 128, "-1"},
  655. {"-128", -128, ""},
  656. {"-129", -129, ""},
  657. {"-128-1", -128, "-1"},
  658. {"-129-1", -129, "-1"},
  659. {"32767", 32767, ""},
  660. {"32768", 32768, ""},
  661. {"32767-1", 32767, "-1"},
  662. {"32768-1", 32768, "-1"},
  663. {"-32768", -32768, ""},
  664. {"-32769", -32769, ""},
  665. {"-32768-1", -32768, "-1"},
  666. {"-32769-1", -32769, "-1"},
  667. {"2147483647", 2147483647LL, ""},
  668. {"2147483648", 2147483648LL, ""},
  669. {"2147483647-1", 2147483647LL, "-1"},
  670. {"2147483648-1", 2147483648LL, "-1"},
  671. {"-2147483648", -2147483648LL, ""},
  672. {"-2147483649", -2147483649LL, ""},
  673. {"-2147483648-1", -2147483648LL, "-1"},
  674. {"-2147483649-1", -2147483649LL, "-1"},
  675. {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},
  676. {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},
  677. {"042", 34, ""},
  678. {"042-1", 34, "-1"},
  679. {"0x42", 66, ""},
  680. {"0x42-1", 66, "-1"},
  681. {"0b101010", 42, ""},
  682. {"0b101010-1", 42, "-1"},
  683. {"-042", -34, ""},
  684. {"-042-1", -34, "-1"},
  685. {"-0x42", -66, ""},
  686. {"-0x42-1", -66, "-1"},
  687. {"-0b101010", -42, ""},
  688. {"-0b101010-1", -42, "-1"}};
  689. TEST(StringRefTest, consumeIntegerUnsigned) {
  690. uint8_t U8;
  691. uint16_t U16;
  692. uint32_t U32;
  693. uint64_t U64;
  694. for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) {
  695. StringRef Str = ConsumeUnsigned[i].Str;
  696. bool U8Success = Str.consumeInteger(0, U8);
  697. if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==
  698. ConsumeUnsigned[i].Expected) {
  699. ASSERT_FALSE(U8Success);
  700. EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);
  701. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  702. } else {
  703. ASSERT_TRUE(U8Success);
  704. }
  705. Str = ConsumeUnsigned[i].Str;
  706. bool U16Success = Str.consumeInteger(0, U16);
  707. if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==
  708. ConsumeUnsigned[i].Expected) {
  709. ASSERT_FALSE(U16Success);
  710. EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);
  711. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  712. } else {
  713. ASSERT_TRUE(U16Success);
  714. }
  715. Str = ConsumeUnsigned[i].Str;
  716. bool U32Success = Str.consumeInteger(0, U32);
  717. if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==
  718. ConsumeUnsigned[i].Expected) {
  719. ASSERT_FALSE(U32Success);
  720. EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);
  721. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  722. } else {
  723. ASSERT_TRUE(U32Success);
  724. }
  725. Str = ConsumeUnsigned[i].Str;
  726. bool U64Success = Str.consumeInteger(0, U64);
  727. if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) ==
  728. ConsumeUnsigned[i].Expected) {
  729. ASSERT_FALSE(U64Success);
  730. EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);
  731. EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);
  732. } else {
  733. ASSERT_TRUE(U64Success);
  734. }
  735. }
  736. }
  737. TEST(StringRefTest, consumeIntegerSigned) {
  738. int8_t S8;
  739. int16_t S16;
  740. int32_t S32;
  741. int64_t S64;
  742. for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) {
  743. StringRef Str = ConsumeSigned[i].Str;
  744. bool S8Success = Str.consumeInteger(0, S8);
  745. if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==
  746. ConsumeSigned[i].Expected) {
  747. ASSERT_FALSE(S8Success);
  748. EXPECT_EQ(S8, ConsumeSigned[i].Expected);
  749. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  750. } else {
  751. ASSERT_TRUE(S8Success);
  752. }
  753. Str = ConsumeSigned[i].Str;
  754. bool S16Success = Str.consumeInteger(0, S16);
  755. if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==
  756. ConsumeSigned[i].Expected) {
  757. ASSERT_FALSE(S16Success);
  758. EXPECT_EQ(S16, ConsumeSigned[i].Expected);
  759. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  760. } else {
  761. ASSERT_TRUE(S16Success);
  762. }
  763. Str = ConsumeSigned[i].Str;
  764. bool S32Success = Str.consumeInteger(0, S32);
  765. if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==
  766. ConsumeSigned[i].Expected) {
  767. ASSERT_FALSE(S32Success);
  768. EXPECT_EQ(S32, ConsumeSigned[i].Expected);
  769. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  770. } else {
  771. ASSERT_TRUE(S32Success);
  772. }
  773. Str = ConsumeSigned[i].Str;
  774. bool S64Success = Str.consumeInteger(0, S64);
  775. if (static_cast<int64_t>(ConsumeSigned[i].Expected) ==
  776. ConsumeSigned[i].Expected) {
  777. ASSERT_FALSE(S64Success);
  778. EXPECT_EQ(S64, ConsumeSigned[i].Expected);
  779. EXPECT_EQ(Str, ConsumeSigned[i].Leftover);
  780. } else {
  781. ASSERT_TRUE(S64Success);
  782. }
  783. }
  784. }
  785. struct GetDoubleStrings {
  786. const char *Str;
  787. bool AllowInexact;
  788. bool ShouldFail;
  789. double D;
  790. } DoubleStrings[] = {{"0", false, false, 0.0},
  791. {"0.0", false, false, 0.0},
  792. {"-0.0", false, false, -0.0},
  793. {"123.45", false, true, 123.45},
  794. {"123.45", true, false, 123.45},
  795. {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
  796. {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
  797. {"0x0.0000000000001P-1023", false, true, 0.0},
  798. {"0x0.0000000000001P-1023", true, false, 0.0},
  799. };
  800. TEST(StringRefTest, getAsDouble) {
  801. for (const auto &Entry : DoubleStrings) {
  802. double Result;
  803. StringRef S(Entry.Str);
  804. EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));
  805. if (!Entry.ShouldFail) {
  806. EXPECT_EQ(Result, Entry.D);
  807. }
  808. }
  809. }
  810. static const char *join_input[] = { "a", "b", "c" };
  811. static const char join_result1[] = "a";
  812. static const char join_result2[] = "a:b:c";
  813. static const char join_result3[] = "a::b::c";
  814. TEST(StringRefTest, joinStrings) {
  815. std::vector<StringRef> v1;
  816. std::vector<std::string> v2;
  817. for (size_t i = 0; i < array_lengthof(join_input); ++i) {
  818. v1.push_back(join_input[i]);
  819. v2.push_back(join_input[i]);
  820. }
  821. bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;
  822. EXPECT_TRUE(v1_join1);
  823. bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;
  824. EXPECT_TRUE(v1_join2);
  825. bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;
  826. EXPECT_TRUE(v1_join3);
  827. bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;
  828. EXPECT_TRUE(v2_join1);
  829. bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;
  830. EXPECT_TRUE(v2_join2);
  831. bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;
  832. EXPECT_TRUE(v2_join3);
  833. v2_join3 = join(v2, "::") == join_result3;
  834. EXPECT_TRUE(v2_join3);
  835. }
  836. TEST(StringRefTest, AllocatorCopy) {
  837. BumpPtrAllocator Alloc;
  838. // First test empty strings. We don't want these to allocate anything on the
  839. // allocator.
  840. StringRef StrEmpty = "";
  841. StringRef StrEmptyc = StrEmpty.copy(Alloc);
  842. EXPECT_TRUE(StrEmpty.equals(StrEmptyc));
  843. EXPECT_EQ(StrEmptyc.data(), nullptr);
  844. EXPECT_EQ(StrEmptyc.size(), 0u);
  845. EXPECT_EQ(Alloc.getTotalMemory(), 0u);
  846. StringRef Str1 = "hello";
  847. StringRef Str2 = "bye";
  848. StringRef Str1c = Str1.copy(Alloc);
  849. StringRef Str2c = Str2.copy(Alloc);
  850. EXPECT_TRUE(Str1.equals(Str1c));
  851. EXPECT_NE(Str1.data(), Str1c.data());
  852. EXPECT_TRUE(Str2.equals(Str2c));
  853. EXPECT_NE(Str2.data(), Str2c.data());
  854. }
  855. TEST(StringRefTest, Drop) {
  856. StringRef Test("StringRefTest::Drop");
  857. StringRef Dropped = Test.drop_front(5);
  858. EXPECT_EQ(Dropped, "gRefTest::Drop");
  859. Dropped = Test.drop_back(5);
  860. EXPECT_EQ(Dropped, "StringRefTest:");
  861. Dropped = Test.drop_front(0);
  862. EXPECT_EQ(Dropped, Test);
  863. Dropped = Test.drop_back(0);
  864. EXPECT_EQ(Dropped, Test);
  865. Dropped = Test.drop_front(Test.size());
  866. EXPECT_TRUE(Dropped.empty());
  867. Dropped = Test.drop_back(Test.size());
  868. EXPECT_TRUE(Dropped.empty());
  869. }
  870. TEST(StringRefTest, Take) {
  871. StringRef Test("StringRefTest::Take");
  872. StringRef Taken = Test.take_front(5);
  873. EXPECT_EQ(Taken, "Strin");
  874. Taken = Test.take_back(5);
  875. EXPECT_EQ(Taken, ":Take");
  876. Taken = Test.take_front(Test.size());
  877. EXPECT_EQ(Taken, Test);
  878. Taken = Test.take_back(Test.size());
  879. EXPECT_EQ(Taken, Test);
  880. Taken = Test.take_front(0);
  881. EXPECT_TRUE(Taken.empty());
  882. Taken = Test.take_back(0);
  883. EXPECT_TRUE(Taken.empty());
  884. }
  885. TEST(StringRefTest, FindIf) {
  886. StringRef Punct("Test.String");
  887. StringRef NoPunct("ABCDEFG");
  888. StringRef Empty;
  889. auto IsPunct = [](char c) { return ::ispunct(c); };
  890. auto IsAlpha = [](char c) { return ::isalpha(c); };
  891. EXPECT_EQ(4U, Punct.find_if(IsPunct));
  892. EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));
  893. EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));
  894. EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));
  895. EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));
  896. EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));
  897. }
  898. TEST(StringRefTest, TakeWhileUntil) {
  899. StringRef Test("String With 1 Number");
  900. StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });
  901. EXPECT_EQ("", Taken);
  902. Taken = Test.take_until([](char c) { return ::isdigit(c); });
  903. EXPECT_EQ("String With ", Taken);
  904. Taken = Test.take_while([](char c) { return true; });
  905. EXPECT_EQ(Test, Taken);
  906. Taken = Test.take_until([](char c) { return true; });
  907. EXPECT_EQ("", Taken);
  908. Test = "";
  909. Taken = Test.take_while([](char c) { return true; });
  910. EXPECT_EQ("", Taken);
  911. }
  912. TEST(StringRefTest, DropWhileUntil) {
  913. StringRef Test("String With 1 Number");
  914. StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });
  915. EXPECT_EQ(Test, Taken);
  916. Taken = Test.drop_until([](char c) { return ::isdigit(c); });
  917. EXPECT_EQ("1 Number", Taken);
  918. Taken = Test.drop_while([](char c) { return true; });
  919. EXPECT_EQ("", Taken);
  920. Taken = Test.drop_until([](char c) { return true; });
  921. EXPECT_EQ(Test, Taken);
  922. StringRef EmptyString = "";
  923. Taken = EmptyString.drop_while([](char c) { return true; });
  924. EXPECT_EQ("", Taken);
  925. }
  926. TEST(StringRefTest, StringLiteral) {
  927. constexpr StringLiteral Strings[] = {"Foo", "Bar"};
  928. EXPECT_EQ(StringRef("Foo"), Strings[0]);
  929. EXPECT_EQ(StringRef("Bar"), Strings[1]);
  930. }
  931. static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable");
  932. } // end anonymous namespace