9#ifndef LLVM_ADT_STRINGREF_H
10#define LLVM_ADT_STRINGREF_H
30 template <
typename T>
class SmallVectorImpl;
35 unsigned long long &Result);
40 unsigned long long &Result);
52 static constexpr size_t npos = ~size_t(0);
60 const char *Data =
nullptr;
67 static int compareMemory(
const char *Lhs,
const char *Rhs,
size_t Length) {
68 if (
Length == 0) {
return 0; }
69 return ::memcmp(Lhs,Rhs,
Length);
87#
if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
90 std::char_traits<
char>::length(Str)
96 constexpr StringRef(
const char *data,
size_t length)
97 : Data(data),
Length(length) {}
101 : Data(Str.data()),
Length(Str.length()) {}
116 return reinterpret_cast<const unsigned char *
>(begin());
119 return reinterpret_cast<const unsigned char *
>(end());
122 return make_range(bytes_begin(), bytes_end());
131 [[nodiscard]]
constexpr const char *
data()
const {
return Data; }
134 [[nodiscard]]
constexpr bool empty()
const {
return Length == 0; }
137 [[nodiscard]]
constexpr size_t size()
const {
return Length; }
146 [[nodiscard]]
char back()
const {
152 template <
typename Allocator>
157 char *S =
A.template Allocate<char>(
Length);
158 std::copy(begin(), end(), S);
166 compareMemory(Data,
RHS.Data,
RHS.Length) == 0);
171 return Length ==
RHS.Length && compare_insensitive(
RHS) == 0;
179 if (
int Res = compareMemory(Data,
RHS.Data, std::min(
Length,
RHS.Length)))
180 return Res < 0 ? -1 : 1;
189 [[nodiscard]]
int compare_insensitive(
StringRef RHS)
const;
214 bool AllowReplacements =
true,
215 unsigned MaxEditDistance = 0)
const;
217 [[nodiscard]]
unsigned
218 edit_distance_insensitive(
StringRef Other,
bool AllowReplacements =
true,
219 unsigned MaxEditDistance = 0)
const;
222 [[nodiscard]] std::string
str()
const {
223 if (!Data)
return std::string();
224 return std::string(Data,
Length);
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 Length >= Prefix.Length &&
259 compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
262 "Use starts_with instead",
268 [[nodiscard]]
bool starts_with_insensitive(
StringRef Prefix)
const;
272 return Length >= Suffix.Length &&
273 compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) ==
277 "Use ends_with instead",
279 return ends_with(Suffix);
283 [[nodiscard]]
bool ends_with_insensitive(
StringRef Suffix)
const;
293 [[nodiscard]]
size_t find(
char C,
size_t From = 0)
const {
294 return std::string_view(*this).find(
C,
From);
301 [[nodiscard]]
size_t find_insensitive(
char C,
size_t From = 0)
const;
308 size_t From = 0)
const {
323 size_t From = 0)
const {
337 [[nodiscard]]
size_t find_insensitive(
StringRef Str,
size_t From = 0)
const;
357 [[nodiscard]]
size_t rfind_insensitive(
char C,
size_t From =
npos)
const;
363 [[nodiscard]]
size_t rfind(
StringRef Str)
const;
369 [[nodiscard]]
size_t rfind_insensitive(
StringRef Str)
const;
381 [[nodiscard]]
size_t find_first_of(
StringRef Chars,
size_t From = 0)
const;
385 [[nodiscard]]
size_t find_first_not_of(
char C,
size_t From = 0)
const;
391 [[nodiscard]]
size_t find_first_not_of(
StringRef Chars,
392 size_t From = 0)
const;
397 return rfind(
C,
From);
404 [[nodiscard]]
size_t find_last_of(
StringRef Chars,
409 [[nodiscard]]
size_t find_last_not_of(
char C,
size_t From =
npos)
const;
415 [[nodiscard]]
size_t find_last_not_of(
StringRef Chars,
427 return find_first_of(
C) !=
npos;
439 return find_insensitive(
C) !=
npos;
447 [[nodiscard]]
size_t count(
char C)
const {
467 if constexpr (std::numeric_limits<T>::is_signed) {
470 static_cast<T>(LLVal) != LLVal)
474 unsigned long long ULLVal;
479 static_cast<unsigned long long>(
static_cast<T>(ULLVal)) != ULLVal)
496 if constexpr (std::numeric_limits<T>::is_signed) {
499 static_cast<long long>(
static_cast<T>(LLVal)) != LLVal)
503 unsigned long long ULLVal;
505 static_cast<unsigned long long>(
static_cast<T>(ULLVal)) != ULLVal)
522 bool getAsInteger(
unsigned Radix,
APInt &Result)
const;
533 bool consumeInteger(
unsigned Radix,
APInt &Result);
542 bool getAsDouble(
double &Result,
bool AllowInexact =
true)
const;
549 [[nodiscard]] std::string lower()
const;
552 [[nodiscard]] std::string upper()
const;
568 size_t N =
npos)
const {
569 Start = std::min(Start,
Length);
579 return drop_back(
size() -
N);
588 return drop_front(
size() -
N);
606 assert(
size() >=
N &&
"Dropping more elements than exist");
613 assert(
size() >=
N &&
"Dropping more elements than exist");
635 *
this =
substr(Prefix.size());
642 if (!starts_with_insensitive(Prefix))
645 *
this =
substr(Prefix.size());
652 if (!ends_with(Suffix))
662 if (!ends_with_insensitive(Suffix))
681 Start = std::min(Start,
Length);
696 [[nodiscard]] std::pair<StringRef, StringRef>
split(
char Separator)
const {
710 [[nodiscard]] std::pair<StringRef, StringRef>
714 return std::make_pair(*
this,
StringRef());
715 return std::make_pair(slice(0,
Idx), slice(
Idx + Separator.
size(),
npos));
728 [[nodiscard]] std::pair<StringRef, StringRef>
730 size_t Idx = rfind(Separator);
732 return std::make_pair(*
this,
StringRef());
733 return std::make_pair(slice(0,
Idx), slice(
Idx + Separator.
size(),
npos));
752 bool KeepEmpty =
true)
const;
769 bool KeepEmpty =
true)
const;
781 [[nodiscard]] std::pair<StringRef, StringRef>
rsplit(
char Separator)
const {
788 return drop_front(std::min(
Length, find_first_not_of(Char)));
794 return drop_front(std::min(
Length, find_first_not_of(Chars)));
800 return drop_back(
Length - std::min(
Length, find_last_not_of(Char) + 1));
806 return drop_back(
Length - std::min(
Length, find_last_not_of(Chars) + 1));
812 return ltrim(Char).
rtrim(Char);
818 return ltrim(Chars).
rtrim(Chars);
828 size_t Pos =
find(
'\r');
833 if (Pos + 1 <
Length && Data[Pos + 1] ==
'\n')
835 if (Pos > 0 && Data[Pos - 1] ==
'\n')
857#if defined(__clang__) && __has_attribute(enable_if)
858#pragma clang diagnostic push
859#pragma clang diagnostic ignored "-Wgcc-compat"
860 __attribute((enable_if(__builtin_strlen(Str) ==
N - 1,
861 "invalid string literal")))
862#pragma clang diagnostic pop
884 return LHS.compare(
RHS) < 0;
888 return LHS.compare(
RHS) <= 0;
892 return LHS.compare(
RHS) > 0;
896 return LHS.compare(
RHS) >= 0;
900 return buffer.append(
string.data(),
string.
size());
906 [[nodiscard]] hash_code
hash_value(StringRef S);
912 reinterpret_cast<const char *
>(~
static_cast<uintptr_t
>(0)), 0);
917 reinterpret_cast<const char *
>(~
static_cast<uintptr_t
>(1)), 0);
923 if (
RHS.data() == getEmptyKey().data())
924 return LHS.data() == getEmptyKey().data();
925 if (
RHS.data() == getTombstoneKey().data())
926 return LHS.data() == getTombstoneKey().data();
BlockVerifier::State From
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_GSL_POINTER
LLVM_GSL_POINTER - Apply this to non-owning classes like StringRef to enable lifetime warnings.
static constexpr size_t npos
static Error split(StringRef Str, char Separator, std::pair< StringRef, StringRef > &Split)
Checked version of split, to ensure mandatory subparts.
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines DenseMapInfo traits for DenseMap.
std::optional< std::vector< StOtherPiece > > Other
static bool startswith(StringRef Magic, const char(&S)[N])
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static StringRef substr(StringRef Str, uint64_t Len)
Class for arbitrary precision integers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
constexpr StringLiteral(const char(&Str)[N])
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
StringRef - 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.
StringRef trim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left and right removed.
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
str - 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
empty - Check if the string is empty.
bool contains_insensitive(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
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...
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.
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
back - 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
size - Get the string size.
char front() const
front - Get the first character in the string.
LLVM_DEPRECATED("Use starts_with instead", "starts_with") bool startswith(StringRef 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
data - Get a pointer to the start of the string (which may not be null terminated).
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.
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.
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
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.
constexpr StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
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.
constexpr StringRef(const char *Str)
Construct a string ref from a cstring.
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.
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 - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
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_DEPRECATED("Use ends_with instead", "ends_with") bool endswith(StringRef Suffix) const
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.
StringRef(const std::string &Str)
Construct a string ref from an std::string.
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.
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.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
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.
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.
std::string & operator+=(std::string &buffer, StringRef string)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
bool operator>(int64_t V1, const APSInt &V2)
auto find_if_not(R &&Range, UnaryPredicate P)
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...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
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 unsigned getHashValue(StringRef Val)
static StringRef getTombstoneKey()
An information struct used to provide DenseMap with the various necessary components for a given valu...