Arduino DLNA Server
Loading...
Searching...
No Matches
Str.h
Go to the documentation of this file.
1#pragma once
2
3#include "StrView.h"
4#include "Vector.h"
5#include <string>
6
7namespace tiny_dlna {
8
27class Str {
28 public:
30 Str() : s() {}
31
33 explicit Str(size_t initialAllocatedLength) : s() {
34 if (initialAllocatedLength > 0) s.reserve(initialAllocatedLength);
35 }
36
38 Str(int initialAllocatedLength) : s() {
39 if (initialAllocatedLength > 0)
40 s.reserve(static_cast<size_t>(initialAllocatedLength));
41 }
43 Str(const char* str) : s(str ? str : "") {}
45 Str(const Str& other) = default;
47 Str(Str&& other) noexcept = default;
49 Str& operator=(const Str& other) = default;
51 Str& operator=(Str&& other) noexcept = default;
52
54 bool isEmpty() const { return s.empty(); }
55
57 int length() const { return static_cast<int>(s.size()); }
58
60 void copyFrom(const char* source, int len) {
61 if (source == nullptr || len <= 0) {
62 s.clear();
63 return;
64 }
65 s.assign(source, static_cast<size_t>(len));
66 }
67
69 void substrView(StrView& from, int start, int end) {
70 if (end <= start) {
71 s.clear();
72 return;
73 }
74 int l = end - start;
75 s.assign(from.c_str() + start, static_cast<size_t>(l));
76 }
77
79 void substrView(const char* from, int start, int end) {
80 if (from == nullptr || end <= start) {
81 s.clear();
82 return;
83 }
84 s.assign(from + start, static_cast<size_t>(end - start));
85 }
86
88 const char* c_str() const { return s.c_str(); }
90 operator const char*() const { return c_str(); }
91
93 void clear() { s.clear(); }
94
96 void add(const char* append) {
97 if (append != nullptr) s.append(append);
98 }
100 void add(const uint8_t* append, int len) {
101 if (append != nullptr && len > 0)
102 s.append(reinterpret_cast<const char*>(append), static_cast<size_t>(len));
103 }
105 void add(char c) { s.push_back(c); }
107 void add(int value) {
108 char buf[32];
109 int n = snprintf(buf, sizeof(buf), "%d", value);
110 if (n > 0) s.append(buf, static_cast<size_t>(n));
111 }
113 void add(double value, int precision = 2, int /*width*/ = 0) {
114 char buf[64];
115 int n = snprintf(buf, sizeof(buf), "%.*f", precision, value);
116 if (n > 0) s.append(buf, static_cast<size_t>(n));
117 }
118
120 void set(const char* v) { s.assign(v ? v : ""); }
122 void set(int v) {
123 char buf[32];
124 snprintf(buf, sizeof(buf), "%d", v);
125 s.assign(buf);
126 }
128 void set(double v, int precision = 2, int width = 0) {
129 char buf[64];
130 snprintf(buf, sizeof(buf), "%.*f", precision, v);
131 (void)width;
132 s.assign(buf);
133 }
134
136 Str& operator=(const char* v) {
137 s.assign(v ? v : "");
138 return *this;
139 }
141 Str& operator=(char* v) {
142 s.assign(v ? v : "");
143 return *this;
144 }
146 Str& operator=(int v) {
147 set(v);
148 return *this;
149 }
151 Str& operator=(double v) {
152 set(v);
153 return *this;
154 }
155
157 void reset() { s.clear(); }
159 void clearAll() { s.clear(); }
161 void release() {
162 s.clear();
163 s.shrink_to_fit();
164 }
165
167 bool equals(const char* other) const {
168 return (other != nullptr) && (s.compare(other) == 0);
169 }
171 bool contains(const char* sub) const {
172 return (sub != nullptr) && (s.find(sub) != std::string::npos);
173 }
175 bool endsWith(const char* suffix) const {
176 if (suffix == nullptr) return false;
177 size_t sl = strlen(suffix);
178 if (sl > s.size()) return false;
179 return s.compare(s.size() - sl, sl, suffix) == 0;
180 }
181
183 bool equalsIgnoreCase(const char* other) const {
184 if (other == nullptr) return false;
185 if (s.size() != strlen(other)) return false;
186 for (size_t i = 0; i < s.size(); ++i)
187 if (tolower(s[i]) != tolower(static_cast<unsigned char>(other[i])))
188 return false;
189 return true;
190 }
191
193 bool endsWithIgnoreCase(const char* suffix) const {
194 if (suffix == nullptr) return false;
195 size_t sl = strlen(suffix);
196 if (sl > s.size()) return false;
197 size_t start = s.size() - sl;
198 for (size_t i = 0; i < sl; ++i)
199 if (tolower(s[start + i]) !=
200 tolower(static_cast<unsigned char>(suffix[i])))
201 return false;
202 return true;
203 }
204
206 bool startsWith(const char* prefix) const {
207 if (prefix == nullptr) return false;
208 size_t pl = strlen(prefix);
209 if (pl > s.size()) return false;
210 return s.compare(0, pl, prefix) == 0;
211 }
212
214 int indexOf(const char* substr, int start = 0) const {
215 if (!substr) return -1;
216 size_t pos = s.find(substr, static_cast<size_t>(start));
217 return pos == std::string::npos ? -1 : static_cast<int>(pos);
218 }
220 int indexOf(char c, int start = 0) const {
221 size_t pos = s.find(c, static_cast<size_t>(start));
222 return pos == std::string::npos ? -1 : static_cast<int>(pos);
223 }
224
226 void remove(int n) {
227 if (n <= 0) return;
228 if (n >= static_cast<int>(s.size()))
229 s.clear();
230 else
231 s.erase(0, static_cast<size_t>(n));
232 }
233
235 void trim() {
236 rtrim();
237 ltrim();
238 }
239
241 void ltrim() {
242 size_t i = 0;
243 while (i < s.size() && s.at(i) == ' ') ++i;
244 if (i > 0) s.erase(0, i);
245 }
246
248 void rtrim() {
249 while (!s.empty() && s.back() == ' ') s.pop_back();
250 }
251
253 void setLength(int newLen, bool addZero = true) {
254 if (newLen < 0) return;
255 if (newLen < static_cast<int>(s.size()))
256 s.erase(static_cast<size_t>(newLen));
257 else if (newLen > static_cast<int>(s.size()))
258 s.append(static_cast<size_t>(newLen - s.size()), '\0');
259 }
260
262 void remove(const char* toRemove) {
263 if (!toRemove) return;
264 size_t pos = s.find(toRemove);
265 if (pos != std::string::npos) s.erase(pos, strlen(toRemove));
266 }
267
269 bool replace(const char* toReplace, const char* replaced, int startPos = 0) {
270 if (!toReplace || !replaced) return false;
271 size_t pos = s.find(toReplace, static_cast<size_t>(startPos));
272 if (pos == std::string::npos) return false;
273 s.erase(pos, strlen(toReplace));
274 s.insert(pos, replaced);
275 return true;
276 }
277
279 int replaceAll(const char* toReplace, const char* replaced) {
280 if (!toReplace || !replaced) return 0;
281 int found = 0;
282 size_t pos = 0;
283 while ((pos = s.find(toReplace, pos)) != std::string::npos) {
284 s.erase(pos, strlen(toReplace));
285 s.insert(pos, replaced);
286 pos += strlen(replaced);
287 ++found;
288 }
289 return found;
290 }
291
293 Str substring(int start, int end) const {
294 if (end <= start) return Str();
295 size_t s1 = static_cast<size_t>(start);
296 size_t len = static_cast<size_t>(end - start);
297 return Str(s.substr(s1, len).c_str());
298 }
299
301 int toInt() const { return s.empty() ? 0 : atoi(s.c_str()); }
303 long toLong() const { return s.empty() ? 0L : atol(s.c_str()); }
305 double toDouble() const {
306 return s.empty() ? 0.0 : strtod(s.c_str(), nullptr);
307 }
309 float toFloat() const { return static_cast<float>(toDouble()); }
310
312 void toLowerCase() {
313 for (size_t i = 0; i < s.size(); ++i)
314 s[i] = static_cast<char>(tolower(static_cast<unsigned char>(s[i])));
315 }
317 void toUpperCase() {
318 for (size_t i = 0; i < s.size(); ++i)
319 s[i] = static_cast<char>(toupper(static_cast<unsigned char>(s[i])));
320 }
321
323 void setCapacity(size_t newLen) { s.reserve(newLen); }
325 size_t capacity() const { return s.capacity(); }
326
328 void operator+=(const char* v) { add(v); }
330 void operator+=(int v) { add(v); }
332 void operator+=(double v) { add(v); }
334 void operator+=(const char c) { add(c); }
335
337 bool operator==(const char* alt) const { return equals(alt); }
339 bool operator!=(const char* alt) const { return !equals(alt); }
340
342 char& operator[](size_t i) { return s[i]; }
344 const char& operator[](size_t i) const { return s[i]; }
345
347 bool isOnHeap() const { return true; }
348
349 private:
350 stringPSRAM s;
351};
352
353} // namespace tiny_dlna
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:376
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
Str & operator=(int v)
Assign from integer (operator=)
Definition: Str.h:146
bool equals(const char *other) const
Exact string equality with C-string.
Definition: Str.h:167
Str & operator=(const Str &other)=default
Copy-assign.
Str(int initialAllocatedLength)
Construct reserving an initial capacity (int overload)
Definition: Str.h:38
int length() const
Current length (int)
Definition: Str.h:57
Str(const Str &other)=default
Copy-construct.
Str & operator=(Str &&other) noexcept=default
Move-assign.
Str(Str &&other) noexcept=default
Move-construct.
void operator+=(const char c)
Append via operator+= (char)
Definition: Str.h:334
bool endsWithIgnoreCase(const char *suffix) const
True if ends with suffix (case-insensitive)
Definition: Str.h:193
void remove(const char *toRemove)
Remove first occurrence of substring (no-op if not found)
Definition: Str.h:262
void toLowerCase()
Lowercase in-place.
Definition: Str.h:312
void add(double value, int precision=2, int=0)
Append floating point as text with precision.
Definition: Str.h:113
bool isEmpty() const
True if empty.
Definition: Str.h:54
bool endsWith(const char *suffix) const
True if ends with suffix (case-sensitive)
Definition: Str.h:175
Str(const char *str)
Construct from C-string (nullptr -> empty)
Definition: Str.h:43
double toDouble() const
Convert to double (0.0 if empty)
Definition: Str.h:305
void setLength(int newLen, bool addZero=true)
Resize logical length (pads with NUL if growing)
Definition: Str.h:253
Str & operator=(char *v)
Assign from char* (operator=)
Definition: Str.h:141
void operator+=(const char *v)
Append via operator+= (C-string)
Definition: Str.h:328
bool operator==(const char *alt) const
Equality with C-string.
Definition: Str.h:337
bool equalsIgnoreCase(const char *other) const
Case-insensitive equality with C-string.
Definition: Str.h:183
void clearAll()
Clear contents (legacy alias)
Definition: Str.h:159
bool startsWith(const char *prefix) const
True if starts with prefix (case-sensitive)
Definition: Str.h:206
void copyFrom(const char *source, int len)
Copy from raw buffer with length.
Definition: Str.h:60
bool isOnHeap() const
Always true: storage is heap-backed.
Definition: Str.h:347
void operator+=(int v)
Append via operator+= (int)
Definition: Str.h:330
int replaceAll(const char *toReplace, const char *replaced)
Replace all occurrences of toReplace with replaced; returns count.
Definition: Str.h:279
void reset()
Clear contents (alias of clear)
Definition: Str.h:157
Str()
Construct empty string.
Definition: Str.h:30
Str & operator=(double v)
Assign from double (operator=)
Definition: Str.h:151
void trim()
Trim spaces on both ends.
Definition: Str.h:235
Str & operator=(const char *v)
Assign from C-string (operator=)
Definition: Str.h:136
int toInt() const
Convert to int (0 if empty)
Definition: Str.h:301
bool operator!=(const char *alt) const
Inequality with C-string.
Definition: Str.h:339
long toLong() const
Convert to long (0 if empty)
Definition: Str.h:303
void substrView(StrView &from, int start, int end)
Assign substring view from StrView [start,end)
Definition: Str.h:69
void add(int value)
Append integer as decimal text.
Definition: Str.h:107
int indexOf(const char *substr, int start=0) const
Index of substring from position (or -1)
Definition: Str.h:214
Str substring(int start, int end) const
Return substring [start,end) as a new Str.
Definition: Str.h:293
void ltrim()
Trim leading spaces.
Definition: Str.h:241
void set(int v)
Assign from integer.
Definition: Str.h:122
void set(const char *v)
Assign from C-string (nullptr -> empty)
Definition: Str.h:120
void add(const char *append)
Append C-string (ignored if nullptr)
Definition: Str.h:96
void clear()
Clear contents (size -> 0)
Definition: Str.h:93
bool contains(const char *sub) const
True if substring occurs.
Definition: Str.h:171
const char & operator[](size_t i) const
Element access (const)
Definition: Str.h:344
void add(char c)
Append single character.
Definition: Str.h:105
float toFloat() const
Convert to float (0.0f if empty)
Definition: Str.h:309
Str(size_t initialAllocatedLength)
Construct reserving an initial capacity.
Definition: Str.h:33
void operator+=(double v)
Append via operator+= (double)
Definition: Str.h:332
void substrView(const char *from, int start, int end)
Assign substring view from const char* [start,end)
Definition: Str.h:79
int indexOf(char c, int start=0) const
Index of character from position (or -1)
Definition: Str.h:220
void setCapacity(size_t newLen)
Reserve capacity.
Definition: Str.h:323
void release()
Clear and shrink capacity to fit.
Definition: Str.h:161
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
void set(double v, int precision=2, int width=0)
Assign from floating point with precision.
Definition: Str.h:128
void add(const uint8_t *append, int len)
Append raw bytes.
Definition: Str.h:100
void remove(int n)
removes the first n characters
Definition: Str.h:226
size_t capacity() const
Current capacity.
Definition: Str.h:325
char & operator[](size_t i)
Element access (mutable)
Definition: Str.h:342
bool replace(const char *toReplace, const char *replaced, int startPos=0)
Replace first occurrence of toReplace with replaced starting at startPos.
Definition: Str.h:269
void toUpperCase()
Uppercase in-place.
Definition: Str.h:317
void rtrim()
Trim trailing spaces.
Definition: Str.h:248
Definition: Allocator.h:13
std::basic_string< char, std::char_traits< char >, AllocatorPSRAM< char > > stringPSRAM
Definition: Allocator.h:106