LLVM 20.0.0git
Public Types | Static Public Attributes | List of all members
llvm::StringRef Class Reference

StringRef - Represent a constant reference to a string, i.e. More...

#include "llvm/ADT/StringRef.h"

Inheritance diagram for llvm::StringRef:
Inheritance graph
[legend]

Public Types

using iterator = const char *
 
using const_iterator = const char *
 
using size_type = size_t
 
using value_type = char
 
using reverse_iterator = std::reverse_iterator< iterator >
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 

Public Member Functions

Constructors
 StringRef ()=default
 Construct an empty string ref.
 
 StringRef (std::nullptr_t)=delete
 Disable conversion from nullptr.
 
constexpr StringRef (const char *Str LLVM_LIFETIME_BOUND)
 Construct a string ref from a cstring.
 
constexpr StringRef (const char *data LLVM_LIFETIME_BOUND, size_t length)
 Construct a string ref from a pointer and length.
 
 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.
 
Iterators
iterator begin () const
 
iterator end () const
 
reverse_iterator rbegin () const
 
reverse_iterator rend () const
 
const unsigned charbytes_begin () const
 
const unsigned charbytes_end () const
 
iterator_range< const unsigned char * > bytes () const
 
String Operations
constexpr const chardata () const
 data - Get a pointer to the start of the string (which may not be null terminated).
 
constexpr bool empty () const
 empty - Check if the string is empty.
 
constexpr size_t size () const
 size - Get the string size.
 
char front () const
 front - Get the first character in the string.
 
char back () const
 back - Get the last character in the string.
 
template<typename Allocator >
StringRef copy (Allocator &A) const
 
bool equals_insensitive (StringRef RHS) const
 Check for string equality, ignoring case.
 
int compare (StringRef RHS) const
 compare - Compare two strings; the result is negative, zero, or positive if this string is lexicographically less than, equal to, or greater than the RHS.
 
int compare_insensitive (StringRef RHS) const
 Compare two strings, ignoring case.
 
int compare_numeric (StringRef RHS) const
 compare_numeric - Compare two strings, treating sequences of digits as numbers.
 
unsigned edit_distance (StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
 Determine the edit distance between this string and another string.
 
unsigned edit_distance_insensitive (StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
 
std::string str () const
 str - Get the contents as an std::string.
 
std::string lower () const
 
std::string upper () const
 Convert the given ASCII string to uppercase.
 
Operator Overloads
char operator[] (size_t Index) const
 
template<typename T >
std::enable_if_t< std::is_same< T, std::string >::value, StringRef > & operator= (T &&Str)=delete
 Disallow accidental assignment from a temporary std::string.
 
Type Conversions
constexpr operator std::string_view () const
 
String Predicates
bool starts_with (StringRef Prefix) const
 Check if this string starts with the given Prefix.
 
bool starts_with (char Prefix) const
 
bool starts_with_insensitive (StringRef Prefix) const
 Check if this string starts with the given Prefix, ignoring case.
 
bool ends_with (StringRef Suffix) const
 Check if this string ends with the given Suffix.
 
bool ends_with (char Suffix) const
 
bool ends_with_insensitive (StringRef Suffix) const
 Check if this string ends with the given Suffix, ignoring case.
 
String Searching
size_t find (char C, size_t From=0) const
 Search for the first character C in the string.
 
size_t find_insensitive (char C, size_t From=0) const
 Search for the first character C in the string, ignoring case.
 
size_t find_if (function_ref< bool(char)> F, size_t From=0) const
 Search for the first character satisfying the predicate F.
 
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.
 
size_t find (StringRef Str, size_t From=0) const
 Search for the first string Str in the string.
 
size_t find_insensitive (StringRef Str, size_t From=0) const
 Search for the first string Str in the string, ignoring case.
 
size_t rfind (char C, size_t From=npos) const
 Search for the last 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.
 
size_t rfind (StringRef Str) const
 Search for the last string Str in the string.
 
size_t rfind_insensitive (StringRef Str) const
 Search for the last string Str in the string, ignoring case.
 
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 find_first_of (StringRef Chars, size_t From=0) const
 Find the first character in the string that is in Chars, or npos if not found.
 
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.
 
size_t find_first_not_of (StringRef Chars, size_t From=0) const
 Find the first character in the string that is not in the string Chars, or npos if not found.
 
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.
 
size_t find_last_of (StringRef Chars, size_t From=npos) const
 Find the last character in the string that is in C, or npos if not found.
 
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.
 
size_t find_last_not_of (StringRef Chars, size_t From=npos) const
 Find the last character in the string that is not in Chars, or npos if not found.
 
bool contains (StringRef Other) const
 Return true if the given string is a substring of *this, and false otherwise.
 
bool contains (char C) const
 Return true if the given character is contained in *this, and false otherwise.
 
bool contains_insensitive (StringRef Other) const
 Return true if the given string is a substring of *this, and false otherwise.
 
bool contains_insensitive (char C) const
 Return true if the given character is contained in *this, and false otherwise.
 
Helpful Algorithms
size_t count (char C) const
 Return the number of occurrences of C in the string.
 
size_t count (StringRef Str) const
 Return the number of non-overlapped occurrences of Str in the string.
 
template<typename T >
bool getAsInteger (unsigned Radix, T &Result) const
 Parse the current string as an integer of the specified radix.
 
template<typename T >
bool consumeInteger (unsigned Radix, T &Result)
 Parse the current string as an integer of the specified radix.
 
bool getAsInteger (unsigned Radix, APInt &Result) const
 Parse the current string as an integer of the specified Radix, or of an autosensed radix if the Radix given is 0.
 
bool consumeInteger (unsigned Radix, APInt &Result)
 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.
 
Substring Operations
constexpr StringRef substr (size_t Start, size_t N=npos) const
 Return a reference to the substring from [Start, Start + N).
 
StringRef take_front (size_t N=1) const
 Return a StringRef equal to 'this' but with only the first N elements remaining.
 
StringRef take_back (size_t N=1) const
 Return a StringRef equal to 'this' but with only the last N elements remaining.
 
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 predicate.
 
StringRef take_until (function_ref< bool(