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
139130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer#include "llvm/Support/type_traits.h"
1478cab947cf8f81fc3cadbedd90c20fbe6e5eb1eeJoerg Sonnenberger#include <algorithm>
1585f49835c288b0107cb4020d4e59e491c146973dDaniel Dunbar#include <cassert>
164cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#include <cstring>
179130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer#include <limits>
184cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#include <string>
199130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer#include <utility>
204cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
214cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbarnamespace llvm {
22e4a4aecffb25df25736fec328d9147ee43335c2bChandler Carruth  template <typename T>
23c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola  class SmallVectorImpl;
241e7ad3993d8700488895fa372ecad55443f53485John McCall  class APInt;
25528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth  class hash_code;
269130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer  class StringRef;
279130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer
289130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer  /// Helper functions for StringRef::getAsInteger.
299130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer  bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
309130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer                            unsigned long long &Result);
319130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer
329130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer  bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
33f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
344cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// StringRef - Represent a constant reference to a string, i.e. a character
354cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// array and a length, which need not be null terminated.
364cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  ///
374cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// This class does not own the string data, it is expected to be used in
384cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// situations where the character data resides in some other buffer, whose
394cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// lifetime extends past that of the StringRef. For this reason, it is not in
404cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  /// general safe to store a StringRef.
414cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  class StringRef {
424cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  public:
434cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    typedef const char *iterator;
44af2a8361e4e5b45441b6e73c332fb60c36892468Chris Lattner    typedef const char *const_iterator;
45b834a7b73ce0dcf8fbf8d8b0d62f69e4b78059adDaniel Dunbar    static const size_t npos = ~size_t(0);
46db513bc0963fbf372f898f8b94fc13994663c0c6Chris Lattner    typedef size_t size_type;
47abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
484cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  private:
494cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// The start of the string, in an external buffer.
504cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    const char *Data;
514cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
524cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// The length of the string.
53f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    size_t Length;
544cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
552fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin    // Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min()
562fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin    // Changing the arg of min to be an integer, instead of a reference to an
572fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin    // integer works around this bug.
58fd813bc85027b8e4f8dc42b485b254cff210012dDan Gohman    static size_t min(size_t a, size_t b) { return a < b ? a : b; }
59fd813bc85027b8e4f8dc42b485b254cff210012dDan Gohman    static size_t max(size_t a, size_t b) { return a > b ? a : b; }
60d433902628e8c48059f66ba316a7182460ac4a84Matt Arsenault
616d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    // Workaround memcmp issue with null pointers (undefined behavior)
626d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    // by providing a specialized version
636d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
646d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      if (Length == 0) { return 0; }
656d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      return ::memcmp(Lhs,Rhs,Length);
666d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis    }
67d433902628e8c48059f66ba316a7182460ac4a84Matt Arsenault
684cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  public:
694cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Constructors
704cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
714cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
724cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct an empty string ref.
734cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /*implicit*/ StringRef() : Data(0), Length(0) {}
744cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
754cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct a string ref from a cstring.
76abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    /*implicit*/ StringRef(const char *Str)
776d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      : Data(Str) {
786d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        assert(Str && "StringRef cannot be built from a NULL argument");
796d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
806d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      }
81abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
824cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct a string ref from a pointer and length.
83abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    /*implicit*/ StringRef(const char *data, size_t length)
846d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      : Data(data), Length(length) {
856d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        assert((data || length == 0) &&
866d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        "StringRef cannot be built from a NULL argument with non-null length");
876d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      }
884cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
894cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// Construct a string ref from an std::string.
90abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    /*implicit*/ StringRef(const std::string &Str)
9124f8e29b4efe70496474c6d43aa6abfa27c21511Dan Gohman      : Data(Str.data()), Length(Str.length()) {}
924cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
934cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
944cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Iterators
954cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
964cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
974cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    iterator begin() const { return Data; }
984cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
994cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    iterator end() const { return Data + Length; }
1004cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1014cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
1024cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name String Operations
1034cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
1044cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1054cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// data - Get a pointer to the start of the string (which may not be null
1064cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// terminated).
1074cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    const char *data() const { return Data; }
1084cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1094cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// empty - Check if the string is empty.
1104cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    bool empty() const { return Length == 0; }
1114cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1124cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// size - Get the string size.
1137cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    size_t size() const { return Length; }
114ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar
115ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    /// front - Get the first character in the string.
116ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    char front() const {
117ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar      assert(!empty());
118ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar      return Data[0];
119ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    }
120abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
121ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    /// back - Get the last character in the string.
122e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner    char back() const {
123e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner      assert(!empty());
124e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner      return Data[Length-1];
125e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner    }
1267cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
1277cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    /// equals - Check for string equality, this is more efficient than
128ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar    /// compare() when the relative ordering of inequal strings isn't needed.
1292928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    bool equals(StringRef RHS) const {
130abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher      return (Length == RHS.Length &&
1316d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis              compareMemory(Data, RHS.Data, RHS.Length) == 0);
1327cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    }
1334cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
13405872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    /// equals_lower - Check for string equality, ignoring case.
13505872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    bool equals_lower(StringRef RHS) const {
13605872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer      return Length == RHS.Length && compare_lower(RHS) == 0;
13705872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    }
13805872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer
1394cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// compare - Compare two strings; the result is -1, 0, or 1 if this string
1402d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// is lexicographically less than, equal to, or greater than the \p RHS.
1412928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    int compare(StringRef RHS) const {
1424cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      // Check the prefix for a mismatch.
1436d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis      if (int Res = compareMemory(Data, RHS.Data, min(Length, RHS.Length)))
1444cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar        return Res < 0 ? -1 : 1;
1454cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1464cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      // Otherwise the prefixes match, so we only need to check the lengths.
1474cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      if (Length == RHS.Length)
1484cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar        return 0;
1494cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      return Length < RHS.Length ? -1 : 1;
1504cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    }
1514cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
15205872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    /// compare_lower - Compare two strings, ignoring case.
15305872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer    int compare_lower(StringRef RHS) const;
15405872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer
155160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen    /// compare_numeric - Compare two strings, treating sequences of digits as
156160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen    /// numbers.
157160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen    int compare_numeric(StringRef RHS) const;
158160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen
159326990f1eb7ff005adabe46a1f982eff8835813eMichael J. Spencer    /// \brief Determine the edit distance between this string and another
160441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// string.
161441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    ///
162441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// \param Other the string to compare this string against.
163441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    ///
164441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// \param AllowReplacements whether to allow character
165441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// replacements (change one character into another) as a single
166441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// operation, rather than as two operations (an insertion and a
167441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// removal).
168441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    ///
1695ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    /// \param MaxEditDistance If non-zero, the maximum edit distance that
1705ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    /// this routine is allowed to compute. If the edit distance will exceed
1715ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    /// that maximum, returns \c MaxEditDistance+1.
1725ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    ///
173441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// \returns the minimum number of character insertions, removals,
174441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// or (if \p AllowReplacements is \c true) replacements needed to
175441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// transform one of the given strings into the other. If zero,
176441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor    /// the strings are identical.
1775ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor    unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
1785ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor                           unsigned MaxEditDistance = 0);
179441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor
1804cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// str - Get the contents as an std::string.
1817f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner    std::string str() const {
18279ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer      if (Data == 0) return std::string();
1837f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner      return std::string(Data, Length);
1847f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner    }
1854cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1864cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
1874cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Operator Overloads
1884cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
1894cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
190abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    char operator[](size_t Index) const {
1914cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      assert(Index < Length && "Invalid index!");
192abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher      return Data[Index];
1934cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    }
1944cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1954cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
1964cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @name Type Conversions
1974cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @{
1984cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
1994cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    operator std::string() const {
2004cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar      return str();
2014cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    }
2024cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
2034cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar    /// @}
2040ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @name String Predicates
2050ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @{
2060ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2072d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Check if this string starts with the given \p Prefix.
2082928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    bool startswith(StringRef Prefix) const {
209d5b1f8a8426e82990dafc6e3336fefc6635c8fa4Eli Friedman      return Length >= Prefix.Length &&
2106d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis             compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
2110ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    }
2120ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2132d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Check if this string ends with the given \p Suffix.
2142928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    bool endswith(StringRef Suffix) const {
215d5b1f8a8426e82990dafc6e3336fefc6635c8fa4Eli Friedman      return Length >= Suffix.Length &&
2166d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis        compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
2170ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    }
2180ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2190ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @}
2200ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @name String Searching
2210ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @{
2220ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2232d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Search for the first character \p C in the string.
2240ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    ///
2252d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \returns The index of the first occurrence of \p C, or npos if not
2260ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// found.
22764066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_t find(char C, size_t From = 0) const {
2282fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      for (size_t i = min(From, Length), e = Length; i != e; ++i)
2290ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar        if (Data[i] == C)
2300ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar          return i;
2310ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      return npos;
2320ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    }
2330ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar
2342d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Search for the first string \p Str in the string.
2350ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    ///
2362d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \returns The index of the first occurrence of \p Str, or npos if not
2370ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// found.
23864066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    size_t find(StringRef Str, size_t From = 0) const;
239abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2402d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Search for the last character \p C in the string.
241e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
2422d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \returns The index of the last occurrence of \p C, or npos if not
243e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// found.
244c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner    size_t rfind(char C, size_t From = npos) const {
2452fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      From = min(From, Length);
246c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner      size_t i = From;
247c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner      while (i != 0) {
248e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar        --i;
249e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar        if (Data[i] == C)
250e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar          return i;
251e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      }
252e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      return npos;
253e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    }
254abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2552d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Search for the last string \p Str in the string.
256e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
2572d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \returns The index of the last occurrence of \p Str, or npos if not
258e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// found.
2592928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    size_t rfind(StringRef Str) const;
260abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2612d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the first character in the string that is \p C, or npos if not
2622d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// found. Same as find.
263a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_first_of(char C, size_t From = 0) const {
26479ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer      return find(C, From);
26579ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer    }
266abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2672d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the first character in the string that is in \p Chars, or npos if
2682d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// not found.
26964066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    ///
2702d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Complexity: O(size() + Chars.size())
271a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_first_of(StringRef Chars, size_t From = 0) const;
272abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2732d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the first character in the string that is not \p C or npos if not
2742d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// found.
275a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_first_not_of(char C, size_t From = 0) const;
27664066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar
2772d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the first character in the string that is not in the string
2782d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \p Chars, or npos if not found.
27964066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar    ///
2802d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Complexity: O(size() + Chars.size())
281a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
282abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
2832d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the last character in the string that is \p C, or npos if not
2842d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// found.
285a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_last_of(char C, size_t From = npos) const {
28663c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer      return rfind(C, From);
28763c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    }
28863c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer
2892d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the last character in the string that is in \p C, or npos if not
2902d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// found.
29163c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer    ///
2922d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Complexity: O(size() + Chars.size())
293a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_last_of(StringRef Chars, size_t From = npos) const;
29463c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer
2952d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the last character in the string that is not \p C, or npos if not
2962d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// found.
297a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_last_not_of(char C, size_t From = npos) const;
298b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer
2992d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Find the last character in the string that is not in \p Chars, or
3002d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// npos if not found.
301b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    ///
3022d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Complexity: O(size() + Chars.size())
303a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman    size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
304b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer
30505a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// @}
30605a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// @name Helpful Algorithms
30705a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner    /// @{
308abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
3092d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return the number of occurrences of \p C in the string.
3105caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    size_t count(char C) const {
3115caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar      size_t Count = 0;
3125caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar      for (size_t i = 0, e = Length; i != e; ++i)
3135caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar        if (Data[i] == C)
314323a3e653340781bad4c4c3245d9b25d5ab02685Daniel Dunbar          ++Count;
3155caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar      return Count;
3165caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    }
317abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
3182d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return the number of non-overlapped occurrences of \p Str in
3195caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar    /// the string.
3202928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar    size_t count(StringRef Str) const;
321abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
3222d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Parse the current string as an integer of the specified radix.  If
3232d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \p Radix is specified as zero, this does radix autosensing using
324cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
325cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    ///
326cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// If the string is invalid or if only a subset of the string is valid,
327cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// this returns true to signify the error.  The string is considered
3289130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    /// erroneous if empty or if it overflows T.
3299130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    template <typename T>
3309130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    typename enable_if_c<std::numeric_limits<T>::is_signed, bool>::type
3319130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    getAsInteger(unsigned Radix, T &Result) const {
3329130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      long long LLVal;
3339130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      if (getAsSignedInteger(*this, Radix, LLVal) ||
3349130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer            static_cast<T>(LLVal) != LLVal)
3359130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer        return true;
3369130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      Result = LLVal;
3379130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      return false;
3389130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    }
339cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner
3409130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    template <typename T>
3419130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    typename enable_if_c<!std::numeric_limits<T>::is_signed, bool>::type
3429130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    getAsInteger(unsigned Radix, T &Result) const {
3439130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      unsigned long long ULLVal;
3449130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
3459130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer            static_cast<T>(ULLVal) != ULLVal)
3469130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer        return true;
3479130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      Result = ULLVal;
3489130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer      return false;
3499130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer    }
350abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
3512d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Parse the current string as an integer of the specified \p Radix, or of
3522d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// an autosensed radix if the \p Radix given is 0.  The current value in
3532d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \p Result is discarded, and the storage is changed to be wide enough to
3542d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// store the parsed integer.
3551e7ad3993d8700488895fa372ecad55443f53485John McCall    ///
3562d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \returns true if the string does not solely consist of a valid
3571e7ad3993d8700488895fa372ecad55443f53485John McCall    /// non-empty number in the appropriate base.
3581e7ad3993d8700488895fa372ecad55443f53485John McCall    ///
3591e7ad3993d8700488895fa372ecad55443f53485John McCall    /// APInt::fromString is superficially similar but assumes the
3601e7ad3993d8700488895fa372ecad55443f53485John McCall    /// string is well-formed in the given radix.
3611e7ad3993d8700488895fa372ecad55443f53485John McCall    bool getAsInteger(unsigned Radix, APInt &Result) const;
3621e7ad3993d8700488895fa372ecad55443f53485John McCall
363cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner    /// @}
364589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar    /// @name String Operations
365589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar    /// @{
366589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar
3672d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    // Convert the given ASCII string to lowercase.
368589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar    std::string lower() const;
369589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar
3702d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Convert the given ASCII string to uppercase.
371589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar    std::string upper() const;
372589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar
373589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar    /// @}
3740ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar    /// @name Substring Operations
375f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// @{
376f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
3772d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return a reference to the substring from [Start, Start + N).
378f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    ///
3792d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \param Start The index of the starting character in the substring; if
380d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// the index is npos or greater than the length of the string then the
381d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// empty substring will be returned.
382f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    ///
3832d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \param N The number of characters to included in the substring. If N
384f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// exceeds the number of characters remaining in the string, the string
3852d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// suffix (starting with \p Start) will be returned.
386f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    StringRef substr(size_t Start, size_t N = npos) const {
3872fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      Start = min(Start, Length);
3882fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      return StringRef(Data + Start, min(N, Length - Start));
389f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    }
390d433902628e8c48059f66ba316a7182460ac4a84Matt Arsenault
3912d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return a StringRef equal to 'this' but with the first \p N elements
3922d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// dropped.
393a45391000ebfcf6193a97f2bb558f5beb845ee65Peng Cheng    StringRef drop_front(size_t N = 1) const {
394a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner      assert(size() >= N && "Dropping more elements than exist");
395a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner      return substr(N);
396a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner    }
397a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner
3982d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return a StringRef equal to 'this' but with the last \p N elements
3992d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// dropped.
400a45391000ebfcf6193a97f2bb558f5beb845ee65Peng Cheng    StringRef drop_back(size_t N = 1) const {
401a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner      assert(size() >= N && "Dropping more elements than exist");
402a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner      return substr(0, size()-N);
403a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner    }
404f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
4052d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return a reference to the substring from [Start, End).
406d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
4072d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \param Start The index of the starting character in the substring; if
408d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// the index is npos or greater than the length of the string then the
409d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// empty substring will be returned.
410d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
4112d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \param End The index following the last character to include in the
4122d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// substring. If this is npos, or less than \p Start, or exceeds the
413d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// number of characters remaining in the string, the string suffix
4142d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// (starting with \p Start) will be returned.
415d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    StringRef slice(size_t Start, size_t End) const {
4162fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      Start = min(Start, Length);
4172fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin      End = min(max(Start, End), Length);
418d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar      return StringRef(Data + Start, End - Start);
419d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    }
420d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar
4212d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Split into two substrings around the first occurrence of a separator
4222d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// character.
423d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
4242d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
425d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// such that (*this == LHS + Separator + RHS) is true and RHS is
4262d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// maximal. If \p Separator is not in the string, then the result is a
427d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
428d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    ///
4292d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \param Separator The character to split on.
4302d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \returns The split substrings.
431d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar    std::pair<StringRef, StringRef> split(char Separator) const {
4320ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      size_t Idx = find(Separator);
4330ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      if (Idx == npos)
434d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar        return std::make_pair(*this, StringRef());
4350ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
436c9af366fc39d657b7d416d16988265d86a641184Daniel Dunbar    }
437c9af366fc39d657b7d416d16988265d86a641184Daniel Dunbar
4382d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Split into two substrings around the first occurrence of a separator
4392d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// string.
440a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    ///
4412d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
442a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// such that (*this == LHS + Separator + RHS) is true and RHS is
4432d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// maximal. If \p Separator is not in the string, then the result is a
444a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
445a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    ///
446a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// \param Separator - The string to split on.
447a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    /// \return - The split substrings.
448a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    std::pair<StringRef, StringRef> split(StringRef Separator) const {
449a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar      size_t Idx = find(Separator);
450a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar      if (Idx == npos)
451a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar        return std::make_pair(*this, StringRef());
452a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar      return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
453a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar    }
454a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar
4552d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Split into substrings around the occurrences of a separator string.
4565ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    ///
4572d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
4582d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// \p MaxSplit splits are done and consequently <= \p MaxSplit
4595ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// elements are added to A.
4602d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// If \p KeepEmpty is false, empty strings are not added to \p A. They
4612d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// still count when considering \p MaxSplit
4625ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// An useful invariant is that
4635ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
4645ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    ///
4655ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \param A - Where to put the substrings.
4665ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \param Separator - The string to split on.
4675ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola    /// \param MaxSplit - The maximum number of times the string is split.
468fb76fe09297ee292129e44d723127f2408602a3dDan Gohman    /// \param KeepEmpty - True if empty substring should be added.
469c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola    void split(SmallVectorImpl<StringRef> &A,
470c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola               StringRef Separator, int MaxSplit = -1,
4715ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola               bool KeepEmpty = true) const;
4725ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola
4732d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Split into two substrings around the last occurrence of a separator
4742d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// character.
475e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
4762d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
477e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// such that (*this == LHS + Separator + RHS) is true and RHS is
4782d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// minimal. If \p Separator is not in the string, then the result is a
479e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
480e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    ///
481e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// \param Separator - The character to split on.
482e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    /// \return - The split substrings.
483e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    std::pair<StringRef, StringRef> rsplit(char Separator) const {
484e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      size_t Idx = rfind(Separator);
485e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      if (Idx == npos)
486e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar        return std::make_pair(*this, StringRef());
487e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
488e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar    }
489e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar
4902d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return string with consecutive characters in \p Chars starting from
4912d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// the left removed.
492b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
493b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer      return drop_front(std::min(Length, find_first_not_of(Chars)));
494b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    }
495b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer
4962d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return string with consecutive characters in \p Chars starting from
4972d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// the right removed.
498b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
499b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer      return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
500b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    }
501b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer
5022d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// Return string with consecutive characters in \p Chars starting from
5032d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko    /// the left and right removed.
504b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
505b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer      return ltrim(Chars).rtrim(Chars);
506b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer    }
507b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer
508f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar    /// @}
5094cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar  };
510f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar
5117cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  /// @name StringRef Comparison Operators
5127cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  /// @{
5137cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
5142928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator==(StringRef LHS, StringRef RHS) {
5157cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    return LHS.equals(RHS);
5167cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
5177cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
5182928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator!=(StringRef LHS, StringRef RHS) {
5197cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar    return !(LHS == RHS);
5207cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
521abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher
5222928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator<(StringRef LHS, StringRef RHS) {
523abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) == -1;
5247cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
5257cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
5262928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator<=(StringRef LHS, StringRef RHS) {
527abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) != 1;
5287cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
5297cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
5302928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator>(StringRef LHS, StringRef RHS) {
531abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) == 1;
5327cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
5337cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
5342928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar  inline bool operator>=(StringRef LHS, StringRef RHS) {
535abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher    return LHS.compare(RHS) != -1;
5367cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  }
5377cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
53896f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  inline std::string &operator+=(std::string &buffer, StringRef string) {
5396765f78efbd9d43f429344af7d997ec2df495b02John McCall    return buffer.append(string.data(), string.size());
5406765f78efbd9d43f429344af7d997ec2df495b02John McCall  }
5416765f78efbd9d43f429344af7d997ec2df495b02John McCall
5427cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar  /// @}
5437cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar
544528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth  /// \brief Compute a hash_code for a StringRef.
545528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth  hash_code hash_value(StringRef S);
546528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth
547847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer  // StringRefs can be treated like a POD type.
548847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer  template <typename T> struct isPodLike;
549847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer  template <> struct isPodLike<StringRef> { static const bool value = true; };
550847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer
551e130dc6cc9ec1423086c37f557ef8854064fef4aBill Wendling  /// Construct a string ref from a boolean.
552e130dc6cc9ec1423086c37f557ef8854064fef4aBill Wendling  inline StringRef toStringRef(bool B) {
553e130dc6cc9ec1423086c37f557ef8854064fef4aBill Wendling    return StringRef(B ? "true" : "false");
554e130dc6cc9ec1423086c37f557ef8854064fef4aBill Wendling  }
5554cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar}
5564cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar
5574cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#endif
558