1 //===--- StringRef.h - Constant String Reference Wrapper --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_STRINGREF_H
11 #define LLVM_ADT_STRINGREF_H
18 /// StringRef - Represent a constant reference to a string, i.e. a character
19 /// array and a length, which need not be null terminated.
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.
27 typedef const char *iterator;
28 static const size_t npos = std::string::npos;
31 /// The start of the string, in an external buffer.
34 /// The length of the string.
38 /// @name Constructors
41 /// Construct an empty string ref.
42 /*implicit*/ StringRef() : Data(0), Length(0) {}
44 /// Construct a string ref from a cstring.
45 /*implicit*/ StringRef(const char *Str)
46 : Data(Str), Length(::strlen(Str)) {}
48 /// Construct a string ref from a pointer and length.
49 /*implicit*/ StringRef(const char *_Data, unsigned _Length)
50 : Data(_Data), Length(_Length) {}
52 /// Construct a string ref from an std::string.
53 /*implicit*/ StringRef(const std::string &Str)
54 : Data(Str.c_str()), Length(Str.length()) {}
60 iterator begin() const { return Data; }
62 iterator end() const { return Data + Length; }
65 /// @name String Operations
68 /// data - Get a pointer to the start of the string (which may not be null
70 const char *data() const { return Data; }
72 /// empty - Check if the string is empty.
73 bool empty() const { return Length == 0; }
75 /// size - Get the string size.
76 size_t size() const { return Length; }
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);
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;
92 // Otherwise the prefixes match, so we only need to check the lengths.
93 if (Length == RHS.Length)
95 return Length < RHS.Length ? -1 : 1;
98 /// str - Get the contents as an std::string.
99 std::string str() const { return std::string(Data, Length); }
102 /// @name Operator Overloads
105 char operator[](size_t Index) const {
106 assert(Index < Length && "Invalid index!");
111 /// @name Type Conversions
114 operator std::string() const {
119 /// @name Utility Functions
122 /// substr - Return a reference to a substring of this object.
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.
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));
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);
144 /// @name StringRef Comparison Operators
147 inline bool operator==(const StringRef &LHS, const StringRef &RHS) {
148 return LHS.equals(RHS);
151 inline bool operator!=(const StringRef &LHS, const StringRef &RHS) {
152 return !(LHS == RHS);
155 inline bool operator<(const StringRef &LHS, const StringRef &RHS) {
156 return LHS.compare(RHS) == -1;
159 inline bool operator<=(const StringRef &LHS, const StringRef &RHS) {
160 return LHS.compare(RHS) != 1;
163 inline bool operator>(const StringRef &LHS, const StringRef &RHS) {
164 return LHS.compare(RHS) == 1;
167 inline bool operator>=(const StringRef &LHS, const StringRef &RHS) {
168 return LHS.compare(RHS) != -1;