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