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
19 /// StringRef - Represent a constant reference to a string, i.e. a character
20 /// array and a length, which need not be null terminated.
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.
28 typedef const char *iterator;
29 static const size_t npos = ~size_t(0);
32 /// The start of the string, in an external buffer.
35 /// The length of the string.
39 /// @name Constructors
42 /// Construct an empty string ref.
43 /*implicit*/ StringRef() : Data(0), Length(0) {}
45 /// Construct a string ref from a cstring.
46 /*implicit*/ StringRef(const char *Str)
47 : Data(Str), Length(::strlen(Str)) {}
49 /// Construct a string ref from a pointer and length.
50 /*implicit*/ StringRef(const char *_Data, unsigned _Length)
51 : Data(_Data), Length(_Length) {}
53 /// Construct a string ref from an std::string.
54 /*implicit*/ StringRef(const std::string &Str)
55 : Data(Str.c_str()), Length(Str.length()) {}
61 iterator begin() const { return Data; }
63 iterator end() const { return Data + Length; }
66 /// @name String Operations
69 /// data - Get a pointer to the start of the string (which may not be null
71 const char *data() const { return Data; }
73 /// empty - Check if the string is empty.
74 bool empty() const { return Length == 0; }
76 /// size - Get the string size.
77 size_t size() const { return Length; }
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);
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;
93 // Otherwise the prefixes match, so we only need to check the lengths.
94 if (Length == RHS.Length)
96 return Length < RHS.Length ? -1 : 1;
99 /// str - Get the contents as an std::string.
100 std::string str() const { return std::string(Data, Length); }
103 /// @name Operator Overloads
106 char operator[](size_t Index) const {
107 assert(Index < Length && "Invalid index!");
112 /// @name Type Conversions
115 operator std::string() const {
120 /// @name Utility Functions
123 /// substr - Return a reference to a substring of this object.
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.
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));
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);
145 /// @name StringRef Comparison Operators
148 inline bool operator==(const StringRef &LHS, const StringRef &RHS) {
149 return LHS.equals(RHS);
152 inline bool operator!=(const StringRef &LHS, const StringRef &RHS) {
153 return !(LHS == RHS);
156 inline bool operator<(const StringRef &LHS, const StringRef &RHS) {
157 return LHS.compare(RHS) == -1;
160 inline bool operator<=(const StringRef &LHS, const StringRef &RHS) {
161 return LHS.compare(RHS) != 1;
164 inline bool operator>(const StringRef &LHS, const StringRef &RHS) {
165 return LHS.compare(RHS) == 1;
168 inline bool operator>=(const StringRef &LHS, const StringRef &RHS) {
169 return LHS.compare(RHS) != -1;