arduino-audio-tools
Str.h
1 #pragma once
2 
3 #include "AudioTools/CoreAudio/AudioBasic/StrView.h"
4 
5 namespace audio_tools {
6 
23 class Str : public StrView {
24  public:
25  Str() = default;
26 
27  Str(int initialAllocatedLength) : StrView() {
28  maxlen = initialAllocatedLength;
29  is_const = false;
30  grow(maxlen);
31  }
32 
33  Str(const char *str) : StrView() {
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  Str(StrView &source) : StrView() { set(source); }
46 
48  Str(Str &source) : StrView() { set(source); }
49 
51  Str(Str &&obj) { move(obj); }
52 
54  ~Str() {
55  len = maxlen = 0;
56  chars = nullptr;
57  }
58 
60  Str &operator=(Str &&obj) {
61  return move(obj);
62  }
63 
65  Str &operator=(Str &obj) {
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(Str &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  Str& move(Str &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: Str.h:23
void urlDecode()
decodes a url encoded string
Definition: Str.h:142
void copyFrom(const char *source, int len, int maxlen=0)
assigns a memory buffer
Definition: Str.h:95
~Str()
Destructor.
Definition: Str.h:54
Str(Str &&obj)
Move constructor.
Definition: Str.h:51
void operator=(int v) override
we can assign an int
Definition: Str.h:79
bool isConst() override
checks if the string is a constant that must not be changed
Definition: Str.h:73
Str(Str &source)
Copy constructor.
Definition: Str.h:48
void operator=(double v) override
we can assign a double
Definition: Str.h:81
void operator=(char *str) override
we can assign a char*
Definition: Str.h:77
void operator=(const char *str) override
we can assign a const char*
Definition: Str.h:75
void setChars(char c, int len)
Fills the string with len chars.
Definition: Str.h:107
Str(StrView &source)
Convert StrView to Str.
Definition: Str.h:45
void urlEncode()
url encode the string
Definition: Str.h:120
Str & operator=(Str &obj)
Copy assingment.
Definition: Str.h:65
Str & operator=(Str &&obj)
Move assignment.
Definition: Str.h:60
bool grow(int newMaxLen) override
only supported in subclasses
Definition: Str.h:192
void clear() override
clears the string by setting the terminating 0 at the beginning
Definition: Str.h:165
bool isOnHeap() override
checks if the string is on the heap
Definition: Str.h:71
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: AudioConfig.h:823