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
6namespace tiny_dlna {
7
22class Str : public StrView {
23 public:
24
25 Str(int initialAllocatedLength = 0) {
26 maxlen = initialAllocatedLength;
27 is_const = false;
28 grow(maxlen);
29 }
30
31 Str(const char *str) : Str() {
32 if (str != nullptr) {
33 len = strlen(str);
34 maxlen = len;
35 grow(maxlen);
36 if (chars != nullptr) {
37 strcpy(chars, str);
38 }
39 }
40 }
41
43 Str(StrView &source) : Str() { set(source); }
44
46 Str(Str &source) : Str() { set(source); }
47
49 Str(Str &&obj) { move(obj); }
50
52 ~Str() {
53 len = maxlen = 0;
54 chars = nullptr;
55 }
56
58 Str &operator=(Str &&obj) {
59 return move(obj);
60 }
61
63 Str &operator=(Str &obj) {
64 //assert(&obj!=nullptr);
65 set(obj.c_str());
66 return *this;
67 };
68
69 bool isOnHeap() override { return true; }
70
71 bool isConst() override { return false; }
72
73 void operator=(const char *str) override { set(str); }
74
75 void operator=(char *str) override { set(str); }
76
77 void operator=(int v) override { set(v); }
78
79 void operator=(double v) override { set(v); }
80
81 size_t capacity() { return maxlen; }
82
83 void setCapacity(size_t newLen) { grow(newLen); }
84
85 // make sure that the max size is allocated
86 void allocate(int len = -1) {
87 int new_size = len < 0 ? maxlen : len;
88 grow(new_size);
89 this->len = new_size;
90 }
91
93 void copyFrom(const char *source, int len, int maxlen = 0) {
94 this->maxlen = maxlen == 0 ? len : maxlen;
95 resize(this->maxlen);
96 if (this->chars != nullptr) {
97 this->len = len;
98 this->is_const = false;
99 memmove(this->chars, source, len);
100 this->chars[len] = 0;
101 }
102 }
103
105 void setChars(char c, int len) {
106 grow(this->maxlen);
107 if (this->chars != nullptr) {
108 for (int j = 0; j < len; j++) {
109 this->chars[j] = c;
110 }
111 this->len = len;
112 this->is_const = false;
113 this->chars[len] = 0;
114 }
115 }
116
118 void urlEncode() {
119 char temp[4];
120 int new_size = 0;
121 // Calculate the new size
122 for (size_t i = 0; i < len; i++) {
123 urlEncodeChar(chars[i], temp, 4);
124 new_size += strlen(temp);
125 }
126 // build new string
127 char result[new_size + 1];
128 memset(result,0, new_size+1);
129 for (size_t i = 0; i < len; i++) {
130 urlEncodeChar(chars[i], temp, 4);
131 strcat(result, temp);
132 }
133 // save result
134 grow(new_size);
135 strcpy(chars, result);
136 this->len = strlen(temp);
137 }
138
140 void urlDecode() {
141 char szTemp[2];
142 size_t i = 0;
143 size_t result_idx = 0;
144 while (i < len) {
145 if (chars[i] == '%') {
146 szTemp[0] = chars[i + 1];
147 szTemp[1] = chars[i + 2];
148 chars[result_idx] = strToBin(szTemp);
149 i = i + 3;
150 } else if (chars[i] == '+') {
151 chars[result_idx] = ' ';
152 i++;
153 } else {
154 chars[result_idx] += chars[i];
155 i++;
156 }
157 result_idx++;
158 }
159 chars[result_idx] = 0;
160 this->len = result_idx;
161 }
162
163 void clear() override {
164 len = 0;
165 maxlen = 0;
166 vector.resize(0);
167 chars = nullptr;
168 }
169
170 void resize(int size) {
171 vector.resize(size+1);
172 maxlen = size;
173 len = size;
174 chars = vector.data();
175 }
176
177 void swap(Str &other){
178 int tmp_len = len;
179 int tmp_maxlen = maxlen;
180 len = other.len;
181 maxlen = other.maxlen;
182 vector.swap(other.vector);
183 chars = vector.data();
184 other.chars = other.vector.data();
185 }
186
187 const char* c_str() { return vector.data(); }
188
189 // just sets the len to 0
190 void reset() {
191 memset(vector.data(),0, len);
192 len = 0;
193 }
195 Str substring(int start, int end) {
196 Str result;
197 if (end > start && this->chars != nullptr) {
198 int len = end - start;
199 result.grow(len);
200 len = len < this->maxlen ? len : this->maxlen;
201 char* target = (char*) result.c_str();
202 strncpy(target, this->c_str() + start, len);
203 target[len] = 0;
204 }
205 return result;
206 }
207
209 void remove(int n) {
210 if (n >= this->length()) {
211 reset();
212 } else if (n <= 0) {
213 return;
214 } else {
215 int remaining = this->length() - n;
216 memmove(this->chars, this->chars + n, remaining);
217 this->chars[remaining] = 0;
218 this->len = remaining;
219 }
220 }
221
222
223 protected:
225
226 Str& move(Str &other) {
227 swap(other);
228 other.clear();
229 return *this;
230 }
231
232 bool grow(int newMaxLen) override {
233 bool grown = false;
234 assert(newMaxLen < 1024 * 10);
235 if (newMaxLen < 0) return false;
236 if (newMaxLen == 0 && chars == nullptr) return false;
237
238 if (chars == nullptr || newMaxLen > maxlen) {
239 grown = true;
240 // we use at minimum the defined maxlen
241 int newSize = newMaxLen > maxlen ? newMaxLen : maxlen;
242 vector.resize(newSize + 1);
243 chars = &vector[0];
244 maxlen = newSize;
245 }
246 return grown;
247 }
248
249 void urlEncodeChar(char c, char *result, int maxLen) {
250 if (isalnum(c)) {
251 snprintf(result, maxLen, "%c", c);
252 } else if (isspace(c)) {
253 snprintf(result, maxLen, "%s", "+");
254 } else {
255 snprintf(result, maxLen, "%%%X%X", c >> 4, c % 16);
256 }
257 }
258
259 char charToInt(char ch) {
260 if (ch >= '0' && ch <= '9') {
261 return (char)(ch - '0');
262 }
263 if (ch >= 'a' && ch <= 'f') {
264 return (char)(ch - 'a' + 10);
265 }
266 if (ch >= 'A' && ch <= 'F') {
267 return (char)(ch - 'A' + 10);
268 }
269 return -1;
270 }
271
272 char strToBin(char *pString) {
273 char szBuffer[2];
274 char ch;
275 szBuffer[0] = charToInt(pString[0]); // make the B to 11 -- 00001011
276 szBuffer[1] = charToInt(pString[1]); // make the 0 to 0 -- 00000000
277 ch = (szBuffer[0] << 4) | szBuffer[1]; // to change the BO to 10110000
278 return ch;
279 }
280};
281
282} // 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:19
char * chars
Definition: StrView.h:756
virtual int length()
Definition: StrView.h:380
int len
Definition: StrView.h:758
int maxlen
Definition: StrView.h:759
virtual void set(const char *alt)
assigs a value
Definition: StrView.h:45
bool is_const
Definition: StrView.h:757
String implementation which keeps the data on the heap. We grow the allocated memory only if the copy...
Definition: Str.h:22
void resize(int size)
Definition: Str.h:170
void operator=(const char *str) override
we can assign a const char*
Definition: Str.h:73
const char * c_str()
provides the string value as const char*
Definition: Str.h:187
void urlEncode()
url encode the string
Definition: Str.h:118
void clear() override
clears the string by setting the terminating 0 at the beginning
Definition: Str.h:163
Str(const char *str)
Definition: Str.h:31
Str substring(int start, int end)
copies a substring into the current string
Definition: Str.h:195
char charToInt(char ch)
Definition: Str.h:259
bool isOnHeap() override
checks if the string is on the heap
Definition: Str.h:69
Str(Str &&obj)
Move constructor.
Definition: Str.h:49
void setChars(char c, int len)
Fills the string with len chars.
Definition: Str.h:105
Str & operator=(Str &&obj)
Move assignment.
Definition: Str.h:58
void copyFrom(const char *source, int len, int maxlen=0)
assigns a memory buffer
Definition: Str.h:93
Str(Str &source)
Copy constructor.
Definition: Str.h:46
size_t capacity()
Definition: Str.h:81
void urlDecode()
decodes a url encoded string
Definition: Str.h:140
Vector< char > vector
Definition: Str.h:224
void reset()
Definition: Str.h:190
void allocate(int len=-1)
Definition: Str.h:86
bool grow(int newMaxLen) override
only supported in subclasses
Definition: Str.h:232
Str & operator=(Str &obj)
Copy assingment.
Definition: Str.h:63
Str(StrView &source)
Convert StrView to Str.
Definition: Str.h:43
void swap(Str &other)
Definition: Str.h:177
char strToBin(char *pString)
Definition: Str.h:272
void operator=(double v) override
we can assign a double
Definition: Str.h:79
bool isConst() override
checks if the string is a constant that must not be changed
Definition: Str.h:71
Str(int initialAllocatedLength=0)
Definition: Str.h:25
~Str()
Destructor.
Definition: Str.h:52
void operator=(char *str) override
we can assign a char*
Definition: Str.h:75
void setCapacity(size_t newLen)
Definition: Str.h:83
void urlEncodeChar(char c, char *result, int maxLen)
Definition: Str.h:249
void remove(int n)
removes the first n characters
Definition: Str.h:209
Str & move(Str &other)
Definition: Str.h:226
void operator=(int v) override
we can assign an int
Definition: Str.h:77
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
bool resize(int newSize, T value)
Definition: Vector.h:251
void swap(Vector< T > &in)
Definition: Vector.h:229
T * data()
Definition: Vector.h:298
Definition: Allocator.h:6