10 #ifndef LLVM_ADT_STRINGREF_H 11 #define LLVM_ADT_STRINGREF_H 14 #include "llvm/Compiler.h" 25 class SmallVectorImpl;
30 bool getAsUnsignedInteger(StringRef Str,
unsigned Radix,
31 unsigned long long &Result);
33 bool getAsSignedInteger(StringRef Str,
unsigned Radix,
long long &Result);
44 typedef const char *iterator;
45 typedef const char *const_iterator;
46 static const size_t npos = ~size_t(0);
47 typedef size_t size_type;
60 static int compareMemory(
const char *Lhs,
const char *Rhs,
size_t Length) {
61 if (Length == 0) {
return 0; }
62 return ::memcmp(Lhs,Rhs,Length);
66 void set_null_terminated(
bool set) {
68 Length |= ((size_t)1 << (
sizeof(
size_t) * 8 - 1));
70 Length &= ~((size_t)1 << (
sizeof(
size_t) * 8 - 1));
80 set_null_terminated(
true);
86 assert(Str &&
"StringRef cannot be built from a NULL argument");
87 Length = ::strlen(Str);
89 assert(Length < ~((
size_t)1 << (
sizeof(
size_t) * 8 - 1)));
91 set_null_terminated(
true);
95 StringRef(
const char *
data,
size_t length,
bool isNullTerminated =
false)
96 : Data(data), Length(length) {
97 assert((data || length == 0) &&
98 "StringRef cannot be built from a NULL argument with non-null length");
100 assert(Length < ~((
size_t)1 << (
sizeof(
size_t) * 8 - 1)));
103 set_null_terminated(isNullTerminated);
108 : Data(Str.
data()), Length(Str.length()) {
110 assert(Length < ~((
size_t)1 << (
sizeof(
size_t) * 8 - 1)));
112 set_null_terminated(
true);
119 iterator begin()
const {
return Data; }
121 iterator end()
const {
return Data +
size(); }
123 const unsigned char *bytes_begin()
const {
124 return reinterpret_cast<const unsigned char *
>(begin());
126 const unsigned char *bytes_end()
const {
127 return reinterpret_cast<const unsigned char *
>(end());
130 return make_range(bytes_begin(), bytes_end());
139 const char *
data()
const {
return Data; }
150 return Length & ~((size_t)1 << (
sizeof(
size_t) * 8 - 1));
155 return (Length & ((
size_t)1 << (
sizeof(
size_t) * 8 - 1))) ==
156 ((size_t)1 << (
sizeof(
size_t) * 8 - 1));
168 return Data[
size()-1];
172 template <
typename Allocator>
StringRef copy(Allocator &A)
const {
176 char *S = A.template Allocate<char>(
size());
177 std::copy(begin(), end(), S);
185 compareMemory(Data, RHS.Data, RHS.
size()) == 0);
197 if (
int Res = compareMemory(Data, RHS.Data, std::min(
size(), RHS.
size())))
198 return Res < 0 ? -1 : 1;
203 return size() < RHS.
size() ? -1 : 1;
215 if (!Data)
return std::string();
216 return std::string(Data,
size());
223 char operator[](
size_t Index)
const {
224 assert(Index <
size() &&
"Invalid index!");
232 operator std::string()
const {
243 compareMemory(Data, Prefix.Data, Prefix.
size()) == 0;
252 compareMemory(end() - Suffix.
size(), Suffix.Data, Suffix.
size()) == 0;
266 size_t find(
char C,
size_t From = 0)
const {
267 size_t FindBegin = std::min(From,
size());
268 if (FindBegin <
size()) {
270 if (
const void *P = ::memchr(Data + FindBegin, C,
size() - FindBegin))
271 return static_cast<const char *
>(P) - Data;
286 size_t rfind(
char C,
size_t From = npos)
const {
287 From = std::min(From,
size());
306 return find(C, From);
328 return rfind(C, From);
354 for (
size_t i = 0, e =
size(); i != e; ++i)
371 template <
typename T>
372 typename std::enable_if<std::numeric_limits<T>::is_signed,
bool>::type
375 if (getAsSignedInteger(*
this, Radix, LLVal) ||
376 static_cast<T>(LLVal) != LLVal)
382 template <
typename T>
383 typename std::enable_if<!std::numeric_limits<T>::is_signed,
bool>::type
385 unsigned long long ULLVal;
389 if (getAsUnsignedInteger(*
this, Radix, ULLVal) ||
390 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
401 std::string lower()
const;
404 std::string
upper()
const;
420 Start = std::min(Start,
size());
427 assert(
size() >= N &&
"Dropping more elements than exist");
434 assert(
size() >= N &&
"Dropping more elements than exist");
450 Start = std::min(Start,
size());
451 End = std::min(std::max(Start, End),
size());
452 return StringRef(Data + Start, End - Start);
465 std::pair<StringRef, StringRef>
split(
char Separator)
const {
466 size_t Idx =
find(Separator);
468 return std::make_pair(*
this,
StringRef());
469 return std::make_pair(
slice(0, Idx),
slice(Idx+1, npos));
483 size_t Idx =
find(Separator);
485 return std::make_pair(*
this,
StringRef());
486 return std::make_pair(
slice(0, Idx),
slice(Idx + Separator.
size(), npos));
505 bool KeepEmpty =
true)
const;
522 bool KeepEmpty =
true)
const;
534 std::pair<StringRef, StringRef>
rsplit(
char Separator)
const {
535 size_t Idx =
rfind(Separator);
537 return std::make_pair(*
this,
StringRef());
538 return std::make_pair(
slice(0, Idx),
slice(Idx+1, npos));
588 return !(LHS == RHS);
607 inline std::string &operator+=(std::string &buffer,
StringRef string) {
608 return buffer.append(
string.
data(),
string.
size());
611 inline std::ostream &operator<<(std::ostream &os,
StringRef string) {
612 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:373
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:52
size_t size() const
size - Get the string size.
Definition: StringRef.h:149
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:266
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:250
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:465
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:286
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:419
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:214
bool is_null_terminated() const
is_null_terminated - Get if the string is guaranteed null terminated
Definition: StringRef.h:154
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:433
std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
Definition: StringRef.h:534
bool endswith_lower(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:67
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:195
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:561
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:54
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:139
StringRef()
Construct an empty string ref.
Definition: StringRef.h:79
const char * c_str(llvm::SmallVectorImpl< char > &buf) const
c_str - Get a null terminated pointer to the start of the string If string is not null terminated...
Definition: StringRef.cpp:123
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:264
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:567
StringRef(const char *data, size_t length, bool isNullTerminated=false)
Construct a string ref from a pointer and length.
Definition: StringRef.h:95
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:573
char back() const
back - Get the last character in the string.
Definition: StringRef.h:166
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:352
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:543
std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
Definition: StringRef.h:482
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:223
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:555
StringRef(const std::string &Str)
Construct a string ref from an std::string.
Definition: StringRef.h:107
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:327
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: Optional.h:147
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:241
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:426
std::string upper() const
Convert the given ASCII string to uppercase.
Definition: StringRef.cpp:115
A range adaptor for a pair of iterators.
Definition: iterator_range.h:32
An opaque object representing a hash code.
Definition: Hashing.h:70
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:183
bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:189
StringRef(const char *Str)
Construct a string ref from a cstring.
Definition: StringRef.h:84
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:305
char front() const
front - Get the first character in the string.
Definition: StringRef.h:160
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:449
bool startswith_lower(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition: StringRef.cpp:61
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:549
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:146