1//===-- StringRef.cpp - Lightweight String References ---------------------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 20// MSVC emits references to this into the translation units which reference it. 25// strncasecmp() is not available on non-POSIX systems, so define an 26// alternative function here. 29unsignedchar LHC = toLower(
LHS[
I]);
30unsignedchar RHC = toLower(
RHS[
I]);
32return LHC < RHC ? -1 : 1;
43returnsize() <
RHS.size() ? -1 : 1;
47returnsize() >= Prefix.size() &&
62/// compare_numeric - Compare strings, handle embedded numbers. 64for (
size_tI = 0, E = std::min(
size(),
RHS.size());
I != E; ++
I) {
65// Check for sequences of digits. 67// The longer sequence of numbers is considered larger. 68// This doesn't really handle prefixed zeros well. 70for (J =
I + 1; J != E + 1; ++J) {
78// The two number sequences have the same length (J-I), just memcmp them. 79if (
int Res = compareMemory(
data() +
I,
RHS.data() +
I, J -
I))
80return Res < 0 ? -1 : 1;
81// Identical number sequences, continue search after the numbers. 86return (
unsignedchar)
data()[
I] < (
unsignedchar)
RHS.data()[
I] ? -1 : 1;
90returnsize() <
RHS.size() ? -1 : 1;
93// Compute the edit distance between the two given strings. 95bool AllowReplacements,
96unsigned MaxEditDistance)
const{
99 AllowReplacements, MaxEditDistance);
103StringRefOther,
bool AllowReplacements,
unsigned MaxEditDistance)
const{
106 llvm::toLower, AllowReplacements, MaxEditDistance);
109//===----------------------------------------------------------------------===// 111//===----------------------------------------------------------------------===// 123//===----------------------------------------------------------------------===// 125//===----------------------------------------------------------------------===// 128/// find - Search for the first string \arg Str in the string. 130/// \return - The index of the first occurrence of \arg Str, or npos if not 139constchar *Needle = Str.data();
146constchar *
Ptr = (
constchar *)::memchr(Start, Needle[0],
Size);
150constchar *Stop = Start + (
Size -
N + 1);
153// Provide a fast path for newline finding (CRLF case) in InclusionRewriter. 154// Not the most optimized strategy, but getting memcmp inlined should be 157if (std::memcmp(Start, Needle, 2) == 0)
160 }
while (Start < Stop);
164// For short haystacks or unsupported needles fall back to the naive algorithm 165if (Size < 16 || N > 255) {
167if (std::memcmp(Start, Needle,
N) == 0)
170 }
while (Start < Stop);
174// Build the bad char heuristic table, with uint8_t to reduce cache thrashing. 176 std::memset(BadCharSkip,
N, 256);
177for (
unsigned i = 0; i !=
N-1; ++i)
178 BadCharSkip[(
uint8_t)Str[i]] =
N-1-i;
183if (std::memcmp(Start, Needle,
N - 1) == 0)
186// Otherwise skip the appropriate number of bytes. 187 Start += BadCharSkip[
Last];
188 }
while (Start < Stop);
195while (This.size() >= Str.size()) {
196if (This.starts_with_insensitive(Str))
198 This = This.drop_front();
209if (toLower(
data()[i]) == toLower(
C))
215/// rfind - Search for the last string \arg Str in the string. 217/// \return - The index of the last occurrence of \arg Str, or npos if not 220return std::string_view(*this).rfind(Str);
227for (
size_t i =
size() -
N + 1, e = 0; i != e;) {
235/// find_first_of - Find the first character in the string that is in \arg 236/// Chars, or npos if not found. 238/// Note: O(size() + Chars.size()) 241 std::bitset<1 << CHAR_BIT> CharBits;
243 CharBits.set((
unsignedchar)
C);
246if (CharBits.test((
unsignedchar)
data()[i]))
251/// find_first_not_of - Find the first character in the string that is not 252/// \arg C or npos if not found. 254return std::string_view(*this).find_first_not_of(
C,
From);
257/// find_first_not_of - Find the first character in the string that is not 258/// in the string \arg Chars, or npos if not found. 260/// Note: O(size() + Chars.size()) 263 std::bitset<1 << CHAR_BIT> CharBits;
265 CharBits.set((
unsignedchar)
C);
268if (!CharBits.test((
unsignedchar)
data()[i]))
273/// find_last_of - Find the last character in the string that is in \arg C, 274/// or npos if not found. 276/// Note: O(size() + Chars.size()) 279 std::bitset<1 << CHAR_BIT> CharBits;
281 CharBits.set((
unsignedchar)
C);
284if (CharBits.test((
unsignedchar)
data()[i]))
289/// find_last_not_of - Find the last character in the string that is not 290/// \arg C, or npos if not found. 298/// find_last_not_of - Find the last character in the string that is not in 299/// \arg Chars, or npos if not found. 301/// Note: O(size() + Chars.size()) 304 std::bitset<1 << CHAR_BIT> CharBits;
306 CharBits.set((
unsignedchar)
C);
309if (!CharBits.test((
unsignedchar)
data()[i]))
316bool KeepEmpty)
const{
319// Count down from MaxSplit. When MaxSplit is -1, this will just split 320// "forever". This doesn't support splitting more than 2^31 times 321// intentionally; if we ever want that we can make MaxSplit a 64-bit integer 322// but that seems unlikely to be useful. 323while (MaxSplit-- != 0) {
329if (KeepEmpty ||
Idx > 0)
337if (KeepEmpty || !S.
empty())
342int MaxSplit,
bool KeepEmpty)
const{
345// Count down from MaxSplit. When MaxSplit is -1, this will just split 346// "forever". This doesn't support splitting more than 2^31 times 347// intentionally; if we ever want that we can make MaxSplit a 64-bit integer 348// but that seems unlikely to be useful. 349while (MaxSplit-- != 0) {
355if (KeepEmpty ||
Idx > 0)
363if (KeepEmpty || !S.
empty())
367//===----------------------------------------------------------------------===// 369//===----------------------------------------------------------------------===// 371/// count - Return the number of non-overlapped occurrences of \arg Str in 377// TODO: For an empty `Str` we return 0 for legacy reasons. Consider changing 378// this to `Length + 1` which is more in-line with the function 382while ((Pos =
find(Str, Pos)) !=
npos) {
393if (Str.consume_front_insensitive(
"0x"))
396if (Str.consume_front_insensitive(
"0b"))
399if (Str.consume_front(
"0o"))
402if (Str[0] ==
'0' && Str.size() > 1 &&
isDigit(Str[1])) {
411unsignedlonglong &Result) {
412// Autosense radix if not specified. 416// Empty strings (after the radix autosense) are invalid. 417if (Str.empty())
returntrue;
419// Parse all the bytes of the string given this radix. Watch for overflow. 422while (!Str2.
empty()) {
424if (Str2[0] >=
'0' && Str2[0] <=
'9')
425 CharVal = Str2[0] -
'0';
426elseif (Str2[0] >=
'a' && Str2[0] <=
'z')
427 CharVal = Str2[0] -
'a' + 10;
428elseif (Str2[0] >=
'A' && Str2[0] <=
'Z')
429 CharVal = Str2[0] -
'A' + 10;
433// If the parsed value is larger than the integer radix, we cannot 434// consume any more characters. 438// Add in this character. 439unsignedlonglong PrevResult = Result;
440 Result = Result * Radix + CharVal;
442// Check for overflow by shifting back and seeing if bits were lost. 443if (Result / Radix < PrevResult)
449// We consider the operation a failure if no characters were consumed 451if (Str.size() == Str2.
size())
460unsignedlonglong ULLVal;
462// Handle positive strings first. 463if (!Str.starts_with(
"-")) {
465// Check for value so large it overflows a signed value. 466 (
longlong)ULLVal < 0)
472// Get the positive part of the value. 475// Reject values so large they'd overflow as negative signed, but allow 476// "-0". This negates the unsigned so that the negative isn't undefined 477// on signed overflow. 478 (
longlong)-ULLVal > 0)
486/// GetAsUnsignedInteger - Workhorse method that converts a integer character 487/// sequence of radix up to 36 to an unsigned long long value. 489unsignedlonglong &Result) {
493// For getAsUnsignedInteger, we require the whole string to be consumed or 494// else we consider it a failure. 503// For getAsSignedInteger, we require the whole string to be consumed or else 504// we consider it a failure. 511// Autosense radix if not specified. 515assert(Radix > 1 && Radix <= 36);
517// Empty strings (after the radix autosense) are invalid. 518if (Str.empty())
returntrue;
520// Skip leading zeroes. This can be a significant improvement if 521// it means we don't need > 64 bits. 522 Str = Str.ltrim(
'0');
524// If it was nothing but zeroes.... 526 Result =
APInt(64, 0);
531// (Over-)estimate the required number of bits. 532unsigned Log2Radix = 0;
533while ((1U << Log2Radix) < Radix) Log2Radix++;
534bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
536unsignedBitWidth = Log2Radix * Str.size();
538BitWidth = Result.getBitWidth();
// don't shrink the result 539elseif (
BitWidth > Result.getBitWidth())
542APInt RadixAP, CharAP;
// unused unless !IsPowerOf2Radix 543if (!IsPowerOf2Radix) {
544// These must have the same bit-width as Result. 549// Parse all the bytes of the string given this radix. 551while (!Str.empty()) {
553if (Str[0] >=
'0' && Str[0] <=
'9')
554 CharVal = Str[0]-
'0';
555elseif (Str[0] >=
'a' && Str[0] <=
'z')
556 CharVal = Str[0]-
'a'+10;
557elseif (Str[0] >=
'A' && Str[0] <=
'Z')
558 CharVal = Str[0]-
'A'+10;
562// If the parsed value is larger than the integer radix, the string is 567// Add in this character. 568if (IsPowerOf2Radix) {
569 Result <<= Log2Radix;
580// We consider the operation a failure if no characters were consumed 582if (
size() == Str.size())
591if (Str.consumeInteger(Radix, Result))
594// For getAsInteger, we require the whole string to be consumed or else we 595// consider it a failure. 611 Result =
F.convertToDouble();
615// Implementation of StringRef hashing. 622"Cannot hash the empty key!");
624"Cannot hash the tombstone key!");
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
BlockVerifier::State From
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_UNLIKELY(EXPR)
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
static bool isDigit(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
static unsigned GetAutoSenseRadix(StringRef &Str)
static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length)
Class for arbitrary precision integers.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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.
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.
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.
bool getAsDouble(double &Result, bool AllowInexact=true) const
Parse the current string as an IEEE double-precision floating point value.
size_t find_if(function_ref< bool(char)> F, size_t From=0) const
Search for the first character satisfying the predicate F.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
bool starts_with_insensitive(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
std::string upper() const
Convert the given ASCII string to uppercase.
unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
Determine the edit distance between this string and another 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.
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).
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
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.
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
size_t rfind_insensitive(char C, size_t From=npos) const
Search for the last character C in the string, ignoring case.
std::string lower() const
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.
static constexpr size_t npos
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
bool ends_with_insensitive(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
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.
int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
unsigned edit_distance_insensitive(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
An opaque object representing a hash code.
This file defines a Levenshtein distance function that works for any two sequences,...
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
hash_code hash_value(const FixedPointSemantics &Val)
mapped_iterator< ItTy, FuncTy > map_iterator(ItTy I, FuncTy F)
bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
unsigned ComputeEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, bool AllowReplacements=true, unsigned MaxEditDistance=0)
bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)
constexpr unsigned BitWidth
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
unsigned ComputeMappedEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, Functor Map, bool AllowReplacements=true, unsigned MaxEditDistance=0)
Determine the edit distance between two sequences.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
static constexpr roundingMode rmNearestTiesToEven
opStatus
IEEE-754R 7: Default exception handling.
An information struct used to provide DenseMap with the various necessary components for a given valu...