3 #include "AudioTools/CoreAudio/AudioBasic/StrView.h"
28 maxlen = initialAllocatedLength;
38 if (chars !=
nullptr) {
83 size_t capacity() {
return maxlen; }
85 void setCapacity(
size_t newLen) {
grow(newLen); }
88 void allocate(
int len = -1) {
89 int new_size = len < 0 ? maxlen : len;
95 void copyFrom(
const char *source,
int len,
int maxlen = 0) {
96 this->maxlen = maxlen == 0 ? len : maxlen;
98 if (this->chars !=
nullptr) {
100 this->is_const =
false;
101 memmove(this->chars, source, len);
102 this->chars[len] = 0;
109 if (this->chars !=
nullptr) {
110 for (
int j = 0; j < len; j++) {
114 this->is_const =
false;
115 this->chars[len] = 0;
124 for (
size_t i = 0; i < len; i++) {
125 urlEncodeChar(chars[i], temp, 4);
126 new_size += strlen(temp);
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);
137 strcpy(chars, result);
138 this->len = strlen(temp);
145 size_t result_idx = 0;
147 if (chars[i] ==
'%') {
148 szTemp[0] = chars[i + 1];
149 szTemp[1] = chars[i + 2];
150 chars[result_idx] = strToBin(szTemp);
152 }
else if (chars[i] ==
'+') {
153 chars[result_idx] =
' ';
156 chars[result_idx] += chars[i];
161 chars[result_idx] = 0;
162 this->len = result_idx;
172 void swap(
Str &other){
174 int tmp_maxlen = maxlen;
176 maxlen = other.maxlen;
177 vector.swap(other.vector);
178 chars = vector.data();
179 other.chars = other.vector.data();
186 Str& move(Str &other) {
192 bool grow(
int newMaxLen)
override {
194 assert(newMaxLen < 1024 * 10);
195 if (newMaxLen < 0)
return false;
197 if (chars ==
nullptr || newMaxLen > maxlen) {
198 LOGD(
"grow(%d)", newMaxLen);
202 int newSize = newMaxLen > maxlen ? newMaxLen : maxlen;
203 vector.resize(newSize + 1);
210 void urlEncodeChar(
char c,
char *result,
int maxLen) {
212 snprintf(result, maxLen,
"%c", c);
213 }
else if (isspace(c)) {
214 snprintf(result, maxLen,
"%s",
"+");
216 snprintf(result, maxLen,
"%%%X%X", c >> 4, c % 16);
220 char charToInt(
char ch) {
221 if (ch >=
'0' && ch <=
'9') {
222 return (
char)(ch -
'0');
224 if (ch >=
'a' && ch <=
'f') {
225 return (
char)(ch -
'a' + 10);
227 if (ch >=
'A' && ch <=
'F') {
228 return (
char)(ch -
'A' + 10);
233 char strToBin(
char *pString) {
236 szBuffer[0] = charToInt(pString[0]);
237 szBuffer[1] = charToInt(pString[1]);
238 ch = (szBuffer[0] << 4) | szBuffer[1];