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
20 /// StringRef - Represent a constant reference to a string, i.e. a character
21 /// array and a length, which need not be null terminated.
23 /// This class does not own the string data, it is expected to be used in
24 /// situations where the character data resides in some other buffer, whose
25 /// lifetime extends past that of the StringRef. For this reason, it is not in
26 /// general safe to store a StringRef.
29 typedef const char *iterator;
30 static const size_t npos = ~size_t(0);
31 typedef size_t size_type;
34 /// The start of the string, in an external buffer.
37 /// The length of the string.
41 /// @name Constructors
44 /// Construct an empty string ref.
45 /*implicit*/ StringRef() : Data(0), Length(0) {}
47 /// Construct a string ref from a cstring.
48 /*implicit*/ StringRef(const char *Str)
49 : Data(Str), Length(::strlen(Str)) {}
51 /// Construct a string ref from a pointer and length.
52 /*implicit*/ StringRef(const char *_Data, unsigned _Length)
53 : Data(_Data), Length(_Length) {}
55 /// Construct a string ref from an std::string.
56 /*implicit*/ StringRef(const std::string &Str)
57 : Data(Str.c_str()), Length(Str.length()) {}
63 iterator begin() const { return Data; }
65 iterator end() const { return Data + Length; }
68 /// @name String Operations
71 /// data - Get a pointer to the start of the string (which may not be null
73 const char *data() const { return Data; }
75 /// empty - Check if the string is empty.
76 bool empty() const { return Length == 0; }
78 /// size - Get the string size.
79 size_t size() const { return Length; }
81 /// front - Get the first character in the string.
87 /// back - Get the last character in the string.
90 return Data[Length-1];
93 /// equals - Check for string equality, this is more efficient than
94 /// compare() when the relative ordering of inequal strings isn't needed.
95 bool equals(const StringRef &RHS) const {
96 return (Length == RHS.Length &&
97 memcmp(Data, RHS.Data, RHS.Length) == 0);
100 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
101 /// is lexicographically less than, equal to, or greater than the \arg RHS.
102 int compare(const StringRef &RHS) const {
103 // Check the prefix for a mismatch.
104 if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
105 return Res < 0 ? -1 : 1;
107 // Otherwise the prefixes match, so we only need to check the lengths.
108 if (Length == RHS.Length)
110 return Length < RHS.Length ? -1 : 1;
113 /// str - Get the contents as an std::string.
114 std::string str() const { return std::string(Data, Length); }
117 /// @name Operator Overloads
120 char operator[](size_t Index) const {
121 assert(Index < Length && "Invalid index!");
126 /// @name Type Conversions
129 operator std::string() const {
134 /// @name String Predicates
137 /// startswith - Check if this string starts with the given \arg Prefix.
138 bool startswith(const StringRef &Prefix) const {
139 return substr(0, Prefix.Length).equals(Prefix);
142 /// endswith - Check if this string ends with the given \arg Suffix.
143 bool endswith(const StringRef &Suffix) const {
144 return slice(size() - Suffix.Length, size()).equals(Suffix);
148 /// @name String Searching
151 /// find - Search for the first character \arg C in the string.
153 /// \return - The index of the first occurence of \arg C, or npos if not
155 size_t find(char C) const {
156 for (size_t i = 0, e = Length; i != e; ++i)
162 /// find - Search for the first string \arg Str in the string.
164 /// \return - The index of the first occurence of \arg Str, or npos if not
166 size_t find(const StringRef &Str) const;
168 /// rfind - Search for the last character \arg C in the string.
170 /// \return - The index of the last occurence of \arg C, or npos if not
172 size_t rfind(char C, size_t From) const {
173 for (size_t i = From, e = 0; i != e;) {
181 size_t rfind(char C) const {
182 return rfind(C, Length);
185 /// rfind - Search for the last string \arg Str in the string.
187 /// \return - The index of the last occurence of \arg Str, or npos if not
189 size_t rfind(const StringRef &Str) const;
191 /// find_first_of - Find the first instance of the specified character or
192 /// return npos if not in string. Same as find.
193 size_type find_first_of(char C) const { return find(C); }
195 /// find_first_of - Find the first character from the string 'Chars' in the
196 /// current string or return npos if not in string.
197 size_type find_first_of(StringRef Chars) const;
199 /// find_first_not_of - Find the first character in the string that is not
200 /// in the string 'Chars' or return npos if all are in string. Same as find.
201 size_type find_first_not_of(StringRef Chars) const;
204 /// @name Helpful Algorithms
207 /// count - Return the number of occurrences of \arg C in the string.
208 size_t count(char C) const {
210 for (size_t i = 0, e = Length; i != e; ++i)
216 /// count - Return the number of non-overlapped occurrences of \arg Str in
218 size_t count(const StringRef &Str) const;
220 /// getAsInteger - Parse the current string as an integer of the specified
221 /// radix. If Radix is specified as zero, this does radix autosensing using
222 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
224 /// If the string is invalid or if only a subset of the string is valid,
225 /// this returns true to signify the error. The string is considered
226 /// erroneous if empty.
228 bool getAsInteger(unsigned Radix, long long &Result) const;
229 bool getAsInteger(unsigned Radix, unsigned long long &Result) const;
230 bool getAsInteger(unsigned Radix, int &Result) const;
231 bool getAsInteger(unsigned Radix, unsigned &Result) const;
233 // TODO: Provide overloads for int/unsigned that check for overflow.
236 /// @name Substring Operations
239 /// substr - Return a reference to the substring from [Start, Start + N).
241 /// \param Start - The index of the starting character in the substring; if
242 /// the index is npos or greater than the length of the string then the
243 /// empty substring will be returned.
245 /// \param N - The number of characters to included in the substring. If N
246 /// exceeds the number of characters remaining in the string, the string
247 /// suffix (starting with \arg Start) will be returned.
248 StringRef substr(size_t Start, size_t N = npos) const {
249 Start = std::min(Start, Length);
250 return StringRef(Data + Start, std::min(N, Length - Start));
253 /// slice - Return a reference to the substring from [Start, End).
255 /// \param Start - The index of the starting character in the substring; if
256 /// the index is npos or greater than the length of the string then the
257 /// empty substring will be returned.
259 /// \param End - The index following the last character to include in the
260 /// substring. If this is npos, or less than \arg Start, or exceeds the
261 /// number of characters remaining in the string, the string suffix
262 /// (starting with \arg Start) will be returned.
263 StringRef slice(size_t Start, size_t End) const {
264 Start = std::min(Start, Length);
265 End = std::min(std::max(Start, End), Length);
266 return StringRef(Data + Start, End - Start);
269 /// split - Split into two substrings around the first occurence of a
270 /// separator character.
272 /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
273 /// such that (*this == LHS + Separator + RHS) is true and RHS is
274 /// maximal. If \arg Separator is not in the string, then the result is a
275 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
277 /// \param Separator - The character to split on.
278 /// \return - The split substrings.
279 std::pair<StringRef, StringRef> split(char Separator) const {
280 size_t Idx = find(Separator);
282 return std::make_pair(*this, StringRef());
283 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
286 /// rsplit - Split into two substrings around the last occurence of a
287 /// separator character.
289 /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
290 /// such that (*this == LHS + Separator + RHS) is true and RHS is
291 /// minimal. If \arg Separator is not in the string, then the result is a
292 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
294 /// \param Separator - The character to split on.
295 /// \return - The split substrings.
296 std::pair<StringRef, StringRef> rsplit(char Separator) const {
297 size_t Idx = rfind(Separator);
299 return std::make_pair(*this, StringRef());
300 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
306 /// @name StringRef Comparison Operators
309 inline bool operator==(const StringRef &LHS, const StringRef &RHS) {
310 return LHS.equals(RHS);
313 inline bool operator!=(const StringRef &LHS, const StringRef &RHS) {
314 return !(LHS == RHS);
317 inline bool operator<(const StringRef &LHS, const StringRef &RHS) {
318 return LHS.compare(RHS) == -1;
321 inline bool operator<=(const StringRef &LHS, const StringRef &RHS) {
322 return LHS.compare(RHS) != 1;
325 inline bool operator>(const StringRef &LHS, const StringRef &RHS) {
326 return LHS.compare(RHS) == 1;
329 inline bool operator>=(const StringRef &LHS, const StringRef &RHS) {
330 return LHS.compare(RHS) != -1;