Add StringRef::{substr, startswith}.
[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     unsigned size() const { return Length; }
77
78     /// compare - Compare two strings; the result is -1, 0, or 1 if this string
79     /// is lexicographically less than, equal to, or greater than the \arg RHS.
80     int compare(const StringRef &RHS) const {
81       // Check the prefix for a mismatch.
82       if (int Res = memcmp(Data, RHS.Data, std::min(Length, RHS.Length)))
83         return Res < 0 ? -1 : 1;
84
85       // Otherwise the prefixes match, so we only need to check the lengths.
86       if (Length == RHS.Length)
87         return 0;
88       return Length < RHS.Length ? -1 : 1;
89     }
90
91     /// str - Get the contents as an std::string.
92     std::string str() const { return std::string(Data, Length); }
93
94     /// @}
95     /// @name Operator Overloads
96     /// @{
97
98     bool operator==(const StringRef &RHS) const { 
99       return Length == RHS.Length && memcmp(Data, RHS.Data, Length) == 0; 
100     }
101
102     bool operator!=(const StringRef &RHS) const { return !(*this == RHS); }
103
104     bool operator<(const StringRef &RHS) const { return compare(RHS) == -1; }
105
106     bool operator<=(const StringRef &RHS) const { return compare(RHS) != 1; }
107
108     bool operator>(const StringRef &RHS) const { return compare(RHS) == 1; }
109
110     bool operator>=(const StringRef &RHS) const { return compare(RHS) != -1; }
111
112     char operator[](size_t Index) const { 
113       assert(Index < Length && "Invalid index!");
114       return Data[Index]; 
115     }
116
117     /// @}
118     /// @name Type Conversions
119     /// @{
120
121     operator std::string() const {
122       return str();
123     }
124
125     /// @}
126     /// @name Utility Functions
127     /// @{
128
129     /// substr - Return a reference to a substring of this object.
130     ///
131     /// \param Start - The index of the starting character in the substring; if
132     /// the index is greater than the length of the string then the empty
133     /// substring will be returned.
134     ///
135     /// \param N - The number of characters to included in the substring. If N
136     /// exceeds the number of characters remaining in the string, the string
137     /// suffix (starting with \arg Start) will be returned.
138     StringRef substr(size_t Start, size_t N = npos) const {
139       Start = std::min(Start, Length);
140       return StringRef(Data + Start, std::min(N, Length - Start));
141     }
142
143     /// startswith - Check if this string starts with the given \arg Prefix.
144     bool startswith(const StringRef &Prefix) const { 
145       return substr(0, Prefix.Length) == Prefix;
146     }
147
148     /// @}
149   };
150
151 }
152
153 #endif