LLVM  mainline
llvm::StringRef Class Reference

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

#include <StringRef.h>

Collaboration diagram for llvm::StringRef:
[legend]

List of all members.

## Public Types

typedef const char * iterator
typedef const char * const_iterator
typedef size_t size_type

## Public Member Functions

iterator begin () const
iterator end () const
const unsigned char * bytes_begin () const
const unsigned char * bytes_end () const
LLVM_ATTRIBUTE_ALWAYS_INLINE
const char *
data () const
data - Get a pointer to the start of the string (which may not be null terminated).
LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty () const
empty - Check if the string is empty.
LLVM_ATTRIBUTE_ALWAYS_INLINE 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
LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals (StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering of inequal strings isn't needed.
bool equals_lower (StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
LLVM_ATTRIBUTE_ALWAYS_INLINE int compare (StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less than, equal to, or greater than the RHS.
int compare_lower (StringRef RHS) const
compare_lower - 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.
std::string str () const
str - Get the contents as an std::string.
char operator[] (size_t Index) const
operator std::string () const
LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith (StringRef Prefix) const
Check if this string starts with the given Prefix.
bool startswith_lower (StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith (StringRef Suffix) const
Check if this string ends with the given Suffix.
bool endswith_lower (StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find (char C, size_t From=0) const
Search for the first character C in the string.
size_t find (StringRef Str, size_t From=0) const
Search for the first string Str in the string.
size_t rfind (char C, size_t From=npos) const
Search for the last character C in the string.
size_t rfind (StringRef Str) const
Search for the last string Str in 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.
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.
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 >
std::enable_if
< std::numeric_limits< T >
::is_signed, bool >::type
getAsInteger (unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
template<typename T >
std::enable_if
<!std::numeric_limits< T >
::is_signed, bool >::type
getAsInteger (unsigned Radix, T &Result) const
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.
std::string lower () const
std::string upper () const
Convert the given ASCII string to uppercase.
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringRef
substr (size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringRef
drop_front (size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringRef
drop_back (size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringRef
slice (size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
std::pair< StringRef, StringRefsplit (char Separator) const
Split into two substrings around the first occurrence of a separator character.
std::pair< StringRef, StringRefsplit (StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
void split (SmallVectorImpl< StringRef > &A, StringRef Separator, int MaxSplit=-1, bool KeepEmpty=true) const
Split into substrings around the occurrences of a separator string.
void split (SmallVectorImpl< StringRef > &A, char Separator, int MaxSplit=-1, bool KeepEmpty=true) const
Split into substrings around the occurrences of a separator character.
std::pair< StringRef, StringRefrsplit (char Separator) const
Split into two substrings around the last occurrence of a separator character.
StringRef ltrim (StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left removed.
StringRef rtrim (StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the right removed.
StringRef trim (StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left and right removed.
Constructors
StringRef ()
Construct an empty string ref.
StringRef (const char *Str)
Construct a string ref from a cstring.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef (const char *data, size_t length)
Construct a string ref from a pointer and length.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef (const std::string &Str)
Construct a string ref from an std::string.

## Static Public Attributes

static const size_t npos = ~size_t(0)

## Detailed Description

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

a character array and a length, which need not be null terminated.

This class does not own the string data, it is expected to be used in situations where the character data resides in some other buffer, whose lifetime extends past that of the StringRef. For this reason, it is not in general safe to store a StringRef.

Definition at line 41 of file StringRef.h.

## Member Typedef Documentation

 typedef const char* llvm::StringRef::const_iterator

Definition at line 44 of file StringRef.h.

 typedef const char* llvm::StringRef::iterator

Definition at line 43 of file StringRef.h.

 typedef size_t llvm::StringRef::size_type

Definition at line 46 of file StringRef.h.

## Constructor & Destructor Documentation

 llvm::StringRef::StringRef ( )  [inline]

Construct an empty string ref.

Definition at line 68 of file StringRef.h.

Referenced by drop_back(), and split().

 llvm::StringRef::StringRef ( const char * Str )  [inline]

Construct a string ref from a cstring.

Definition at line 71 of file StringRef.h.

 LLVM_ATTRIBUTE_ALWAYS_INLINE llvm::StringRef::StringRef ( const char * data, size_t length )  [inline]

Construct a string ref from a pointer and length.

Definition at line 79 of file StringRef.h.

 LLVM_ATTRIBUTE_ALWAYS_INLINE llvm::StringRef::StringRef ( const std::string & Str )  [inline]

Construct a string ref from an std::string.

Definition at line 87 of file StringRef.h.

## Member Function Documentation

 char llvm::StringRef::back ( ) const [inline]

back - Get the last character in the string.

Definition at line 125 of file StringRef.h.

 const unsigned char* llvm::StringRef::bytes_begin ( ) const [inline]

Definition at line 96 of file StringRef.h.

 const unsigned char* llvm::StringRef::bytes_end ( ) const [inline]

Definition at line 99 of file StringRef.h.

Referenced by llvm::MDString::bytes_end(), and WriteModuleMetadata().

 LLVM_ATTRIBUTE_ALWAYS_INLINE int llvm::StringRef::compare ( StringRef RHS ) const [inline]

compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less than, equal to, or greater than the RHS.

Definition at line 153 of file StringRef.h.

References fuzzer::min().

 int StringRef::compare_lower ( StringRef RHS ) const

compare_lower - Compare two strings, ignoring case.

compare_lower - Compare strings, ignoring case.

Definition at line 52 of file StringRef.cpp.

References ascii_strncasecmp(), and fuzzer::min().

Referenced by llvm::SmallString< 256 >::compare_numeric().

 int StringRef::compare_numeric ( StringRef RHS ) const

compare_numeric - Compare two strings, treating sequences of digits as numbers.

compare_numeric - Compare strings, handle embedded numbers.

Definition at line 73 of file StringRef.cpp.

References ascii_isdigit(), E, I, and fuzzer::min().

template<typename Allocator >
 StringRef llvm::StringRef::copy ( Allocator & A ) const [inline]

Definition at line 131 of file StringRef.h.

Referenced by llvm::yaml::Document::parseBlockNode().

 size_t llvm::StringRef::count ( char C ) const [inline]

Return the number of occurrences of C in the string.

Definition at line 324 of file StringRef.h.

 size_t StringRef::count ( StringRef Str ) const

Return the number of non-overlapped occurrences of Str in the string.

count - Return the number of non-overlapped occurrences of

• Str in the string.

Definition at line 342 of file StringRef.cpp.

References equals(), N, size(), and substr().

 LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef llvm::StringRef::drop_back ( size_t N = 1 ) const [inline]

Return a StringRef equal to 'this' but with the last N elements dropped.

Definition at line 416 of file StringRef.h.

References fuzzer::min(), N, and StringRef().

 LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef llvm::StringRef::drop_front ( size_t N = 1 ) const [inline]

Return a StringRef equal to 'this' but with the first N elements dropped.

Definition at line 408 of file StringRef.h.

 unsigned StringRef::edit_distance ( llvm::StringRef Other, bool AllowReplacements = true, unsigned MaxEditDistance = 0 ) const

Determine the edit distance between this string and another string.

Parameters:
 Other the string to compare this string against. AllowReplacements whether to allow character replacements (change one character into another) as a single operation, rather than as two operations (an insertion and a removal). MaxEditDistance If non-zero, the maximum edit distance that this routine is allowed to compute. If the edit distance will exceed that maximum, returns MaxEditDistance+1.
Returns:
the minimum number of character insertions, removals, or (if AllowReplacements is true) replacements needed to transform one of the given strings into the other. If zero, the strings are identical.

Definition at line 104 of file StringRef.cpp.

References llvm::ComputeEditDistance(), data(), llvm::makeArrayRef(), and size().

Referenced by LookupNearestOption().

 LLVM_ATTRIBUTE_ALWAYS_INLINE bool llvm::StringRef::endswith ( StringRef Suffix ) const [inline]

Check if this string ends with the given Suffix.

Definition at line 225 of file StringRef.h.

 bool StringRef::endswith_lower ( StringRef Suffix ) const

Check if this string ends with the given Suffix, ignoring case.

Definition at line 67 of file StringRef.cpp.

References ascii_strncasecmp(), and end().

 LLVM_ATTRIBUTE_ALWAYS_INLINE bool llvm::StringRef::equals ( StringRef RHS ) const [inline]

equals - Check for string equality, this is more efficient than compare() when the relative ordering of inequal strings isn't needed.

Definition at line 140 of file StringRef.h.

 bool llvm::StringRef::equals_lower ( StringRef RHS ) const [inline]

equals_lower - Check for string equality, ignoring case.

Definition at line 146 of file StringRef.h.

 LLVM_ATTRIBUTE_ALWAYS_INLINE size_t llvm::StringRef::find ( char C, size_t From = 0 ) const [inline]

Search for the first character C in the string.

Returns:
The index of the first occurrence of C, or npos if not found.

Definition at line 240 of file StringRef.h.

References P.

 size_t StringRef::find ( StringRef Str, size_t From = 0 ) const

Search for the first string Str in the string.

find - Search for the first string

Returns:
The index of the first occurrence of Str, or npos if not found.
• Str in the string.
Returns:
- The index of the first occurrence of
• Str, or npos if not found.

Definition at line 142 of file StringRef.cpp.

References data(), fuzzer::memcmp, N, npos, and size().

 StringRef::size_type StringRef::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.

find_first_not_of - Find the first character in the string that is not

• C or npos if not found.

Definition at line 219 of file StringRef.cpp.

References fuzzer::min(), and npos.

 StringRef::size_type StringRef::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.

find_first_not_of - Find the first character in the string that is not in the string

Complexity: O(size() + Chars.size())

• Chars, or npos if not found.

Note: O(size() + Chars.size())

Definition at line 230 of file StringRef.cpp.

References fuzzer::min(), npos, and size().

 size_t llvm::StringRef::find_first_of ( char C, size_t From = 0 ) const [inline]

Find the first character in the string that is C, or npos if not found.

Same as find.

Definition at line 279 of file StringRef.h.

 StringRef::size_type StringRef::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.

find_first_of - Find the first character in the string that is in

Complexity: O(size() + Chars.size())

• Chars, or npos if not found.

Note: O(size() + Chars.size())

Definition at line 205 of file StringRef.cpp.

References fuzzer::min(), npos, and size().

 StringRef::size_type StringRef::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.

find_last_not_of - Find the last character in the string that is not

• C, or npos if not found.

Definition at line 260 of file StringRef.cpp.

References fuzzer::min(), and npos.

 StringRef::size_type StringRef::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.

find_last_not_of - Find the last character in the string that is not in

Complexity: O(size() + Chars.size())

• Chars, or npos if not found.

Note: O(size() + Chars.size())

Definition at line 271 of file StringRef.cpp.

References fuzzer::min(), npos, and size().

 size_t llvm::StringRef::find_last_of ( char C, size_t From = npos ) const [inline]

Find the last character in the string that is C, or npos if not found.

Definition at line 301 of file StringRef.h.

 StringRef::size_type StringRef::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.

find_last_of - Find the last character in the string that is in

Complexity: O(size() + Chars.size())

• C, or npos if not found.

Note: O(size() + Chars.size())

Definition at line 246 of file StringRef.cpp.

References fuzzer::min(), npos, and size().

 char llvm::StringRef::front ( ) const [inline]

front - Get the first character in the string.

Definition at line 119 of file StringRef.h.

template<typename T >
 std::enable_if::is_signed, bool>::type llvm::StringRef::getAsInteger ( unsigned Radix, T & Result ) const [inline]

Parse the current string as an integer of the specified radix.

If Radix is specified as zero, this does radix autosensing using extended C rules: 0 is octal, 0x is hex, 0b is binary.

If the string is invalid or if only a subset of the string is valid, this returns true to signify the error. The string is considered erroneous if empty or if it overflows T.

Definition at line 345 of file StringRef.h.

template<typename T >
 std::enable_if::is_signed, bool>::type llvm::StringRef::getAsInteger ( unsigned Radix, T & Result ) const [inline]

Definition at line 356 of file StringRef.h.

References llvm::getAsSignedInteger().

 bool StringRef::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.

The current value in Result is discarded, and the storage is changed to be wide enough to store the parsed integer.

Returns:
true if the string does not solely consist of a valid non-empty number in the appropriate base.

APInt::fromString is superficially similar but assumes the string is well-formed in the given radix.

Definition at line 445 of file StringRef.cpp.

 StringRef llvm::StringRef::ltrim ( StringRef Chars = " \t\n\v\f\r" ) const [inline]

Return string with consecutive characters in Chars starting from the left removed.

Definition at line 526 of file StringRef.h.

 llvm::StringRef::operator std::string ( ) const [inline]

Definition at line 207 of file StringRef.h.

 char llvm::StringRef::operator[] ( size_t Index ) const [inline]

Definition at line 200 of file StringRef.h.

 size_t llvm::StringRef::rfind ( char C, size_t From = npos ) const [inline]

Search for the last character C in the string.

Returns:
The index of the last occurrence of C, or npos if not found.

Definition at line 260 of file StringRef.h.

 size_t StringRef::rfind ( StringRef Str ) const

Search for the last string Str in the string.

rfind - Search for the last string

Returns:
The index of the last occurrence of Str, or npos if not found.
• Str in the string.
Returns:
- The index of the last occurrence of
• Str, or npos if not found.

Definition at line 189 of file StringRef.cpp.

References equals(), N, npos, size(), and substr().

 std::pair llvm::StringRef::rsplit ( char Separator ) const [inline]

Split into two substrings around the last occurrence of a separator character.

If Separator is in the string, then the result is a pair (LHS, RHS) such that (*this == LHS + Separator + RHS) is true and RHS is minimal. If Separator is not in the string, then the result is a pair (LHS, RHS) where (*this == LHS) and (RHS == "").

Parameters:
 Separator - The character to split on.
Returns:
- The split substrings.

Definition at line 517 of file StringRef.h.

 StringRef llvm::StringRef::rtrim ( StringRef Chars = " \t\n\v\f\r" ) const [inline]

Return string with consecutive characters in Chars starting from the right removed.

Definition at line 532 of file StringRef.h.

References npos, and rfind().

 LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef llvm::StringRef::slice ( size_t Start, size_t End ) const [inline]

Return a reference to the substring from [Start, End).

Parameters:
 Start The index of the starting character in the substring; if the index is npos or greater than the length of the string then the empty substring will be returned. End The index following the last character to include in the substring. If this is npos, or less than Start, or exceeds the number of characters remaining in the string, the string suffix (starting with Start) will be returned.

Definition at line 432 of file StringRef.h.

References N, size(), and substr().

 std::pair llvm::StringRef::split ( char Separator ) const [inline]

Split into two substrings around the first occurrence of a separator character.

If Separator is in the string, then the result is a pair (LHS, RHS) such that (*this == LHS + Separator + RHS) is true and RHS is maximal. If Separator is not in the string, then the result is a pair (LHS, RHS) where (*this == LHS) and (RHS == "").

Parameters:
 Separator The character to split on.
Returns:
The split substrings.

Definition at line 448 of file StringRef.h.

References fuzzer::min(), and StringRef().

 std::pair llvm::StringRef::split ( StringRef Separator ) const [inline]

Split into two substrings around the first occurrence of a separator string.

If Separator is in the string, then the result is a pair (LHS, RHS) such that (*this == LHS + Separator + RHS) is true and RHS is maximal. If Separator is not in the string, then the result is a pair (LHS, RHS) where (*this == LHS) and (RHS == "").

Parameters:
 Separator - The string to split on.
Returns:
- The split substrings.

Definition at line 465 of file StringRef.h.

References find(), npos, slice(), and StringRef().

 void StringRef::split ( SmallVectorImpl< StringRef > & A, StringRef Separator, int MaxSplit = -1, bool KeepEmpty = true ) const

Split into substrings around the occurrences of a separator string.

Each substring is stored in A. If MaxSplit is >= 0, at most MaxSplit splits are done and consequently <= MaxSplit + 1 elements are added to A. If KeepEmpty is false, empty strings are not added to A. They still count when considering MaxSplit An useful invariant is that Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true

Parameters:
 A - Where to put the substrings. Separator - The string to split on. MaxSplit - The maximum number of times the string is split. KeepEmpty - True if empty substring should be added.

Definition at line 283 of file StringRef.cpp.

 void StringRef::split ( SmallVectorImpl< StringRef > & A, char Separator, int MaxSplit = -1, bool KeepEmpty = true ) const

Split into substrings around the occurrences of a separator character.

Each substring is stored in A. If MaxSplit is >= 0, at most MaxSplit splits are done and consequently <= MaxSplit + 1 elements are added to A. If KeepEmpty is false, empty strings are not added to A. They still count when considering MaxSplit An useful invariant is that Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true

Parameters:
 A - Where to put the substrings. Separator - The string to split on. MaxSplit - The maximum number of times the string is split. KeepEmpty - True if empty substring should be added.

Definition at line 310 of file StringRef.cpp.

 bool StringRef::startswith_lower ( StringRef Prefix ) const

Check if this string starts with the given Prefix, ignoring case.

Definition at line 61 of file StringRef.cpp.

References ascii_strncasecmp().

Referenced by matchOption().

 LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef llvm::StringRef::substr ( size_t Start, size_t N = npos ) const [inline]

Return a reference to the substring from [Start, Start + N).

Parameters:
 Start The index of the starting character in the substring; if the index is npos or greater than the length of the string then the empty substring will be returned. N The number of characters to included in the substring. If N exceeds the number of characters remaining in the string, the string suffix (starting with Start) will be returned.

Definition at line 400 of file StringRef.h.

 StringRef llvm::StringRef::trim ( StringRef Chars = " \t\n\v\f\r" ) const [inline]

Return string with consecutive characters in Chars starting from the left and right removed.

Definition at line 538 of file StringRef.h.

 std::string StringRef::upper ( ) const

Convert the given ASCII string to uppercase.

Definition at line 125 of file StringRef.cpp.

References ascii_toupper(), and size().

## Member Data Documentation

The documentation for this class was generated from the following files: