1198090Srdivacky//===--- StringRef.h - Constant String Reference Wrapper --------*- C++ -*-===// 2198090Srdivacky// 3198090Srdivacky// The LLVM Compiler Infrastructure 4198090Srdivacky// 5198090Srdivacky// This file is distributed under the University of Illinois Open Source 6198090Srdivacky// License. See LICENSE.TXT for details. 7198090Srdivacky// 8198090Srdivacky//===----------------------------------------------------------------------===// 9198090Srdivacky 10198090Srdivacky#ifndef LLVM_ADT_STRINGREF_H 11198090Srdivacky#define LLVM_ADT_STRINGREF_H 12198090Srdivacky 13235633Sdim#include "llvm/Support/type_traits.h" 14245431Sdim#include <algorithm> 15198090Srdivacky#include <cassert> 16198090Srdivacky#include <cstring> 17235633Sdim#include <limits> 18235633Sdim#include <string> 19199989Srdivacky#include <utility> 20198090Srdivacky 21198090Srdivackynamespace llvm { 22263509Sdim template <typename T> 23199481Srdivacky class SmallVectorImpl; 24204642Srdivacky class APInt; 25235633Sdim class hash_code; 26235633Sdim class StringRef; 27198090Srdivacky 28235633Sdim /// Helper functions for StringRef::getAsInteger. 29235633Sdim bool getAsUnsignedInteger(StringRef Str, unsigned Radix, 30235633Sdim unsigned long long &Result); 31235633Sdim 32235633Sdim bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result); 33235633Sdim 34198090Srdivacky /// StringRef - Represent a constant reference to a string, i.e. a character 35198090Srdivacky /// array and a length, which need not be null terminated. 36198090Srdivacky /// 37198090Srdivacky /// This class does not own the string data, it is expected to be used in 38198090Srdivacky /// situations where the character data resides in some other buffer, whose 39198090Srdivacky /// lifetime extends past that of the StringRef. For this reason, it is not in 40198090Srdivacky /// general safe to store a StringRef. 41198090Srdivacky class StringRef { 42198090Srdivacky public: 43198090Srdivacky typedef const char *iterator; 44202375Srdivacky typedef const char *const_iterator; 45198090Srdivacky static const size_t npos = ~size_t(0); 46198090Srdivacky typedef size_t size_type; 47198396Srdivacky 48198090Srdivacky private: 49198090Srdivacky /// The start of the string, in an external buffer. 50198090Srdivacky const char *Data; 51198090Srdivacky 52198090Srdivacky /// The length of the string. 53198090Srdivacky size_t Length; 54198090Srdivacky 55199989Srdivacky // Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min() 56199989Srdivacky // Changing the arg of min to be an integer, instead of a reference to an 57199989Srdivacky // integer works around this bug. 58207618Srdivacky static size_t min(size_t a, size_t b) { return a < b ? a : b; } 59207618Srdivacky static size_t max(size_t a, size_t b) { return a > b ? a : b; } 60252723Sdim 61223017Sdim // Workaround memcmp issue with null pointers (undefined behavior) 62223017Sdim // by providing a specialized version 63223017Sdim static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) { 64223017Sdim if (Length == 0) { return 0; } 65223017Sdim return ::memcmp(Lhs,Rhs,Length); 66223017Sdim } 67252723Sdim 68198090Srdivacky public: 69198090Srdivacky /// @name Constructors 70198090Srdivacky /// @{ 71198090Srdivacky 72198090Srdivacky /// Construct an empty string ref. 73198090Srdivacky /*implicit*/ StringRef() : Data(0), Length(0) {} 74198090Srdivacky 75198090Srdivacky /// Construct a string ref from a cstring. 76198396Srdivacky /*implicit*/ StringRef(const char *Str) 77223017Sdim : Data(Str) { 78223017Sdim assert(Str && "StringRef cannot be built from a NULL argument"); 79223017Sdim Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior 80223017Sdim } 81198396Srdivacky 82198090Srdivacky /// Construct a string ref from a pointer and length. 83198396Srdivacky /*implicit*/ StringRef(const char *data, size_t length) 84223017Sdim : Data(data), Length(length) { 85223017Sdim assert((data || length == 0) && 86223017Sdim "StringRef cannot be built from a NULL argument with non-null length"); 87223017Sdim } 88198090Srdivacky 89198090Srdivacky /// Construct a string ref from an std::string. 90198396Srdivacky /*implicit*/ StringRef(const std::string &Str) 91199481Srdivacky : Data(Str.data()), Length(Str.length()) {} 92198090Srdivacky 93198090Srdivacky /// @} 94198090Srdivacky /// @name Iterators 95198090Srdivacky /// @{ 96198090Srdivacky 97198090Srdivacky iterator begin() const { return Data; } 98198090Srdivacky 99198090Srdivacky iterator end() const { return Data + Length; } 100198090Srdivacky 101198090Srdivacky /// @} 102198090Srdivacky /// @name String Operations 103198090Srdivacky /// @{ 104198090Srdivacky 105198090Srdivacky /// data - Get a pointer to the start of the string (which may not be null 106198090Srdivacky /// terminated). 107198090Srdivacky const char *data() const { return Data; } 108198090Srdivacky 109198090Srdivacky /// empty - Check if the string is empty. 110198090Srdivacky bool empty() const { return Length == 0; } 111198090Srdivacky 112198090Srdivacky /// size - Get the string size. 113198090Srdivacky size_t size() const { return Length; } 114198090Srdivacky 115198090Srdivacky /// front - Get the first character in the string. 116198090Srdivacky char front() const { 117198090Srdivacky assert(!empty()); 118198090Srdivacky return Data[0]; 119198090Srdivacky } 120198396Srdivacky 121198090Srdivacky /// back - Get the last character in the string. 122198090Srdivacky char back() const { 123198090Srdivacky assert(!empty()); 124198090Srdivacky return Data[Length-1]; 125198090Srdivacky } 126198090Srdivacky 127198090Srdivacky /// equals - Check for string equality, this is more efficient than 128198090Srdivacky /// compare() when the relative ordering of inequal strings isn't needed. 129199481Srdivacky bool equals(StringRef RHS) const { 130198396Srdivacky return (Length == RHS.Length && 131223017Sdim compareMemory(Data, RHS.Data, RHS.Length) == 0); 132198090Srdivacky } 133198090Srdivacky 134199481Srdivacky /// equals_lower - Check for string equality, ignoring case. 135199481Srdivacky bool equals_lower(StringRef RHS) const { 136199481Srdivacky return Length == RHS.Length && compare_lower(RHS) == 0; 137199481Srdivacky } 138199481Srdivacky 139198090Srdivacky /// compare - Compare two strings; the result is -1, 0, or 1 if this string 140245431Sdim /// is lexicographically less than, equal to, or greater than the \p RHS. 141199481Srdivacky int compare(StringRef RHS) const { 142198090Srdivacky // Check the prefix for a mismatch. 143223017Sdim if (int Res = compareMemory(Data, RHS.Data, min(Length, RHS.Length))) 144198090Srdivacky return Res < 0 ? -1 : 1; 145198090Srdivacky 146198090Srdivacky // Otherwise the prefixes match, so we only need to check the lengths. 147198090Srdivacky if (Length == RHS.Length) 148198090Srdivacky return 0; 149198090Srdivacky return Length < RHS.Length ? -1 : 1; 150198090Srdivacky } 151198090Srdivacky 152199481Srdivacky /// compare_lower - Compare two strings, ignoring case. 153199481Srdivacky int compare_lower(StringRef RHS) const; 154199481Srdivacky 155208599Srdivacky /// compare_numeric - Compare two strings, treating sequences of digits as 156208599Srdivacky /// numbers. 157208599Srdivacky int compare_numeric(StringRef RHS) const; 158208599Srdivacky 159218893Sdim /// \brief Determine the edit distance between this string and another 160201360Srdivacky /// string. 161201360Srdivacky /// 162201360Srdivacky /// \param Other the string to compare this string against. 163201360Srdivacky /// 164201360Srdivacky /// \param AllowReplacements whether to allow character 165201360Srdivacky /// replacements (change one character into another) as a single 166201360Srdivacky /// operation, rather than as two operations (an insertion and a 167201360Srdivacky /// removal). 168201360Srdivacky /// 169218893Sdim /// \param MaxEditDistance If non-zero, the maximum edit distance that 170218893Sdim /// this routine is allowed to compute. If the edit distance will exceed 171218893Sdim /// that maximum, returns \c MaxEditDistance+1. 172218893Sdim /// 173201360Srdivacky /// \returns the minimum number of character insertions, removals, 174201360Srdivacky /// or (if \p AllowReplacements is \c true) replacements needed to 175201360Srdivacky /// transform one of the given strings into the other. If zero, 176201360Srdivacky /// the strings are identical. 177218893Sdim unsigned edit_distance(StringRef Other, bool AllowReplacements = true, 178263509Sdim unsigned MaxEditDistance = 0) const; 179201360Srdivacky 180198090Srdivacky /// str - Get the contents as an std::string. 181212904Sdim std::string str() const { 182212904Sdim if (Data == 0) return std::string(); 183212904Sdim return std::string(Data, Length); 184212904Sdim } 185198090Srdivacky 186198090Srdivacky /// @} 187198090Srdivacky /// @name Operator Overloads 188198090Srdivacky /// @{ 189198090Srdivacky 190198396Srdivacky char operator[](size_t Index) const { 191198090Srdivacky assert(Index < Length && "Invalid index!"); 192198396Srdivacky return Data[Index]; 193198090Srdivacky } 194198090Srdivacky 195198090Srdivacky /// @} 196198090Srdivacky /// @name Type Conversions 197198090Srdivacky /// @{ 198198090Srdivacky 199198090Srdivacky operator std::string() const { 200198090Srdivacky return str(); 201198090Srdivacky } 202198090Srdivacky 203198090Srdivacky /// @} 204198090Srdivacky /// @name String Predicates 205198090Srdivacky /// @{ 206198090Srdivacky 207245431Sdim /// Check if this string starts with the given \p Prefix. 208199481Srdivacky bool startswith(StringRef Prefix) const { 209201360Srdivacky return Length >= Prefix.Length && 210223017Sdim compareMemory(Data, Prefix.Data, Prefix.Length) == 0; 211198090Srdivacky } 212198090Srdivacky 213263509Sdim /// Check if this string starts with the given \p Prefix, ignoring case. 214263509Sdim bool startswith_lower(StringRef Prefix) const; 215263509Sdim 216245431Sdim /// Check if this string ends with the given \p Suffix. 217199481Srdivacky bool endswith(StringRef Suffix) const { 218201360Srdivacky return Length >= Suffix.Length && 219223017Sdim compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0; 220198090Srdivacky } 221198090Srdivacky 222263509Sdim /// Check if this string ends with the given \p Suffix, ignoring case. 223263509Sdim bool endswith_lower(StringRef Suffix) const; 224263509Sdim 225198090Srdivacky /// @} 226198090Srdivacky /// @name String Searching 227198090Srdivacky /// @{ 228198090Srdivacky 229245431Sdim /// Search for the first character \p C in the string. 230198090Srdivacky /// 231245431Sdim /// \returns The index of the first occurrence of \p C, or npos if not 232198090Srdivacky /// found. 233199481Srdivacky size_t find(char C, size_t From = 0) const { 234199989Srdivacky for (size_t i = min(From, Length), e = Length; i != e; ++i) 235198090Srdivacky if (Data[i] == C) 236198090Srdivacky return i; 237198090Srdivacky return npos; 238198090Srdivacky } 239198090Srdivacky 240245431Sdim /// Search for the first string \p Str in the string. 241198090Srdivacky /// 242245431Sdim /// \returns The index of the first occurrence of \p Str, or npos if not 243198090Srdivacky /// found. 244199481Srdivacky size_t find(StringRef Str, size_t From = 0) const; 245198396Srdivacky 246245431Sdim /// Search for the last character \p C in the string. 247198090Srdivacky /// 248245431Sdim /// \returns The index of the last occurrence of \p C, or npos if not 249198090Srdivacky /// found. 250198090Srdivacky size_t rfind(char C, size_t From = npos) const { 251199989Srdivacky From = min(From, Length); 252198090Srdivacky size_t i = From; 253198090Srdivacky while (i != 0) { 254198090Srdivacky --i; 255198090Srdivacky if (Data[i] == C) 256198090Srdivacky return i; 257198090Srdivacky } 258198090Srdivacky return npos; 259198090Srdivacky } 260198396Srdivacky 261245431Sdim /// Search for the last string \p Str in the string. 262198090Srdivacky /// 263245431Sdim /// \returns The index of the last occurrence of \p Str, or npos if not 264198090Srdivacky /// found. 265199481Srdivacky size_t rfind(StringRef Str) const; 266198396Srdivacky 267245431Sdim /// Find the first character in the string that is \p C, or npos if not 268245431Sdim /// found. Same as find. 269252723Sdim size_t find_first_of(char C, size_t From = 0) const { 270212904Sdim return find(C, From); 271212904Sdim } 272198396Srdivacky 273245431Sdim /// Find the first character in the string that is in \p Chars, or npos if 274245431Sdim /// not found. 275199481Srdivacky /// 276245431Sdim /// Complexity: O(size() + Chars.size()) 277252723Sdim size_t find_first_of(StringRef Chars, size_t From = 0) const; 278198396Srdivacky 279245431Sdim /// Find the first character in the string that is not \p C or npos if not 280245431Sdim /// found. 281252723Sdim size_t find_first_not_of(char C, size_t From = 0) const; 282198396Srdivacky 283245431Sdim /// Find the first character in the string that is not in the string 284245431Sdim /// \p Chars, or npos if not found. 285199481Srdivacky /// 286245431Sdim /// Complexity: O(size() + Chars.size()) 287252723Sdim size_t find_first_not_of(StringRef Chars, size_t From = 0) const; 288199481Srdivacky 289245431Sdim /// Find the last character in the string that is \p C, or npos if not 290245431Sdim /// found. 291252723Sdim size_t find_last_of(char C, size_t From = npos) const { 292218893Sdim return rfind(C, From); 293218893Sdim } 294218893Sdim 295245431Sdim /// Find the last character in the string that is in \p C, or npos if not 296245431Sdim /// found. 297218893Sdim /// 298245431Sdim /// Complexity: O(size() + Chars.size()) 299252723Sdim size_t find_last_of(StringRef Chars, size_t From = npos) const; 300218893Sdim 301245431Sdim /// Find the last character in the string that is not \p C, or npos if not 302245431Sdim /// found. 303252723Sdim size_t find_last_not_of(char C, size_t From = npos) const; 304245431Sdim 305245431Sdim /// Find the last character in the string that is not in \p Chars, or 306245431Sdim /// npos if not found. 307245431Sdim /// 308245431Sdim /// Complexity: O(size() + Chars.size()) 309252723Sdim size_t find_last_not_of(StringRef Chars, size_t From = npos) const; 310245431Sdim 311198090Srdivacky /// @} 312198090Srdivacky /// @name Helpful Algorithms 313198090Srdivacky /// @{ 314198396Srdivacky 315245431Sdim /// Return the number of occurrences of \p C in the string. 316198090Srdivacky size_t count(char C) const { 317198090Srdivacky size_t Count = 0; 318198090Srdivacky for (size_t i = 0, e = Length; i != e; ++i) 319198090Srdivacky if (Data[i] == C) 320198090Srdivacky ++Count; 321198090Srdivacky return Count; 322198090Srdivacky } 323198396Srdivacky 324245431Sdim /// Return the number of non-overlapped occurrences of \p Str in 325198090Srdivacky /// the string. 326199481Srdivacky size_t count(StringRef Str) const; 327198396Srdivacky 328245431Sdim /// Parse the current string as an integer of the specified radix. If 329245431Sdim /// \p Radix is specified as zero, this does radix autosensing using 330198090Srdivacky /// extended C rules: 0 is octal, 0x is hex, 0b is binary. 331198090Srdivacky /// 332198090Srdivacky /// If the string is invalid or if only a subset of the string is valid, 333198090Srdivacky /// this returns true to signify the error. The string is considered 334235633Sdim /// erroneous if empty or if it overflows T. 335235633Sdim template <typename T> 336235633Sdim typename enable_if_c<std::numeric_limits<T>::is_signed, bool>::type 337235633Sdim getAsInteger(unsigned Radix, T &Result) const { 338235633Sdim long long LLVal; 339235633Sdim if (getAsSignedInteger(*this, Radix, LLVal) || 340235633Sdim static_cast<T>(LLVal) != LLVal) 341235633Sdim return true; 342235633Sdim Result = LLVal; 343235633Sdim return false; 344235633Sdim } 345198090Srdivacky 346235633Sdim template <typename T> 347235633Sdim typename enable_if_c<!std::numeric_limits<T>::is_signed, bool>::type 348235633Sdim getAsInteger(unsigned Radix, T &Result) const { 349235633Sdim unsigned long long ULLVal; 350235633Sdim if (getAsUnsignedInteger(*this, Radix, ULLVal) || 351235633Sdim static_cast<T>(ULLVal) != ULLVal) 352235633Sdim return true; 353235633Sdim Result = ULLVal; 354235633Sdim return false; 355235633Sdim } 356198396Srdivacky 357245431Sdim /// Parse the current string as an integer of the specified \p Radix, or of 358245431Sdim /// an autosensed radix if the \p Radix given is 0. The current value in 359245431Sdim /// \p Result is discarded, and the storage is changed to be wide enough to 360245431Sdim /// store the parsed integer. 361204642Srdivacky /// 362245431Sdim /// \returns true if the string does not solely consist of a valid 363204642Srdivacky /// non-empty number in the appropriate base. 364204642Srdivacky /// 365204642Srdivacky /// APInt::fromString is superficially similar but assumes the 366204642Srdivacky /// string is well-formed in the given radix. 367204642Srdivacky bool getAsInteger(unsigned Radix, APInt &Result) const; 368204642Srdivacky 369198090Srdivacky /// @} 370235633Sdim /// @name String Operations 371235633Sdim /// @{ 372235633Sdim 373245431Sdim // Convert the given ASCII string to lowercase. 374235633Sdim std::string lower() const; 375235633Sdim 376245431Sdim /// Convert the given ASCII string to uppercase. 377235633Sdim std::string upper() const; 378235633Sdim 379235633Sdim /// @} 380198090Srdivacky /// @name Substring Operations 381198090Srdivacky /// @{ 382198090Srdivacky 383245431Sdim /// Return a reference to the substring from [Start, Start + N). 384198090Srdivacky /// 385245431Sdim /// \param Start The index of the starting character in the substring; if 386198090Srdivacky /// the index is npos or greater than the length of the string then the 387198090Srdivacky /// empty substring will be returned. 388198090Srdivacky /// 389245431Sdim /// \param N The number of characters to included in the substring. If N 390198090Srdivacky /// exceeds the number of characters remaining in the string, the string 391245431Sdim /// suffix (starting with \p Start) will be returned. 392198090Srdivacky StringRef substr(size_t Start, size_t N = npos) const { 393199989Srdivacky Start = min(Start, Length); 394199989Srdivacky return StringRef(Data + Start, min(N, Length - Start)); 395198090Srdivacky } 396252723Sdim 397245431Sdim /// Return a StringRef equal to 'this' but with the first \p N elements 398245431Sdim /// dropped. 399252723Sdim StringRef drop_front(size_t N = 1) const { 400235633Sdim assert(size() >= N && "Dropping more elements than exist"); 401235633Sdim return substr(N); 402235633Sdim } 403198090Srdivacky 404245431Sdim /// Return a StringRef equal to 'this' but with the last \p N elements 405245431Sdim /// dropped. 406252723Sdim StringRef drop_back(size_t N = 1) const { 407235633Sdim assert(size() >= N && "Dropping more elements than exist"); 408235633Sdim return substr(0, size()-N); 409235633Sdim } 410235633Sdim 411245431Sdim /// Return a reference to the substring from [Start, End). 412198090Srdivacky /// 413245431Sdim /// \param Start The index of the starting character in the substring; if 414198090Srdivacky /// the index is npos or greater than the length of the string then the 415198090Srdivacky /// empty substring will be returned. 416198090Srdivacky /// 417245431Sdim /// \param End The index following the last character to include in the 418245431Sdim /// substring. If this is npos, or less than \p Start, or exceeds the 419198090Srdivacky /// number of characters remaining in the string, the string suffix 420245431Sdim /// (starting with \p Start) will be returned. 421198090Srdivacky StringRef slice(size_t Start, size_t End) const { 422199989Srdivacky Start = min(Start, Length); 423199989Srdivacky End = min(max(Start, End), Length); 424198090Srdivacky return StringRef(Data + Start, End - Start); 425198090Srdivacky } 426198090Srdivacky 427245431Sdim /// Split into two substrings around the first occurrence of a separator 428245431Sdim /// character. 429198090Srdivacky /// 430245431Sdim /// If \p Separator is in the string, then the result is a pair (LHS, RHS) 431198090Srdivacky /// such that (*this == LHS + Separator + RHS) is true and RHS is 432245431Sdim /// maximal. If \p Separator is not in the string, then the result is a 433198090Srdivacky /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). 434198090Srdivacky /// 435245431Sdim /// \param Separator The character to split on. 436245431Sdim /// \returns The split substrings. 437198090Srdivacky std::pair<StringRef, StringRef> split(char Separator) const { 438198090Srdivacky size_t Idx = find(Separator); 439198090Srdivacky if (Idx == npos) 440198090Srdivacky return std::make_pair(*this, StringRef()); 441198090Srdivacky return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); 442198090Srdivacky } 443198090Srdivacky 444245431Sdim /// Split into two substrings around the first occurrence of a separator 445245431Sdim /// string. 446199481Srdivacky /// 447245431Sdim /// If \p Separator is in the string, then the result is a pair (LHS, RHS) 448199481Srdivacky /// such that (*this == LHS + Separator + RHS) is true and RHS is 449245431Sdim /// maximal. If \p Separator is not in the string, then the result is a 450199481Srdivacky /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). 451199481Srdivacky /// 452199481Srdivacky /// \param Separator - The string to split on. 453199481Srdivacky /// \return - The split substrings. 454199481Srdivacky std::pair<StringRef, StringRef> split(StringRef Separator) const { 455199481Srdivacky size_t Idx = find(Separator); 456199481Srdivacky if (Idx == npos) 457199481Srdivacky return std::make_pair(*this, StringRef()); 458199481Srdivacky return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos)); 459199481Srdivacky } 460199481Srdivacky 461245431Sdim /// Split into substrings around the occurrences of a separator string. 462199481Srdivacky /// 463245431Sdim /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most 464245431Sdim /// \p MaxSplit splits are done and consequently <= \p MaxSplit 465199481Srdivacky /// elements are added to A. 466245431Sdim /// If \p KeepEmpty is false, empty strings are not added to \p A. They 467245431Sdim /// still count when considering \p MaxSplit 468199481Srdivacky /// An useful invariant is that 469199481Srdivacky /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true 470199481Srdivacky /// 471199481Srdivacky /// \param A - Where to put the substrings. 472199481Srdivacky /// \param Separator - The string to split on. 473199481Srdivacky /// \param MaxSplit - The maximum number of times the string is split. 474204642Srdivacky /// \param KeepEmpty - True if empty substring should be added. 475199481Srdivacky void split(SmallVectorImpl<StringRef> &A, 476199481Srdivacky StringRef Separator, int MaxSplit = -1, 477199481Srdivacky bool KeepEmpty = true) const; 478199481Srdivacky 479245431Sdim /// Split into two substrings around the last occurrence of a separator 480245431Sdim /// character. 481198090Srdivacky /// 482245431Sdim /// If \p Separator is in the string, then the result is a pair (LHS, RHS) 483198090Srdivacky /// such that (*this == LHS + Separator + RHS) is true and RHS is 484245431Sdim /// minimal. If \p Separator is not in the string, then the result is a 485198090Srdivacky /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). 486198090Srdivacky /// 487198090Srdivacky /// \param Separator - The character to split on. 488198090Srdivacky /// \return - The split substrings. 489198090Srdivacky std::pair<StringRef, StringRef> rsplit(char Separator) const { 490198090Srdivacky size_t Idx = rfind(Separator); 491198090Srdivacky if (Idx == npos) 492198090Srdivacky return std::make_pair(*this, StringRef()); 493198090Srdivacky return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); 494198090Srdivacky } 495198090Srdivacky 496245431Sdim /// Return string with consecutive characters in \p Chars starting from 497245431Sdim /// the left removed. 498245431Sdim StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const { 499245431Sdim return drop_front(std::min(Length, find_first_not_of(Chars))); 500245431Sdim } 501245431Sdim 502245431Sdim /// Return string with consecutive characters in \p Chars starting from 503245431Sdim /// the right removed. 504245431Sdim StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const { 505245431Sdim return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1)); 506245431Sdim } 507245431Sdim 508245431Sdim /// Return string with consecutive characters in \p Chars starting from 509245431Sdim /// the left and right removed. 510245431Sdim StringRef trim(StringRef Chars = " \t\n\v\f\r") const { 511245431Sdim return ltrim(Chars).rtrim(Chars); 512245431Sdim } 513245431Sdim 514198090Srdivacky /// @} 515198090Srdivacky }; 516198090Srdivacky 517198090Srdivacky /// @name StringRef Comparison Operators 518198090Srdivacky /// @{ 519198090Srdivacky 520199481Srdivacky inline bool operator==(StringRef LHS, StringRef RHS) { 521198090Srdivacky return LHS.equals(RHS); 522198090Srdivacky } 523198090Srdivacky 524199481Srdivacky inline bool operator!=(StringRef LHS, StringRef RHS) { 525198090Srdivacky return !(LHS == RHS); 526198090Srdivacky } 527198396Srdivacky 528199481Srdivacky inline bool operator<(StringRef LHS, StringRef RHS) { 529198396Srdivacky return LHS.compare(RHS) == -1; 530198090Srdivacky } 531198090Srdivacky 532199481Srdivacky inline bool operator<=(StringRef LHS, StringRef RHS) { 533198396Srdivacky return LHS.compare(RHS) != 1; 534198090Srdivacky } 535198090Srdivacky 536199481Srdivacky inline bool operator>(StringRef LHS, StringRef RHS) { 537198396Srdivacky return LHS.compare(RHS) == 1; 538198090Srdivacky } 539198090Srdivacky 540199481Srdivacky inline bool operator>=(StringRef LHS, StringRef RHS) { 541198396Srdivacky return LHS.compare(RHS) != -1; 542198090Srdivacky } 543198090Srdivacky 544252723Sdim inline std::string &operator+=(std::string &buffer, StringRef string) { 545223017Sdim return buffer.append(string.data(), string.size()); 546223017Sdim } 547223017Sdim 548198090Srdivacky /// @} 549198090Srdivacky 550235633Sdim /// \brief Compute a hash_code for a StringRef. 551235633Sdim hash_code hash_value(StringRef S); 552235633Sdim 553218893Sdim // StringRefs can be treated like a POD type. 554218893Sdim template <typename T> struct isPodLike; 555218893Sdim template <> struct isPodLike<StringRef> { static const bool value = true; }; 556218893Sdim 557263509Sdim /// Construct a string ref from a boolean. 558263509Sdim inline StringRef toStringRef(bool B) { 559263509Sdim return StringRef(B ? "true" : "false"); 560263509Sdim } 561198090Srdivacky} 562198090Srdivacky 563198090Srdivacky#endif 564