Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
SmallString.h
Go to the documentation of this file.
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
21namespacellvm {
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).
25template<unsigned InternalLen>
26classSmallString :publicSmallVector<char, InternalLen> {
27public:
28 /// Default ctor - Initialize to empty.
29SmallString() =default;
30
31 /// Initialize from a StringRef.
32SmallString(StringRef S) :SmallVector<char, InternalLen>(S.begin(), S.end()) {}
33
34 /// Initialize by concatenating a list of StringRefs.
35SmallString(std::initializer_list<StringRef> Refs)
36 :SmallVector<char, InternalLen>() {
37 this->append(Refs);
38 }
39
40 /// Initialize with a range.
41template<typename ItTy>
42SmallString(ItTy S,ItTyE) :SmallVector<char, InternalLen>(S,E) {}
43
44 /// @}
45 /// @name String Assignment
46 /// @{
47
48usingSmallVector<char, InternalLen>::assign;
49
50 /// Assign from a StringRef.
51voidassign(StringRefRHS) {
52SmallVectorImpl<char>::assign(RHS.begin(),RHS.end());
53 }
54
55 /// Assign from a list of StringRefs.
56voidassign(std::initializer_list<StringRef> Refs) {
57 this->clear();
58append(Refs);
59 }
60
61 /// @}
62 /// @name String Concatenation
63 /// @{
64
65usingSmallVector<char, InternalLen>::append;
66
67 /// Append from a StringRef.
68voidappend(StringRefRHS) {
69SmallVectorImpl<char>::append(RHS.begin(),RHS.end());
70 }
71
72 /// Append from a list of StringRefs.
73voidappend(std::initializer_list<StringRef> Refs) {
74size_t CurrentSize = this->size();
75size_t SizeNeeded = CurrentSize;
76for (constStringRef &Ref : Refs)
77 SizeNeeded +=Ref.size();
78 this->resize_for_overwrite(SizeNeeded);
79for (constStringRef &Ref : Refs) {
80 std::copy(Ref.begin(),Ref.end(), this->begin() + CurrentSize);
81 CurrentSize +=Ref.size();
82 }
83assert(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]]boolequals(StringRefRHS) const{returnstr() ==RHS; }
93
94 /// Check for string equality, ignoring case.
95 [[nodiscard]]boolequals_insensitive(StringRefRHS) const{
96returnstr().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]]intcompare(StringRefRHS) const{returnstr().compare(RHS); }
103
104 /// compare_insensitive - Compare two strings, ignoring case.
105 [[nodiscard]]intcompare_insensitive(StringRefRHS) const{
106returnstr().compare_insensitive(RHS);
107 }
108
109 /// compare_numeric - Compare two strings, treating sequences of digits as
110 /// numbers.
111 [[nodiscard]]intcompare_numeric(StringRefRHS) const{
112returnstr().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]]boolstarts_with(StringRef Prefix) const{
121returnstr().starts_with(Prefix);
122 }
123
124 /// ends_with - Check if this string ends with the given \p Suffix.
125 [[nodiscard]]boolends_with(StringRef Suffix) const{
126returnstr().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_tfind(charC,size_tFrom = 0) const{
138returnstr().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_tfind(StringRef Str,size_tFrom = 0) const{
146returnstr().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_trfind(charC,size_tFrom =StringRef::npos) const{
154returnstr().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_trfind(StringRef Str) const{returnstr().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_tfind_first_of(charC,size_tFrom = 0) const{
166returnstr().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_tfind_first_of(StringRef Chars,size_tFrom = 0) const{
174returnstr().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_tfind_first_not_of(charC,size_tFrom = 0) const{
180returnstr().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_tfind_first_not_of(StringRef Chars,
188size_tFrom = 0) const{
189returnstr().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_tfind_last_of(charC,
195size_tFrom =StringRef::npos) const{
196returnstr().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_tfind_last_of(StringRef Chars,
204size_tFrom =StringRef::npos) const{
205returnstr().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_tcount(charC) const{returnstr().count(C); }
214
215 /// Return the number of non-overlapped occurrences of \p Str in the
216 /// string.
217 [[nodiscard]]size_tcount(StringRef Str) const{returnstr().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]]StringRefsubstr(size_t Start,
233size_tN =StringRef::npos) const{
234returnstr().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]]StringRefslice(size_t Start,size_tEnd) const{
248returnstr().slice(Start,End);
249 }
250
251// Extra methods.
252
253 /// Explicit conversion to StringRef.
254 [[nodiscard]]StringRefstr() const{
255returnStringRef(this->data(), this->size());
256 }
257
258// TODO: Make this const, if it's safe...
259constchar*c_str() {
260 this->push_back(0);
261 this->pop_back();
262return this->data();
263 }
264
265 /// Implicit conversion to StringRef.
266operatorStringRef() const{returnstr(); }
267
268explicitoperator std::string() const{
269return std::string(this->data(), this->size());
270 }
271
272// Extra operators.
273SmallString &operator=(StringRefRHS) {
274 this->assign(RHS);
275return *this;
276 }
277
278SmallString &operator+=(StringRefRHS) {
279 this->append(RHS.begin(),RHS.end());
280return *this;
281 }
282SmallString &operator+=(charC) {
283 this->push_back(C);
284return *this;
285 }
286};
287
288}// end namespace llvm
289
290#endif// LLVM_ADT_SMALLSTRING_H
From
BlockVerifier::State From
Definition:BlockVerifier.cpp:57
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
End
bool End
Definition:ELF_riscv.cpp:480
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector.h
This file defines the SmallVector class.
data
static Split data
Definition:StaticDataSplitter.cpp:176
StringRef.h
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
ItTy
char
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition:SmallString.h:26
llvm::SmallString::rfind
size_t rfind(StringRef Str) const
Search for the last string Str in the string.
Definition:SmallString.h:161
llvm::SmallString::find_first_of
size_t find_first_of(StringRef Chars, size_t From=0) const
Find the first character in the string that is in Chars, or npos if not found.
Definition:SmallString.h:173
llvm::SmallString::SmallString
SmallString(StringRef S)
Initialize from a StringRef.
Definition:SmallString.h:32
llvm::SmallString::find_last_of
size_t find_last_of(StringRef Chars, size_t From=StringRef::npos) const
Find the last character in the string that is in C, or npos if not found.
Definition:SmallString.h:203
llvm::SmallString::find_last_of
size_t find_last_of(char C, size_t From=StringRef::npos) const
Find the last character in the string that is C, or npos if not found.
Definition:SmallString.h:194
llvm::SmallString::operator=
SmallString & operator=(StringRef RHS)
Definition:SmallString.h:273
llvm::SmallString::assign
void assign(std::initializer_list< StringRef > Refs)
Assign from a list of StringRefs.
Definition:SmallString.h:56
llvm::SmallString::SmallString
SmallString()=default
Default ctor - Initialize to empty.
llvm::SmallString::find
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition:SmallString.h:137
llvm::SmallString::ends_with
bool ends_with(StringRef Suffix) const
ends_with - Check if this string ends with the given Suffix.
Definition:SmallString.h:125
llvm::SmallString::find
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
Definition:SmallString.h:145
llvm::SmallString::equals
bool equals(StringRef RHS) const
Check for string equality.
Definition:SmallString.h:92
llvm::SmallString::count
size_t count(StringRef Str) const
Return the number of non-overlapped occurrences of Str in the string.
Definition:SmallString.h:217
llvm::SmallString::slice
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition:SmallString.h:247
llvm::SmallString::rfind
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
Definition:SmallString.h:153
llvm::SmallString::append
void append(std::initializer_list< StringRef > Refs)
Append from a list of StringRefs.
Definition:SmallString.h:73
llvm::SmallString::operator+=
SmallString & operator+=(char C)
Definition:SmallString.h:282
llvm::SmallString::assign
void assign(StringRef RHS)
Assign from a StringRef.
Definition:SmallString.h:51
llvm::SmallString::operator+=
SmallString & operator+=(StringRef RHS)
Definition:SmallString.h:278
llvm::SmallString::equals_insensitive
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition:SmallString.h:95
llvm::SmallString::compare_numeric
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition:SmallString.h:111
llvm::SmallString::compare
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition:SmallString.h:102
llvm::SmallString::starts_with
bool starts_with(StringRef Prefix) const
starts_with - Check if this string starts with the given Prefix.
Definition:SmallString.h:120
llvm::SmallString::substr
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
Definition:SmallString.h:232
llvm::SmallString::find_first_not_of
size_t find_first_not_of(StringRef Chars, size_t From=0) const
Find the first character in the string that is not in the string Chars, or npos if not found.
Definition:SmallString.h:187
llvm::SmallString::append
void append(StringRef RHS)
Append from a StringRef.
Definition:SmallString.h:68
llvm::SmallString::SmallString
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition:SmallString.h:42
llvm::SmallString::compare_insensitive
int compare_insensitive(StringRef RHS) const
compare_insensitive - Compare two strings, ignoring case.
Definition:SmallString.h:105
llvm::SmallString::count
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition:SmallString.h:213
llvm::SmallString::c_str
const char * c_str()
Definition:SmallString.h:259
llvm::SmallString::find_first_of
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition:SmallString.h:165
llvm::SmallString::SmallString
SmallString(std::initializer_list< StringRef > Refs)
Initialize by concatenating a list of StringRefs.
Definition:SmallString.h:35
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition:SmallString.h:254
llvm::SmallString::find_first_not_of
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition:SmallString.h:179
llvm::SmallVectorBase< SmallVectorSizeType< T > >::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl::resize_for_overwrite
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition:SmallVector.h:641
llvm::SmallVectorImpl::assign
void assign(size_type NumElts, ValueParamT Elt)
Definition:SmallVector.h:704
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition:SmallVector.h:683
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::pop_back
void pop_back()
Definition:SmallVector.h:425
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVectorTemplateCommon::end
iterator end()
Definition:SmallVector.h:269
llvm::SmallVectorTemplateCommon::begin
iterator begin()
Definition:SmallVector.h:267
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringRef::substr
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition:StringRef.h:571
llvm::StringRef::starts_with
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition:StringRef.h:265
llvm::StringRef::begin
iterator begin() const
Definition:StringRef.h:116
llvm::StringRef::slice
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition:StringRef.h:684
llvm::StringRef::find_last_of
size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition:StringRef.h:400
llvm::StringRef::compare_numeric
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition:StringRef.cpp:63
llvm::StringRef::find_first_of
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition:StringRef.h:377
llvm::StringRef::rfind
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition:StringRef.h:347
llvm::StringRef::find
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition:StringRef.h:297
llvm::StringRef::count
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition:StringRef.h:451
llvm::StringRef::ends_with
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition:StringRef.h:277
llvm::StringRef::npos
static constexpr size_t npos
Definition:StringRef.h:53
llvm::StringRef::compare
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition:StringRef.h:183
llvm::StringRef::equals_insensitive
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition:StringRef.h:176
llvm::StringRef::find_first_not_of
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition:StringRef.cpp:253
llvm::StringRef::compare_insensitive
int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
Definition:StringRef.cpp:37
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
N
#define N

Generated on Thu Jul 17 2025 04:49:33 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp