add a version of the APFloat constructor that initializes to 0.0
[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     /// front - Get the first character in the string.
81     char front() const {
82       assert(!empty());
83       return Data[0];
84     }
85     
86     /// back - Get the last character in the string.
87     char back() const {
88       assert(!empty());
89       return Data[Length-1];
90     }
91
92     /// equals - Check for string equality, this is more efficient than
93     /// compare() when the relative ordering of inequal strings isn't needed.
94     bool equals(const StringRef &RHS) const {
95       return (Length == RHS.Length && 
96               memcmp(Data, RHS.Data, RHS.Length) == 0);
97     }
98
99     /// compare - Compare two strings; the result is -1, 0, or 1 if this string
100     /// is lexicographically less than, equal to, or greater than the \arg RHS.
101     int compare(const StringRef &RHS) const {
102       // Check the prefix for a mismatch.
103       if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
104         return Res < 0 ? -1 : 1;
105
106       // Otherwise the prefixes match, so we only need to check the lengths.
107       if (Length == RHS.Length)
108         return 0;
109       return Length < RHS.Length ? -1 : 1;
110     }
111
112     /// str - Get the contents as an std::string.
113     std::string str() const { return std::string(Data, Length); }
114
115     /// @}
116     /// @name Operator Overloads
117     /// @{
118
119     char operator[](size_t Index) const { 
120       assert(Index < Length && "Invalid index!");
121       return Data[Index]; 
122     }
123
124     /// @}
125     /// @name Type Conversions
126     /// @{
127
128     operator std::string() const {
129       return str();
130     }
131
132     /// @}
133     /// @name String Predicates
134     /// @{
135
136     /// startswith - Check if this string starts with the given \arg Prefix.
137     bool startswith(const StringRef &Prefix) const { 
138       return substr(0, Prefix.Length).equals(Prefix);
139     }
140
141     /// endswith - Check if this string ends with the given \arg Suffix.
142     bool endswith(const StringRef &Suffix) const {
143       return slice(size() - Suffix.Length, size()).equals(Suffix);
144     }
145
146     /// @}
147     /// @name String Searching
148     /// @{
149
150     /// find - Search for the first character \arg C in the string.
151     ///
152     /// \return - The index of the first occurence of \arg C, or npos if not
153     /// found.
154     size_t find(char C) const {
155       for (size_t i = 0, e = Length; i != e; ++i)
156         if (Data[i] == C)
157           return i;
158       return npos;
159     }
160
161     /// find - Search for the first string \arg Str in the string.
162     ///
163     /// \return - The index of the first occurence of \arg Str, or npos if not
164     /// found.
165     size_t find(const StringRef &Str) const {
166       size_t N = Str.size();
167       if (N > Length)
168         return npos;
169       for (size_t i = 0, e = Length - N + 1; i != e; ++i)
170         if (substr(i, N).equals(Str))
171           return i;
172       return npos;
173     }
174
175     /// rfind - Search for the last character \arg C in the string.
176     ///
177     /// \return - The index of the last occurence of \arg C, or npos if not
178     /// found.
179     size_t rfind(char C) const {
180       for (size_t i = Length, e = 0; i != e;) {
181         --i;
182         if (Data[i] == C)
183           return i;
184       }
185       return npos;
186     }
187
188     /// rfind - Search for the last string \arg Str in the string.
189     ///
190     /// \return - The index of the last occurence of \arg Str, or npos if not
191     /// found.
192     size_t rfind(const StringRef &Str) const {
193       size_t N = Str.size();
194       if (N > Length)
195         return npos;
196       for (size_t i = Length - N + 1, e = 0; i != e;) {
197         --i;
198         if (substr(i, N).equals(Str))
199           return i;
200       }
201       return npos;
202     }
203
204     /// count - Return the number of occurrences of \arg C in the string.
205     size_t count(char C) const {
206       size_t Count = 0;
207       for (size_t i = 0, e = Length; i != e; ++i)
208         if (Data[i] == C)
209           ++Count;
210       return Count;
211     }
212
213     /// count - Return the number of non-overlapped occurrences of \arg Str in
214     /// the string.
215     size_t count(const StringRef &Str) const {
216       size_t Count = 0;
217       size_t N = Str.size();
218       if (N > Length)
219         return 0;
220       for (size_t i = 0, e = Length - N + 1; i != e; ++i)
221         if (substr(i, N).equals(Str))
222           ++Count;
223       return Count;
224     }
225
226     /// @}
227     /// @name Substring Operations
228     /// @{
229
230     /// substr - Return a reference to the substring from [Start, Start + N).
231     ///
232     /// \param Start - The index of the starting character in the substring; if
233     /// the index is npos or greater than the length of the string then the
234     /// empty substring will be returned.
235     ///
236     /// \param N - The number of characters to included in the substring. If N
237     /// exceeds the number of characters remaining in the string, the string
238     /// suffix (starting with \arg Start) will be returned.
239     StringRef substr(size_t Start, size_t N = npos) const {
240       Start = std::min(Start, Length);
241       return StringRef(Data + Start, std::min(N, Length - Start));
242     }
243
244     /// slice - Return a reference to the substring from [Start, End).
245     ///
246     /// \param Start - The index of the starting character in the substring; if
247     /// the index is npos or greater than the length of the string then the
248     /// empty substring will be returned.
249     ///
250     /// \param End - The index following the last character to include in the
251     /// substring. If this is npos, or less than \arg Start, or exceeds the
252     /// number of characters remaining in the string, the string suffix
253     /// (starting with \arg Start) will be returned.
254     StringRef slice(size_t Start, size_t End) const {
255       Start = std::min(Start, Length);
256       End = std::min(std::max(Start, End), Length);
257       return StringRef(Data + Start, End - Start);
258     }
259
260     /// split - Split into two substrings around the first occurence of a
261     /// separator character.
262     ///
263     /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
264     /// such that (*this == LHS + Separator + RHS) is true and RHS is
265     /// maximal. If \arg Separator is not in the string, then the result is a
266     /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
267     ///
268     /// \param Separator - The character to split on.
269     /// \return - The split substrings.
270     std::pair<StringRef, StringRef> split(char Separator) const {
271       size_t Idx = find(Separator);
272       if (Idx == npos)
273         return std::make_pair(*this, StringRef());
274       return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
275     }
276
277     /// rsplit - Split into two substrings around the last occurence of a
278     /// separator character.
279     ///
280     /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
281     /// such that (*this == LHS + Separator + RHS) is true and RHS is
282     /// minimal. If \arg Separator is not in the string, then the result is a
283     /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
284     ///
285     /// \param Separator - The character to split on.
286     /// \return - The split substrings.
287     std::pair<StringRef, StringRef> rsplit(char Separator) const {
288       size_t Idx = rfind(Separator);
289       if (Idx == npos)
290         return std::make_pair(*this, StringRef());
291       return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
292     }
293
294     /// @}
295   };
296
297   /// @name StringRef Comparison Operators
298   /// @{
299
300   inline bool operator==(const StringRef &LHS, const StringRef &RHS) {
301     return LHS.equals(RHS);
302   }
303
304   inline bool operator!=(const StringRef &LHS, const StringRef &RHS) { 
305     return !(LHS == RHS);
306   }
307   
308   inline bool operator<(const StringRef &LHS, const StringRef &RHS) {
309     return LHS.compare(RHS) == -1; 
310   }
311
312   inline bool operator<=(const StringRef &LHS, const StringRef &RHS) {
313     return LHS.compare(RHS) != 1; 
314   }
315
316   inline bool operator>(const StringRef &LHS, const StringRef &RHS) {
317     return LHS.compare(RHS) == 1; 
318   }
319
320   inline bool operator>=(const StringRef &LHS, const StringRef &RHS) {
321     return LHS.compare(RHS) != -1; 
322   }
323
324   /// @}
325
326 }
327
328 #endif