1 | //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file |
10 | /// This file defines the SmallString class. |
11 | /// |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_ADT_SMALLSTRING_H |
15 | #define LLVM_ADT_SMALLSTRING_H |
16 | |
17 | #include "llvm/ADT/SmallVector.h" |
18 | #include "llvm/ADT/StringRef.h" |
19 | #include <cstddef> |
20 | |
21 | namespace llvm { |
22 | |
23 | /// SmallString - A SmallString is just a SmallVector with methods and accessors |
24 | /// that make it work better as a string (e.g. operator+ etc). |
25 | template<unsigned InternalLen> |
26 | class SmallString : public SmallVector<char, InternalLen> { |
27 | public: |
28 | /// Default ctor - Initialize to empty. |
29 | SmallString() = default; |
30 | |
31 | /// Initialize from a StringRef. |
32 | SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {} |
33 | |
34 | /// Initialize by concatenating a list of StringRefs. |
35 | SmallString(std::initializer_list<StringRef> Refs) |
36 | : SmallVector<char, InternalLen>() { |
37 | this->append(Refs); |
38 | } |
39 | |
40 | /// Initialize with a range. |
41 | template<typename ItTy> |
42 | SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {} |
43 | |
44 | /// @} |
45 | /// @name String Assignment |
46 | /// @{ |
47 | |
48 | using SmallVector<char, InternalLen>::assign; |
49 | |
50 | /// Assign from a StringRef. |
51 | void assign(StringRef RHS) { |
52 | SmallVectorImpl<char>::assign(RHS.begin(), RHS.end()); |
53 | } |
54 | |
55 | /// Assign from a list of StringRefs. |
56 | void assign(std::initializer_list<StringRef> Refs) { |
57 | this->clear(); |
58 | append(Refs); |
59 | } |
60 | |
61 | /// @} |
62 | /// @name String Concatenation |
63 | /// @{ |
64 | |
65 | using SmallVector<char, InternalLen>::append; |
66 | |
67 | /// Append from a StringRef. |
68 | void append(StringRef RHS) { |
69 | SmallVectorImpl<char>::append(RHS.begin(), RHS.end()); |
70 | } |
71 | |
72 | /// Append from a list of StringRefs. |
73 | void append(std::initializer_list<StringRef> Refs) { |
74 | size_t CurrentSize = this->size(); |
75 | size_t SizeNeeded = CurrentSize; |
76 | for (const StringRef &Ref : Refs) |
77 | SizeNeeded += Ref.size(); |
78 | this->resize_for_overwrite(SizeNeeded); |
79 | for (const StringRef &Ref : Refs) { |
80 | std::copy(Ref.begin(), Ref.end(), this->begin() + CurrentSize); |
81 | CurrentSize += Ref.size(); |
82 | } |
83 | assert(CurrentSize == this->size()); |
84 | } |
85 | |
86 | /// @} |
87 | /// @name String Comparison |
88 | /// @{ |
89 | |
90 | /// Check for string equality. This is more efficient than compare() when |
91 | /// the relative ordering of inequal strings isn't needed. |
92 | [[nodiscard]] bool equals(StringRef RHS) const { return str().equals(RHS); } |
93 | |
94 | /// Check for string equality, ignoring case. |
95 | [[nodiscard]] bool equals_insensitive(StringRef RHS) const { |
96 | return str().equals_insensitive(RHS); |
97 | } |
98 | |
99 | /// compare - Compare two strings; the result is negative, zero, or positive |
100 | /// if this string is lexicographically less than, equal to, or greater than |
101 | /// the \p RHS. |
102 | [[nodiscard]] int compare(StringRef RHS) const { return str().compare(RHS); } |
103 | |
104 | /// compare_insensitive - Compare two strings, ignoring case. |
105 | [[nodiscard]] int compare_insensitive(StringRef RHS) const { |
106 | return str().compare_insensitive(RHS); |
107 | } |
108 | |
109 | /// compare_numeric - Compare two strings, treating sequences of digits as |
110 | /// numbers. |
111 | [[nodiscard]] int compare_numeric(StringRef RHS) const { |
112 | return str().compare_numeric(RHS); |
113 | } |
114 | |
115 | /// @} |
116 | /// @name String Predicates |
117 | /// @{ |
118 | |
119 | /// starts_with - Check if this string starts with the given \p Prefix. |
120 | [[nodiscard]] bool starts_with(StringRef Prefix) const { |
121 | return str().starts_with(Prefix); |
122 | } |
123 | |
124 | /// ends_with - Check if this string ends with the given \p Suffix. |
125 | [[nodiscard]] bool ends_with(StringRef Suffix) const { |
126 | return str().ends_with(Suffix); |
127 | } |
128 | |
129 | /// @} |
130 | /// @name String Searching |
131 | /// @{ |
132 | |
133 | /// find - Search for the first character \p C in the string. |
134 | /// |
135 | /// \return - The index of the first occurrence of \p C, or npos if not |
136 | /// found. |
137 | [[nodiscard]] size_t find(char C, size_t From = 0) const { |
138 | return str().find(C, From); |
139 | } |
140 | |
141 | /// Search for the first string \p Str in the string. |
142 | /// |
143 | /// \returns The index of the first occurrence of \p Str, or npos if not |
144 | /// found. |
145 | [[nodiscard]] size_t find(StringRef Str, size_t From = 0) const { |
146 | return str().find(Str, From); |
147 | } |
148 | |
149 | /// Search for the last character \p C in the string. |
150 | /// |
151 | /// \returns The index of the last occurrence of \p C, or npos if not |
152 | /// found. |
153 | [[nodiscard]] size_t rfind(char C, size_t From = StringRef::npos) const { |
154 | return str().rfind(C, From); |
155 | } |
156 | |
157 | /// Search for the last string \p Str in the string. |
158 | /// |
159 | /// \returns The index of the last occurrence of \p Str, or npos if not |
160 | /// found. |
161 | [[nodiscard]] size_t rfind(StringRef Str) const { return str().rfind(Str); } |
162 | |
163 | /// Find the first character in the string that is \p C, or npos if not |
164 | /// found. Same as find. |
165 | [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const { |
166 | return str().find_first_of(C, From); |
167 | } |
168 | |
169 | /// Find the first character in the string that is in \p Chars, or npos if |
170 | /// not found. |
171 | /// |
172 | /// Complexity: O(size() + Chars.size()) |
173 | [[nodiscard]] size_t find_first_of(StringRef Chars, size_t From = 0) const { |
174 | return str().find_first_of(Chars, From); |
175 | } |
176 | |
177 | /// Find the first character in the string that is not \p C or npos if not |
178 | /// found. |
179 | [[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const { |
180 | return str().find_first_not_of(C, From); |
181 | } |
182 | |
183 | /// Find the first character in the string that is not in the string |
184 | /// \p Chars, or npos if not found. |
185 | /// |
186 | /// Complexity: O(size() + Chars.size()) |
187 | [[nodiscard]] size_t find_first_not_of(StringRef Chars, |
188 | size_t From = 0) const { |
189 | return str().find_first_not_of(Chars, From); |
190 | } |
191 | |
192 | /// Find the last character in the string that is \p C, or npos if not |
193 | /// found. |
194 | [[nodiscard]] size_t find_last_of(char C, |
195 | size_t From = StringRef::npos) const { |
196 | return str().find_last_of(C, From); |
197 | } |
198 | |
199 | /// Find the last character in the string that is in \p C, or npos if not |
200 | /// found. |
201 | /// |
202 | /// Complexity: O(size() + Chars.size()) |
203 | [[nodiscard]] size_t find_last_of(StringRef Chars, |
204 | size_t From = StringRef::npos) const { |
205 | return str().find_last_of(Chars, From); |
206 | } |
207 | |
208 | /// @} |
209 | /// @name Helpful Algorithms |
210 | /// @{ |
211 | |
212 | /// Return the number of occurrences of \p C in the string. |
213 | [[nodiscard]] size_t count(char C) const { return str().count(C); } |
214 | |
215 | /// Return the number of non-overlapped occurrences of \p Str in the |
216 | /// string. |
217 | [[nodiscard]] size_t count(StringRef Str) const { return str().count(Str); } |
218 | |
219 | /// @} |
220 | /// @name Substring Operations |
221 | /// @{ |
222 | |
223 | /// Return a reference to the substring from [Start, Start + N). |
224 | /// |
225 | /// \param Start The index of the starting character in the substring; if |
226 | /// the index is npos or greater than the length of the string then the |
227 | /// empty substring will be returned. |
228 | /// |
229 | /// \param N The number of characters to included in the substring. If \p N |
230 | /// exceeds the number of characters remaining in the string, the string |
231 | /// suffix (starting with \p Start) will be returned. |
232 | [[nodiscard]] StringRef substr(size_t Start, |
233 | size_t N = StringRef::npos) const { |
234 | return str().substr(Start, N); |
235 | } |
236 | |
237 | /// Return a reference to the substring from [Start, End). |
238 | /// |
239 | /// \param Start The index of the starting character in the substring; if |
240 | /// the index is npos or greater than the length of the string then the |
241 | /// empty substring will be returned. |
242 | /// |
243 | /// \param End The index following the last character to include in the |
244 | /// substring. If this is npos, or less than \p Start, or exceeds the |
245 | /// number of characters remaining in the string, the string suffix |
246 | /// (starting with \p Start) will be returned. |
247 | [[nodiscard]] StringRef slice(size_t Start, size_t End) const { |
248 | return str().slice(Start, End); |
249 | } |
250 | |
251 | // Extra methods. |
252 | |
253 | /// Explicit conversion to StringRef. |
254 | [[nodiscard]] StringRef str() const { |
255 | return StringRef(this->data(), this->size()); |
256 | } |
257 | |
258 | // TODO: Make this const, if it's safe... |
259 | const char* c_str() { |
260 | this->push_back(0); |
261 | this->pop_back(); |
262 | return this->data(); |
263 | } |
264 | |
265 | /// Implicit conversion to StringRef. |
266 | operator StringRef() const { return str(); } |
267 | |
268 | explicit operator std::string() const { |
269 | return std::string(this->data(), this->size()); |
270 | } |
271 | |
272 | // Extra operators. |
273 | SmallString &operator=(StringRef RHS) { |
274 | this->assign(RHS); |
275 | return *this; |
276 | } |
277 | |
278 | SmallString &operator+=(StringRef RHS) { |
279 | this->append(RHS.begin(), RHS.end()); |
280 | return *this; |
281 | } |
282 | SmallString &operator+=(char C) { |
283 | this->push_back(C); |
284 | return *this; |
285 | } |
286 | }; |
287 | |
288 | } // end namespace llvm |
289 | |
290 | #endif // LLVM_ADT_SMALLSTRING_H |
291 | |