arduino-audio-tools
Loading...
Searching...
No Matches
Str.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
4#include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
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 this->len = len;
101 this->is_const = false;
102 memmove(this->chars, source, len);
103 this->chars[len] = 0;
104 }
105 }
106
108 void setChars(char c, int len) {
109 grow(this->maxlen);
110 if (this->chars != nullptr) {
111 for (int j = 0; j < len; j++) {
112 this->chars[j] = c;
113 }
114 this->len = len;
115 this->is_const = false;
116 this->chars[len] = 0;
117 }
118 }
119
121 void urlEncode() {
122 char temp[4];
123 int new_size = 0;
124 // Calculate the new size
125 for (size_t i = 0; i < len; i++) {
126 urlEncodeChar(chars[i], temp, 4);
127 new_size += strlen(temp);
128 }
129 // build new string
130 char result[new_size + 1];
131 memset(result,0, new_size+1);
132 for (size_t i = 0; i < len; i++) {
133 urlEncodeChar(chars[i], temp, 4);
134 strcat(result, temp);
135 }
136 // save result
137 grow(new_size);
138 strcpy(chars, result);
139 this->len = strlen(temp);
140 }
141
143 void urlDecode() {
144 char szTemp[2];
145 size_t i = 0;
146 size_t result_idx = 0;
147 while (i < len) {
148 if (chars[i] == '%') {
149 szTemp[0] = chars[i + 1];
150 szTemp[1] = chars[i + 2];
151 chars[result_idx] = strToBin(szTemp);
152 i = i + 3;
153 } else if (chars[i] == '+') {
154 chars[result_idx] = ' ';
155 i++;
156 } else {
157 chars[result_idx] += chars[i];
158 i++;
159 }
160 result_idx++;
161 }
162 chars[result_idx] = 0;
163 this->len = result_idx;
164 }
165
167 void clear() override {
168 clear(true);
169 }
170
171 void clear(bool resize) {
172 len = 0;
173 if (resize){
174 maxlen = 0;
175 vector.resize(0);
176 }
177 chars = nullptr;
178 }
179
180 void swap(Str &other){
181 int tmp_len = len;
182 int tmp_maxlen = maxlen;
183 len = other.len;
184 maxlen = other.maxlen;
185 vector.swap(other.vector);
186 chars = vector.data();
187 other.chars = other.vector.data();
188 }
189
190
191 protected:
192 Vector<char> vector;
193
194 Str& move(Str &other) {
195 swap(other);
196 other.clear();
197 return *this;
198 }
199
200 bool grow(int newMaxLen) override {
201 bool grown = false;
202 assert(newMaxLen < 1024 * 10);
203 if (newMaxLen < 0) return false;
204
205 if (chars == nullptr || newMaxLen > maxlen) {
206 LOGD("grow(%d)", newMaxLen);
207
208 grown = true;
209 // we use at minimum the defined maxlen
210 int newSize = newMaxLen > maxlen ? newMaxLen : maxlen;
211 vector.resize(newSize + 1);
212 chars = &vector[0];
213 maxlen = newSize;
214 }
215 return grown;
216 }
217
218 void urlEncodeChar(char c, char *result, int maxLen) {
219 if (isalnum(c)) {
220 snprintf(result, maxLen, "%c", c);
221 } else if (isspace(c)) {
222 snprintf(result, maxLen, "%s", "+");
223 } else {
224 snprintf(result, maxLen, "%%%X%X", c >> 4, c % 16);
225 }
226 }
227
228 char charToInt(char ch) {
229 if (ch >= '0' && ch <= '9') {
230 return (char)(ch - '0');
231 }
232 if (ch >= 'a' && ch <= 'f') {
233 return (char)(ch - 'a' + 10);
234 }
235 if (ch >= 'A' && ch <= 'F') {
236 return (char)(ch - 'A' + 10);
237 }
238 return -1;
239 }
240
241 char strToBin(char *pString) {
242 char szBuffer[2];
243 char ch;
244 szBuffer[0] = charToInt(pString[0]); // make the B to 11 -- 00001011
245 szBuffer[1] = charToInt(pString[1]); // make the 0 to 0 -- 00000000
246 ch = (szBuffer[0] << 4) | szBuffer[1]; // to change the BO to 10110000
247 return ch;
248 }
249};
250
251} // namespace audio_tools
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 & operator=(Str &obj)
Copy assingment.
Definition Str.h:66
Str & operator=(Str &&obj)
Move assignment.
Definition Str.h:61
void urlDecode()
decodes a url encoded string
Definition Str.h:143
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
void operator=(int v) override
we can assign an int
Definition Str.h:80
bool isConst() override
checks if the string is a constant that must not be changed
Definition Str.h:74
Str(Str &source)
Copy constructor.
Definition Str.h:49
void operator=(double v) override
we can assign a double
Definition Str.h:82
void operator=(char *str) override
we can assign a char*
Definition Str.h:78
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:108
Str(StrView &source)
Convert StrView to Str.
Definition Str.h:46
void urlEncode()
url encode the string
Definition Str.h:121
bool grow(int newMaxLen) override
only supported in subclasses
Definition Str.h:200
void clear() override
clears and resizes to 0
Definition Str.h:167
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:28
virtual void set(const char *alt)
assigs a value
Definition StrView.h:47
virtual const char * c_str()
provides the string value as const char*
Definition StrView.h:379
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10