1 //===--- StringRef.h - Constant String Reference Wrapper --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_STRINGREF_H
11 #define LLVM_ADT_STRINGREF_H
18 /// StringRef - Represent a constant reference to a string, i.e. a character
19 /// array and a length, which need not be null terminated.
21 /// This class does not own the string data, it is expected to be used in
22 /// situations where the character data resides in some other buffer, whose
23 /// lifetime extends past that of the StringRef. For this reason, it is not in
24 /// general safe to store a StringRef.
27 typedef const char *iterator;
28 static const size_t npos = std::string::npos;
31 /// The start of the string, in an external buffer.
34 /// The length of the string.
38 /// @name Constructors
41 /// Construct an empty string ref.
42 /*implicit*/ StringRef() : Data(0), Length(0) {}
44 /// Construct a string ref from a cstring.
45 /*implicit*/ StringRef(const char *Str)
46 : Data(Str), Length(::strlen(Str)) {}
48 /// Construct a string ref from a pointer and length.
49 /*implicit*/ StringRef(const char *_Data, unsigned _Length)
50 : Data(_Data), Length(_Length) {}
52 /// Construct a string ref from an std::string.
53 /*implicit*/ StringRef(const std::string &Str)
54 : Data(Str.c_str()), Length(Str.length()) {}
60 iterator begin() const { return Data; }
62 iterator end() const { return Data + Length; }
65 /// @name String Operations
68 /// data - Get a pointer to the start of the string (which may not be null
70 const char *data() const { return Data; }
72 /// empty - Check if the string is empty.
73 bool empty() const { return Length == 0; }
75 /// size - Get the string size.
76 unsigned size() const { return Length; }
78 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
79 /// is lexicographically less than, equal to, or greater than the \arg RHS.
80 int compare(const StringRef &RHS) const {
81 // Check the prefix for a mismatch.
82 if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
83 return Res < 0 ? -1 : 1;
85 // Otherwise the prefixes match, so we only need to check the lengths.
86 if (Length == RHS.Length)
88 return Length < RHS.Length ? -1 : 1;
91 /// str - Get the contents as an std::string.
92 std::string str() const { return std::string(Data, Length); }
95 /// @name Operator Overloads
98 bool operator==(const StringRef &RHS) const {
99 return Length == RHS.Length && memcmp(Data, RHS.Data, Length) == 0;
102 bool operator!=(const StringRef &RHS) const { return !(*this == RHS); }
104 bool operator<(const StringRef &RHS) const { return compare(RHS) == -1; }
106 bool operator<=(const StringRef &RHS) const { return compare(RHS) != 1; }
108 bool operator>(const StringRef &RHS) const { return compare(RHS) == 1; }
110 bool operator>=(const StringRef &RHS) const { return compare(RHS) != -1; }
112 char operator[](size_t Index) const {
113 assert(Index < Length && "Invalid index!");
118 /// @name Type Conversions
121 operator std::string() const {
126 /// @name Utility Functions
129 /// substr - Return a reference to a substring of this object.
131 /// \param Start - The index of the starting character in the substring; if
132 /// the index is greater than the length of the string then the empty
133 /// substring will be returned.
135 /// \param N - The number of characters to included in the substring. If N
136 /// exceeds the number of characters remaining in the string, the string
137 /// suffix (starting with \arg Start) will be returned.
138 StringRef substr(size_t Start, size_t N = npos) const {
139 Start = std::min(Start, Length);
140 return StringRef(Data + Start, std::min(N, Length - Start));
143 /// startswith - Check if this string starts with the given \arg Prefix.
144 bool startswith(const StringRef &Prefix) const {
145 return substr(0, Prefix.Length) == Prefix;