WPILibC++ 2025.0.0-alpha-1-24-g6478ba6
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 WPIUTIL_WPI_SMALLSTRING_H
15#define WPIUTIL_WPI_SMALLSTRING_H
16
17#include "wpi/SmallVector.h"
18#include <cstddef>
19#include <string>
20#include <string_view>
21
22namespace wpi {
23
24/// SmallString - A SmallString is just a SmallVector with methods and accessors
25/// that make it work better as a string (e.g. operator+ etc).
26template<unsigned InternalLen>
27class SmallString : public SmallVector<char, InternalLen> {
28public:
29 /// Default ctor - Initialize to empty.
30 SmallString() = default;
31
32 /// Initialize from a std::string_view.
33 SmallString(std::string_view S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
34
35 /// Initialize by concatenating a list of std::string_views.
36 SmallString(std::initializer_list<std::string_view> Refs)
37 : SmallVector<char, InternalLen>() {
38 this->append(Refs);
39 }
40
41 /// Initialize with a range.
42 template<typename ItTy>
43 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
44
45 /// @}
46 /// @name String Assignment
47 /// @{
48
49 using SmallVector<char, InternalLen>::assign;
50
51 /// Assign from a std::string_view.
53 SmallVectorImpl<char>::assign(RHS.begin(), RHS.end());
54 }
55
56 /// Assign from a list of std::string_views.
57 void assign(std::initializer_list<std::string_view> Refs) {
58 this->clear();
59 append(Refs);
60 }
61
62 /// @}
63 /// @name String Concatenation
64 /// @{
65
66 using SmallVector<char, InternalLen>::append;
67
68 /// Append from a std::string_view.
70 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
71 }
72
73 /// Append from a list of std::string_views.
74 void append(std::initializer_list<std::string_view> Refs) {
75 size_t CurrentSize = this->size();
76 size_t SizeNeeded = CurrentSize;
77 for (std::string_view Ref : Refs)
78 SizeNeeded += Ref.size();
79 this->resize_for_overwrite(SizeNeeded);
80 for (std::string_view Ref : Refs) {
81 std::copy(Ref.begin(), Ref.end(), this->begin() + CurrentSize);
82 CurrentSize += Ref.size();
83 }
84 assert(CurrentSize == this->size());
85 }
86
87 /// @}
88 /// @name String Comparison
89 /// @{
90
91 /// compare - Compare two strings; the result is negative, zero, or positive
92 /// if this string is lexicographically less than, equal to, or greater than
93 /// the \p RHS.
94 [[nodiscard]] int compare(std::string_view RHS) const {
95 return str().compare(RHS);
96 }
97
98 /// @}
99 /// @name String Searching
100 /// @{
101
102 /// find - Search for the first character \p C in the string.
103 ///
104 /// \return - The index of the first occurrence of \p C, or npos if not
105 /// found.
106 [[nodiscard]] size_t find(char C, size_t From = 0) const {
107 return str().find(C, From);
108 }
109
110 /// Search for the first string \p Str in the string.
111 ///
112 /// \returns The index of the first occurrence of \p Str, or npos if not
113 /// found.
114 [[nodiscard]] size_t find(std::string_view Str, size_t From = 0) const {
115 return str().find(Str, From);
116 }
117
118 /// Search for the last character \p C in the string.
119 ///
120 /// \returns The index of the last occurrence of \p C, or npos if not
121 /// found.
122 [[nodiscard]] size_t rfind(char C,
123 size_t From = std::string_view::npos) const {
124 return str().rfind(C, From);
125 }
126
127 /// Search for the last string \p Str in the string.
128 ///
129 /// \returns The index of the last occurrence of \p Str, or npos if not
130 /// found.
131 [[nodiscard]] size_t rfind(std::string_view Str) const {
132 return str().rfind(Str);
133 }
134
135 /// Find the first character in the string that is \p C, or npos if not
136 /// found. Same as find.
137 [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const {
138 return str().find_first_of(C, From);
139 }
140
141 /// Find the first character in the string that is in \p Chars, or npos if
142 /// not found.
143 ///
144 /// Complexity: O(size() + Chars.size())
145 [[nodiscard]] size_t find_first_of(std::string_view Chars,
146 size_t From = 0) const {
147 return str().find_first_of(Chars, From);
148 }
149
150 /// Find the first character in the string that is not \p C or npos if not
151 /// found.
152 [[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const {
153 return str().find_first_not_of(C, From);
154 }
155
156 /// Find the first character in the string that is not in the string
157 /// \p Chars, or npos if not found.
158 ///
159 /// Complexity: O(size() + Chars.size())
160 [[nodiscard]] size_t find_first_not_of(std::string_view Chars,
161 size_t From = 0) const {
162 return str().find_first_not_of(Chars, From);
163 }
164
165 /// Find the last character in the string that is \p C, or npos if not
166 /// found.
167 [[nodiscard]] size_t find_last_of(
168 char C, size_t From = std::string_view::npos) const {
169 return str().find_last_of(C, From);
170 }
171
172 /// Find the last character in the string that is in \p C, or npos if not
173 /// found.
174 ///
175 /// Complexity: O(size() + Chars.size())
176 [[nodiscard]] size_t find_last_of(
177 std::string_view Chars, size_t From = std::string_view::npos) const {
178 return str().find_last_of(Chars, From);
179 }
180
181 /// @}
182
183 // Extra methods.
184
185 /// Explicit conversion to std::string_view.
186 [[nodiscard]] std::string_view str() const {
187 return std::string_view(this->begin(), this->size());
188 }
189
190 // TODO: Make this const, if it's safe...
191 const char* c_str() {
192 this->push_back(0);
193 this->pop_back();
194 return this->data();
195 }
196
197 /// Implicit conversion to std::string_view.
198 operator std::string_view() const { return str(); }
199
200 explicit operator std::string() const {
201 return std::string(this->data(), this->size());
202 }
203
204 // Extra operators.
206 this->assign(RHS);
207 return *this;
208 }
209
211 this->append(RHS.begin(), RHS.end());
212 return *this;
213 }
215 this->push_back(C);
216 return *this;
217 }
218};
219
220} // end namespace wpi
221
222#endif // WPIUTIL_WPI_SMALLSTRING_H
This file defines the SmallVector class.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:27
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:152
size_t find_last_of(std::string_view Chars, size_t From=std::string_view::npos) const
Find the last character in the string that is in C, or npos if not found.
Definition: SmallString.h:176
size_t find_first_of(std::string_view 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:145
size_t rfind(char C, size_t From=std::string_view::npos) const
Search for the last character C in the string.
Definition: SmallString.h:122
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:137
void assign(std::initializer_list< std::string_view > Refs)
Assign from a list of std::string_views.
Definition: SmallString.h:57
std::string_view str() const
Explicit conversion to std::string_view.
Definition: SmallString.h:186
size_t find_first_not_of(std::string_view 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:160
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition: SmallString.h:106
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:43
const char * c_str()
Definition: SmallString.h:191
size_t find_last_of(char C, size_t From=std::string_view::npos) const
Find the last character in the string that is C, or npos if not found.
Definition: SmallString.h:167
void append(std::string_view RHS)
Append from a std::string_view.
Definition: SmallString.h:69
size_t rfind(std::string_view Str) const
Search for the last string Str in the string.
Definition: SmallString.h:131
void assign(std::string_view RHS)
Assign from a std::string_view.
Definition: SmallString.h:52
void append(std::initializer_list< std::string_view > Refs)
Append from a list of std::string_views.
Definition: SmallString.h:74
SmallString & operator=(std::string_view RHS)
Definition: SmallString.h:205
size_t find(std::string_view Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:114
SmallString(std::string_view S)
Initialize from a std::string_view.
Definition: SmallString.h:33
SmallString & operator+=(std::string_view RHS)
Definition: SmallString.h:210
SmallString()=default
Default ctor - Initialize to empty.
SmallString(std::initializer_list< std::string_view > Refs)
Initialize by concatenating a list of std::string_views.
Definition: SmallString.h:36
int compare(std::string_view RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition: SmallString.h:94
SmallString & operator+=(char C)
Definition: SmallString.h:214
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1211
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:656
void clear()
Definition: SmallVector.h:625
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:719
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:698
void pop_back()
Definition: SmallVector.h:440
void push_back(const char &Elt)
Definition: SmallVector.h:428
size_t size() const
Definition: SmallVector.h:98
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:301
iterator begin()
Definition: SmallVector.h:282
iterator end()
Definition: SmallVector.h:284
FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, counting_iterator it) -> counting_iterator
Definition: compile.h:25
Definition: ntcore_cpp.h:26
#define S(label, offset, message)
Definition: Errors.h:119
basic_string_view< char > string_view
Definition: base.h:601