[PATCH] D47973: [ADT] Change the behavior of `StringRef::rsplit()` when the separator is not found.

Henry Wong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 8 22:07:20 PDT 2018


MTC created this revision.
MTC added reviewers: zturner, vsk, xbolva00.

For `StringRef::split()`, it performs the search from the front to the back to find the first occurence of the separator, so when the separator is not found, returning <*this, ""> makes sense. For `SrtingRef::rsplit()`, it performs the search from the back to the front to find the "first" occurence of the separator too, so when the separator is not found, returning <"", *this> makes more sense.


Repository:
  rL LLVM

https://reviews.llvm.org/D47973

Files:
  include/llvm/ADT/StringRef.h
  unittests/ADT/StringRefTest.cpp


Index: unittests/ADT/StringRefTest.cpp
===================================================================
--- unittests/ADT/StringRefTest.cpp
+++ unittests/ADT/StringRefTest.cpp
@@ -171,7 +171,7 @@
   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
             Str.split('o'));
 
-  EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
+  EXPECT_EQ(std::make_pair(StringRef(""), StringRef("hello")),
             Str.rsplit('X'));
   EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")),
             Str.rsplit('e'));
@@ -188,7 +188,7 @@
 		    Str.rsplit("h"));
   EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")),
 	      Str.rsplit("o"));
-  EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")),
+  EXPECT_EQ(std::make_pair(StringRef(""), StringRef("hello")),
 		    Str.rsplit("::"));
   EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")),
 		    Str.rsplit("l"));
Index: include/llvm/ADT/StringRef.h
===================================================================
--- include/llvm/ADT/StringRef.h
+++ include/llvm/ADT/StringRef.h
@@ -750,17 +750,17 @@
     /// string.
     ///
     /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
-    /// such that (*this == LHS + Separator + RHS) is true and RHS is
-    /// minimal. If \p Separator is not in the string, then the result is a
-    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
+    /// such that (*this == LHS + Separator + RHS) is true and RHS is minimal.
+    /// If \p Separator is not in the string, then the result is a pair (LHS,
+    /// RHS) where (LHS == "") and (*this == RHS).
     ///
     /// \param Separator - The string to split on.
     /// \return - The split substrings.
     LLVM_NODISCARD
     std::pair<StringRef, StringRef> rsplit(StringRef Separator) const {
       size_t Idx = rfind(Separator);
       if (Idx == npos)
-        return std::make_pair(*this, StringRef());
+        return std::make_pair(StringRef(), *this);
       return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
     }
 
@@ -803,9 +803,9 @@
     /// character.
     ///
     /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
-    /// such that (*this == LHS + Separator + RHS) is true and RHS is
-    /// minimal. If \p Separator is not in the string, then the result is a
-    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
+    /// such that (*this == LHS + Separator + RHS) is true and RHS is minimal.
+    /// If \p Separator is not in the string, then the result is a pair (LHS,
+    /// RHS) where (LHS == "") and (*this == RHS).
     ///
     /// \param Separator - The character to split on.
     /// \return - The split substrings.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47973.150606.patch
Type: text/x-patch
Size: 2751 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180609/fa91bc58/attachment.bin>


More information about the llvm-commits mailing list