Define npos in a way that should make MSVC happier.
[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 <cassert>
14 #include <cstring>
15 #include <string>
16
17 namespace llvm {
18
19   /// StringRef - Represent a constant reference to a string, i.e. a character
20   /// array and a length, which need not be null terminated.
21   ///
22   /// This class does not own the string data, it is expected to be used in
23   /// situations where the character data resides in some other buffer, whose
24   /// lifetime extends past that of the StringRef. For this reason, it is not in
25   /// general safe to store a StringRef.
26   class StringRef {
27   public:
28     typedef const char *iterator;
29     static const size_t npos = ~size_t(0);
30
31   private:
32     /// The start of the string, in an external buffer.
33     const char *Data;
34
35     /// The length of the string.
36     size_t Length;
37
38   public:
39     /// @name Constructors
40     /// @{
41
42     /// Construct an empty string ref.
43     /*implicit*/ StringRef() : Data(0), Length(0) {}
44
45     /// Construct a string ref from a cstring.
46     /*implicit*/ StringRef(const char *Str) 
47       : Data(Str), Length(::strlen(Str)) {}
48  
49     /// Construct a string ref from a pointer and length.
50     /*implicit*/ StringRef(const char *_Data, unsigned _Length)
51       : Data(_Data), Length(_Length) {}
52
53     /// Construct a string ref from an std::string.
54     /*implicit*/ StringRef(const std::string &Str) 
55       : Data(Str.c_str()), Length(Str.length()) {}
56
57     /// @}
58     /// @name Iterators
59     /// @{
60
61     iterator begin() const { return Data; }
62
63     iterator end() const { return Data + Length; }
64
65     /// @}
66     /// @name String Operations
67     /// @{
68
69     /// data - Get a pointer to the start of the string (which may not be null
70     /// terminated).
71     const char *data() const { return Data; }
72
73     /// empty - Check if the string is empty.
74     bool empty() const { return Length == 0; }
75
76     /// size - Get the string size.
77     size_t size() const { return Length; }
78
79     /// equals - Check for string equality, this is more efficient than
80     /// compare() in when the relative ordering of inequal strings isn't needed.
81     bool equals(const StringRef &RHS) const {
82       return (Length == RHS.Length && 
83               memcmp(Data, RHS.Data, Length) == 0);
84     }
85
86     /// compare - Compare two strings; the result is -1, 0, or 1 if this string
87     /// is lexicographically less than, equal to, or greater than the \arg RHS.
88     int compare(const StringRef &RHS) const {
89       // Check the prefix for a mismatch.
90       if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
91         return Res < 0 ? -1 : 1;
92
93       // Otherwise the prefixes match, so we only need to check the lengths.
94       if (Length == RHS.Length)
95         return 0;
96       return Length < RHS.Length ? -1 : 1;
97     }
98
99     /// str - Get the contents as an std::string.
100     std::string str() const { return std::string(Data, Length); }
101
102     /// @}
103     /// @name Operator Overloads
104     /// @{
105
106     char operator[](size_t Index) const { 
107       assert(Index < Length && "Invalid index!");
108       return Data[Index]; 
109     }
110
111     /// @}
112     /// @name Type Conversions
113     /// @{
114
115     operator std::string() const {
116       return str();
117     }
118
119     /// @}
120     /// @name Utility Functions
121     /// @{
122
123     /// substr - Return a reference to a substring of this object.
124     ///
125     /// \param Start - The index of the starting character in the substring; if
126     /// the index is greater than the length of the string then the empty
127     /// substring will be returned.
128     ///
129     /// \param N - The number of characters to included in the substring. If N
130     /// exceeds the number of characters remaining in the string, the string
131     /// suffix (starting with \arg Start) will be returned.
132     StringRef substr(size_t Start, size_t N = npos) const {
133       Start = std::min(Start, Length);
134       return StringRef(Data + Start, std::min(N, Length - Start));
135     }
136
137     /// startswith - Check if this string starts with the given \arg Prefix.
138     bool startswith(const StringRef &Prefix) const { 
139       return substr(0, Prefix.Length).equals(Prefix);
140     }
141
142     /// @}
143   };
144
145   /// @name StringRef Comparison Operators
146   /// @{
147
148   inline bool operator==(const StringRef &LHS, const StringRef &RHS) {
149     return LHS.equals(RHS);
150   }
151
152   inline bool operator!=(const StringRef &LHS, const StringRef &RHS) { 
153     return !(LHS == RHS);
154   }
155   
156   inline bool operator<(const StringRef &LHS, const StringRef &RHS) {
157     return LHS.compare(RHS) == -1; 
158   }
159
160   inline bool operator<=(const StringRef &LHS, const StringRef &RHS) {
161     return LHS.compare(RHS) != 1; 
162   }
163
164   inline bool operator>(const StringRef &LHS, const StringRef &RHS) {
165     return LHS.compare(RHS) == 1; 
166   }
167
168   inline bool operator>=(const StringRef &LHS, const StringRef &RHS) {
169     return LHS.compare(RHS) != -1; 
170   }
171
172   /// @}
173
174 }
175
176 #endif