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