arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
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
166 void clear() override {
167 len = 0;
168 maxlen = 0;
169 vector.resize(0);
170 chars = nullptr;
171 }
172
173 void swap(Str &other){
174 int tmp_len = len;
175 int tmp_maxlen = maxlen;
176 len = other.len;
177 maxlen = other.maxlen;
178 vector.swap(other.vector);
179 chars = vector.data();
180 other.chars = other.vector.data();
181 }
182
183
184 protected:
185 Vector<char> vector;
186
187 Str& move(Str &other) {
188 swap(other);
189 other.clear();
190 return *this;
191 }
192
193 bool grow(int newMaxLen) override {
194 bool grown = false;
195 assert(newMaxLen < 1024 * 10);
196 if (newMaxLen < 0) return false;
197
198 if (chars == nullptr || newMaxLen > maxlen) {
199 LOGD("grow(%d)", newMaxLen);
200
201 grown = true;
202 // we use at minimum the defined maxlen
203 int newSize = newMaxLen > maxlen ? newMaxLen : maxlen;
204 vector.resize(newSize + 1);
205 chars = &vector[0];
206 maxlen = newSize;
207 }
208 return grown;
209 }
210
211 void urlEncodeChar(char c, char *result, int maxLen) {
212 if (isalnum(c)) {
213 snprintf(result, maxLen, "%c", c);
214 } else if (isspace(c)) {
215 snprintf(result, maxLen, "%s", "+");
216 } else {
217 snprintf(result, maxLen, "%%%X%X", c >> 4, c % 16);
218 }
219 }
220
221 char charToInt(char ch) {
222 if (ch >= '0' && ch <= '9') {
223 return (char)(ch - '0');
224 }
225 if (ch >= 'a' && ch <= 'f') {
226 return (char)(ch - 'a' + 10);
227 }
228 if (ch >= 'A' && ch <= 'F') {
229 return (char)(ch - 'A' + 10);
230 }
231 return -1;
232 }
233
234 char strToBin(char *pString) {
235 char szBuffer[2];
236 char ch;
237 szBuffer[0] = charToInt(pString[0]); // make the B to 11 -- 00001011
238 szBuffer[1] = charToInt(pString[1]); // make the 0 to 0 -- 00000000
239 ch = (szBuffer[0] << 4) | szBuffer[1]; // to change the BO to 10110000
240 return ch;
241 }
242};
243
244} // 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:193
void clear() override
clears the string by setting the terminating 0 at the beginning
Definition Str.h:166
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