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