Use the RHS length instead of the LHS length. They are both the same,
[oota-llvm.git] / include / llvm / ADT / StringRef.h
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 <string>
17
18 namespace llvm {
19
20   /// StringRef - Represent a constant reference to a string, i.e. a character
21   /// array and a length, which need not be null terminated.
22   ///
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.
27   class StringRef {
28   public:
29     typedef const char *iterator;
30     static const size_t npos = ~size_t(0);
31
32   private:
33     /// The start of the string, in an external buffer.
34     const char *Data;
35
36     /// The length of the string.
37     size_t Length;
38
39   public:
40     /// @name Constructors
41     /// @{
42
43     /// Construct an empty string ref.
44     /*implicit*/ StringRef() : Data(0), Length(0) {}
45
46     /// Construct a string ref from a cstring.
47     /*implicit*/ StringRef(const char *Str) 
48       : Data(Str), Length(::strlen(Str)) {}
49  
50     /// Construct a string ref from a pointer and length.
51     /*implicit*/ StringRef(const char *_Data, unsigned _Length)
52       : Data(_Data), Length(_Length) {}
53
54     /// Construct a string ref from an std::string.
55     /*implicit*/ StringRef(const std::string &Str) 
56       : Data(Str.c_str()), Length(Str.length()) {}
57
58     /// @}
59     /// @name Iterators
60     /// @{
61
62     iterator begin() const { return Data; }
63
64     iterator end() const { return Data + Length; }
65
66     /// @}
67     /// @name String Operations
68     /// @{
69
70     /// data - Get a pointer to the start of the string (which may not be null
71     /// terminated).
72     const char *data() const { return Data; }
73
74     /// empty - Check if the string is empty.
75     bool empty() const { return Length == 0; }
76
77     /// size - Get the string size.
78     size_t size() const { return Length; }
79
80     /// equals - Check for string equality, this is more efficient than
81     /// compare() in when the relative ordering of inequal strings isn't needed.
82     bool equals(const StringRef &RHS) const {
83       return (Length == RHS.Length && 
84               memcmp(Data, RHS.Data, RHS.Length) == 0);
85     }
86
87     /// compare - Compare two strings; the result is -1, 0, or 1 if this string
88     /// is lexicographically less than, equal to, or greater than the \arg RHS.
89     int compare(const StringRef &RHS) const {
90       // Check the prefix for a mismatch.
91       if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
92         return Res < 0 ? -1 : 1;
93
94       // Otherwise the prefixes match, so we only need to check the lengths.
95       if (Length == RHS.Length)
96         return 0;
97       return Length < RHS.Length ? -1 : 1;
98     }
99
100     /// str - Get the contents as an std::string.
101     std::string str() const { return std::string(Data, Length); }
102
103     /// @}
104     /// @name Operator Overloads
105     /// @{
106
107     char operator[](size_t Index) const { 
108       assert(Index < Length && "Invalid index!");
109       return Data[Index]; 
110     }
111
112     /// @}
113     /// @name Type Conversions
114     /// @{
115
116     operator std::string() const {
117       return str();
118     }
119
120     /// @}
121     /// @name Utility Functions
122     /// @{
123
124     /// substr - Return a reference to the substring from [Start, Start + N).
125     ///
126     /// \param Start - The index of the starting character in the substring; if
127     /// the index is npos or greater than the length of the string then the
128     /// empty substring will be returned.
129     ///
130     /// \param N - The number of characters to included in the substring. If N
131     /// exceeds the number of characters remaining in the string, the string
132     /// suffix (starting with \arg Start) will be returned.
133     StringRef substr(size_t Start, size_t N = npos) const {
134       Start = std::min(Start, Length);
135       return StringRef(Data + Start, std::min(N, Length - Start));
136     }
137
138     /// slice - Return a reference to the substring from [Start, End).
139     ///
140     /// \param Start - The index of the starting character in the substring; if
141     /// the index is npos or greater than the length of the string then the
142     /// empty substring will be returned.
143     ///
144     /// \param End - The index following the last character to include in the
145     /// substring. If this is npos, or less than \arg Start, or exceeds the
146     /// number of characters remaining in the string, the string suffix
147     /// (starting with \arg Start) will be returned.
148     StringRef slice(size_t Start, size_t End) const {
149       Start = std::min(Start, Length);
150       End = std::min(std::max(Start, End), Length);
151       return StringRef(Data + Start, End - Start);
152     }
153
154     /// split - Split into two substrings around the first occurence of a
155     /// separator character.
156     ///
157     /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
158     /// such that (*this == LHS + Separator + RHS) is true and RHS is
159     /// maximal. If \arg Separator is not in the string, then the result is a
160     /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
161     ///
162     /// \param Separator - The character to split on.
163     /// \return - The split substrings.
164     std::pair<StringRef, StringRef> split(char Separator) const {
165       iterator it = std::find(begin(), end(), Separator);
166       if (it == end())
167         return std::make_pair(*this, StringRef());
168       return std::make_pair(StringRef(begin(), it - begin()),
169                             StringRef(it + 1, end() - (it + 1)));
170     }
171
172     /// startswith - Check if this string starts with the given \arg Prefix.
173     bool startswith(const StringRef &Prefix) const { 
174       return substr(0, Prefix.Length).equals(Prefix);
175     }
176
177     /// @}
178   };
179
180   /// @name StringRef Comparison Operators
181   /// @{
182
183   inline bool operator==(const StringRef &LHS, const StringRef &RHS) {
184     return LHS.equals(RHS);
185   }
186
187   inline bool operator!=(const StringRef &LHS, const StringRef &RHS) { 
188     return !(LHS == RHS);
189   }
190   
191   inline bool operator<(const StringRef &LHS, const StringRef &RHS) {
192     return LHS.compare(RHS) == -1; 
193   }
194
195   inline bool operator<=(const StringRef &LHS, const StringRef &RHS) {
196     return LHS.compare(RHS) != 1; 
197   }
198
199   inline bool operator>(const StringRef &LHS, const StringRef &RHS) {
200     return LHS.compare(RHS) == 1; 
201   }
202
203   inline bool operator>=(const StringRef &LHS, const StringRef &RHS) {
204     return LHS.compare(RHS) != -1; 
205   }
206
207   /// @}
208
209 }
210
211 #endif