10 #ifndef LLVM_ADT_STRINGREF_H
11 #define LLVM_ADT_STRINGREF_H
27 bool getAsUnsignedInteger(
StringRef Str,
unsigned Radix,
28 unsigned long long &Result);
30 bool getAsSignedInteger(
StringRef Str,
unsigned Radix,
long long &Result);
41 typedef const char *iterator;
42 typedef const char *const_iterator;
43 static const size_t npos = ~size_t(0);
44 typedef size_t size_type;
55 static int compareMemory(
const char *Lhs,
const char *Rhs,
size_t Length) {
56 if (Length == 0) {
return 0; }
57 return ::memcmp(Lhs,Rhs,Length);
70 assert(Str &&
"StringRef cannot be built from a NULL argument");
71 Length = ::strlen(Str);
76 : Data(data), Length(length) {
77 assert((data || length == 0) &&
78 "StringRef cannot be built from a NULL argument with non-null length");
83 : Data(Str.
data()), Length(Str.length()) {}
89 iterator begin()
const {
return Data; }
91 iterator end()
const {
return Data + Length; }
93 const unsigned char *bytes_begin()
const {
94 return reinterpret_cast<const unsigned char *
>(begin());
96 const unsigned char *bytes_end()
const {
97 return reinterpret_cast<const unsigned char *
>(end());
106 const char *
data()
const {
return Data; }
109 bool empty()
const {
return Length == 0; }
112 size_t size()
const {
return Length; }
123 return Data[Length-1];
127 template <
typename Allocator>
StringRef copy(Allocator &A)
const {
128 char *S = A.template Allocate<char>(Length);
129 std::copy(begin(), end(), S);
136 return (Length == RHS.Length &&
137 compareMemory(Data, RHS.Data, RHS.Length) == 0);
149 if (
int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
150 return Res < 0 ? -1 : 1;
153 if (Length == RHS.Length)
155 return Length < RHS.Length ? -1 : 1;
167 if (!Data)
return std::string();
168 return std::string(Data, Length);
175 char operator[](
size_t Index)
const {
176 assert(Index < Length &&
"Invalid index!");
184 operator std::string()
const {
194 return Length >= Prefix.Length &&
195 compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
203 return Length >= Suffix.Length &&
204 compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
218 size_t find(
char C,
size_t From = 0)
const {
219 size_t FindBegin = std::min(From, Length);
220 if (FindBegin < Length) {
222 if (
const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
223 return static_cast<const char *
>(P) - Data;
238 size_t rfind(
char C,
size_t From = npos)
const {
239 From = std::min(From, Length);
258 return find(C, From);
280 return rfind(C, From);
306 for (
size_t i = 0, e = Length; i != e; ++i)
323 template <
typename T>
324 typename std::enable_if<std::numeric_limits<T>::is_signed,
bool>::type
327 if (getAsSignedInteger(*
this, Radix, LLVal) ||
328 static_cast<T>(LLVal) != LLVal)
334 template <
typename T>
335 typename std::enable_if<!std::numeric_limits<T>::is_signed,
bool>::type
337 unsigned long long ULLVal;
341 if (getAsUnsignedInteger(*
this, Radix, ULLVal) ||
342 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
353 std::string lower()
const;
356 std::string
upper()
const;
372 Start = std::min(Start, Length);
373 return StringRef(Data + Start, std::min(N, Length - Start));
379 assert(
size() >= N &&
"Dropping more elements than exist");
386 assert(
size() >= N &&
"Dropping more elements than exist");
401 Start = std::min(Start, Length);
402 End = std::min(std::max(Start, End), Length);
403 return StringRef(Data + Start, End - Start);
416 std::pair<StringRef, StringRef>
split(
char Separator)
const {
417 size_t Idx =
find(Separator);
419 return std::make_pair(*
this,
StringRef());
420 return std::make_pair(
slice(0, Idx),
slice(Idx+1, npos));
434 size_t Idx =
find(Separator);
436 return std::make_pair(*
this,
StringRef());
437 return std::make_pair(
slice(0, Idx),
slice(Idx + Separator.
size(), npos));
456 bool KeepEmpty =
true)
const;
468 std::pair<StringRef, StringRef>
rsplit(
char Separator)
const {
469 size_t Idx =
rfind(Separator);
471 return std::make_pair(*
this,
StringRef());
472 return std::make_pair(
slice(0, Idx),
slice(Idx+1, npos));
499 inline bool operator==(StringRef LHS, StringRef RHS) {
500 return LHS.equals(RHS);
503 inline bool operator!=(StringRef LHS, StringRef RHS) {
504 return !(LHS == RHS);
507 inline bool operator<(StringRef LHS, StringRef RHS) {
508 return LHS.compare(RHS) == -1;
511 inline bool operator<=(StringRef LHS, StringRef RHS) {
512 return LHS.compare(RHS) != 1;
515 inline bool operator>(StringRef LHS, StringRef RHS) {
516 return LHS.compare(RHS) == 1;
519 inline bool operator>=(StringRef LHS, StringRef RHS) {
520 return LHS.compare(RHS) != -1;
523 inline std::string &operator+=(std::string &buffer, StringRef
string) {
524 return buffer.append(
string.data(),
string.size());
527 inline std::ostream &operator<<(std::ostream &os, StringRef
string) {
528 os.write(
string.data(),
string.size());
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:325
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:51
size_t size() const
size - Get the string size.
Definition: StringRef.h:112
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:218
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:202
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:416
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:238
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:371
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:166
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:385
std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
Definition: StringRef.h:468
bool endswith_lower(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:66
int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:147
StringRef rtrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the right removed.
Definition: StringRef.h:483
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: StringRef.h:23
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:106
StringRef()
Construct an empty string ref.
Definition: StringRef.h:65
size_t find_last_not_of(char C, size_t From=npos) const
Find the last character in the string that is not C, or npos if not found.
Definition: StringRef.cpp:242
StringRef trim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left and right removed...
Definition: StringRef.h:489
char back() const
back - Get the last character in the string.
Definition: StringRef.h:121
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:304
StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
Definition: StringRef.h:75
std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
Definition: StringRef.h:433
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:201
StringRef(const std::string &Str)
Construct a string ref from an std::string.
Definition: StringRef.h:82
size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:279
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: StringRef.h:535
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:193
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:378
std::string upper() const
Convert the given ASCII string to uppercase.
Definition: StringRef.cpp:114
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:135
bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:141
StringRef(const char *Str)
Construct a string ref from a cstring.
Definition: StringRef.h:68
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:257
char front() const
front - Get the first character in the string.
Definition: StringRef.h:115
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:400
bool startswith_lower(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition: StringRef.cpp:60
StringRef ltrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left removed.
Definition: StringRef.h:477
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:109