Go to the documentation of this file.
9 #ifndef LLVM_ADT_STRINGREF_H
10 #define LLVM_ADT_STRINGREF_H
22 #if __cplusplus > 201402L
23 #include <string_view>
25 #include <type_traits>
31 extern "C" size_t __builtin_strlen(
const char *);
38 template <
typename T>
class SmallVectorImpl;
43 unsigned long long &Result);
48 unsigned long long &Result);
60 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) {
76 if (Length == 0) {
return 0; }
81 static constexpr
size_t strLen(
const char *Str) {
82 #if __cplusplus > 201402L
83 return std::char_traits<char>::length(Str);
84 #elif __has_builtin(__builtin_strlen) || defined(__GNUC__) || \
85 (defined(_MSC_VER) && _MSC_VER >= 1916)
86 return __builtin_strlen(Str);
88 const char *Begin = Str;
100 StringRef() =
default;
104 StringRef(std::nullptr_t) =
delete;
108 :
Data(Str), Length(Str ? strLen(Str) : 0) {}
112 :
Data(data), Length(length) {}
116 :
Data(Str.data()), Length(Str.length()) {}
118 #if __cplusplus > 201402L
120 constexpr
StringRef(std::string_view Str)
121 :
Data(Str.data()), Length(Str.
size()) {}
133 return reinterpret_cast<const unsigned char *
>(
begin());
136 return reinterpret_cast<const unsigned char *
>(
end());
139 return make_range(bytes_begin(), bytes_end());
153 constexpr
bool empty()
const {
return Length == 0; }
157 constexpr
size_t size()
const {
return Length; }
170 return Data[Length-1];
174 template <
typename Allocator>
179 char *
S = A.template Allocate<char>(Length);
188 return (Length ==
RHS.Length &&
189 compareMemory(
Data,
RHS.Data,
RHS.Length) == 0);
195 return Length ==
RHS.Length && compare_insensitive(
RHS) == 0;
204 return Res < 0 ? -1 : 1;
207 if (Length ==
RHS.Length)
209 return Length <
RHS.Length ? -1 : 1;
240 unsigned edit_distance(
StringRef Other,
bool AllowReplacements =
true,
241 unsigned MaxEditDistance = 0)
const;
244 edit_distance_insensitive(
StringRef Other,
bool AllowReplacements =
true,
245 unsigned MaxEditDistance = 0)
const;
250 if (!
Data)
return std::string();
251 return std::string(
Data, Length);
268 template <
typename T>
269 std::enable_if_t<std::is_same<T, std::string>::value,
StringRef> &
270 operator=(T &&Str) =
delete;
276 explicit operator std::string()
const {
return str(); }
278 #if __cplusplus > 201402L
279 operator std::string_view()
const {
280 return std::string_view(data(),
size());
291 return Length >=
Prefix.Length &&
302 return Length >= Suffix.Length &&
303 compareMemory(
end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
308 bool endswith_insensitive(
StringRef Suffix)
const;
321 if (FindBegin < Length) {
323 if (
const void *
P = ::memchr(
Data + FindBegin,
C, Length - FindBegin))
324 return static_cast<const char *
>(
P) -
Data;
334 size_t find_insensitive(
char C,
size_t From = 0)
const;
345 return size() -
S.size();
372 size_t find_insensitive(
StringRef Str,
size_t From = 0)
const;
395 size_t rfind_insensitive(
char C,
size_t From = npos)
const;
409 size_t rfind_insensitive(
StringRef Str)
const;
423 size_t find_first_of(
StringRef Chars,
size_t From = 0)
const;
428 size_t find_first_not_of(
char C,
size_t From = 0)
const;
435 size_t find_first_not_of(
StringRef Chars,
size_t From = 0)
const;
441 return rfind(
C,
From);
449 size_t find_last_of(
StringRef Chars,
size_t From = npos)
const;
454 size_t find_last_not_of(
char C,
size_t From = npos)
const;
461 size_t find_last_not_of(
StringRef Chars,
size_t From = npos)
const;
471 bool contains(
char C)
const {
return find_first_of(
C) != npos; }
477 return find_insensitive(
Other) != npos;
484 return find_insensitive(
C) != npos;
495 for (
size_t i = 0,
e = Length;
i !=
e; ++
i)
512 template <
typename T>
513 std::enable_if_t<std::numeric_limits<T>::is_signed,
bool>
517 static_cast<T>(LLVal) != LLVal)
523 template <
typename T>
524 std::enable_if_t<!std::numeric_limits<T>::is_signed,
bool>
526 unsigned long long ULLVal;
531 static_cast<unsigned long long>(
static_cast<T>(ULLVal)) != ULLVal)
546 template <
typename T>
547 std::enable_if_t<std::numeric_limits<T>::is_signed,
bool>
551 static_cast<long long>(
static_cast<T>(LLVal)) != LLVal)
557 template <
typename T>
558 std::enable_if_t<!std::numeric_limits<T>::is_signed,
bool>
560 unsigned long long ULLVal;
562 static_cast<unsigned long long>(
static_cast<T>(ULLVal)) != ULLVal)
578 bool getAsInteger(
unsigned Radix,
APInt &Result)
const;
587 bool getAsDouble(
double &Result,
bool AllowInexact =
true)
const;
595 std::string lower()
const;
599 std::string upper()
const;
627 return drop_back(
size() -
N);
637 return drop_front(
size() -
N);
658 assert(
size() >=
N &&
"Dropping more elements than exist");
666 assert(
size() >=
N &&
"Dropping more elements than exist");
690 *
this = drop_front(
Prefix.size());
697 if (!startswith_insensitive(
Prefix))
700 *
this = drop_front(
Prefix.size());
707 if (!endswith(Suffix))
710 *
this = drop_back(Suffix.
size());
717 if (!endswith_insensitive(Suffix))
720 *
this = drop_back(Suffix.
size());
753 std::pair<StringRef, StringRef>
split(
char Separator)
const {
769 size_t Idx =
find(Separator);
771 return std::make_pair(*
this,
StringRef());
772 return std::make_pair(slice(0, Idx), slice(Idx + Separator.
size(), npos));
787 size_t Idx = rfind(Separator);
789 return std::make_pair(*
this,
StringRef());
790 return std::make_pair(slice(0, Idx), slice(Idx + Separator.
size(), npos));
809 bool KeepEmpty =
true)
const;
826 bool KeepEmpty =
true)
const;
839 std::pair<StringRef, StringRef>
rsplit(
char Separator)
const {
847 return drop_front(
std::min(Length, find_first_not_of(Char)));
854 return drop_front(
std::min(Length, find_first_not_of(Chars)));
861 return drop_back(Length -
std::min(Length, find_last_not_of(Char) + 1));
868 return drop_back(Length -
std::min(Length, find_last_not_of(Chars) + 1));
875 return ltrim(Char).
rtrim(Char);
882 return ltrim(Chars).
rtrim(Chars);
893 size_t Pos =
find(
'\r');
898 if (Pos + 1 < Length &&
Data[Pos + 1] ==
'\n')
900 if (Pos > 0 &&
Data[Pos - 1] ==
'\n')
922 #if defined(__clang__) && __has_attribute(enable_if)
923 #pragma clang diagnostic push
924 #pragma clang diagnostic ignored "-Wgcc-compat"
925 __attribute((enable_if(__builtin_strlen(Str) ==
N - 1,
926 "invalid string literal")))
927 #pragma clang diagnostic pop
949 return LHS.compare(
RHS) == -1;
953 return LHS.compare(
RHS) != 1;
957 return LHS.compare(
RHS) == 1;
961 return LHS.compare(
RHS) != -1;
965 return buffer.append(
string.data(),
string.
size());
978 reinterpret_cast<const char *
>(~
static_cast<uintptr_t
>(0)), 0);
983 reinterpret_cast<const char *
>(~
static_cast<uintptr_t
>(1)), 0);
986 static unsigned getHashValue(
StringRef Val);
989 if (
RHS.data() == getEmptyKey().data())
990 return LHS.data() == getEmptyKey().data();
991 if (
RHS.data() == getTombstoneKey().data())
992 return LHS.data() == getTombstoneKey().data();
999 #endif // LLVM_ADT_STRINGREF_H
LLVM_NODISCARD StringRef copy(Allocator &A) const
LLVM_NODISCARD char back() const
back - Get the last character in the string.
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_NODISCARD char operator[](size_t Index) const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
bool operator<=(int64_t V1, const APSInt &V2)
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
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 size_t find(char C, size_t From=0) const
Search for the first character C in the string.
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...
LLVM_NODISCARD StringRef rtrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the right removed.
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Merge contiguous icmps into a memcmp
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
static Error split(StringRef Str, char Separator, std::pair< StringRef, StringRef > &Split)
Checked version of split, to ensure mandatory subparts.
LLVM_NODISCARD std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
static bool startswith(StringRef Magic, const char(&S)[N])
constexpr StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
static StringRef getTombstoneKey()
LLVM_NODISCARD std::pair< StringRef, StringRef > rsplit(StringRef Separator) const
Split into two substrings around the last occurrence of a separator string.
const_iterator end(StringRef path)
Get end iterator over path.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
bool operator!=(uint64_t V1, const APInt &V2)
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
std::enable_if_t<!std::numeric_limits< T >::is_signed, bool > consumeInteger(unsigned Radix, T &Result)
bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)
An information struct used to provide DenseMap with the various necessary components for a given valu...
LLVM_NODISCARD StringRef trim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left and right removed.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
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 StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
(vector float) vec_cmpeq(*A, *B) C
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
std::string & operator+=(std::string &buffer, StringRef string)
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
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...
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.
the resulting code requires compare and branches when and if the revised code is with conditional branches instead of More there is a byte word extend before each where there should be only and the condition codes are not remembered when the same two values are compared twice More LSR enhancements i8 and i32 load store addressing modes are identical int int c
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
std::enable_if_t< std::numeric_limits< T >::is_signed, bool > getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
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...
An efficient, type-erasing, non-owning reference to a callable.
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
StringRef(const std::string &Str)
Construct a string ref from an std::string.
bool operator>=(int64_t V1, const APSInt &V2)
LLVM_NODISCARD bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
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_NODISCARD bool contains(char C) const
Return true if the given character is contained in *this, and false otherwise.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
std::enable_if_t<!std::numeric_limits< T >::is_signed, bool > getAsInteger(unsigned Radix, T &Result) const
bool operator<(int64_t V1, const APSInt &V2)
LLVM_NODISCARD char front() const
front - Get the first character in the string.
static bool isEqual(StringRef LHS, StringRef RHS)
constexpr StringRef(const char *Str)
Construct a string ref from a cstring.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator==(uint64_t V1, const APInt &V2)
const unsigned char * bytes_begin() const
LLVM_NODISCARD bool contains_insensitive(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Class for arbitrary precision integers.
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
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.
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
LLVM_NODISCARD StringRef ltrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left removed.
StringRef - Represent a constant reference to a string, i.e.
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
static StringRef getEmptyKey()
std::enable_if_t< std::numeric_limits< T >::is_signed, bool > consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
StringRef()=default
Construct an empty string ref.
LLVM_NODISCARD StringRef detectEOL() const
Detect the line ending style of the string.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
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 std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
constexpr StringLiteral(const char(&Str)[N])
iterator_range< const unsigned char * > bytes() const
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
#define LLVM_GSL_POINTER
LLVM_GSL_POINTER - Apply this to non-owning classes like StringRef to enable lifetime warnings.
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
const char * const_iterator
bool operator>(int64_t V1, const APSInt &V2)
A range adaptor for a pair of iterators.
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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.
static StringRef substr(StringRef Str, uint64_t Len)
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.
BlockVerifier::State From
bool consume_back_insensitive(StringRef Suffix)
Returns true if this StringRef has the given suffix, ignoring case, and removes that suffix.
we should consider alternate ways to model stack dependencies Lots of things could be done in WebAssemblyTargetTransformInfo cpp there are numerous optimization related hooks that can be overridden in WebAssemblyTargetLowering Instead of the OptimizeReturned which should consider preserving the returned attribute through to MachineInstrs and extending the MemIntrinsicResults pass to do this optimization on calls too That would also let the WebAssemblyPeephole pass clean up dead defs for such as it does for stores Consider implementing and or getMachineCombinerPatterns Find a clean way to fix the problem which leads to the Shrink Wrapping pass being run after the WebAssembly PEI pass When setting multiple variables to the same we currently get code like const It could be done with a smaller encoding like local tee $pop5 local copy
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...
auto find_if_not(R &&Range, UnaryPredicate P)
LLVM_NODISCARD bool contains_insensitive(char C) const
Return true if the given character is contained in *this, and false otherwise.
Optional< std::vector< StOtherPiece > > Other
const unsigned char * bytes_end() const
LLVM_NODISCARD StringRef take_back(size_t N=1) const
Return a StringRef equal to 'this' but with only the last N elements remaining.
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.