9 #ifndef LLVM_ADT_STRINGREF_H 10 #define LLVM_ADT_STRINGREF_H 21 #include <type_traits> 27 extern "C" size_t __builtin_strlen(
const char *);
34 template <
typename T>
class SmallVectorImpl;
39 unsigned long long &Result);
44 unsigned long long &Result);
64 const char *Data =
nullptr;
71 static int compareMemory(
const char *Lhs,
const char *Rhs,
size_t Length) {
72 if (Length == 0) {
return 0; }
77 static constexpr
size_t strLen(
const char *Str) {
78 #if __cplusplus > 201402L 79 return std::char_traits<char>::length(Str);
80 #elif __has_builtin(__builtin_strlen) || defined(__GNUC__) || \ 81 (defined(_MSC_VER) && _MSC_VER >= 1916) 82 return __builtin_strlen(Str);
84 const char *Begin = Str;
104 : Data(Str), Length(Str ? strLen(Str) : 0) {}
108 : Data(data), Length(length) {}
112 : Data(Str.
data()), Length(Str.length()) {}
127 return reinterpret_cast<const unsigned char *
>(
begin());
130 return reinterpret_cast<const unsigned char *
>(
end());
143 const char *
data()
const {
return Data; }
147 bool empty()
const {
return Length == 0; }
151 size_t size()
const {
return Length; }
164 return Data[Length-1];
168 template <
typename Allocator>
173 char *S = A.template Allocate<char>(Length);
182 return (Length == RHS.Length &&
183 compareMemory(Data, RHS.Data, RHS.Length) == 0);
197 if (
int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
198 return Res < 0 ? -1 : 1;
201 if (Length == RHS.Length)
203 return Length < RHS.Length ? -1 : 1;
235 unsigned MaxEditDistance = 0)
const;
240 if (!Data)
return std::string();
241 return std::string(Data, Length);
250 assert(Index < Length &&
"Invalid index!");
258 template <
typename T>
259 typename std::enable_if<std::is_same<T, std::string>::value,
267 operator std::string()
const {
278 return Length >= Prefix.Length &&
279 compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
289 return Length >= Suffix.Length &&
290 compareMemory(
end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
307 size_t FindBegin = std::min(
From, Length);
308 if (FindBegin < Length) {
310 if (
const void *
P = ::memchr(Data + FindBegin, C, Length - FindBegin))
311 return static_cast<const char *
>(
P) - Data;
480 for (
size_t i = 0,
e = Length; i !=
e; ++i)
497 template <
typename T>
498 typename std::enable_if<std::numeric_limits<T>::is_signed,
bool>
::type 502 static_cast<T>(LLVal) != LLVal)
508 template <
typename T>
509 typename std::enable_if<!std::numeric_limits<T>::is_signed,
bool>
::type 511 unsigned long long ULLVal;
516 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
531 template <
typename T>
532 typename std::enable_if<std::numeric_limits<T>::is_signed,
bool>
::type 536 static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
542 template <
typename T>
543 typename std::enable_if<!std::numeric_limits<T>::is_signed,
bool>
::type 545 unsigned long long ULLVal;
547 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
572 bool getAsDouble(
double &Result,
bool AllowInexact =
true)
const;
580 std::string
lower()
const;
584 std::string
upper()
const;
601 Start = std::min(Start, Length);
602 return StringRef(Data + Start, std::min(
N, Length - Start));
643 assert(
size() >=
N &&
"Dropping more elements than exist");
651 assert(
size() >=
N &&
"Dropping more elements than exist");
702 Start = std::min(Start, Length);
703 End = std::min(
std::max(Start, End), Length);
704 return StringRef(Data + Start, End - Start);
718 std::pair<StringRef, StringRef>
split(
char Separator)
const {
734 size_t Idx =
find(Separator);
736 return std::make_pair(*
this,
StringRef());
752 size_t Idx =
rfind(Separator);
754 return std::make_pair(*
this,
StringRef());
774 bool KeepEmpty =
true)
const;
791 bool KeepEmpty =
true)
const;
804 std::pair<StringRef, StringRef>
rsplit(
char Separator)
const {
868 #if defined(__clang__) && __has_attribute(enable_if) 869 #pragma clang diagnostic push 870 #pragma clang diagnostic ignored "-Wgcc-compat" 871 __attribute((enable_if(__builtin_strlen(Str) == N - 1,
872 "invalid string literal")))
873 #pragma clang diagnostic pop 881 return StringLiteral(Str, N - 1);
911 return buffer.append(
string.
data(),
string.
size());
922 #endif // LLVM_ADT_STRINGREF_H
std::string & operator+=(std::string &buffer, StringRef string)
std::enable_if<!std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
static StringRef withNullAsEmpty(const char *data)
LLVM_NODISCARD StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
LLVM_NODISCARD char operator[](size_t Index) const
LLVM_NODISCARD bool startswith_lower(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
This class represents lattice values for constants.
const unsigned char * bytes_end() const
LLVM_NODISCARD StringRef take_until(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that no character in the prefix satisfies the given predicat...
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
LLVM_NODISCARD int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
bool operator>(int64_t V1, const APSInt &V2)
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
An efficient, type-erasing, non-owning reference to a callable.
LLVM_NODISCARD 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.
LLVM_NODISCARD std::pair< StringRef, StringRef > rsplit(StringRef Separator) const
Split into two substrings around the last occurrence of a separator string.
LLVM_NODISCARD bool endswith_lower(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
std::enable_if<!std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
LLVM_NODISCARD unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
Determine the edit distance between this string and another string.
bool operator<=(int64_t V1, const APSInt &V2)
LLVM_NODISCARD bool contains(char C) const
Return true if the given character is contained in *this, and false otherwise.
bool operator>=(int64_t V1, const APSInt &V2)
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
LLVM_NODISCARD StringRef copy(Allocator &A) const
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
LLVM_NODISCARD StringRef take_back(size_t N=1) const
Return a StringRef equal to 'this' but with only the last N elements remaining.
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
iterator_range< const unsigned char * > bytes() const
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
LLVM_NODISCARD StringRef take_while(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that every character in the prefix satisfies the given predi...
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
LLVM_NODISCARD StringRef rtrim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the right removed.
LLVM_NODISCARD std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
LLVM_NODISCARD size_t size() const
size - Get the string size.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
StringRef()=default
Construct an empty string ref.
LLVM_NODISCARD std::string upper() const
Convert the given ASCII string to uppercase.
LLVM_NODISCARD StringRef drop_until(function_ref< bool(char)> F) const
Return a StringRef equal to 'this', but with all characters not satisfying the given predicate droppe...
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
LLVM_NODISCARD 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.
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
LLVM_NODISCARD bool contains_lower(char C) const
Return true if the given character is contained in *this, and false otherwise.
LLVM_NODISCARD size_t rfind_lower(char C, size_t From=npos) const
Search for the last character C in the string, ignoring case.
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
LLVM_NODISCARD size_t find_if_not(function_ref< bool(char)> F, size_t From=0) const
Search for the first character not satisfying the predicate F.
LLVM_NODISCARD StringRef ltrim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the left removed.
LLVM_NODISCARD char back() const
back - Get the last character in the string.
StringRef(const std::string &Str)
Construct a string ref from an std::string.
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.
LLVM_NODISCARD int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
BlockVerifier::State From
Align max(MaybeAlign Lhs, Align Rhs)
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
bool getAsDouble(double &Result, bool AllowInexact=true) const
Parse the current string as an IEEE double-precision floating point value.
std::enable_if< std::is_same< T, std::string >::value, StringRef >::type & operator=(T &&Str)=delete
Disallow accidental assignment from a temporary std::string.
LLVM_NODISCARD size_t find_lower(char C, size_t From=0) const
Search for the first character C in the string, ignoring case.
LLVM_NODISCARD 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.
A range adaptor for a pair of iterators.
Class for arbitrary precision integers.
An opaque object representing a hash code.
LLVM_NODISCARD int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
constexpr StringLiteral(const char(&Str)[N])
bool operator!=(uint64_t V1, const APInt &V2)
const unsigned char * bytes_begin() const
constexpr StringRef(const char *Str)
Construct a string ref from a cstring.
Merge contiguous icmps into a memcmp
LLVM_NODISCARD StringRef drop_while(function_ref< bool(char)> F) const
Return a StringRef equal to 'this', but with all characters satisfying the given predicate dropped fr...
LLVM_NODISCARD StringRef trim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the left and right removed...
LLVM_NODISCARD 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.
LLVM_NODISCARD bool contains_lower(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
LLVM_NODISCARD size_t find_if(function_ref< bool(char)> F, size_t From=0) const
Search for the first character satisfying the predicate F.
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
LLVM_NODISCARD std::string lower() const
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
bool operator<(int64_t V1, const APSInt &V2)
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
constexpr StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
StringRef - Represent a constant reference to a string, i.e.
bool operator==(uint64_t V1, const APInt &V2)
bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)
OutputIt copy(R &&Range, OutputIt Out)
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)