[llvm-commits] [llvm] r77117 - in /llvm/trunk: include/llvm/ADT/StringRef.h unittests/ADT/StringRefTest.cpp
Daniel Dunbar
daniel at zuster.org
Sat Jul 25 20:18:19 PDT 2009
Author: ddunbar
Date: Sat Jul 25 22:18:15 2009
New Revision: 77117
URL: http://llvm.org/viewvc/llvm-project?rev=77117&view=rev
Log:
Add StringRef::{slice, split}, two convenient string operations which are simple
and efficient on a StringRef.
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/unittests/ADT/StringRefTest.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=77117&r1=77116&r2=77117&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Sat Jul 25 22:18:15 2009
@@ -10,6 +10,7 @@
#ifndef LLVM_ADT_STRINGREF_H
#define LLVM_ADT_STRINGREF_H
+#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>
@@ -120,11 +121,11 @@
/// @name Utility Functions
/// @{
- /// substr - Return a reference to a substring of this object.
+ /// substr - Return a reference to the substring from [Start, Start + N).
///
/// \param Start - The index of the starting character in the substring; if
- /// the index is greater than the length of the string then the empty
- /// substring will be returned.
+ /// the index is npos or greater than the length of the string then the
+ /// empty substring will be returned.
///
/// \param N - The number of characters to included in the substring. If N
/// exceeds the number of characters remaining in the string, the string
@@ -134,6 +135,40 @@
return StringRef(Data + Start, std::min(N, Length - Start));
}
+ /// slice - Return a reference to the substring from [Start, End).
+ ///
+ /// \param Start - The index of the starting character in the substring; if
+ /// the index is npos or greater than the length of the string then the
+ /// empty substring will be returned.
+ ///
+ /// \param End - The index following the last character to include in the
+ /// substring. If this is npos, or less than \arg Start, or exceeds the
+ /// number of characters remaining in the string, the string suffix
+ /// (starting with \arg Start) will be returned.
+ StringRef slice(size_t Start, size_t End) const {
+ Start = std::min(Start, Length);
+ End = std::min(std::max(Start, End), Length);
+ return StringRef(Data + Start, End - Start);
+ }
+
+ /// split - Split into two substrings around the first occurence of a
+ /// separator character.
+ ///
+ /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
+ /// such that (*this == LHS + Separator + RHS) is true and RHS is
+ /// maximal. If \arg Separator is not in the string, then the result is a
+ /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
+ ///
+ /// \param Separator - The character to split on.
+ /// \return - The split substrings.
+ std::pair<StringRef, StringRef> split(char Separator) const {
+ iterator it = std::find(begin(), end(), Separator);
+ if (it == end())
+ return std::make_pair(*this, StringRef());
+ return std::make_pair(StringRef(begin(), it - begin()),
+ StringRef(it + 1, end() - (it + 1)));
+ }
+
/// startswith - Check if this string starts with the given \arg Prefix.
bool startswith(const StringRef &Prefix) const {
return substr(0, Prefix.Length).equals(Prefix);
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=77117&r1=77116&r2=77117&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Sat Jul 25 22:18:15 2009
@@ -64,6 +64,21 @@
EXPECT_TRUE(Str.substr(0, 100) == "hello");
EXPECT_TRUE(Str.substr(4, 10) == "o");
+ EXPECT_TRUE(Str.slice(2, 3) == "l");
+ EXPECT_TRUE(Str.slice(1, 4) == "ell");
+ EXPECT_TRUE(Str.slice(2, 100) == "llo");
+ EXPECT_TRUE(Str.slice(2, 1) == "");
+ EXPECT_TRUE(Str.slice(10, 20) == "");
+
+ EXPECT_TRUE(Str.split('X') == std::make_pair(StringRef("hello"),
+ StringRef("")));
+ EXPECT_TRUE(Str.split('e') == std::make_pair(StringRef("h"),
+ StringRef("llo")));
+ EXPECT_TRUE(Str.split('h') == std::make_pair(StringRef(""),
+ StringRef("ello")));
+ EXPECT_TRUE(Str.split('o') == std::make_pair(StringRef("hell"),
+ StringRef("")));
+
EXPECT_TRUE(Str.startswith("he"));
EXPECT_FALSE(Str.startswith("helloworld"));
EXPECT_FALSE(Str.startswith("hi"));
More information about the llvm-commits
mailing list