3#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
4#include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
29 maxlen = initialAllocatedLength;
39 if (chars !=
nullptr) {
84 size_t capacity() {
return maxlen; }
86 void setCapacity(
size_t newLen) {
grow(newLen); }
89 void allocate(
int len = -1) {
90 int new_size = len < 0 ? maxlen : len;
96 void copyFrom(
const char *source,
int len,
int maxlen = 0) {
97 this->maxlen = maxlen == 0 ? len : maxlen;
99 if (this->chars !=
nullptr) {
101 this->is_const =
false;
102 memmove(this->chars, source, len);
103 this->chars[len] = 0;
110 if (this->chars !=
nullptr) {
111 for (
int j = 0; j < len; j++) {
115 this->is_const =
false;
116 this->chars[len] = 0;
125 for (
size_t i = 0; i < len; i++) {
126 urlEncodeChar(chars[i], temp, 4);
127 new_size += strlen(temp);
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);
138 strcpy(chars, result);
139 this->len = strlen(temp);
146 size_t result_idx = 0;
148 if (chars[i] ==
'%') {
149 szTemp[0] = chars[i + 1];
150 szTemp[1] = chars[i + 2];
151 chars[result_idx] = strToBin(szTemp);
153 }
else if (chars[i] ==
'+') {
154 chars[result_idx] =
' ';
157 chars[result_idx] += chars[i];
162 chars[result_idx] = 0;
163 this->len = result_idx;
171 void clear(
bool resize) {
180 void swap(Str &other){
182 int tmp_maxlen = maxlen;
184 maxlen = other.maxlen;
185 vector.swap(other.vector);
186 chars = vector.data();
187 other.chars = other.vector.data();
194 Str& move(Str &other) {
200 bool grow(
int newMaxLen)
override {
202 assert(newMaxLen < 1024 * 10);
203 if (newMaxLen < 0)
return false;
205 if (chars ==
nullptr || newMaxLen > maxlen) {
206 LOGD(
"grow(%d)", newMaxLen);
210 int newSize = newMaxLen > maxlen ? newMaxLen : maxlen;
211 vector.resize(newSize + 1);
218 void urlEncodeChar(
char c,
char *result,
int maxLen) {
220 snprintf(result, maxLen,
"%c", c);
221 }
else if (isspace(c)) {
222 snprintf(result, maxLen,
"%s",
"+");
224 snprintf(result, maxLen,
"%%%X%X", c >> 4, c % 16);
228 char charToInt(
char ch) {
229 if (ch >=
'0' && ch <=
'9') {
230 return (
char)(ch -
'0');
232 if (ch >=
'a' && ch <=
'f') {
233 return (
char)(ch -
'a' + 10);
235 if (ch >=
'A' && ch <=
'F') {
236 return (
char)(ch -
'A' + 10);
241 char strToBin(
char *pString) {
244 szBuffer[0] = charToInt(pString[0]);
245 szBuffer[1] = charToInt(pString[1]);
246 ch = (szBuffer[0] << 4) | szBuffer[1];