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 class SmallVectorImpl;
22 /// StringRef - Represent a constant reference to a string, i.e. a character
23 /// array and a length, which need not be null terminated.
25 /// This class does not own the string data, it is expected to be used in
26 /// situations where the character data resides in some other buffer, whose
27 /// lifetime extends past that of the StringRef. For this reason, it is not in
28 /// general safe to store a StringRef.
31 typedef const char *iterator;
32 static const size_t npos = ~size_t(0);
33 typedef size_t size_type;
36 /// The start of the string, in an external buffer.
39 /// The length of the string.
43 /// @name Constructors
46 /// Construct an empty string ref.
47 /*implicit*/ StringRef() : Data(0), Length(0) {}
49 /// Construct a string ref from a cstring.
50 /*implicit*/ StringRef(const char *Str)
51 : Data(Str), Length(::strlen(Str)) {}
53 /// Construct a string ref from a pointer and length.
54 /*implicit*/ StringRef(const char *data, size_t length)
55 : Data(data), Length(length) {}
57 /// Construct a string ref from an std::string.
58 /*implicit*/ StringRef(const std::string &Str)
59 : Data(Str.data()), Length(Str.length()) {}
65 iterator begin() const { return Data; }
67 iterator end() const { return Data + Length; }
70 /// @name String Operations
73 /// data - Get a pointer to the start of the string (which may not be null
75 const char *data() const { return Data; }
77 /// empty - Check if the string is empty.
78 bool empty() const { return Length == 0; }
80 /// size - Get the string size.
81 size_t size() const { return Length; }
83 /// front - Get the first character in the string.
89 /// back - Get the last character in the string.
92 return Data[Length-1];
95 /// equals - Check for string equality, this is more efficient than
96 /// compare() when the relative ordering of inequal strings isn't needed.
97 bool equals(StringRef RHS) const {
98 return (Length == RHS.Length &&
99 memcmp(Data, RHS.Data, RHS.Length) == 0);
102 /// equals_lower - Check for string equality, ignoring case.
103 bool equals_lower(StringRef RHS) const {
104 return Length == RHS.Length && compare_lower(RHS) == 0;
107 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
108 /// is lexicographically less than, equal to, or greater than the \arg RHS.
109 int compare(StringRef RHS) const {
110 // Check the prefix for a mismatch.
111 if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
112 return Res < 0 ? -1 : 1;
114 // Otherwise the prefixes match, so we only need to check the lengths.
115 if (Length == RHS.Length)
117 return Length < RHS.Length ? -1 : 1;
120 /// compare_lower - Compare two strings, ignoring case.
121 int compare_lower(StringRef RHS) const;
123 /// str - Get the contents as an std::string.
124 std::string str() const { return std::string(Data, Length); }
127 /// @name Operator Overloads
130 char operator[](size_t Index) const {
131 assert(Index < Length && "Invalid index!");
136 /// @name Type Conversions
139 operator std::string() const {
144 /// @name String Predicates
147 /// startswith - Check if this string starts with the given \arg Prefix.
148 bool startswith(StringRef Prefix) const {
149 return substr(0, Prefix.Length).equals(Prefix);
152 /// endswith - Check if this string ends with the given \arg Suffix.
153 bool endswith(StringRef Suffix) const {
154 return slice(size() - Suffix.Length, size()).equals(Suffix);
158 /// @name String Searching
161 /// find - Search for the first character \arg C in the string.
163 /// \return - The index of the first occurence of \arg C, or npos if not
165 size_t find(char C, size_t From = 0) const {
166 for (size_t i = std::min(From, Length), e = Length; i != e; ++i)
172 /// find - Search for the first string \arg Str in the string.
174 /// \return - The index of the first occurence of \arg Str, or npos if not
176 size_t find(StringRef Str, size_t From = 0) const;
178 /// rfind - Search for the last character \arg C in the string.
180 /// \return - The index of the last occurence of \arg C, or npos if not
182 size_t rfind(char C, size_t From = npos) const {
183 From = std::min(From, Length);
193 /// rfind - Search for the last string \arg Str in the string.
195 /// \return - The index of the last occurence of \arg Str, or npos if not
197 size_t rfind(StringRef Str) const;
199 /// find_first_of - Find the first character in the string that is \arg C,
200 /// or npos if not found. Same as find.
201 size_type find_first_of(char C, size_t = 0) const { return find(C); }
203 /// find_first_of - Find the first character in the string that is in \arg
204 /// Chars, or npos if not found.
206 /// Note: O(size() * Chars.size())
207 size_type find_first_of(StringRef Chars, size_t From = 0) const;
209 /// find_first_not_of - Find the first character in the string that is not
210 /// \arg C or npos if not found.
211 size_type find_first_not_of(char C, size_t From = 0) const;
213 /// find_first_not_of - Find the first character in the string that is not
214 /// in the string \arg Chars, or npos if not found.
216 /// Note: O(size() * Chars.size())
217 size_type find_first_not_of(StringRef Chars, size_t From = 0) const;
220 /// @name Helpful Algorithms
223 /// count - Return the number of occurrences of \arg C in the string.
224 size_t count(char C) const {
226 for (size_t i = 0, e = Length; i != e; ++i)
232 /// count - Return the number of non-overlapped occurrences of \arg Str in
234 size_t count(StringRef Str) const;
236 /// getAsInteger - Parse the current string as an integer of the specified
237 /// radix. If Radix is specified as zero, this does radix autosensing using
238 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
240 /// If the string is invalid or if only a subset of the string is valid,
241 /// this returns true to signify the error. The string is considered
242 /// erroneous if empty.
244 bool getAsInteger(unsigned Radix, long long &Result) const;
245 bool getAsInteger(unsigned Radix, unsigned long long &Result) const;
246 bool getAsInteger(unsigned Radix, int &Result) const;
247 bool getAsInteger(unsigned Radix, unsigned &Result) const;
249 // TODO: Provide overloads for int/unsigned that check for overflow.
252 /// @name Substring Operations
255 /// substr - Return a reference to the substring from [Start, Start + N).
257 /// \param Start - The index of the starting character in the substring; if
258 /// the index is npos or greater than the length of the string then the
259 /// empty substring will be returned.
261 /// \param N - The number of characters to included in the substring. If N
262 /// exceeds the number of characters remaining in the string, the string
263 /// suffix (starting with \arg Start) will be returned.
264 StringRef substr(size_t Start, size_t N = npos) const {
265 Start = std::min(Start, Length);
266 return StringRef(Data + Start, std::min(N, Length - Start));
269 /// slice - Return a reference to the substring from [Start, End).
271 /// \param Start - The index of the starting character in the substring; if
272 /// the index is npos or greater than the length of the string then the
273 /// empty substring will be returned.
275 /// \param End - The index following the last character to include in the
276 /// substring. If this is npos, or less than \arg Start, or exceeds the
277 /// number of characters remaining in the string, the string suffix
278 /// (starting with \arg Start) will be returned.
279 StringRef slice(size_t Start, size_t End) const {
280 Start = std::min(Start, Length);
281 End = std::min(std::max(Start, End), Length);
282 return StringRef(Data + Start, End - Start);
285 /// split - Split into two substrings around the first occurence of a
286 /// separator character.
288 /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
289 /// such that (*this == LHS + Separator + RHS) is true and RHS is
290 /// maximal. If \arg Separator is not in the string, then the result is a
291 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
293 /// \param Separator - The character to split on.
294 /// \return - The split substrings.
295 std::pair<StringRef, StringRef> split(char Separator) const {
296 size_t Idx = find(Separator);
298 return std::make_pair(*this, StringRef());
299 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
302 /// split - Split into two substrings around the first occurence of a
303 /// separator string.
305 /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
306 /// such that (*this == LHS + Separator + RHS) is true and RHS is
307 /// maximal. If \arg Separator is not in the string, then the result is a
308 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
310 /// \param Separator - The string to split on.
311 /// \return - The split substrings.
312 std::pair<StringRef, StringRef> split(StringRef Separator) const {
313 size_t Idx = find(Separator);
315 return std::make_pair(*this, StringRef());
316 return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
319 /// split - Split into substrings around the occurences of a separator
322 /// Each substring is stored in \arg A. If \arg MaxSplit is >= 0, at most
323 /// \arg MaxSplit splits are done and consequently <= \arg MaxSplit
324 /// elements are added to A.
325 /// If \arg KeepEmpty is false, empty strings are not added to \arg A. They
326 /// still count when considering \arg MaxSplit
327 /// An useful invariant is that
328 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
330 /// \param A - Where to put the substrings.
331 /// \param Separator - The string to split on.
332 /// \param MaxSplit - The maximum number of times the string is split.
333 /// \parm KeepEmpty - True if empty substring should be added.
334 void split(SmallVectorImpl<StringRef> &A,
335 StringRef Separator, int MaxSplit = -1,
336 bool KeepEmpty = true) const;
338 /// rsplit - Split into two substrings around the last occurence of a
339 /// separator character.
341 /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
342 /// such that (*this == LHS + Separator + RHS) is true and RHS is
343 /// minimal. If \arg Separator is not in the string, then the result is a
344 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
346 /// \param Separator - The character to split on.
347 /// \return - The split substrings.
348 std::pair<StringRef, StringRef> rsplit(char Separator) const {
349 size_t Idx = rfind(Separator);
351 return std::make_pair(*this, StringRef());
352 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
358 /// @name StringRef Comparison Operators
361 inline bool operator==(StringRef LHS, StringRef RHS) {
362 return LHS.equals(RHS);
365 inline bool operator!=(StringRef LHS, StringRef RHS) {
366 return !(LHS == RHS);
369 inline bool operator<(StringRef LHS, StringRef RHS) {
370 return LHS.compare(RHS) == -1;
373 inline bool operator<=(StringRef LHS, StringRef RHS) {
374 return LHS.compare(RHS) != 1;
377 inline bool operator>(StringRef LHS, StringRef RHS) {
378 return LHS.compare(RHS) == 1;
381 inline bool operator>=(StringRef LHS, StringRef RHS) {
382 return LHS.compare(RHS) != -1;