StringRef.h revision 6d5502eb4966fb2f81fe951d0acf11cfa5cd4acf
14cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar//===--- StringRef.h - Constant String Reference Wrapper --------*- C++ -*-===//
24cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar//
34cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar//                     The LLVM Compiler Infrastructure
44cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar//
54cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar// This file is distributed under the University of Illinois Open Source
64cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar// License. See LICENSE.TXT for details.
74cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar//
84cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar//===----------------------------------------------------------------------===//
94cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
104cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#ifndef LLVM_ADT_STRINGREF_H
114cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#define LLVM_ADT_STRINGREF_H
124cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1385f49835c288b0107cb4020d4e59e491c146973dDaniel Dunbar#include <cassert>
144cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#include <cstring>
151acdcd5b0d0893e6c9a3b7618a7facf9e2d5dec6Benjamin Kramer#include <utility>
164cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#include <string>
174cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
184cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbarnamespace llvm {
19c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola  template<typename T>
20c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola  class SmallVectorImpl;
211e7ad3993d8700488895fa372ecad55443f53485John McCall  class APInt;
22f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
234cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// StringRef - Represent a constant reference to a string, i.e. a character
244cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// array and a length, which need not be null terminated.
254cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  ///
264cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// This class does not own the string data, it is expected to be used in
274cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// situations where the character data resides in some other buffer, whose
284cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// lifetime extends past that of the StringRef. For this reason, it is not in
294cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// general safe to store a StringRef.
304cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  class StringRef {
314cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  public:
324cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    typedef const char *iterator;
33af2a8361e4e5b45441b6e73c332fb60c36892468Chris Lattner    typedef const char *const_iterator;
34b834a7b73ce0dcf8fbf8d8b0d62f69e4b78059adDaniel Dunbar    static const size_t npos = ~size_t(0);
35db513bc0963fbf372f898f8b94fc13994663c0c6Chris Lattner    typedef size_t size_type;
36abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
374cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  private:
384cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// The start of the string, in an external buffer.
394cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    const char *Data;
404cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
414cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// The length of the string.
42f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    size_t Length;
434cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
442fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin    // Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min()
452fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin    // Changing the arg of min to be an integer, instead of a reference to an
462fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin    // integer works around this bug.
47fd813bc85027b8e4f8dc42b485b254cff210012dDan Gohman    static size_t min(size_t a, size_t b) { return a < b ? a : b; }
48fd813bc85027b8e4f8dc42b485b254cff210012dDan Gohman    static size_t max(size_t a, size_t b) { return a > b ? a : b; }
496d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis
506d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    // Workaround memcmp issue with null pointers (undefined behavior)
516d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    // by providing a specialized version
526d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
536d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      if (Length == 0) { return 0; }
546d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      return ::memcmp(Lhs,Rhs,Length);
556d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    }
566d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis
574cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  public:
584cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Constructors
594cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
604cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
614cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct an empty string ref.
624cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /*implicit*/ StringRef() : Data(0), Length(0) {}
634cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
644cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct a string ref from a cstring.
65abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    /*implicit*/ StringRef(const char *Str)
666d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      : Data(Str) {
676d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        assert(Str && "StringRef cannot be built from a NULL argument");
686d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
696d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      }
70abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
714cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct a string ref from a pointer and length.
72abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    /*implicit*/ StringRef(const char *data, size_t length)
736d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      : Data(data), Length(length) {
746d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        assert((data || length == 0) &&
756d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        "StringRef cannot be built from a NULL argument with non-null length");
766d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      }
774cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
784cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct a string ref from an std::string.
79abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    /*implicit*/ StringRef(const std::string &Str)
8024f8e29b4efe70496474c6d43aa6abfa27c21511Dan Gohman      : Data(Str.data()), Length(Str.length()) {}
814cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
824cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
834cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Iterators
844cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
854cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
864cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    iterator begin() const { return Data; }
874cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
884cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    iterator end() const { return Data + Length; }
894cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
904cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
914cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name String Operations
924cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
934cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
944cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// data - Get a pointer to the start of the string (which may not be null
954cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// terminated).
964cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    const char *data() const { return Data; }
974cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
984cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// empty - Check if the string is empty.
994cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    bool empty() const { return Length == 0; }
1004cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1014cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// size - Get the string size.
1027cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    size_t size() const { return Length; }
103ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar
104ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    /// front - Get the first character in the string.
105ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    char front() const {
106ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar      assert(!empty());
107ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar      return Data[0];
108ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    }
109abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
110ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    /// back - Get the last character in the string.
111e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner    char back() const {
112e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner      assert(!empty());
113e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner      return Data[Length-1];
114e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner    }
1157cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
1167cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    /// equals - Check for string equality, this is more efficient than
117ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    /// compare() when the relative ordering of inequal strings isn't needed.
1182928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    bool equals(StringRef RHS) const {
119abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher      return (Length == RHS.Length &&
1206d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis              compareMemory(Data, RHS.Data, RHS.Length) == 0);
1217cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    }
1224cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
12305872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    /// equals_lower - Check for string equality, ignoring case.
12405872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    bool equals_lower(StringRef RHS) const {
12505872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer      return Length == RHS.Length && compare_lower(RHS) == 0;
12605872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    }
12705872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer
1284cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// compare - Compare two strings; the result is -1, 0, or 1 if this string
1294cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// is lexicographically less than, equal to, or greater than the \arg RHS.
1302928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    int compare(StringRef RHS) const {
1314cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      // Check the prefix for a mismatch.
1326d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      if (int Res = compareMemory(Data, RHS.Data, min(Length, RHS.Length)))
1334cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar        return Res < 0 ? -1 : 1;
1344cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1354cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      // Otherwise the prefixes match, so we only need to check the lengths.
1364cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      if (Length == RHS.Length)
1374cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar        return 0;
1384cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      return Length < RHS.Length ? -1 : 1;
1394cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    }
1404cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
14105872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    /// compare_lower - Compare two strings, ignoring case.
14205872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    int compare_lower(StringRef RHS) const;
14305872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer
144160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen    /// compare_numeric - Compare two strings, treating sequences of digits as
145160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen    /// numbers.
146160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen    int compare_numeric(StringRef RHS) const;
147160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen
148326990f1eb7ff005adabe46a1f982eff8835813eMichael J. Spencer    /// \brief Determine the edit distance between this string and another
149441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// string.
150441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    ///
151441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// \param Other the string to compare this string against.
152441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    ///
153441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// \param AllowReplacements whether to allow character
154441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// replacements (change one character into another) as a single
155441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// operation, rather than as two operations (an insertion and a
156441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// removal).
157441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    ///
1585ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    /// \param MaxEditDistance If non-zero, the maximum edit distance that
1595ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    /// this routine is allowed to compute. If the edit distance will exceed
1605ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    /// that maximum, returns \c MaxEditDistance+1.
1615ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    ///
162441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// \returns the minimum number of character insertions, removals,
163441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// or (if \p AllowReplacements is \c true) replacements needed to
164441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// transform one of the given strings into the other. If zero,
165441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// the strings are identical.
1665ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
1675ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor                           unsigned MaxEditDistance = 0);
168441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor
1694cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// str - Get the contents as an std::string.
1707f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner    std::string str() const {
17179ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer      if (Data == 0) return std::string();
1727f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner      return std::string(Data, Length);
1737f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner    }
1744cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1754cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
1764cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Operator Overloads
1774cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
1784cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
179abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    char operator[](size_t Index) const {
1804cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      assert(Index < Length && "Invalid index!");
181abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher      return Data[Index];
1824cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    }
1834cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1844cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
1854cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Type Conversions
1864cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
1874cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1884cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    operator std::string() const {
1894cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      return str();
1904cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    }
1914cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1924cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
1930ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @name String Predicates
1940ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @{
1950ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
1960ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// startswith - Check if this string starts with the given \arg Prefix.
1972928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    bool startswith(StringRef Prefix) const {
198d5b1f8a8426e82990dafc6e3336fefc6635c8fa4Eli Friedman      return Length >= Prefix.Length &&
1996d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis             compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
2000ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    }
2010ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2020ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// endswith - Check if this string ends with the given \arg Suffix.
2032928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    bool endswith(StringRef Suffix) const {
204d5b1f8a8426e82990dafc6e3336fefc6635c8fa4Eli Friedman      return Length >= Suffix.Length &&
2056d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
2060ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    }
2070ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2080ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @}
2090ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @name String Searching
2100ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @{
2110ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
212e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// find - Search for the first character \arg C in the string.
2130ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    ///
2143c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// \return - The index of the first occurrence of \arg C, or npos if not
2150ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// found.
21664066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_t find(char C, size_t From = 0) const {
2172fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      for (size_t i = min(From, Length), e = Length; i != e; ++i)
2180ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar        if (Data[i] == C)
2190ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar          return i;
2200ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      return npos;
2210ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    }
2220ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
223e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// find - Search for the first string \arg Str in the string.
2240ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    ///
2253c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// \return - The index of the first occurrence of \arg Str, or npos if not
2260ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// found.
22764066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_t find(StringRef Str, size_t From = 0) const;
228abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
229e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// rfind - Search for the last character \arg C in the string.
230e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
2313c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// \return - The index of the last occurrence of \arg C, or npos if not
232e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// found.
233c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner    size_t rfind(char C, size_t From = npos) const {
2342fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      From = min(From, Length);
235c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner      size_t i = From;
236c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner      while (i != 0) {
237e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar        --i;
238e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar        if (Data[i] == C)
239e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar          return i;
240e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      }
241e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      return npos;
242e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    }
243abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
244e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// rfind - Search for the last string \arg Str in the string.
245e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
2463c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// \return - The index of the last occurrence of \arg Str, or npos if not
247e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// found.
2482928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    size_t rfind(StringRef Str) const;
249abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
25064066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// find_first_of - Find the first character in the string that is \arg C,
25164066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// or npos if not found. Same as find.
25279ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer    size_type find_first_of(char C, size_t From = 0) const {
25379ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer      return find(C, From);
25479ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer    }
255abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
25664066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// find_first_of - Find the first character in the string that is in \arg
25764066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// Chars, or npos if not found.
25864066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    ///
259250eb005d91e80b05a61345394bae9e9528151acBenjamin Kramer    /// Note: O(size() + Chars.size())
26064066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_type find_first_of(StringRef Chars, size_t From = 0) const;
261abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
26205a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// find_first_not_of - Find the first character in the string that is not
26364066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// \arg C or npos if not found.
26464066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_type find_first_not_of(char C, size_t From = 0) const;
26564066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar
26664066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// find_first_not_of - Find the first character in the string that is not
26764066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    /// in the string \arg Chars, or npos if not found.
26864066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    ///
269250eb005d91e80b05a61345394bae9e9528151acBenjamin Kramer    /// Note: O(size() + Chars.size())
27064066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_type find_first_not_of(StringRef Chars, size_t From = 0) const;
271abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
27263c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    /// find_last_of - Find the last character in the string that is \arg C, or
27363c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    /// npos if not found.
27463c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    size_type find_last_of(char C, size_t From = npos) const {
27563c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer      return rfind(C, From);
27663c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    }
27763c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer
27863c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    /// find_last_of - Find the last character in the string that is in \arg C,
27963c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    /// or npos if not found.
28063c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    ///
28163c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    /// Note: O(size() + Chars.size())
28263c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    size_type find_last_of(StringRef Chars, size_t From = npos) const;
28363c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer
28405a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// @}
28505a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// @name Helpful Algorithms
28605a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// @{
287abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2885caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    /// count - Return the number of occurrences of \arg C in the string.
2895caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    size_t count(char C) const {
2905caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar      size_t Count = 0;
2915caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar      for (size_t i = 0, e = Length; i != e; ++i)
2925caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar        if (Data[i] == C)
293323a3e653340781bad4c4c3245d9b25d5ab02685Daniel Dunbar          ++Count;
2945caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar      return Count;
2955caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    }
296abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2975caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    /// count - Return the number of non-overlapped occurrences of \arg Str in
2985caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    /// the string.
2992928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    size_t count(StringRef Str) const;
300abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
301cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// getAsInteger - Parse the current string as an integer of the specified
302cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// radix.  If Radix is specified as zero, this does radix autosensing using
303cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
304cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    ///
305cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// If the string is invalid or if only a subset of the string is valid,
306cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// this returns true to signify the error.  The string is considered
307cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// erroneous if empty.
308cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    ///
30963c6b7dc67f0061340c0701f3d5b1de142f58cecChris Lattner    bool getAsInteger(unsigned Radix, long long &Result) const;
310cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    bool getAsInteger(unsigned Radix, unsigned long long &Result) const;
31163c6b7dc67f0061340c0701f3d5b1de142f58cecChris Lattner    bool getAsInteger(unsigned Radix, int &Result) const;
31263c6b7dc67f0061340c0701f3d5b1de142f58cecChris Lattner    bool getAsInteger(unsigned Radix, unsigned &Result) const;
313cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner
314cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    // TODO: Provide overloads for int/unsigned that check for overflow.
315abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
3161e7ad3993d8700488895fa372ecad55443f53485John McCall    /// getAsInteger - Parse the current string as an integer of the
3171e7ad3993d8700488895fa372ecad55443f53485John McCall    /// specified radix, or of an autosensed radix if the radix given
3181e7ad3993d8700488895fa372ecad55443f53485John McCall    /// is 0.  The current value in Result is discarded, and the
3191e7ad3993d8700488895fa372ecad55443f53485John McCall    /// storage is changed to be wide enough to store the parsed
3201e7ad3993d8700488895fa372ecad55443f53485John McCall    /// integer.
3211e7ad3993d8700488895fa372ecad55443f53485John McCall    ///
3221e7ad3993d8700488895fa372ecad55443f53485John McCall    /// Returns true if the string does not solely consist of a valid
3231e7ad3993d8700488895fa372ecad55443f53485John McCall    /// non-empty number in the appropriate base.
3241e7ad3993d8700488895fa372ecad55443f53485John McCall    ///
3251e7ad3993d8700488895fa372ecad55443f53485John McCall    /// APInt::fromString is superficially similar but assumes the
3261e7ad3993d8700488895fa372ecad55443f53485John McCall    /// string is well-formed in the given radix.
3271e7ad3993d8700488895fa372ecad55443f53485John McCall    bool getAsInteger(unsigned Radix, APInt &Result) const;
3281e7ad3993d8700488895fa372ecad55443f53485John McCall
329cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// @}
3300ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @name Substring Operations
331f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// @{
332f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
333d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// substr - Return a reference to the substring from [Start, Start + N).
334f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    ///
335f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// \param Start - The index of the starting character in the substring; if
336d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// the index is npos or greater than the length of the string then the
337d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// empty substring will be returned.
338f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    ///
339f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// \param N - The number of characters to included in the substring. If N
340f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// exceeds the number of characters remaining in the string, the string
341f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// suffix (starting with \arg Start) will be returned.
342f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    StringRef substr(size_t Start, size_t N = npos) const {
3432fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      Start = min(Start, Length);
3442fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      return StringRef(Data + Start, min(N, Length - Start));
345f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    }
346f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
347d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// slice - Return a reference to the substring from [Start, End).
348d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
349d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// \param Start - The index of the starting character in the substring; if
350d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// the index is npos or greater than the length of the string then the
351d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// empty substring will be returned.
352d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
353d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// \param End - The index following the last character to include in the
354d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// substring. If this is npos, or less than \arg Start, or exceeds the
355d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// number of characters remaining in the string, the string suffix
356d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// (starting with \arg Start) will be returned.
357d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    StringRef slice(size_t Start, size_t End) const {
3582fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      Start = min(Start, Length);
3592fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      End = min(max(Start, End), Length);
360d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar      return StringRef(Data + Start, End - Start);
361d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    }
362d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar
3633c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// split - Split into two substrings around the first occurrence of a
364d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// separator character.
365d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
366d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
367d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// such that (*this == LHS + Separator + RHS) is true and RHS is
368d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// maximal. If \arg Separator is not in the string, then the result is a
369d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
370d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
371d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// \param Separator - The character to split on.
372d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// \return - The split substrings.
373d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    std::pair<StringRef, StringRef> split(char Separator) const {
3740ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      size_t Idx = find(Separator);
3750ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      if (Idx == npos)
376d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar        return std::make_pair(*this, StringRef());
3770ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
378c9af366fc39d657b7d416d16988265d86a641184Daniel Dunbar    }
379c9af366fc39d657b7d416d16988265d86a641184Daniel Dunbar
3803c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// split - Split into two substrings around the first occurrence of a
381a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// separator string.
382a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    ///
383a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
384a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// such that (*this == LHS + Separator + RHS) is true and RHS is
385a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// maximal. If \arg Separator is not in the string, then the result is a
386a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
387a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    ///
388a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// \param Separator - The string to split on.
389a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// \return - The split substrings.
390a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    std::pair<StringRef, StringRef> split(StringRef Separator) const {
391a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar      size_t Idx = find(Separator);
392a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar      if (Idx == npos)
393a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar        return std::make_pair(*this, StringRef());
394a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar      return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
395a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    }
396a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar
3973c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// split - Split into substrings around the occurrences of a separator
3985ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// string.
3995ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    ///
4005ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// Each substring is stored in \arg A. If \arg MaxSplit is >= 0, at most
4015ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \arg MaxSplit splits are done and consequently <= \arg MaxSplit
4025ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// elements are added to A.
4035ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// If \arg KeepEmpty is false, empty strings are not added to \arg A. They
4045ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// still count when considering \arg MaxSplit
4055ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// An useful invariant is that
4065ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
4075ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    ///
4085ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \param A - Where to put the substrings.
4095ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \param Separator - The string to split on.
4105ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \param MaxSplit - The maximum number of times the string is split.
411fb76fe09297ee292129e44d723127f2408602a3dDan Gohman    /// \param KeepEmpty - True if empty substring should be added.
412c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola    void split(SmallVectorImpl<StringRef> &A,
413c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola               StringRef Separator, int MaxSplit = -1,
4145ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola               bool KeepEmpty = true) const;
4155ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola
4163c37bb8dbe886993dcd8f37dec0d94762393f3d4Kovarththanan Rajaratnam    /// rsplit - Split into two substrings around the last occurrence of a
417e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// separator character.
418e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
419e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
420e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// such that (*this == LHS + Separator + RHS) is true and RHS is
421e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// minimal. If \arg Separator is not in the string, then the result is a
422e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
423e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
424e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// \param Separator - The character to split on.
425e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// \return - The split substrings.
426e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    std::pair<StringRef, StringRef> rsplit(char Separator) const {
427e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      size_t Idx = rfind(Separator);
428e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      if (Idx == npos)
429e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar        return std::make_pair(*this, StringRef());
430e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
431e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    }
432e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar
433f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// @}
4344cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  };
435f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
4367cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  /// @name StringRef Comparison Operators
4377cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  /// @{
4387cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
4392928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator==(StringRef LHS, StringRef RHS) {
4407cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    return LHS.equals(RHS);
4417cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
4427cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
4432928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator!=(StringRef LHS, StringRef RHS) {
4447cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    return !(LHS == RHS);
4457cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
446abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
4472928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator<(StringRef LHS, StringRef RHS) {
448abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) == -1;
4497cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
4507cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
4512928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator<=(StringRef LHS, StringRef RHS) {
452abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) != 1;
4537cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
4547cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
4552928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator>(StringRef LHS, StringRef RHS) {
456abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) == 1;
4577cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
4587cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
4592928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator>=(StringRef LHS, StringRef RHS) {
460abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) != -1;
4617cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
4627cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
4636765f78efbd9d43f429344af7d997ec2df495b02John McCall  inline std::string &operator+=(std::string &buffer, llvm::StringRef string) {
4646765f78efbd9d43f429344af7d997ec2df495b02John McCall    return buffer.append(string.data(), string.size());
4656765f78efbd9d43f429344af7d997ec2df495b02John McCall  }
4666765f78efbd9d43f429344af7d997ec2df495b02John McCall
4677cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  /// @}
4687cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
469847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer  // StringRefs can be treated like a POD type.
470847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer  template <typename T> struct isPodLike;
471847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer  template <> struct isPodLike<StringRef> { static const bool value = true; };
472847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer
4734cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar}
4744cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
4754cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#endif
476