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 1378cab947cf8f81fc3cadbedd90c20fbe6e5eb1eeJoerg Sonnenberger#include <algorithm> 1485f49835c288b0107cb4020d4e59e491c146973dDaniel Dunbar#include <cassert> 154cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#include <cstring> 169130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer#include <limits> 174cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#include <string> 189130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer#include <utility> 194cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 204cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbarnamespace llvm { 21e4a4aecffb25df25736fec328d9147ee43335c2bChandler Carruth template <typename T> 22c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola class SmallVectorImpl; 231e7ad3993d8700488895fa372ecad55443f53485John McCall class APInt; 24528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth class hash_code; 259130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer class StringRef; 269130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer 279130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer /// Helper functions for StringRef::getAsInteger. 289130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer bool getAsUnsignedInteger(StringRef Str, unsigned Radix, 299130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer unsigned long long &Result); 309130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer 319130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result); 32f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar 334cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// StringRef - Represent a constant reference to a string, i.e. a character 344cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// array and a length, which need not be null terminated. 354cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// 364cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// This class does not own the string data, it is expected to be used in 374cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// situations where the character data resides in some other buffer, whose 384cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// lifetime extends past that of the StringRef. For this reason, it is not in 394cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// general safe to store a StringRef. 404cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar class StringRef { 414cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar public: 424cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar typedef const char *iterator; 43af2a8361e4e5b45441b6e73c332fb60c36892468Chris Lattner typedef const char *const_iterator; 44b834a7b73ce0dcf8fbf8d8b0d62f69e4b78059adDaniel Dunbar static const size_t npos = ~size_t(0); 45db513bc0963fbf372f898f8b94fc13994663c0c6Chris Lattner typedef size_t size_type; 46abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 474cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar private: 484cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// The start of the string, in an external buffer. 494cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar const char *Data; 504cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 514cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// The length of the string. 52f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar size_t Length; 534cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 542fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin // Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min() 552fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin // Changing the arg of min to be an integer, instead of a reference to an 562fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin // integer works around this bug. 57fd813bc85027b8e4f8dc42b485b254cff210012dDan Gohman static size_t min(size_t a, size_t b) { return a < b ? a : b; } 58fd813bc85027b8e4f8dc42b485b254cff210012dDan Gohman static size_t max(size_t a, size_t b) { return a > b ? a : b; } 59d433902628e8c48059f66ba316a7182460ac4a84Matt Arsenault 606d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis // Workaround memcmp issue with null pointers (undefined behavior) 616d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis // by providing a specialized version 626d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) { 636d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis if (Length == 0) { return 0; } 646d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis return ::memcmp(Lhs,Rhs,Length); 656d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis } 66d433902628e8c48059f66ba316a7182460ac4a84Matt Arsenault 674cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar public: 684cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @name Constructors 694cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @{ 704cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 714cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// Construct an empty string ref. 72dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines /*implicit*/ StringRef() : Data(nullptr), Length(0) {} 734cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 744cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// Construct a string ref from a cstring. 75abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher /*implicit*/ StringRef(const char *Str) 766d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis : Data(Str) { 776d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis assert(Str && "StringRef cannot be built from a NULL argument"); 786d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior 796d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis } 80abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 814cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// Construct a string ref from a pointer and length. 82abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher /*implicit*/ StringRef(const char *data, size_t length) 836d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis : Data(data), Length(length) { 846d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis assert((data || length == 0) && 856d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis "StringRef cannot be built from a NULL argument with non-null length"); 866d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis } 874cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 884cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// Construct a string ref from an std::string. 89abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher /*implicit*/ StringRef(const std::string &Str) 9024f8e29b4efe70496474c6d43aa6abfa27c21511Dan Gohman : Data(Str.data()), Length(Str.length()) {} 914cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 924cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @} 934cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @name Iterators 944cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @{ 954cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 964cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar iterator begin() const { return Data; } 974cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 984cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar iterator end() const { return Data + Length; } 994cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 1004cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @} 1014cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @name String Operations 1024cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @{ 1034cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 1044cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// data - Get a pointer to the start of the string (which may not be null 1054cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// terminated). 1064cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar const char *data() const { return Data; } 1074cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 1084cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// empty - Check if the string is empty. 1094cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar bool empty() const { return Length == 0; } 1104cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 1114cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// size - Get the string size. 1127cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar size_t size() const { return Length; } 113ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar 114ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar /// front - Get the first character in the string. 115ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar char front() const { 116ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar assert(!empty()); 117ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar return Data[0]; 118ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar } 119abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 120ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar /// back - Get the last character in the string. 121e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner char back() const { 122e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner assert(!empty()); 123e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner return Data[Length-1]; 124e36df3fd31a08a41d9ad04fcba182b616b030c9cChris Lattner } 1257cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 126dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // copy - Allocate copy in Allocator and return StringRef to it. 127dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines template <typename Allocator> StringRef copy(Allocator &A) { 128dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines char *S = A.template Allocate<char>(Length); 12936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::copy(begin(), end(), S); 13036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return StringRef(S, Length); 13136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 13236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1337cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar /// equals - Check for string equality, this is more efficient than 134ac55b85438da378bab227fd34167bb0c4a9249aaDaniel Dunbar /// compare() when the relative ordering of inequal strings isn't needed. 1352928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar bool equals(StringRef RHS) const { 136abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher return (Length == RHS.Length && 1376d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis compareMemory(Data, RHS.Data, RHS.Length) == 0); 1387cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 1394cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 14005872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer /// equals_lower - Check for string equality, ignoring case. 14105872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer bool equals_lower(StringRef RHS) const { 14205872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer return Length == RHS.Length && compare_lower(RHS) == 0; 14305872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer } 14405872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer 1454cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// compare - Compare two strings; the result is -1, 0, or 1 if this string 1462d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// is lexicographically less than, equal to, or greater than the \p RHS. 1472928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar int compare(StringRef RHS) const { 1484cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar // Check the prefix for a mismatch. 1496d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis if (int Res = compareMemory(Data, RHS.Data, min(Length, RHS.Length))) 1504cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar return Res < 0 ? -1 : 1; 1514cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 1524cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar // Otherwise the prefixes match, so we only need to check the lengths. 1534cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar if (Length == RHS.Length) 1544cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar return 0; 1554cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar return Length < RHS.Length ? -1 : 1; 1564cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar } 1574cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 15805872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer /// compare_lower - Compare two strings, ignoring case. 15905872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer int compare_lower(StringRef RHS) const; 16005872ea804cdc9534960b30d28a391928c61481aBenjamin Kramer 161160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen /// compare_numeric - Compare two strings, treating sequences of digits as 162160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen /// numbers. 163160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen int compare_numeric(StringRef RHS) const; 164160a3bf74d1a2b048f65e2162d038ed96eddde01Jakob Stoklund Olesen 165326990f1eb7ff005adabe46a1f982eff8835813eMichael J. Spencer /// \brief Determine the edit distance between this string and another 166441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// string. 167441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// 168441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// \param Other the string to compare this string against. 169441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// 170441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// \param AllowReplacements whether to allow character 171441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// replacements (change one character into another) as a single 172441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// operation, rather than as two operations (an insertion and a 173441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// removal). 174441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// 1755ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor /// \param MaxEditDistance If non-zero, the maximum edit distance that 1765ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor /// this routine is allowed to compute. If the edit distance will exceed 1775ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor /// that maximum, returns \c MaxEditDistance+1. 1785ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor /// 179441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// \returns the minimum number of character insertions, removals, 180441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// or (if \p AllowReplacements is \c true) replacements needed to 181441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// transform one of the given strings into the other. If zero, 182441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor /// the strings are identical. 1835ee568ac2704d7302017d42ad162d4b53d076cbcDouglas Gregor unsigned edit_distance(StringRef Other, bool AllowReplacements = true, 1844a48389b27cafe30a38592b50e0f4b9e97b9d65eDmitri Gribenko unsigned MaxEditDistance = 0) const; 185441c8b4ad17c0d029b2247c367111395e7ad068cDouglas Gregor 1864cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// str - Get the contents as an std::string. 1877f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner std::string str() const { 188dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!Data) return std::string(); 1897f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner return std::string(Data, Length); 1907f7274ce7f3243bd71588c7a75a142c39e5c7e34Chris Lattner } 1914cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 1924cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @} 1934cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @name Operator Overloads 1944cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @{ 1954cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 196abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher char operator[](size_t Index) const { 1974cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar assert(Index < Length && "Invalid index!"); 198abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher return Data[Index]; 1994cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar } 2004cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 2014cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @} 2024cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @name Type Conversions 2034cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @{ 2044cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 2054cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar operator std::string() const { 2064cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar return str(); 2074cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar } 2084cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 2094cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar /// @} 2100ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// @name String Predicates 2110ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// @{ 2120ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar 2132d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Check if this string starts with the given \p Prefix. 2142928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar bool startswith(StringRef Prefix) const { 215d5b1f8a8426e82990dafc6e3336fefc6635c8fa4Eli Friedman return Length >= Prefix.Length && 2166d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis compareMemory(Data, Prefix.Data, Prefix.Length) == 0; 2170ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar } 2180ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar 219f34c3ca3046378bdb7e49b7366bafca0e0bafb9aRui Ueyama /// Check if this string starts with the given \p Prefix, ignoring case. 220f34c3ca3046378bdb7e49b7366bafca0e0bafb9aRui Ueyama bool startswith_lower(StringRef Prefix) const; 221f34c3ca3046378bdb7e49b7366bafca0e0bafb9aRui Ueyama 2222d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Check if this string ends with the given \p Suffix. 2232928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar bool endswith(StringRef Suffix) const { 224d5b1f8a8426e82990dafc6e3336fefc6635c8fa4Eli Friedman return Length >= Suffix.Length && 2256d5502eb4966fb2f81fe951d0acf11cfa5cd4acfArgyrios Kyrtzidis compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0; 2260ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar } 2270ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar 228f34c3ca3046378bdb7e49b7366bafca0e0bafb9aRui Ueyama /// Check if this string ends with the given \p Suffix, ignoring case. 229f34c3ca3046378bdb7e49b7366bafca0e0bafb9aRui Ueyama bool endswith_lower(StringRef Suffix) const; 230f34c3ca3046378bdb7e49b7366bafca0e0bafb9aRui Ueyama 2310ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// @} 2320ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// @name String Searching 2330ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// @{ 2340ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar 2352d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Search for the first character \p C in the string. 2360ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// 2372d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \returns The index of the first occurrence of \p C, or npos if not 2380ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// found. 23964066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar size_t find(char C, size_t From = 0) const { 2402fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin for (size_t i = min(From, Length), e = Length; i != e; ++i) 2410ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar if (Data[i] == C) 2420ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar return i; 2430ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar return npos; 2440ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar } 2450ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar 2462d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Search for the first string \p Str in the string. 2470ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// 2482d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \returns The index of the first occurrence of \p Str, or npos if not 2490ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// found. 25064066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar size_t find(StringRef Str, size_t From = 0) const; 251abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 2522d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Search for the last character \p C in the string. 253e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// 2542d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \returns The index of the last occurrence of \p C, or npos if not 255e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// found. 256c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner size_t rfind(char C, size_t From = npos) const { 2572fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin From = min(From, Length); 258c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner size_t i = From; 259c936fe8cb356931f23e6f17ce1bd5789eeae1ecbChris Lattner while (i != 0) { 260e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar --i; 261e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar if (Data[i] == C) 262e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar return i; 263e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar } 264e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar return npos; 265e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar } 266abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 2672d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Search for the last string \p Str in the string. 268e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// 2692d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \returns The index of the last occurrence of \p Str, or npos if not 270e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// found. 2712928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar size_t rfind(StringRef Str) const; 272abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 2732d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the first character in the string that is \p C, or npos if not 2742d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// found. Same as find. 275a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_first_of(char C, size_t From = 0) const { 27679ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer return find(C, From); 27779ed2c597a134cb4dd5da90199053f322b7d8a2dBenjamin Kramer } 278abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 2792d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the first character in the string that is in \p Chars, or npos if 2802d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// not found. 28164066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar /// 2822d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Complexity: O(size() + Chars.size()) 283a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_first_of(StringRef Chars, size_t From = 0) const; 284abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 2852d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the first character in the string that is not \p C or npos if not 2862d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// found. 287a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_first_not_of(char C, size_t From = 0) const; 28864066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar 2892d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the first character in the string that is not in the string 2902d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \p Chars, or npos if not found. 29164066bd8b593082f622bbc25716938a453363d2fDaniel Dunbar /// 2922d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Complexity: O(size() + Chars.size()) 293a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_first_not_of(StringRef Chars, size_t From = 0) const; 294abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 2952d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the last character in the string that is \p C, or npos if not 2962d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// found. 297a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_last_of(char C, size_t From = npos) const { 29863c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer return rfind(C, From); 29963c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer } 30063c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer 3012d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the last character in the string that is in \p C, or npos if not 3022d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// found. 30363c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer /// 3042d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Complexity: O(size() + Chars.size()) 305a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_last_of(StringRef Chars, size_t From = npos) const; 30663c133b67d61b0c457ff46c957aed2b8d90b599cMichael J. Spencer 3072d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the last character in the string that is not \p C, or npos if not 3082d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// found. 309a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_last_not_of(char C, size_t From = npos) const; 310b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer 3112d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Find the last character in the string that is not in \p Chars, or 3122d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// npos if not found. 313b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer /// 3142d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Complexity: O(size() + Chars.size()) 315a7a05ee70cb07f32996a0587a636b406c746b71bAaron Ballman size_t find_last_not_of(StringRef Chars, size_t From = npos) const; 316b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer 31705a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner /// @} 31805a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner /// @name Helpful Algorithms 31905a32c8ab118d9c92dc9b4ecaa7a6fed67241215Chris Lattner /// @{ 320abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 3212d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return the number of occurrences of \p C in the string. 3225caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar size_t count(char C) const { 3235caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar size_t Count = 0; 3245caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar for (size_t i = 0, e = Length; i != e; ++i) 3255caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar if (Data[i] == C) 326323a3e653340781bad4c4c3245d9b25d5ab02685Daniel Dunbar ++Count; 3275caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar return Count; 3285caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar } 329abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 3302d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return the number of non-overlapped occurrences of \p Str in 3315caba3bcb14fae4b36924463ed2bcf3846f029a9Daniel Dunbar /// the string. 3322928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar size_t count(StringRef Str) const; 333abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 3342d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Parse the current string as an integer of the specified radix. If 3352d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \p Radix is specified as zero, this does radix autosensing using 336cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner /// extended C rules: 0 is octal, 0x is hex, 0b is binary. 337cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner /// 338cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner /// If the string is invalid or if only a subset of the string is valid, 339cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner /// this returns true to signify the error. The string is considered 3409130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer /// erroneous if empty or if it overflows T. 3419130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer template <typename T> 34236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type 3439130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer getAsInteger(unsigned Radix, T &Result) const { 3449130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer long long LLVal; 3459130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer if (getAsSignedInteger(*this, Radix, LLVal) || 3469130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer static_cast<T>(LLVal) != LLVal) 3479130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer return true; 3489130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer Result = LLVal; 3499130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer return false; 3509130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer } 351cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner 3529130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer template <typename T> 35336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type 3549130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer getAsInteger(unsigned Radix, T &Result) const { 3559130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer unsigned long long ULLVal; 3569130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer if (getAsUnsignedInteger(*this, Radix, ULLVal) || 3579130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer static_cast<T>(ULLVal) != ULLVal) 3589130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer return true; 3599130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer Result = ULLVal; 3609130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer return false; 3619130b42a85998238b7bbe25ed2989e0605f636f0Michael J. Spencer } 362abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 3632d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Parse the current string as an integer of the specified \p Radix, or of 3642d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// an autosensed radix if the \p Radix given is 0. The current value in 3652d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \p Result is discarded, and the storage is changed to be wide enough to 3662d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// store the parsed integer. 3671e7ad3993d8700488895fa372ecad55443f53485John McCall /// 3682d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \returns true if the string does not solely consist of a valid 3691e7ad3993d8700488895fa372ecad55443f53485John McCall /// non-empty number in the appropriate base. 3701e7ad3993d8700488895fa372ecad55443f53485John McCall /// 3711e7ad3993d8700488895fa372ecad55443f53485John McCall /// APInt::fromString is superficially similar but assumes the 3721e7ad3993d8700488895fa372ecad55443f53485John McCall /// string is well-formed in the given radix. 3731e7ad3993d8700488895fa372ecad55443f53485John McCall bool getAsInteger(unsigned Radix, APInt &Result) const; 3741e7ad3993d8700488895fa372ecad55443f53485John McCall 375cea1438cf59c7cd3a632d61d78e68589315510d3Chris Lattner /// @} 376589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar /// @name String Operations 377589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar /// @{ 378589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar 3792d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko // Convert the given ASCII string to lowercase. 380589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar std::string lower() const; 381589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar 3822d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Convert the given ASCII string to uppercase. 383589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar std::string upper() const; 384589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar 385589fbb1770df5f7bee1c5e24e9e8f4ca5091d528Daniel Dunbar /// @} 3860ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar /// @name Substring Operations 387f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar /// @{ 388f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar 3892d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return a reference to the substring from [Start, Start + N). 390f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar /// 3912d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \param Start The index of the starting character in the substring; if 392d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// the index is npos or greater than the length of the string then the 393d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// empty substring will be returned. 394f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar /// 3952d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \param N The number of characters to included in the substring. If N 396f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar /// exceeds the number of characters remaining in the string, the string 3972d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// suffix (starting with \p Start) will be returned. 398f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar StringRef substr(size_t Start, size_t N = npos) const { 3992fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin Start = min(Start, Length); 4002fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin return StringRef(Data + Start, min(N, Length - Start)); 401f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar } 402d433902628e8c48059f66ba316a7182460ac4a84Matt Arsenault 4032d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return a StringRef equal to 'this' but with the first \p N elements 4042d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// dropped. 405a45391000ebfcf6193a97f2bb558f5beb845ee65Peng Cheng StringRef drop_front(size_t N = 1) const { 406a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner assert(size() >= N && "Dropping more elements than exist"); 407a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner return substr(N); 408a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner } 409a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner 4102d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return a StringRef equal to 'this' but with the last \p N elements 4112d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// dropped. 412a45391000ebfcf6193a97f2bb558f5beb845ee65Peng Cheng StringRef drop_back(size_t N = 1) const { 413a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner assert(size() >= N && "Dropping more elements than exist"); 414a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner return substr(0, size()-N); 415a97a5eabe2a412d7cc078f94b7df7ee8ac840853Chris Lattner } 416f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar 4172d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return a reference to the substring from [Start, End). 418d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// 4192d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \param Start The index of the starting character in the substring; if 420d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// the index is npos or greater than the length of the string then the 421d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// empty substring will be returned. 422d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// 4232d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \param End The index following the last character to include in the 4242d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// substring. If this is npos, or less than \p Start, or exceeds the 425d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// number of characters remaining in the string, the string suffix 4262d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// (starting with \p Start) will be returned. 427d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar StringRef slice(size_t Start, size_t End) const { 4282fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin Start = min(Start, Length); 4292fe05d80d396b6ca0bdc66ec17732b8c514d575eTorok Edwin End = min(max(Start, End), Length); 430d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar return StringRef(Data + Start, End - Start); 431d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar } 432d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar 4332d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Split into two substrings around the first occurrence of a separator 4342d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// character. 435d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// 4362d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// If \p Separator is in the string, then the result is a pair (LHS, RHS) 437d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// such that (*this == LHS + Separator + RHS) is true and RHS is 4382d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// maximal. If \p Separator is not in the string, then the result is a 439d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). 440d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar /// 4412d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \param Separator The character to split on. 4422d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \returns The split substrings. 443d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar std::pair<StringRef, StringRef> split(char Separator) const { 4440ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar size_t Idx = find(Separator); 4450ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar if (Idx == npos) 446d61918fc6898a89df8b0a03e068f234ded010cdfDaniel Dunbar return std::make_pair(*this, StringRef()); 4470ad7f9bb2f806387e53ffeaf6a564b9a80b962afDaniel Dunbar return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); 448c9af366fc39d657b7d416d16988265d86a641184Daniel Dunbar } 449c9af366fc39d657b7d416d16988265d86a641184Daniel Dunbar 4502d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Split into two substrings around the first occurrence of a separator 4512d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// string. 452a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar /// 4532d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// If \p Separator is in the string, then the result is a pair (LHS, RHS) 454a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar /// such that (*this == LHS + Separator + RHS) is true and RHS is 4552d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// maximal. If \p Separator is not in the string, then the result is a 456a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). 457a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar /// 458a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar /// \param Separator - The string to split on. 459a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar /// \return - The split substrings. 460a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar std::pair<StringRef, StringRef> split(StringRef Separator) const { 461a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar size_t Idx = find(Separator); 462a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar if (Idx == npos) 463a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar return std::make_pair(*this, StringRef()); 464a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos)); 465a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar } 466a8333d3d107df1e0b6bde986ed0532915e154b65Daniel Dunbar 4672d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Split into substrings around the occurrences of a separator string. 4685ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// 4692d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most 4702d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// \p MaxSplit splits are done and consequently <= \p MaxSplit 4715ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// elements are added to A. 4722d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// If \p KeepEmpty is false, empty strings are not added to \p A. They 4732d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// still count when considering \p MaxSplit 4745ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// An useful invariant is that 4755ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true 4765ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// 4775ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// \param A - Where to put the substrings. 4785ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// \param Separator - The string to split on. 4795ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola /// \param MaxSplit - The maximum number of times the string is split. 480fb76fe09297ee292129e44d723127f2408602a3dDan Gohman /// \param KeepEmpty - True if empty substring should be added. 481c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola void split(SmallVectorImpl<StringRef> &A, 482c78c0c99a0fe1703ae72fc51e440aaa8e4e19e91Rafael Espindola StringRef Separator, int MaxSplit = -1, 4835ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola bool KeepEmpty = true) const; 4845ccac247263ab62975f3b72421fc783f10ccf5f6Rafael Espindola 4852d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Split into two substrings around the last occurrence of a separator 4862d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// character. 487e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// 4882d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// If \p Separator is in the string, then the result is a pair (LHS, RHS) 489e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// such that (*this == LHS + Separator + RHS) is true and RHS is 4902d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// minimal. If \p Separator is not in the string, then the result is a 491e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). 492e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// 493e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// \param Separator - The character to split on. 494e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar /// \return - The split substrings. 495e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar std::pair<StringRef, StringRef> rsplit(char Separator) const { 496e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar size_t Idx = rfind(Separator); 497e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar if (Idx == npos) 498e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar return std::make_pair(*this, StringRef()); 499e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); 500e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar } 501e65512809a4144c17538aac4cc59fac6d325a7e4Daniel Dunbar 5022d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return string with consecutive characters in \p Chars starting from 5032d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// the left removed. 504b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const { 505b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer return drop_front(std::min(Length, find_first_not_of(Chars))); 506b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer } 507b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer 5082d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return string with consecutive characters in \p Chars starting from 5092d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// the right removed. 510b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const { 511b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1)); 512b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer } 513b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer 5142d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// Return string with consecutive characters in \p Chars starting from 5152d9eb72178af8e79dc6432cd1b7d29bde16da1b9Dmitri Gribenko /// the left and right removed. 516b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer StringRef trim(StringRef Chars = " \t\n\v\f\r") const { 517b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer return ltrim(Chars).rtrim(Chars); 518b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer } 519b0940b46edbbe9d3f62d7f6f70330fd87f3507e1Michael J. Spencer 520f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar /// @} 5214cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar }; 522f5fdf73238dfd923f33bcbbd397cff6752d9c41eDaniel Dunbar 5237cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar /// @name StringRef Comparison Operators 5247cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar /// @{ 5257cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 5262928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar inline bool operator==(StringRef LHS, StringRef RHS) { 5277cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar return LHS.equals(RHS); 5287cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 5297cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 5302928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar inline bool operator!=(StringRef LHS, StringRef RHS) { 5317cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar return !(LHS == RHS); 5327cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 533abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher 5342928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar inline bool operator<(StringRef LHS, StringRef RHS) { 535abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher return LHS.compare(RHS) == -1; 5367cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 5377cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 5382928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar inline bool operator<=(StringRef LHS, StringRef RHS) { 539abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher return LHS.compare(RHS) != 1; 5407cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 5417cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 5422928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar inline bool operator>(StringRef LHS, StringRef RHS) { 543abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher return LHS.compare(RHS) == 1; 5447cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 5457cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 5462928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar inline bool operator>=(StringRef LHS, StringRef RHS) { 547abc5ac728961cb23b1041a3466fe2eb4521603c3Eric Christopher return LHS.compare(RHS) != -1; 5487cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar } 5497cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 55096f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko inline std::string &operator+=(std::string &buffer, StringRef string) { 5516765f78efbd9d43f429344af7d997ec2df495b02John McCall return buffer.append(string.data(), string.size()); 5526765f78efbd9d43f429344af7d997ec2df495b02John McCall } 5536765f78efbd9d43f429344af7d997ec2df495b02John McCall 5547cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar /// @} 5557cb6860185187c74a1205deebff9e0f09a0ddae4Daniel Dunbar 556528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth /// \brief Compute a hash_code for a StringRef. 557528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth hash_code hash_value(StringRef S); 558528f0bbe19553dfadedca040df13a389daa7593dChandler Carruth 559847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer // StringRefs can be treated like a POD type. 560847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer template <typename T> struct isPodLike; 561847d2f93ca692fa627a97ba9743bc1e9b8c5cbaeBenjamin Kramer template <> struct isPodLike<StringRef> { static const bool value = true; }; 5624cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar} 5634cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar 5644cf95d75c65f37677d306952b0d2306bc6d20b1fDaniel Dunbar#endif 565