LLVM  mainline
StringRef.h
Go to the documentation of this file.
00001 //===--- StringRef.h - Constant String Reference Wrapper --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_ADT_STRINGREF_H
00011 #define LLVM_ADT_STRINGREF_H
00012 
00013 #include "llvm/Support/Compiler.h"
00014 #include <algorithm>
00015 #include <cassert>
00016 #include <cstring>
00017 #include <limits>
00018 #include <string>
00019 #include <utility>
00020 
00021 namespace llvm {
00022   template <typename T>
00023   class SmallVectorImpl;
00024   class APInt;
00025   class hash_code;
00026   class StringRef;
00027 
00028   /// Helper functions for StringRef::getAsInteger.
00029   bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
00030                             unsigned long long &Result);
00031 
00032   bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
00033 
00034   /// StringRef - Represent a constant reference to a string, i.e. a character
00035   /// array and a length, which need not be null terminated.
00036   ///
00037   /// This class does not own the string data, it is expected to be used in
00038   /// situations where the character data resides in some other buffer, whose
00039   /// lifetime extends past that of the StringRef. For this reason, it is not in
00040   /// general safe to store a StringRef.
00041   class StringRef {
00042   public:
00043     typedef const char *iterator;
00044     typedef const char *const_iterator;
00045     static const size_t npos = ~size_t(0);
00046     typedef size_t size_type;
00047 
00048   private:
00049     /// The start of the string, in an external buffer.
00050     const char *Data;
00051 
00052     /// The length of the string.
00053     size_t Length;
00054 
00055     // Workaround memcmp issue with null pointers (undefined behavior)
00056     // by providing a specialized version
00057     LLVM_ATTRIBUTE_ALWAYS_INLINE
00058     static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
00059       if (Length == 0) { return 0; }
00060       return ::memcmp(Lhs,Rhs,Length);
00061     }
00062 
00063   public:
00064     /// @name Constructors
00065     /// @{
00066 
00067     /// Construct an empty string ref.
00068     /*implicit*/ StringRef() : Data(nullptr), Length(0) {}
00069 
00070     /// Construct a string ref from a cstring.
00071     /*implicit*/ StringRef(const char *Str)
00072       : Data(Str) {
00073         assert(Str && "StringRef cannot be built from a NULL argument");
00074         Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
00075       }
00076 
00077     /// Construct a string ref from a pointer and length.
00078     LLVM_ATTRIBUTE_ALWAYS_INLINE
00079     /*implicit*/ StringRef(const char *data, size_t length)
00080       : Data(data), Length(length) {
00081         assert((data || length == 0) &&
00082         "StringRef cannot be built from a NULL argument with non-null length");
00083       }
00084 
00085     /// Construct a string ref from an std::string.
00086     LLVM_ATTRIBUTE_ALWAYS_INLINE
00087     /*implicit*/ StringRef(const std::string &Str)
00088       : Data(Str.data()), Length(Str.length()) {}
00089 
00090     /// @}
00091     /// @name Iterators
00092     /// @{
00093 
00094     iterator begin() const { return Data; }
00095 
00096     iterator end() const { return Data + Length; }
00097 
00098     const unsigned char *bytes_begin() const {
00099       return reinterpret_cast<const unsigned char *>(begin());
00100     }
00101     const unsigned char *bytes_end() const {
00102       return reinterpret_cast<const unsigned char *>(end());
00103     }
00104 
00105     /// @}
00106     /// @name String Operations
00107     /// @{
00108 
00109     /// data - Get a pointer to the start of the string (which may not be null
00110     /// terminated).
00111     LLVM_ATTRIBUTE_ALWAYS_INLINE
00112     const char *data() const { return Data; }
00113 
00114     /// empty - Check if the string is empty.
00115     LLVM_ATTRIBUTE_ALWAYS_INLINE
00116     bool empty() const { return Length == 0; }
00117 
00118     /// size - Get the string size.
00119     LLVM_ATTRIBUTE_ALWAYS_INLINE
00120     size_t size() const { return Length; }
00121 
00122     /// front - Get the first character in the string.
00123     char front() const {
00124       assert(!empty());
00125       return Data[0];
00126     }
00127 
00128     /// back - Get the last character in the string.
00129     char back() const {
00130       assert(!empty());
00131       return Data[Length-1];
00132     }
00133 
00134     // copy - Allocate copy in Allocator and return StringRef to it.
00135     template <typename Allocator> StringRef copy(Allocator &A) const {
00136       char *S = A.template Allocate<char>(Length);
00137       std::copy(begin(), end(), S);
00138       return StringRef(S, Length);
00139     }
00140 
00141     /// equals - Check for string equality, this is more efficient than
00142     /// compare() when the relative ordering of inequal strings isn't needed.
00143     LLVM_ATTRIBUTE_ALWAYS_INLINE
00144     bool equals(StringRef RHS) const {
00145       return (Length == RHS.Length &&
00146               compareMemory(Data, RHS.Data, RHS.Length) == 0);
00147     }
00148 
00149     /// equals_lower - Check for string equality, ignoring case.
00150     bool equals_lower(StringRef RHS) const {
00151       return Length == RHS.Length && compare_lower(RHS) == 0;
00152     }
00153 
00154     /// compare - Compare two strings; the result is -1, 0, or 1 if this string
00155     /// is lexicographically less than, equal to, or greater than the \p RHS.
00156     LLVM_ATTRIBUTE_ALWAYS_INLINE
00157     int compare(StringRef RHS) const {
00158       // Check the prefix for a mismatch.
00159       if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
00160         return Res < 0 ? -1 : 1;
00161 
00162       // Otherwise the prefixes match, so we only need to check the lengths.
00163       if (Length == RHS.Length)
00164         return 0;
00165       return Length < RHS.Length ? -1 : 1;
00166     }
00167 
00168     /// compare_lower - Compare two strings, ignoring case.
00169     int compare_lower(StringRef RHS) const;
00170 
00171     /// compare_numeric - Compare two strings, treating sequences of digits as
00172     /// numbers.
00173     int compare_numeric(StringRef RHS) const;
00174 
00175     /// \brief Determine the edit distance between this string and another
00176     /// string.
00177     ///
00178     /// \param Other the string to compare this string against.
00179     ///
00180     /// \param AllowReplacements whether to allow character
00181     /// replacements (change one character into another) as a single
00182     /// operation, rather than as two operations (an insertion and a
00183     /// removal).
00184     ///
00185     /// \param MaxEditDistance If non-zero, the maximum edit distance that
00186     /// this routine is allowed to compute. If the edit distance will exceed
00187     /// that maximum, returns \c MaxEditDistance+1.
00188     ///
00189     /// \returns the minimum number of character insertions, removals,
00190     /// or (if \p AllowReplacements is \c true) replacements needed to
00191     /// transform one of the given strings into the other. If zero,
00192     /// the strings are identical.
00193     unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
00194                            unsigned MaxEditDistance = 0) const;
00195 
00196     /// str - Get the contents as an std::string.
00197     std::string str() const {
00198       if (!Data) return std::string();
00199       return std::string(Data, Length);
00200     }
00201 
00202     /// @}
00203     /// @name Operator Overloads
00204     /// @{
00205 
00206     char operator[](size_t Index) const {
00207       assert(Index < Length && "Invalid index!");
00208       return Data[Index];
00209     }
00210 
00211     /// @}
00212     /// @name Type Conversions
00213     /// @{
00214 
00215     operator std::string() const {
00216       return str();
00217     }
00218 
00219     /// @}
00220     /// @name String Predicates
00221     /// @{
00222 
00223     /// Check if this string starts with the given \p Prefix.
00224     LLVM_ATTRIBUTE_ALWAYS_INLINE
00225     bool startswith(StringRef Prefix) const {
00226       return Length >= Prefix.Length &&
00227              compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
00228     }
00229 
00230     /// Check if this string starts with the given \p Prefix, ignoring case.
00231     bool startswith_lower(StringRef Prefix) const;
00232 
00233     /// Check if this string ends with the given \p Suffix.
00234     LLVM_ATTRIBUTE_ALWAYS_INLINE
00235     bool endswith(StringRef Suffix) const {
00236       return Length >= Suffix.Length &&
00237         compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
00238     }
00239 
00240     /// Check if this string ends with the given \p Suffix, ignoring case.
00241     bool endswith_lower(StringRef Suffix) const;
00242 
00243     /// @}
00244     /// @name String Searching
00245     /// @{
00246 
00247     /// Search for the first character \p C in the string.
00248     ///
00249     /// \returns The index of the first occurrence of \p C, or npos if not
00250     /// found.
00251     LLVM_ATTRIBUTE_ALWAYS_INLINE
00252     size_t find(char C, size_t From = 0) const {
00253       size_t FindBegin = std::min(From, Length);
00254       if (FindBegin < Length) { // Avoid calling memchr with nullptr.
00255         // Just forward to memchr, which is faster than a hand-rolled loop.
00256         if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
00257           return static_cast<const char *>(P) - Data;
00258       }
00259       return npos;
00260     }
00261 
00262     /// Search for the first string \p Str in the string.
00263     ///
00264     /// \returns The index of the first occurrence of \p Str, or npos if not
00265     /// found.
00266     size_t find(StringRef Str, size_t From = 0) const;
00267 
00268     /// Search for the last character \p C in the string.
00269     ///
00270     /// \returns The index of the last occurrence of \p C, or npos if not
00271     /// found.
00272     size_t rfind(char C, size_t From = npos) const {
00273       From = std::min(From, Length);
00274       size_t i = From;
00275       while (i != 0) {
00276         --i;
00277         if (Data[i] == C)
00278           return i;
00279       }
00280       return npos;
00281     }
00282 
00283     /// Search for the last string \p Str in the string.
00284     ///
00285     /// \returns The index of the last occurrence of \p Str, or npos if not
00286     /// found.
00287     size_t rfind(StringRef Str) const;
00288 
00289     /// Find the first character in the string that is \p C, or npos if not
00290     /// found. Same as find.
00291     size_t find_first_of(char C, size_t From = 0) const {
00292       return find(C, From);
00293     }
00294 
00295     /// Find the first character in the string that is in \p Chars, or npos if
00296     /// not found.
00297     ///
00298     /// Complexity: O(size() + Chars.size())
00299     size_t find_first_of(StringRef Chars, size_t From = 0) const;
00300 
00301     /// Find the first character in the string that is not \p C or npos if not
00302     /// found.
00303     size_t find_first_not_of(char C, size_t From = 0) const;
00304 
00305     /// Find the first character in the string that is not in the string
00306     /// \p Chars, or npos if not found.
00307     ///
00308     /// Complexity: O(size() + Chars.size())
00309     size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
00310 
00311     /// Find the last character in the string that is \p C, or npos if not
00312     /// found.
00313     size_t find_last_of(char C, size_t From = npos) const {
00314       return rfind(C, From);
00315     }
00316 
00317     /// Find the last character in the string that is in \p C, or npos if not
00318     /// found.
00319     ///
00320     /// Complexity: O(size() + Chars.size())
00321     size_t find_last_of(StringRef Chars, size_t From = npos) const;
00322 
00323     /// Find the last character in the string that is not \p C, or npos if not
00324     /// found.
00325     size_t find_last_not_of(char C, size_t From = npos) const;
00326 
00327     /// Find the last character in the string that is not in \p Chars, or
00328     /// npos if not found.
00329     ///
00330     /// Complexity: O(size() + Chars.size())
00331     size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
00332 
00333     /// @}
00334     /// @name Helpful Algorithms
00335     /// @{
00336 
00337     /// Return the number of occurrences of \p C in the string.
00338     size_t count(char C) const {
00339       size_t Count = 0;
00340       for (size_t i = 0, e = Length; i != e; ++i)
00341         if (Data[i] == C)
00342           ++Count;
00343       return Count;
00344     }
00345 
00346     /// Return the number of non-overlapped occurrences of \p Str in
00347     /// the string.
00348     size_t count(StringRef Str) const;
00349 
00350     /// Parse the current string as an integer of the specified radix.  If
00351     /// \p Radix is specified as zero, this does radix autosensing using
00352     /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
00353     ///
00354     /// If the string is invalid or if only a subset of the string is valid,
00355     /// this returns true to signify the error.  The string is considered
00356     /// erroneous if empty or if it overflows T.
00357     template <typename T>
00358     typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
00359     getAsInteger(unsigned Radix, T &Result) const {
00360       long long LLVal;
00361       if (getAsSignedInteger(*this, Radix, LLVal) ||
00362             static_cast<T>(LLVal) != LLVal)
00363         return true;
00364       Result = LLVal;
00365       return false;
00366     }
00367 
00368     template <typename T>
00369     typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
00370     getAsInteger(unsigned Radix, T &Result) const {
00371       unsigned long long ULLVal;
00372       // The additional cast to unsigned long long is required to avoid the
00373       // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
00374       // 'unsigned __int64' when instantiating getAsInteger with T = bool.
00375       if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
00376           static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
00377         return true;
00378       Result = ULLVal;
00379       return false;
00380     }
00381 
00382     /// Parse the current string as an integer of the specified \p Radix, or of
00383     /// an autosensed radix if the \p Radix given is 0.  The current value in
00384     /// \p Result is discarded, and the storage is changed to be wide enough to
00385     /// store the parsed integer.
00386     ///
00387     /// \returns true if the string does not solely consist of a valid
00388     /// non-empty number in the appropriate base.
00389     ///
00390     /// APInt::fromString is superficially similar but assumes the
00391     /// string is well-formed in the given radix.
00392     bool getAsInteger(unsigned Radix, APInt &Result) const;
00393 
00394     /// @}
00395     /// @name String Operations
00396     /// @{
00397 
00398     // Convert the given ASCII string to lowercase.
00399     std::string lower() const;
00400 
00401     /// Convert the given ASCII string to uppercase.
00402     std::string upper() const;
00403 
00404     /// @}
00405     /// @name Substring Operations
00406     /// @{
00407 
00408     /// Return a reference to the substring from [Start, Start + N).
00409     ///
00410     /// \param Start The index of the starting character in the substring; if
00411     /// the index is npos or greater than the length of the string then the
00412     /// empty substring will be returned.
00413     ///
00414     /// \param N The number of characters to included in the substring. If N
00415     /// exceeds the number of characters remaining in the string, the string
00416     /// suffix (starting with \p Start) will be returned.
00417     LLVM_ATTRIBUTE_ALWAYS_INLINE
00418     StringRef substr(size_t Start, size_t N = npos) const {
00419       Start = std::min(Start, Length);
00420       return StringRef(Data + Start, std::min(N, Length - Start));
00421     }
00422 
00423     /// Return a StringRef equal to 'this' but with the first \p N elements
00424     /// dropped.
00425     LLVM_ATTRIBUTE_ALWAYS_INLINE
00426     StringRef drop_front(size_t N = 1) const {
00427       assert(size() >= N && "Dropping more elements than exist");
00428       return substr(N);
00429     }
00430 
00431     /// Return a StringRef equal to 'this' but with the last \p N elements
00432     /// dropped.
00433     LLVM_ATTRIBUTE_ALWAYS_INLINE
00434     StringRef drop_back(size_t N = 1) const {
00435       assert(size() >= N && "Dropping more elements than exist");
00436       return substr(0, size()-N);
00437     }
00438 
00439     /// Return a reference to the substring from [Start, End).
00440     ///
00441     /// \param Start The index of the starting character in the substring; if
00442     /// the index is npos or greater than the length of the string then the
00443     /// empty substring will be returned.
00444     ///
00445     /// \param End The index following the last character to include in the
00446     /// substring. If this is npos, or less than \p Start, or exceeds the
00447     /// number of characters remaining in the string, the string suffix
00448     /// (starting with \p Start) will be returned.
00449     LLVM_ATTRIBUTE_ALWAYS_INLINE
00450     StringRef slice(size_t Start, size_t End) const {
00451       Start = std::min(Start, Length);
00452       End = std::min(std::max(Start, End), Length);
00453       return StringRef(Data + Start, End - Start);
00454     }
00455 
00456     /// Split into two substrings around the first occurrence of a separator
00457     /// character.
00458     ///
00459     /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
00460     /// such that (*this == LHS + Separator + RHS) is true and RHS is
00461     /// maximal. If \p Separator is not in the string, then the result is a
00462     /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
00463     ///
00464     /// \param Separator The character to split on.
00465     /// \returns The split substrings.
00466     std::pair<StringRef, StringRef> split(char Separator) const {
00467       size_t Idx = find(Separator);
00468       if (Idx == npos)
00469         return std::make_pair(*this, StringRef());
00470       return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
00471     }
00472 
00473     /// Split into two substrings around the first occurrence of a separator
00474     /// string.
00475     ///
00476     /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
00477     /// such that (*this == LHS + Separator + RHS) is true and RHS is
00478     /// maximal. If \p Separator is not in the string, then the result is a
00479     /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
00480     ///
00481     /// \param Separator - The string to split on.
00482     /// \return - The split substrings.
00483     std::pair<StringRef, StringRef> split(StringRef Separator) const {
00484       size_t Idx = find(Separator);
00485       if (Idx == npos)
00486         return std::make_pair(*this, StringRef());
00487       return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
00488     }
00489 
00490     /// Split into substrings around the occurrences of a separator string.
00491     ///
00492     /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
00493     /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
00494     /// elements are added to A.
00495     /// If \p KeepEmpty is false, empty strings are not added to \p A. They
00496     /// still count when considering \p MaxSplit
00497     /// An useful invariant is that
00498     /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
00499     ///
00500     /// \param A - Where to put the substrings.
00501     /// \param Separator - The string to split on.
00502     /// \param MaxSplit - The maximum number of times the string is split.
00503     /// \param KeepEmpty - True if empty substring should be added.
00504     void split(SmallVectorImpl<StringRef> &A,
00505                StringRef Separator, int MaxSplit = -1,
00506                bool KeepEmpty = true) const;
00507 
00508     /// Split into substrings around the occurrences of a separator character.
00509     ///
00510     /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
00511     /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
00512     /// elements are added to A.
00513     /// If \p KeepEmpty is false, empty strings are not added to \p A. They
00514     /// still count when considering \p MaxSplit
00515     /// An useful invariant is that
00516     /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
00517     ///
00518     /// \param A - Where to put the substrings.
00519     /// \param Separator - The string to split on.
00520     /// \param MaxSplit - The maximum number of times the string is split.
00521     /// \param KeepEmpty - True if empty substring should be added.
00522     void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1,
00523                bool KeepEmpty = true) const;
00524 
00525     /// Split into two substrings around the last occurrence of a separator
00526     /// character.
00527     ///
00528     /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
00529     /// such that (*this == LHS + Separator + RHS) is true and RHS is
00530     /// minimal. If \p Separator is not in the string, then the result is a
00531     /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
00532     ///
00533     /// \param Separator - The character to split on.
00534     /// \return - The split substrings.
00535     std::pair<StringRef, StringRef> rsplit(char Separator) const {
00536       size_t Idx = rfind(Separator);
00537       if (Idx == npos)
00538         return std::make_pair(*this, StringRef());
00539       return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
00540     }
00541 
00542     /// Return string with consecutive characters in \p Chars starting from
00543     /// the left removed.
00544     StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
00545       return drop_front(std::min(Length, find_first_not_of(Chars)));
00546     }
00547 
00548     /// Return string with consecutive characters in \p Chars starting from
00549     /// the right removed.
00550     StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
00551       return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
00552     }
00553 
00554     /// Return string with consecutive characters in \p Chars starting from
00555     /// the left and right removed.
00556     StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
00557       return ltrim(Chars).rtrim(Chars);
00558     }
00559 
00560     /// @}
00561   };
00562 
00563   /// @name StringRef Comparison Operators
00564   /// @{
00565 
00566   LLVM_ATTRIBUTE_ALWAYS_INLINE
00567   inline bool operator==(StringRef LHS, StringRef RHS) {
00568     return LHS.equals(RHS);
00569   }
00570 
00571   LLVM_ATTRIBUTE_ALWAYS_INLINE
00572   inline bool operator!=(StringRef LHS, StringRef RHS) {
00573     return !(LHS == RHS);
00574   }
00575 
00576   inline bool operator<(StringRef LHS, StringRef RHS) {
00577     return LHS.compare(RHS) == -1;
00578   }
00579 
00580   inline bool operator<=(StringRef LHS, StringRef RHS) {
00581     return LHS.compare(RHS) != 1;
00582   }
00583 
00584   inline bool operator>(StringRef LHS, StringRef RHS) {
00585     return LHS.compare(RHS) == 1;
00586   }
00587 
00588   inline bool operator>=(StringRef LHS, StringRef RHS) {
00589     return LHS.compare(RHS) != -1;
00590   }
00591 
00592   inline std::string &operator+=(std::string &buffer, StringRef string) {
00593     return buffer.append(string.data(), string.size());
00594   }
00595 
00596   /// @}
00597 
00598   /// \brief Compute a hash_code for a StringRef.
00599   hash_code hash_value(StringRef S);
00600 
00601   // StringRefs can be treated like a POD type.
00602   template <typename T> struct isPodLike;
00603   template <> struct isPodLike<StringRef> { static const bool value = true; };
00604 }
00605 
00606 #endif