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
22 class SmallVectorImpl;
27 /// Helper functions for StringRef::getAsInteger.
28 bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
29 unsigned long long &Result);
31 bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
33 /// StringRef - Represent a constant reference to a string, i.e. a character
34 /// array and a length, which need not be null terminated.
36 /// This class does not own the string data, it is expected to be used in
37 /// situations where the character data resides in some other buffer, whose
38 /// lifetime extends past that of the StringRef. For this reason, it is not in
39 /// general safe to store a StringRef.
42 typedef const char *iterator;
43 typedef const char *const_iterator;
44 static const size_t npos = ~size_t(0);
45 typedef size_t size_type;
48 /// The start of the string, in an external buffer.
51 /// The length of the string.
54 // Workaround memcmp issue with null pointers (undefined behavior)
55 // by providing a specialized version
56 static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
57 if (Length == 0) { return 0; }
58 return ::memcmp(Lhs,Rhs,Length);
62 /// @name Constructors
65 /// Construct an empty string ref.
66 /*implicit*/ StringRef() : Data(nullptr), Length(0) {}
68 /// Construct a string ref from a cstring.
69 /*implicit*/ StringRef(const char *Str)
71 assert(Str && "StringRef cannot be built from a NULL argument");
72 Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
75 /// Construct a string ref from a pointer and length.
76 /*implicit*/ StringRef(const char *data, size_t length)
77 : Data(data), Length(length) {
78 assert((data || length == 0) &&
79 "StringRef cannot be built from a NULL argument with non-null length");
82 /// Construct a string ref from an std::string.
83 /*implicit*/ StringRef(const std::string &Str)
84 : Data(Str.data()), Length(Str.length()) {}
90 iterator begin() const { return Data; }
92 iterator end() const { return Data + Length; }
94 const unsigned char *bytes_begin() const {
95 return reinterpret_cast<const unsigned char *>(begin());
97 const unsigned char *bytes_end() const {
98 return reinterpret_cast<const unsigned char *>(end());
102 /// @name String Operations
105 /// data - Get a pointer to the start of the string (which may not be null
107 const char *data() const { return Data; }
109 /// empty - Check if the string is empty.
110 bool empty() const { return Length == 0; }
112 /// size - Get the string size.
113 size_t size() const { return Length; }
115 /// front - Get the first character in the string.
121 /// back - Get the last character in the string.
124 return Data[Length-1];
127 // copy - Allocate copy in Allocator and return StringRef to it.
128 template <typename Allocator> StringRef copy(Allocator &A) const {
129 char *S = A.template Allocate<char>(Length);
130 std::copy(begin(), end(), S);
131 return StringRef(S, Length);
134 /// equals - Check for string equality, this is more efficient than
135 /// compare() when the relative ordering of inequal strings isn't needed.
136 bool equals(StringRef RHS) const {
137 return (Length == RHS.Length &&
138 compareMemory(Data, RHS.Data, RHS.Length) == 0);
141 /// equals_lower - Check for string equality, ignoring case.
142 bool equals_lower(StringRef RHS) const {
143 return Length == RHS.Length && compare_lower(RHS) == 0;
146 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
147 /// is lexicographically less than, equal to, or greater than the \p RHS.
148 int compare(StringRef RHS) const {
149 // Check the prefix for a mismatch.
150 if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
151 return Res < 0 ? -1 : 1;
153 // Otherwise the prefixes match, so we only need to check the lengths.
154 if (Length == RHS.Length)
156 return Length < RHS.Length ? -1 : 1;
159 /// compare_lower - Compare two strings, ignoring case.
160 int compare_lower(StringRef RHS) const;
162 /// compare_numeric - Compare two strings, treating sequences of digits as
164 int compare_numeric(StringRef RHS) const;
166 /// \brief Determine the edit distance between this string and another
169 /// \param Other the string to compare this string against.
171 /// \param AllowReplacements whether to allow character
172 /// replacements (change one character into another) as a single
173 /// operation, rather than as two operations (an insertion and a
176 /// \param MaxEditDistance If non-zero, the maximum edit distance that
177 /// this routine is allowed to compute. If the edit distance will exceed
178 /// that maximum, returns \c MaxEditDistance+1.
180 /// \returns the minimum number of character insertions, removals,
181 /// or (if \p AllowReplacements is \c true) replacements needed to
182 /// transform one of the given strings into the other. If zero,
183 /// the strings are identical.
184 unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
185 unsigned MaxEditDistance = 0) const;
187 /// str - Get the contents as an std::string.
188 std::string str() const {
189 if (!Data) return std::string();
190 return std::string(Data, Length);
194 /// @name Operator Overloads
197 char operator[](size_t Index) const {
198 assert(Index < Length && "Invalid index!");
203 /// @name Type Conversions
206 operator std::string() const {
211 /// @name String Predicates
214 /// Check if this string starts with the given \p Prefix.
215 bool startswith(StringRef Prefix) const {
216 return Length >= Prefix.Length &&
217 compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
220 /// Check if this string starts with the given \p Prefix, ignoring case.
221 bool startswith_lower(StringRef Prefix) const;
223 /// Check if this string ends with the given \p Suffix.
224 bool endswith(StringRef Suffix) const {
225 return Length >= Suffix.Length &&
226 compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
229 /// Check if this string ends with the given \p Suffix, ignoring case.
230 bool endswith_lower(StringRef Suffix) const;
233 /// @name String Searching
236 /// Search for the first character \p C in the string.
238 /// \returns The index of the first occurrence of \p C, or npos if not
240 size_t find(char C, size_t From = 0) const {
241 for (size_t i = std::min(From, Length), e = Length; i != e; ++i)
247 /// Search for the first string \p Str in the string.
249 /// \returns The index of the first occurrence of \p Str, or npos if not
251 size_t find(StringRef Str, size_t From = 0) const;
253 /// Search for the last character \p C in the string.
255 /// \returns The index of the last occurrence of \p C, or npos if not
257 size_t rfind(char C, size_t From = npos) const {
258 From = std::min(From, Length);
268 /// Search for the last string \p Str in the string.
270 /// \returns The index of the last occurrence of \p Str, or npos if not
272 size_t rfind(StringRef Str) const;
274 /// Find the first character in the string that is \p C, or npos if not
275 /// found. Same as find.
276 size_t find_first_of(char C, size_t From = 0) const {
277 return find(C, From);
280 /// Find the first character in the string that is in \p Chars, or npos if
283 /// Complexity: O(size() + Chars.size())
284 size_t find_first_of(StringRef Chars, size_t From = 0) const;
286 /// Find the first character in the string that is not \p C or npos if not
288 size_t find_first_not_of(char C, size_t From = 0) const;
290 /// Find the first character in the string that is not in the string
291 /// \p Chars, or npos if not found.
293 /// Complexity: O(size() + Chars.size())
294 size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
296 /// Find the last character in the string that is \p C, or npos if not
298 size_t find_last_of(char C, size_t From = npos) const {
299 return rfind(C, From);
302 /// Find the last character in the string that is in \p C, or npos if not
305 /// Complexity: O(size() + Chars.size())
306 size_t find_last_of(StringRef Chars, size_t From = npos) const;
308 /// Find the last character in the string that is not \p C, or npos if not
310 size_t find_last_not_of(char C, size_t From = npos) const;
312 /// Find the last character in the string that is not in \p Chars, or
313 /// npos if not found.
315 /// Complexity: O(size() + Chars.size())
316 size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
319 /// @name Helpful Algorithms
322 /// Return the number of occurrences of \p C in the string.
323 size_t count(char C) const {
325 for (size_t i = 0, e = Length; i != e; ++i)
331 /// Return the number of non-overlapped occurrences of \p Str in
333 size_t count(StringRef Str) const;
335 /// Parse the current string as an integer of the specified radix. If
336 /// \p Radix is specified as zero, this does radix autosensing using
337 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
339 /// If the string is invalid or if only a subset of the string is valid,
340 /// this returns true to signify the error. The string is considered
341 /// erroneous if empty or if it overflows T.
342 template <typename T>
343 typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
344 getAsInteger(unsigned Radix, T &Result) const {
346 if (getAsSignedInteger(*this, Radix, LLVal) ||
347 static_cast<T>(LLVal) != LLVal)
353 template <typename T>
354 typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
355 getAsInteger(unsigned Radix, T &Result) const {
356 unsigned long long ULLVal;
357 // The additional cast to unsigned long long is required to avoid the
358 // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
359 // 'unsigned __int64' when instantiating getAsInteger with T = bool.
360 if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
361 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
367 /// Parse the current string as an integer of the specified \p Radix, or of
368 /// an autosensed radix if the \p Radix given is 0. The current value in
369 /// \p Result is discarded, and the storage is changed to be wide enough to
370 /// store the parsed integer.
372 /// \returns true if the string does not solely consist of a valid
373 /// non-empty number in the appropriate base.
375 /// APInt::fromString is superficially similar but assumes the
376 /// string is well-formed in the given radix.
377 bool getAsInteger(unsigned Radix, APInt &Result) const;
380 /// @name String Operations
383 // Convert the given ASCII string to lowercase.
384 std::string lower() const;
386 /// Convert the given ASCII string to uppercase.
387 std::string upper() const;
390 /// @name Substring Operations
393 /// Return a reference to the substring from [Start, Start + N).
395 /// \param Start The index of the starting character in the substring; if
396 /// the index is npos or greater than the length of the string then the
397 /// empty substring will be returned.
399 /// \param N The number of characters to included in the substring. If N
400 /// exceeds the number of characters remaining in the string, the string
401 /// suffix (starting with \p Start) will be returned.
402 StringRef substr(size_t Start, size_t N = npos) const {
403 Start = std::min(Start, Length);
404 return StringRef(Data + Start, std::min(N, Length - Start));
407 /// Return a StringRef equal to 'this' but with the first \p N elements
409 StringRef drop_front(size_t N = 1) const {
410 assert(size() >= N && "Dropping more elements than exist");
414 /// Return a StringRef equal to 'this' but with the last \p N elements
416 StringRef drop_back(size_t N = 1) const {
417 assert(size() >= N && "Dropping more elements than exist");
418 return substr(0, size()-N);
421 /// Return a reference to the substring from [Start, End).
423 /// \param Start The index of the starting character in the substring; if
424 /// the index is npos or greater than the length of the string then the
425 /// empty substring will be returned.
427 /// \param End The index following the last character to include in the
428 /// substring. If this is npos, or less than \p Start, or exceeds the
429 /// number of characters remaining in the string, the string suffix
430 /// (starting with \p Start) will be returned.
431 StringRef slice(size_t Start, size_t End) const {
432 Start = std::min(Start, Length);
433 End = std::min(std::max(Start, End), Length);
434 return StringRef(Data + Start, End - Start);
437 /// Split into two substrings around the first occurrence of a separator
440 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
441 /// such that (*this == LHS + Separator + RHS) is true and RHS is
442 /// maximal. If \p Separator is not in the string, then the result is a
443 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
445 /// \param Separator The character to split on.
446 /// \returns The split substrings.
447 std::pair<StringRef, StringRef> split(char Separator) const {
448 size_t Idx = find(Separator);
450 return std::make_pair(*this, StringRef());
451 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
454 /// Split into two substrings around the first occurrence of a separator
457 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
458 /// such that (*this == LHS + Separator + RHS) is true and RHS is
459 /// maximal. If \p Separator is not in the string, then the result is a
460 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
462 /// \param Separator - The string to split on.
463 /// \return - The split substrings.
464 std::pair<StringRef, StringRef> split(StringRef Separator) const {
465 size_t Idx = find(Separator);
467 return std::make_pair(*this, StringRef());
468 return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
471 /// Split into substrings around the occurrences of a separator string.
473 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
474 /// \p MaxSplit splits are done and consequently <= \p MaxSplit
475 /// elements are added to A.
476 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
477 /// still count when considering \p MaxSplit
478 /// An useful invariant is that
479 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
481 /// \param A - Where to put the substrings.
482 /// \param Separator - The string to split on.
483 /// \param MaxSplit - The maximum number of times the string is split.
484 /// \param KeepEmpty - True if empty substring should be added.
485 void split(SmallVectorImpl<StringRef> &A,
486 StringRef Separator, int MaxSplit = -1,
487 bool KeepEmpty = true) const;
489 /// Split into two substrings around the last occurrence of a separator
492 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
493 /// such that (*this == LHS + Separator + RHS) is true and RHS is
494 /// minimal. If \p Separator is not in the string, then the result is a
495 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
497 /// \param Separator - The character to split on.
498 /// \return - The split substrings.
499 std::pair<StringRef, StringRef> rsplit(char Separator) const {
500 size_t Idx = rfind(Separator);
502 return std::make_pair(*this, StringRef());
503 return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
506 /// Return string with consecutive characters in \p Chars starting from
507 /// the left removed.
508 StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
509 return drop_front(std::min(Length, find_first_not_of(Chars)));
512 /// Return string with consecutive characters in \p Chars starting from
513 /// the right removed.
514 StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
515 return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
518 /// Return string with consecutive characters in \p Chars starting from
519 /// the left and right removed.
520 StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
521 return ltrim(Chars).rtrim(Chars);
527 /// @name StringRef Comparison Operators
530 inline bool operator==(StringRef LHS, StringRef RHS) {
531 return LHS.equals(RHS);
534 inline bool operator!=(StringRef LHS, StringRef RHS) {
535 return !(LHS == RHS);
538 inline bool operator<(StringRef LHS, StringRef RHS) {
539 return LHS.compare(RHS) == -1;
542 inline bool operator<=(StringRef LHS, StringRef RHS) {
543 return LHS.compare(RHS) != 1;
546 inline bool operator>(StringRef LHS, StringRef RHS) {
547 return LHS.compare(RHS) == 1;
550 inline bool operator>=(StringRef LHS, StringRef RHS) {
551 return LHS.compare(RHS) != -1;
554 inline std::string &operator+=(std::string &buffer, StringRef string) {
555 return buffer.append(string.data(), string.size());
560 /// \brief Compute a hash_code for a StringRef.
561 hash_code hash_value(StringRef S);
563 // StringRefs can be treated like a POD type.
564 template <typename T> struct isPodLike;
565 template <> struct isPodLike<StringRef> { static const bool value = true; };