WPILibC++ 2023.4.3
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 two strings; the result is -1, 0, or 1 if this string is
92 /// lexicographically less than, equal to, or greater than the \p RHS.
93 int compare(std::string_view RHS) const {
94 return str().compare(RHS);
95 }
96
97 /// @}
98 /// @name String Searching
99 /// @{
100
101 /// find - Search for the first character \p C in the string.
102 ///
103 /// \return - The index of the first occurrence of \p C, or npos if not
104 /// found.
105 size_t find(char C, size_t From = 0) const {
106 return str().find(C, From);
107 }
108
109 /// Search for the first string \p Str in the string.
110 ///
111 /// \returns The index of the first occurrence of \p Str, or npos if not
112 /// found.
113 size_t find(std::string_view Str, size_t From = 0) const {
114 return str().find(Str, From);
115 }
116
117 /// Search for the last character \p C in the string.
118 ///
119 /// \returns The index of the last occurrence of \p C, or npos if not
120 /// found.
121 size_t rfind(char C, size_t From = std::string_view::npos) const {
122 return str().rfind(C, From);
123 }
124
125 /// Search for the last string \p Str in the string.
126 ///
127 /// \returns The index of the last occurrence of \p Str, or npos if not
128 /// found.
129 size_t rfind(std::string_view Str) const {
130 return str().rfind(Str);
131 }
132
133 /// Find the first character in the string that is \p C, or npos if not
134 /// found. Same as find.
135 size_t find_first_of(char C, size_t From = 0) const {
136 return str().find_first_of(C, From);
137 }
138
139 /// Find the first character in the string that is in \p Chars, or npos if
140 /// not found.
141 ///
142 /// Complexity: O(size() + Chars.size())
143 size_t find_first_of(std::string_view Chars, size_t From = 0) const {
144 return str().find_first_of(Chars, From);
145 }
146
147 /// Find the first character in the string that is not \p C or npos if not
148 /// found.
149 size_t find_first_not_of(char C, size_t From = 0) const {
150 return str().find_first_not_of(C, From);
151 }
152
153 /// Find the first character in the string that is not in the string
154 /// \p Chars, or npos if not found.
155 ///
156 /// Complexity: O(size() + Chars.size())
157 size_t find_first_not_of(std::string_view Chars, size_t From = 0) const {
158 return str().find_first_not_of(Chars, From);
159 }
160
161 /// Find the last character in the string that is \p C, or npos if not
162 /// found.
163 size_t find_last_of(char C, size_t From = std::string_view::npos) const {
164 return str().find_last_of(C, From);
165 }
166
167 /// Find the last character in the string that is in \p C, or npos if not
168 /// found.
169 ///
170 /// Complexity: O(size() + Chars.size())
172 std::string_view Chars, size_t From = std::string_view::npos) const {
173 return str().find_last_of(Chars, From);
174 }
175
176 /// @}
177
178 // Extra methods.
179
180 /// Explicit conversion to std::string_view.
181 std::string_view str() const { return std::string_view(this->begin(), this->size()); }
182
183 // TODO: Make this const, if it's safe...
184 const char* c_str() {
185 this->push_back(0);
186 this->pop_back();
187 return this->data();
188 }
189
190 /// Implicit conversion to std::string_view.
191 operator std::string_view() const { return str(); }
192
193 explicit operator std::string() const {
194 return std::string(this->data(), this->size());
195 }
196
197 // Extra operators.
199 this->assign(RHS);
200 return *this;
201 }
202
204 this->append(RHS.begin(), RHS.end());
205 return *this;
206 }
208 this->push_back(C);
209 return *this;
210 }
211};
212
213} // end namespace wpi
214
215#endif // WPIUTIL_WPI_SMALLSTRING_H
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:149
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:171
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:143
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:121
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:135
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:181
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:157
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition: SmallString.h:105
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:43
const char * c_str()
Definition: SmallString.h:184
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:163
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:129
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:198
size_t find(std::string_view Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:113
SmallString(std::string_view S)
Initialize from a std::string_view.
Definition: SmallString.h:33
SmallString & operator+=(std::string_view RHS)
Definition: SmallString.h:203
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 two strings; the result is -1, 0, or 1 if this string is lexicographically less than,...
Definition: SmallString.h:93
SmallString & operator+=(char C)
Definition: SmallString.h:207
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1186
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:625
void clear()
Definition: SmallVector.h:594
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:670
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:691
void pop_back()
Definition: SmallVector.h:415
void push_back(const char &Elt)
Definition: SmallVector.h:403
size_t size() const
Definition: SmallVector.h:78
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:271
iterator begin()
Definition: SmallVector.h:252
iterator end()
Definition: SmallVector.h:254
basic_string_view< char > string_view
Definition: core.h:520
OutputIterator copy(const RangeT &range, OutputIterator out)
Definition: ranges.h:26
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
#define S(label, offset, message)
Definition: Errors.h:118