1//===--- StringRef.h - Constant String Reference Wrapper --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_ADT_STRINGREF_H
11#define LLVM_ADT_STRINGREF_H
12
13#include <algorithm>
14#include <cassert>
15#include <cstring>
16#include <limits>
17#include <string>
18#include <utility>
19
20namespace llvm {
21  template <typename T>
22  class SmallVectorImpl;
23  class APInt;
24  class hash_code;
25  class StringRef;
26
27  /// Helper functions for StringRef::getAsInteger.
28  bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
29                            unsigned long long &Result);
30
31  bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
32
33  /// StringRef - Represent a constant reference to a string, i.e. a character
34  /// array and a length, which need not be null terminated.
35  ///
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.
40  class StringRef {
41  public:
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;
46
47  private:
48    /// The start of the string, in an external buffer.
49    const char *Data;
50
51    /// The length of the string.
52    size_t Length;
53
54    // Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min()
55    // Changing the arg of min to be an integer, instead of a reference to an
56    // integer works around this bug.
57    static size_t min(size_t a, size_t b) { return a < b ? a : b; }
58    static size_t max(size_t a, size_t b) { return a > b ? a : b; }
59
60    // Workaround memcmp issue with null pointers (undefined behavior)
61    // by providing a specialized version
62    static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
63      if (Length == 0) { return 0; }
64      return ::memcmp(Lhs,Rhs,Length);
65    }
66
67  public:
68    /// @name Constructors
69    /// @{
70
71    /// Construct an empty string ref.
72    /*implicit*/ StringRef() : Data(nullptr), Length(0) {}
73
74    /// Construct a string ref from a cstring.
75    /*implicit*/ StringRef(const char *Str)
76      : Data(Str) {
77        assert(Str && "StringRef cannot be built from a NULL argument");
78        Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
79      }
80
81    /// Construct a string ref from a pointer and length.
82    /*implicit*/ StringRef(const char *data, size_t length)
83      : Data(data), Length(length) {
84        assert((data || length == 0) &&
85        "StringRef cannot be built from a NULL argument with non-null length");
86      }
87
88    /// Construct a string ref from an std::string.
89    /*implicit*/ StringRef(const std::string &Str)
90      : Data(Str.data()), Length(Str.length()) {}
91
92    /// @}
93    /// @name Iterators
94    /// @{
95
96    iterator begin() const { return Data; }
97
98    iterator end() const { return Data + Length; }
99
100    /// @}
101    /// @name String Operations
102    /// @{
103
104    /// data - Get a pointer to the start of the string (which may not be null
105    /// terminated).
106    const char *data() const { return Data; }
107
108    /// empty - Check if the string is empty.
109    bool empty() const { return Length == 0; }
110
111    /// size - Get the string size.
112    size_t size() const { return Length; }
113
114    /// front - Get the first character in the string.
115    char front() const {
116      assert(!empty());
117      return Data[0];
118    }
119
120    /// back - Get the last character in the string.
121    char back() const {
122      assert(!empty());
123      return Data[Length-1];
124    }
125
126    // copy - Allocate copy in Allocator and return StringRef to it.
127    template <typename Allocator> StringRef copy(Allocator &A) {
128      char *S = A.template Allocate<char>(Length);
129      std::copy(begin(), end(), S);
130      return StringRef(S, Length);
131    }
132
133    /// equals - Check for string equality, this is more efficient than
134    /// compare() when the relative ordering of inequal strings isn't needed.
135    bool equals(StringRef RHS) const {
136      return (Length == RHS.Length &&
137              compareMemory(Data, RHS.Data, RHS.Length) == 0);
138    }
139
140    /// equals_lower - Check for string equality, ignoring case.
141    bool equals_lower(StringRef RHS) const {
142      return Length == RHS.Length && compare_lower(RHS) == 0;
143    }
144
145    /// compare - Compare two strings; the result is -1, 0, or 1 if this string
146    /// is lexicographically less than, equal to, or greater than the \p RHS.
147    int compare(StringRef RHS) const {
148      // Check the prefix for a mismatch.
149      if (int Res = compareMemory(Data, RHS.Data, min(Length, RHS.Length)))
150        return Res < 0 ? -1 : 1;
151
152      // Otherwise the prefixes match, so we only need to check the lengths.
153      if (Length == RHS.Length)
154        return 0;
155      return Length < RHS.Length ? -1 : 1;
156    }
157
158    /// compare_lower - Compare two strings, ignoring case.
159    int compare_lower(StringRef RHS) const;
160
161    /// compare_numeric - Compare two strings, treating sequences of digits as
162    /// numbers.
163    int compare_numeric(StringRef RHS) const;
164
165    /// \brief Determine the edit distance between this string and another
166    /// string.
167    ///
168    /// \param Other the string to compare this string against.
169    ///
170    /// \param AllowReplacements whether to allow character
171    /// replacements (change one character into another) as a single
172    /// operation, rather than as two operations (an insertion and a
173    /// removal).
174    ///
175    /// \param MaxEditDistance If non-zero, the maximum edit distance that
176    /// this routine is allowed to compute. If the edit distance will exceed
177    /// that maximum, returns \c MaxEditDistance+1.
178    ///
179    /// \returns the minimum number of character insertions, removals,
180    /// or (if \p AllowReplacements is \c true) replacements needed to
181    /// transform one of the given strings into the other. If zero,
182    /// the strings are identical.
183    unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
184                           unsigned MaxEditDistance = 0) const;
185
186    /// str - Get the contents as an std::string.
187    std::string str() const {
188      if (!Data) return std::string();
189      return std::string(Data, Length);
190    }
191
192    /// @}
193    /// @name Operator Overloads
194    /// @{
195
196    char operator[](size_t Index) const {
197      assert(Index < Length && "Invalid index!");
198      return Data[Index];
199    }
200
201    /// @}
202    /// @name Type Conversions
203    /// @{
204
205    operator std::string() const {
206      return str();
207    }
208
209    /// @}
210    /// @name String Predicates
211    /// @{
212
213    /// Check if this string starts with the given \p Prefix.
214    bool startswith(StringRef Prefix) const {
215      return Length >= Prefix.Length &&
216             compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
217    }
218
219    /// Check if this string starts with the given \p Prefix, ignoring case.
220    bool startswith_lower(StringRef Prefix) const;
221
222    /// Check if this string ends with the given \p Suffix.
223    bool endswith(StringRef Suffix) const {
224      return Length >= Suffix.Length &&
225        compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
226    }
227
228    /// Check if this string ends with the given \p Suffix, ignoring case.
229    bool endswith_lower(StringRef Suffix) const;
230
231    /// @}
232    /// @name String Searching
233    /// @{
234
235    /// Search for the first character \p C in the string.
236    ///
237    /// \returns The index of the first occurrence of \p C, or npos if not
238    /// found.
239    size_t find(char C, size_t From = 0) const {
240      for (size_t i = min(From, Length), e = Length; i != e; ++i)
241        if (Data[i] == C)
242          return i;
243      return npos;
244    }
245
246    /// Search for the first string \p Str in the string.
247    ///
248    /// \returns The index of the first occurrence of \p Str, or npos if not
249    /// found.
250    size_t find(StringRef Str, size_t From = 0) const;
251
252    /// Search for the last character \p C in the string.
253    ///
254    /// \returns The index of the last occurrence of \p C, or npos if not
255    /// found.
256    size_t rfind(char C, size_t From = npos) const {
257      From = min(From, Length);
258      size_t i = From;
259      while (i != 0) {
260        --i;
261        if (Data[i] == C)
262          return i;
263      }
264      return npos;
265    }
266
267    /// Search for the last string \p Str in the string.
268    ///
269    /// \returns The index of the last occurrence of \p Str, or npos if not
270    /// found.
271    size_t rfind(StringRef Str) const;
272
273    /// Find the first character in the string that is \p C, or npos if not
274    /// found. Same as find.
275    size_t find_first_of(char C, size_t From = 0) const {
276      return find(C, From);
277    }
278
279    /// Find the first character in the string that is in \p Chars, or npos if
280    /// not found.
281    ///
282    /// Complexity: O(size() + Chars.size())
283    size_t find_first_of(StringRef Chars, size_t From = 0) const;
284
285    /// Find the first character in the string that is not \p C or npos if not
286    /// found.
287    size_t find_first_not_of(char C, size_t From = 0) const;
288
289    /// Find the first character in the string that is not in the string
290    /// \p Chars, or npos if not found.
291    ///
292    /// Complexity: O(size() + Chars.size())
293    size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
294
295    /// Find the last character in the string that is \p C, or npos if not
296    /// found.
297    size_t find_last_of(char C, size_t From = npos) const {
298      return rfind(C, From);
299    }
300
301    /// Find the last character in the string that is in \p C, or npos if not
302    /// found.
303    ///
304    /// Complexity: O(size() + Chars.size())
305    size_t find_last_of(StringRef Chars, size_t From = npos) const;
306
307    /// Find the last character in the string that is not \p C, or npos if not
308    /// found.
309    size_t find_last_not_of(char C, size_t From = npos) const;
310
311    /// Find the last character in the string that is not in \p Chars, or
312    /// npos if not found.
313    ///
314    /// Complexity: O(size() + Chars.size())
315    size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
316
317    /// @}
318    /// @name Helpful Algorithms
319    /// @{
320
321    /// Return the number of occurrences of \p C in the string.
322    size_t count(char C) const {
323      size_t Count = 0;
324      for (size_t i = 0, e = Length; i != e; ++i)
325        if (Data[i] == C)
326          ++Count;
327      return Count;
328    }
329
330    /// Return the number of non-overlapped occurrences of \p Str in
331    /// the string.
332    size_t count(StringRef Str) const;
333
334    /// Parse the current string as an integer of the specified radix.  If
335    /// \p Radix is specified as zero, this does radix autosensing using
336    /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
337    ///
338    /// If the string is invalid or if only a subset of the string is valid,
339    /// this returns true to signify the error.  The string is considered
340    /// erroneous if empty or if it overflows T.
341    template <typename T>
342    typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
343    getAsInteger(unsigned Radix, T &Result) const {
344      long long LLVal;
345      if (getAsSignedInteger(*this, Radix, LLVal) ||
346            static_cast<T>(LLVal) != LLVal)
347        return true;
348      Result = LLVal;
349      return false;
350    }
351
352    template <typename T>
353    typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
354    getAsInteger(unsigned Radix, T &Result) const {
355      unsigned long long ULLVal;
356      if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
357            static_cast<T>(ULLVal) != ULLVal)
358        return true;
359      Result = ULLVal;
360      return false;
361    }
362
363    /// Parse the current string as an integer of the specified \p Radix, or of
364    /// an autosensed radix if the \p Radix given is 0.  The current value in
365    /// \p Result is discarded, and the storage is changed to be wide enough to
366    /// store the parsed integer.
367    ///
368    /// \returns true if the string does not solely consist of a valid
369    /// non-empty number in the appropriate base.
370    ///
371    /// APInt::fromString is superficially similar but assumes the
372    /// string is well-formed in the given radix.
373    bool getAsInteger(unsigned Radix, APInt &Result) const;
374
375    /// @}
376    /// @name String Operations
377    /// @{
378
379    // Convert the given ASCII string to lowercase.
380    std::string lower() const;
381
382    /// Convert the given ASCII string to uppercase.
383    std::string upper() const;
384
385    /// @}
386    /// @name Substring Operations
387    /// @{
388
389    /// Return a reference to the substring from [Start, Start + N).
390    ///
391    /// \param Start The index of the starting character in the substring; if
392    /// the index is npos or greater than the length of the string then the
393    /// empty substring will be returned.
394    ///
395    /// \param N The number of characters to included in the substring. If N
396    /// exceeds the number of characters remaining in the string, the string
397    /// suffix (starting with \p Start) will be returned.
398    StringRef substr(size_t Start, size_t N = npos) const {
399      Start = min(Start, Length);
400      return StringRef(Data + Start, min(N, Length - Start));
401    }
402
403    /// Return a StringRef equal to 'this' but with the first \p N elements
404    /// dropped.
405    StringRef drop_front(size_t N = 1) const {
406      assert(size() >= N && "Dropping more elements than exist");
407      return substr(N);
408    }
409
410    /// Return a StringRef equal to 'this' but with the last \p N elements
411    /// dropped.
412    StringRef drop_back(size_t N = 1) const {
413      assert(size() >= N && "Dropping more elements than exist");
414      return substr(0, size()-N);
415    }
416
417    /// Return a reference to the substring from [Start, End).
418    ///
419    /// \param Start The index of the starting character in the substring; if
420    /// the index is npos or greater than the length of the string then the
421    /// empty substring will be returned.
422    ///
423    /// \param End The index following the last character to include in the
424    /// substring. If this is npos, or less than \p Start, or exceeds the
425    /// number of characters remaining in the string, the string suffix
426    /// (starting with \p Start) will be returned.
427    StringRef slice(size_t Start, size_t End) const {
428      Start = min(Start, Length);
429      End = min(max(Start, End), Length);
430      return StringRef(Data + Start, End - Start);
431    }
432
433    /// Split into two substrings around the first occurrence of a separator
434    /// character.
435    ///
436    /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
437    /// such that (*this == LHS + Separator + RHS) is true and RHS is
438    /// maximal. If \p Separator is not in the string, then the result is a
439    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
440    ///
441    /// \param Separator The character to split on.
442    /// \returns The split substrings.
443    std::pair<StringRef, StringRef> split(char Separator) const {
444      size_t Idx = find(Separator);
445      if (Idx == npos)
446        return std::make_pair(*this, StringRef());
447      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
448    }
449
450    /// Split into two substrings around the first occurrence of a separator
451    /// string.
452    ///
453    /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
454    /// such that (*this == LHS + Separator + RHS) is true and RHS is
455    /// maximal. If \p Separator is not in the string, then the result is a
456    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
457    ///
458    /// \param Separator - The string to split on.
459    /// \return - The split substrings.
460    std::pair<StringRef, StringRef> split(StringRef Separator) const {
461      size_t Idx = find(Separator);
462      if (Idx == npos)
463        return std::make_pair(*this, StringRef());
464      return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
465    }
466
467    /// Split into substrings around the occurrences of a separator string.
468    ///
469    /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
470    /// \p MaxSplit splits are done and consequently <= \p MaxSplit
471    /// elements are added to A.
472    /// If \p KeepEmpty is false, empty strings are not added to \p A. They
473    /// still count when considering \p MaxSplit
474    /// An useful invariant is that
475    /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
476    ///
477    /// \param A - Where to put the substrings.
478    /// \param Separator - The string to split on.
479    /// \param MaxSplit - The maximum number of times the string is split.
480    /// \param KeepEmpty - True if empty substring should be added.
481    void split(SmallVectorImpl<StringRef> &A,
482               StringRef Separator, int MaxSplit = -1,
483               bool KeepEmpty = true) const;
484
485    /// Split into two substrings around the last occurrence of a separator
486    /// character.
487    ///
488    /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
489    /// such that (*this == LHS + Separator + RHS) is true and RHS is
490    /// minimal. If \p Separator is not in the string, then the result is a
491    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
492    ///
493    /// \param Separator - The character to split on.
494    /// \return - The split substrings.
495    std::pair<StringRef, StringRef> rsplit(char Separator) const {
496      size_t Idx = rfind(Separator);
497      if (Idx == npos)
498        return std::make_pair(*this, StringRef());
499      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
500    }
501
502    /// Return string with consecutive characters in \p Chars starting from
503    /// the left removed.
504    StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
505      return drop_front(std::min(Length, find_first_not_of(Chars)));
506    }
507
508    /// Return string with consecutive characters in \p Chars starting from
509    /// the right removed.
510    StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
511      return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
512    }
513
514    /// Return string with consecutive characters in \p Chars starting from
515    /// the left and right removed.
516    StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
517      return ltrim(Chars).rtrim(Chars);
518    }
519
520    /// @}
521  };
522
523  /// @name StringRef Comparison Operators
524  /// @{
525
526  inline bool operator==(StringRef LHS, StringRef RHS) {
527    return LHS.equals(RHS);
528  }
529
530  inline bool operator!=(StringRef LHS, StringRef RHS) {
531    return !(LHS == RHS);
532  }
533
534  inline bool operator<(StringRef LHS, StringRef RHS) {
535    return LHS.compare(RHS) == -1;
536  }
537
538  inline bool operator<=(StringRef LHS, StringRef RHS) {
539    return LHS.compare(RHS) != 1;
540  }
541
542  inline bool operator>(StringRef LHS, StringRef RHS) {
543    return LHS.compare(RHS) == 1;
544  }
545
546  inline bool operator>=(StringRef LHS, StringRef RHS) {
547    return LHS.compare(RHS) != -1;
548  }
549
550  inline std::string &operator+=(std::string &buffer, StringRef string) {
551    return buffer.append(string.data(), string.size());
552  }
553
554  /// @}
555
556  /// \brief Compute a hash_code for a StringRef.
557  hash_code hash_value(StringRef S);
558
559  // StringRefs can be treated like a POD type.
560  template <typename T> struct isPodLike;
561  template <> struct isPodLike<StringRef> { static const bool value = true; };
562}
563
564#endif
565