9#ifndef LLVM_ADT_STRINGREF_H
10#define LLVM_ADT_STRINGREF_H
36 unsigned long long &Result);
44 unsigned long long &Result);
57 static constexpr size_t npos = ~size_t(0);
68 const char *Data =
nullptr;
75 static int compareMemory(
const char *Lhs,
const char *Rhs,
size_t Length) {
78 return ::memcmp(Lhs, Rhs, Length);
99 : Data(
data), Length(length) {}
103 : Data(Str.
data()), Length(Str.length()) {}
107 : Data(Str.
data()), Length(Str.
size()) {}
122 return reinterpret_cast<const unsigned char *
>(
begin());
125 return reinterpret_cast<const unsigned char *
>(
end());
137 [[nodiscard]]
constexpr const char *
data()
const {
return Data; }
140 [[nodiscard]]
constexpr bool empty()
const {
return size() == 0; }
143 [[nodiscard]]
constexpr size_t size()
const {
return Length; }
152 [[nodiscard]]
char back()
const {
158 template <
typename Allocator>
163 char *S =
A.template Allocate<char>(
size());
180 return Res < 0 ? -1 : 1;
185 return size() <
RHS.size() ? -1 : 1;
214 unsigned MaxEditDistance = 0)
const;
217 edit_distance_insensitive(
StringRef Other,
bool AllowReplacements =
true,
218 unsigned MaxEditDistance = 0)
const;
221 [[nodiscard]] std::string
str()
const {
223 return std::string();
233 return data()[Index];
240 template <
typename T>
241 std::enable_if_t<std::is_same<T, std::string>::value,
StringRef> &
248 constexpr operator std::string_view()
const {
249 return std::string_view(
data(),
size());
258 return size() >= Prefix.size() &&
259 compareMemory(
data(), Prefix.data(), Prefix.size()) == 0;
289 [[nodiscard]]
size_t find(
char C,
size_t From = 0)
const {
290 return std::string_view(*this).find(
C, From);
297 [[nodiscard]]
LLVM_ABI size_t find_insensitive(
char C,
size_t From = 0)
const;
304 size_t From = 0)
const {
319 size_t From = 0)
const {
320 return find_if([
F](
char c) {
return !
F(c); }, From);
328 size_t End =
npos)
const {
329 size_t I = std::min(End,
size());
343 size_t End =
npos)
const {
358 size_t From = 0)
const;
364 [[nodiscard]]
size_t rfind(
char C,
size_t From =
npos)
const {
365 size_t I = std::min(From,
size());
378 [[nodiscard]]
LLVM_ABI size_t rfind_insensitive(
char C,
379 size_t From =
npos)
const;
396 return find(
C, From);
404 size_t From = 0)
const;
408 [[nodiscard]]
LLVM_ABI size_t find_first_not_of(
char C,
409 size_t From = 0)
const;
416 size_t From = 0)
const;
429 size_t From =
npos)
const;
433 [[nodiscard]]
LLVM_ABI size_t find_last_not_of(
char C,
434 size_t From =
npos)
const;
441 size_t From =
npos)
const;
470 [[nodiscard]]
size_t count(
char C)
const {
472 for (
size_t I = 0;
I !=
size(); ++
I)
490 if constexpr (std::numeric_limits<T>::is_signed) {
493 static_cast<T>(LLVal) != LLVal)
497 unsigned long long ULLVal;
502 static_cast<unsigned long long>(
static_cast<T>(ULLVal)) != ULLVal)
519 if constexpr (std::numeric_limits<T>::is_signed) {
522 static_cast<long long>(
static_cast<T>(LLVal)) != LLVal)
526 unsigned long long ULLVal;
528 static_cast<unsigned long long>(
static_cast<T>(ULLVal)) != ULLVal)
545 LLVM_ABI bool getAsInteger(
unsigned Radix,
APInt &Result)
const;
565 LLVM_ABI bool getAsDouble(
double &Result,
bool AllowInexact =
true)
const;
572 [[nodiscard]]
LLVM_ABI std::string lower()
const;
575 [[nodiscard]]
LLVM_ABI std::string upper()
const;
591 size_t N =
npos)
const {
592 Start = std::min(Start,
size());
629 assert(
size() >=
N &&
"Dropping more elements than exist");
636 assert(
size() >=
N &&
"Dropping more elements than exist");
668 *
this =
substr(Prefix.size());
678 *
this =
substr(Prefix.size());
714 Start = std::min(Start,
size());
715 End = std::clamp(End, Start,
size());
729 [[nodiscard]] std::pair<StringRef, StringRef>
split(
char Separator)
const {
743 [[nodiscard]] std::pair<StringRef, StringRef>
745 size_t Idx =
find(Separator);
761 [[nodiscard]] std::pair<StringRef, StringRef>
763 size_t Idx =
rfind(Separator);
784 int MaxSplit = -1,
bool KeepEmpty =
true)
const;
801 int MaxSplit = -1,
bool KeepEmpty =
true)
const;
813 [[nodiscard]] std::pair<StringRef, StringRef>
rsplit(
char Separator)
const {
844 return ltrim(Char).rtrim(Char);
850 return ltrim(Chars).rtrim(Chars);
860 size_t Pos =
find(
'\r');
865 if (Pos + 1 <
size() &&
data()[Pos + 1] ==
'\n')
867 if (Pos > 0 &&
data()[Pos - 1] ==
'\n')
883 constexpr StringLiteral(
const char *Str,
size_t N) :
StringRef(Str,
N) {}
888#if defined(__clang__) && __has_attribute(enable_if)
889#pragma clang diagnostic push
890#pragma clang diagnostic ignored "-Wgcc-compat"
891 __attribute((enable_if(__builtin_strlen(Str) ==
N - 1,
892 "invalid string literal")))
893#pragma clang diagnostic pop
901 return StringLiteral(Str,
N - 1);
909 if (
LHS.size() !=
RHS.size())
913 return ::memcmp(
LHS.data(),
RHS.data(),
LHS.size()) == 0;
919 return LHS.compare(
RHS) < 0;
923 return LHS.compare(
RHS) <= 0;
927 return LHS.compare(
RHS) > 0;
931 return LHS.compare(
RHS) >= 0;
935 return buffer.append(
string.
data(),
string.
size());
946 return StringRef(
reinterpret_cast<const char *
>(~
static_cast<uintptr_t
>(0)),
951 return StringRef(
reinterpret_cast<const char *
>(~
static_cast<uintptr_t
>(1)),
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_LIFETIME_BOUND
#define LLVM_GSL_POINTER
LLVM_GSL_POINTER - Apply this to non-owning classes like StringRef to enable lifetime warnings.
static constexpr size_t npos
This file defines DenseMapInfo traits for DenseMap.
static StringRef substr(StringRef Str, uint64_t Len)
static const BasicSubtargetSubTypeKV * find(StringRef S, ArrayRef< BasicSubtargetSubTypeKV > A)
Find KV in array using binary search.
Class for arbitrary precision integers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
constexpr StringLiteral(const char(&Str)[N])
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
LLVM_ABI 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.
StringRef trim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left and right removed.
static constexpr size_t npos
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
iterator_range< const unsigned char * > bytes() const
std::string str() const
Get the contents as an std::string.
size_t find_if(function_ref< bool(char)> F, size_t From=0) const
Search for the first character satisfying the predicate F.
const unsigned char * bytes_end() const
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
Check if the string is empty.
std::reverse_iterator< const_iterator > const_reverse_iterator
bool contains_insensitive(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
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...
bool ends_with(char Suffix) const
size_t rfind_if_not(function_ref< bool(char)> F, size_t End=npos) const
Search for the last character not satisfying the predicate F.
char operator[](size_t Index) const
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
bool contains_insensitive(char C) const
Return true if the given character is contained in *this, and false otherwise.
std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
const char * const_iterator
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...
char back() const
Get the last character in the string.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
Get the string size.
char front() const
Get the first character in the string.
reverse_iterator rbegin() const
constexpr StringRef(const char *data LLVM_LIFETIME_BOUND, size_t length)
Construct a string ref from a pointer and length.
std::reverse_iterator< iterator > reverse_iterator
bool starts_with(char Prefix) const
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.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
size_t rfind_if(function_ref< bool(char)> F, size_t End=npos) const
Search for the last character satisfying the predicate F.
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
StringRef detectEOL() const
Detect the line ending style of the string.
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.
StringRef()=default
Construct an empty string ref.
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
constexpr StringRef(const char *Str LLVM_LIFETIME_BOUND)
Construct a string ref from a cstring.
bool contains(char C) const
Return true if the given character is contained in *this, and false otherwise.
StringRef(std::nullptr_t)=delete
Disable conversion from nullptr.
StringRef take_back(size_t N=1) const
Return a StringRef equal to 'this' but with only the last N elements remaining.
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
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...
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
LLVM_ABI size_t find_insensitive(char C, size_t From=0) const
Search for the first character C in the string, ignoring case.
size_t count(char C) const
Return the number of occurrences of C in the string.
bool consume_back_insensitive(StringRef Suffix)
Returns true if this StringRef has the given suffix, ignoring case, and removes that suffix.
StringRef copy(Allocator &A) const
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
std::pair< StringRef, StringRef > rsplit(StringRef Separator) const
Split into two substrings around the last occurrence of a separator string.
std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
StringRef ltrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left removed.
std::enable_if_t< std::is_same< T, std::string >::value, StringRef > & operator=(T &&Str)=delete
Disallow accidental assignment from a temporary std::string.
StringRef rtrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the right removed.
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
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...
const unsigned char * bytes_begin() const
int compare(StringRef RHS) const
Compare two strings; the result is negative, zero, or positive if this string is lexicographically le...
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
LLVM_ABI bool ends_with_insensitive(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
LLVM_ABI 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.
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.
LLVM_ABI int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
StringRef(const std::string &Str)
Construct a string ref from an std::string.
constexpr operator std::string_view() const
reverse_iterator rend() const
constexpr StringRef(std::string_view Str)
Construct a string ref from an std::string_view.
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.
An efficient, type-erasing, non-owning reference to a callable.
An opaque object representing a hash code.
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
LLVM_ABI bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
hash_code hash_value(const FixedPointSemantics &Val)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str)
bool operator!=(uint64_t V1, const APInt &V2)
bool operator>=(int64_t V1, const APSInt &V2)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt & operator+=(DynamicAPInt &A, int64_t B)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
bool operator>(int64_t V1, const APSInt &V2)
FunctionAddr VTableAddr Count
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
LLVM_ABI bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
LLVM_ABI bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
bool operator<=(int64_t V1, const APSInt &V2)
Implement std::hash so that hash_code can be used in STL containers.
static StringRef getEmptyKey()
static bool isEqual(StringRef LHS, StringRef RHS)
static LLVM_ABI unsigned getHashValue(StringRef Val)
static StringRef getTombstoneKey()
An information struct used to provide DenseMap with the various necessary components for a given valu...