[llvm] d5b9417 - [LLVM][ADT] Add some more `starts_with`, `ends_with`, and `contains` overloads to `SmallString` (#182692)

via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 21 18:49:07 PST 2026


Author: Victor Chernyakin
Date: 2026-02-21T18:49:03-08:00
New Revision: d5b94174971f4ce5d8659e4ade27e21858221514

URL: https://github.com/llvm/llvm-project/commit/d5b94174971f4ce5d8659e4ade27e21858221514
DIFF: https://github.com/llvm/llvm-project/commit/d5b94174971f4ce5d8659e4ade27e21858221514.diff

LOG: [LLVM][ADT] Add some more `starts_with`, `ends_with`, and `contains` overloads to `SmallString` (#182692)

This makes `SmallString` consistent with `std::string`,
`std::string_view`, and `StringRef`.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallString.h
    llvm/unittests/ADT/SmallStringTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallString.h b/llvm/include/llvm/ADT/SmallString.h
index be3193c6ef9be..a9344d572f48c 100644
--- a/llvm/include/llvm/ADT/SmallString.h
+++ b/llvm/include/llvm/ADT/SmallString.h
@@ -121,11 +121,25 @@ class SmallString : public SmallVector<char, InternalLen> {
     return str().starts_with(Prefix);
   }
 
+  /// starts_with - Check if this string starts with the given character \p C.
+  [[nodiscard]] bool starts_with(char C) const { return str().starts_with(C); }
+
   /// ends_with - Check if this string ends with the given \p Suffix.
   [[nodiscard]] bool ends_with(StringRef Suffix) const {
     return str().ends_with(Suffix);
   }
 
+  /// ends_with - Check if this string ends with the given character \p C.
+  [[nodiscard]] bool ends_with(char C) const { return str().ends_with(C); }
+
+  /// contains - Check if \p Other is a substring of this string.
+  [[nodiscard]] bool contains(StringRef Other) const {
+    return str().contains(Other);
+  }
+
+  /// contains - Check if this string contains the character \p C.
+  [[nodiscard]] bool contains(char C) const { return str().contains(C); }
+
   /// @}
   /// @name String Searching
   /// @{

diff  --git a/llvm/unittests/ADT/SmallStringTest.cpp b/llvm/unittests/ADT/SmallStringTest.cpp
index db858246c9bbf..57e829ad7fa99 100644
--- a/llvm/unittests/ADT/SmallStringTest.cpp
+++ b/llvm/unittests/ADT/SmallStringTest.cpp
@@ -115,6 +115,37 @@ TEST_F(SmallStringTest, AppendStringRefs) {
   EXPECT_STREQ("abcdefghijklmnopqrstu", theString.c_str());
 }
 
+TEST_F(SmallStringTest, StartsWith) {
+  SmallString<32> Str("hello");
+  EXPECT_TRUE(Str.starts_with(""));
+  EXPECT_TRUE(Str.starts_with("he"));
+  EXPECT_FALSE(Str.starts_with("helloworld"));
+  EXPECT_FALSE(Str.starts_with("hi"));
+  EXPECT_TRUE(Str.starts_with('h'));
+  EXPECT_FALSE(Str.starts_with('i'));
+}
+
+TEST_F(SmallStringTest, EndsWith) {
+  SmallString<32> Str("hello");
+  EXPECT_TRUE(Str.ends_with(""));
+  EXPECT_TRUE(Str.ends_with("lo"));
+  EXPECT_FALSE(Str.ends_with("helloworld"));
+  EXPECT_FALSE(Str.ends_with("worldhello"));
+  EXPECT_FALSE(Str.ends_with("so"));
+  EXPECT_TRUE(Str.ends_with('o'));
+  EXPECT_FALSE(Str.ends_with('p'));
+}
+
+TEST_F(SmallStringTest, Contains) {
+  SmallString<32> Str("hello");
+  EXPECT_TRUE(Str.contains(""));
+  EXPECT_TRUE(Str.contains("ell"));
+  EXPECT_TRUE(Str.contains("hello"));
+  EXPECT_FALSE(Str.contains("world"));
+  EXPECT_TRUE(Str.contains('e'));
+  EXPECT_FALSE(Str.contains('w'));
+}
+
 TEST_F(SmallStringTest, StringRefConversion) {
   StringRef abc = "abc";
   theString.assign(abc.begin(), abc.end());


        


More information about the llvm-commits mailing list