[llvm] [NFC][ADT] Add reverse iterators and `value_type` to StringRef (PR #105579)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 19:59:25 PDT 2024


================
@@ -72,9 +72,18 @@ TEST(StringRefTest, EmptyInitializerList) {
 
 TEST(StringRefTest, Iteration) {
   StringRef S("hello");
-  const char *p = "hello";
-  for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p)
-    EXPECT_EQ(*it, *p);
+  constexpr StringLiteral CS("hello");
+
+  // Note: Cannot use literal strings in equal() as iteration over a literal
+  // string includes the null terminator.
+  constexpr std::string_view RefFwd("hello");
+  constexpr std::string_view RefRev("olleh");
+
+  EXPECT_TRUE(equal(S, RefFwd));
+  EXPECT_TRUE(equal(CS, RefFwd));
+  // reverse() builds an iterator range using StringRef::rbegin()/rend().
+  EXPECT_TRUE(equal(reverse(S), RefRev));
----------------
kuhar wrote:

Maybe use `make_range(S.rbegin(), S.rend())` to make sure that these exact member functions get called?

https://github.com/llvm/llvm-project/pull/105579


More information about the llvm-commits mailing list