arduino-audio-tools
Loading...
Searching...
No Matches
Str.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace audio_tools {
7
24class Str : public StrView {
25 public:
26 Str() = default;
27
28 Str(int initialAllocatedLength) : StrView() {
29 maxlen = initialAllocatedLength;
30 is_const = false;
31 grow(maxlen);
32 }
33
34 Str(const char *str) : StrView() {
35 if (str != nullptr) {
36 len = strlen(str);
37 maxlen = len;
38 grow(maxlen);
39 if (chars != nullptr) {
40 strcpy(chars, str);
41 }
42 }
43 }
44
46 Str(StrView &source) : StrView() { set(source); }
47
49 Str(Str &source) : StrView() { set(source); }
50
52 Str(Str &&obj) { move(obj); }
53
55 ~Str() {
56 len = maxlen = 0;
57 chars = nullptr;
58 }
59
61 Str &operator=(Str &&obj) {
62 return move(obj);
63 }
64
66 Str &operator=(Str &obj) {
67 //assert(&obj!=nullptr);
68 set(obj.c_str());
69 return *this;
70 };
71
72 bool isOnHeap() override { return true; }
73
74 bool isConst() override { return false; }
75
76 void operator=(const char *str) override { set(str); }
77
78 void operator=(char *str) override { set(str); }
79
80 void operator=(int v) override { set(v); }
81
82 void operator=(double v) override { set(v); }
83
84 size_t capacity() { return maxlen; }
85
86 void setCapacity(size_t newLen) { grow(newLen); }
87
88 // make sure that the max size is allocated
89 void allocate(int len = -1) {
90 int new_size = len < 0 ? maxlen : len;
91 grow(new_size);
92 this->len = new_size;
93 }
94
96 void copyFrom(const char *source, int len, int maxlen = 0) {
97 this->maxlen = maxlen == 0 ? len : maxlen;
98 grow(this->maxlen);
99 if (this->chars != nullptr) {
100 int copy_len = len < this->maxlen ? len : this->maxlen;
101 this->len = copy_len;
102 this->is_const = false;
103 memmove(this->chars, source, copy_len);
104 this->chars[copy_len] = 0;
105 }
106 }
107
109 void setChars(char c, int len) {
110 grow(len);
111 if (this->chars != nullptr) {
112 for (int j = 0; j < len; j++) {
113 this->chars[j] = c;
114 }
115 this->len = len;
116 this->is_const = false;
117 this->chars[len] = 0;
118 }
119 }
120
122 void urlEncode() {
123 char temp[4];
124 int new_size = 0;
125 // Calculate the new size
126 for (size_t i = 0; i < len; i++) {
127 urlEncodeChar(chars[i], temp, 4);
128 new_size += strlen(temp);
129 }
130 // build new string
131 char result[new_size + 1];
132 memset(result,0, new_size+1);
133 for (size_t i = 0; i < len; i++) {
134 urlEncodeChar(chars[i], temp, 4);
135 strcat(result, temp);
136 }
137 // save result
138 grow(new_size);
139 strcpy(chars, result);
140 this->len = strlen(result);
141 }
142
144 void urlDecode() {
145 char szTemp[2];
146 size_t i = 0;
147 size_t result_idx = 0;
148 while (i < len) {
149 if (chars[i] == '%' && i + 2 < len) {
150 szTemp[0] = chars[i + 1];
151 szTemp[1] = chars[i + 2];
152 chars[result_idx] = strToBin(szTemp);
153 i = i + 3;
154 } else if (chars[i] == '+') {
155 chars[result_idx] = ' ';
156 i++;
157 } else {
158 chars[result_idx] = chars[i];
159 i++;
160 }
161 result_idx++;
162 }
163 chars[result_idx] = 0;
164 this->len = result_idx;
165 }
166
168 void clear() override {
169 clear(true);
170 }
171
172 void clear(bool resize) {
173 len = 0;
174 if (resize){
175 maxlen = 0;
176 vector.resize(0);
177 }
178 chars = nullptr;
179 }
180
181 void swap(Str &other){
182 int tmp_len = len;
183 int tmp_maxlen = maxlen;
184 len = other.len;
185 maxlen = other.maxlen;
186 vector.swap(other.vector);
187 chars = vector.data();
188 other.chars = other.vector.data();
189 }
190
191
192 protected:
194
195 Str& move(Str &other) {
196 swap(other);
197 other.clear();
198 return *this;
199 }
200
201 bool grow(int newMaxLen) override {
202 bool grown = false;
203 assert(newMaxLen < 1024 * 10);
204 if (newMaxLen < 0) return false;
205
206 if (chars == nullptr || newMaxLen > maxlen) {
207 LOGD("grow(%d)", newMaxLen);
208
209 grown = true;
210 // we use at minimum the defined maxlen
211 int newSize = newMaxLen > maxlen ? newMaxLen : maxlen;
212 vector.resize(newSize + 1);
213 chars = &vector[0];
214 maxlen = newSize;
215 }
216 return grown;
217 }
218
219 void urlEncodeChar(char c, char *result, int maxLen) {
220 if (isalnum(c)) {
221 snprintf(result, maxLen, "%c", c);
222 } else if (isspace(c)) {
223 snprintf(result, maxLen, "%s", "+");
224 } else {
225 snprintf(result, maxLen, "%%%X%X", c >> 4, c % 16);
226 }
227 }
228
229 char charToInt(char ch) {
230 if (ch >= '0' && ch <= '9') {
231 return (char)(ch - '0');
232 }
233 if (ch >= 'a' && ch <= 'f') {
234 return (char)(ch - 'a' + 10);
235 }
236 if (ch >= 'A' && ch <= 'F') {
237 return (char)(ch - 'A' + 10);
238 }
239 return -1;
240 }
241
242 char strToBin(char *pString) {
243 char szBuffer[2];
244 char ch;
245 szBuffer[0] = charToInt(pString[0]); // make the B to 11 -- 00001011
246 szBuffer[1] = charToInt(pString[1]); // make the 0 to 0 -- 00000000
247 ch = (szBuffer[0] << 4) | szBuffer[1]; // to change the BO to 10110000
248 return ch;
249 }
250};
251
252} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define assert(T)
Definition avr.h:10
Str which keeps the data on the heap. We grow the allocated memory only if the copy source is not fit...
Definition Str.h:24
Str(int initialAllocatedLength)
Definition Str.h:28
Str & operator=(Str &obj)
Copy assingment.
Definition Str.h:66
Str & operator=(Str &&obj)
Move assignment.
Definition Str.h:61
void swap(Str &other)
Definition Str.h:181
void urlDecode()
decodes a url encoded string
Definition Str.h:144
void copyFrom(const char *source, int len, int maxlen=0)
assigns a memory buffer
Definition Str.h:96
~Str()
Destructor.
Definition Str.h:55
Str(Str &&obj)
Move constructor.
Definition Str.h:52
char charToInt(char ch)
Definition Str.h:229
Str & move(Str &other)
Definition Str.h:195
void urlEncodeChar(char c, char *result, int maxLen)
Definition Str.h:219
void operator=(int v) override
we can assign an int
Definition Str.h:80
void clear(bool resize)
Definition Str.h:172
bool isConst() override
checks if the string is a constant that must not be changed
Definition Str.h:74
void allocate(int len=-1)
Definition Str.h:89
Str(const char *str)
Definition Str.h:34
Vector< char > vector
Definition Str.h:193
Str(Str &source)
Copy constructor.
Definition Str.h:49
void operator=(double v) override
we can assign a double
Definition Str.h:82
char strToBin(char *pString)
Definition Str.h:242
void operator=(char *str) override
we can assign a char*
Definition Str.h:78
void setCapacity(size_t newLen)
Definition Str.h:86
void operator=(const char *str) override
we can assign a const char*
Definition Str.h:76
void setChars(char c, int len)
Fills the string with len chars.
Definition Str.h:109
Str(StrView &source)
Convert StrView to Str.
Definition Str.h:46
void urlEncode()
url encode the string
Definition Str.h:122
bool grow(int newMaxLen) override
only supported in subclasses
Definition Str.h:201
void clear() override
clears and resizes to 0
Definition Str.h:168
size_t capacity()
Definition Str.h:84
bool isOnHeap() override
checks if the string is on the heap
Definition Str.h:72
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:29
bool is_const
Definition StrView.h:811
int maxlen
Definition StrView.h:813
char * chars
Definition StrView.h:810
virtual void set(const char *alt)
assigs a value
Definition StrView.h:48
virtual const char * c_str()
provides the string value as const char*
Definition StrView.h:456
int len
Definition StrView.h:812
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
void swap(Vector< T > &in)
Definition Vector.h:240
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6