6 #include "AudioTools/AudioLogger.h"
32 Str(
const char* chars) {
33 if (chars !=
nullptr) {
34 int len = strlen(chars);
35 set((
char*)chars, len, len,
true);
37 this->is_const =
true;
43 Str(
char chars[],
int maxlen,
int len = 0) {
set(chars, maxlen, len,
false); }
46 virtual void set(
const char* alt) {
50 int new_len = strlen(alt);
55 this->maxlen = this->len;
56 this->chars = (
char*)alt;
59 strncpy(this->chars, alt, this->maxlen);
71 this->chars = alt.chars;
74 strncpy(this->chars, alt.chars, this->maxlen);
79 virtual void set(
const char c) {
84 virtual void set(
int value) {
89 virtual void set(
double value,
int precision = 2,
int withd = 0) {
94 virtual void swap(Str& str) {
95 char* cpy_chars = chars;
97 bool cpy_is_const = is_const;
99 int cpy_maxlen = maxlen;
102 is_const = str.is_const;
106 str.chars = cpy_chars;
107 str.is_const = cpy_is_const;
109 str.maxlen = cpy_maxlen;
113 virtual void set(
char chars[],
int maxlen,
int len = 0,
116 this->maxlen = maxlen;
125 virtual void add(
int value) {
128 snprintf(this->chars + len, 10,
"%d", value);
134 virtual void add(
double value,
int precision = 2,
int withd = 0) {
143 virtual void add(
const char* append) {
144 if (!
isConst() && append !=
nullptr) {
145 int append_len = strlen(append);
147 int n = (len + append_len) < maxlen - 1 ? append_len : maxlen - len - 1;
148 strncat(chars, append, n);
155 virtual void add(
const char c) {
156 if (!
isConst() && len < maxlen - 1) {
165 if (str ==
nullptr)
return false;
166 return strcmp(this->chars, str) == 0;
171 if (str ==
nullptr)
return false;
172 int len = strlen(str);
173 return strncmp(this->chars, str, len) == 0;
178 if (str ==
nullptr)
return false;
179 int endlen = strlen(str);
180 return strncmp(this->chars + (len - endlen), str, endlen) == 0;
185 if (str ==
nullptr)
return false;
186 int endlen = strlen(str);
187 return strncmp_i(this->chars + (len - endlen), str, endlen) == 0;
208 const char* line = this->chars;
210 const char* last_pattern_start = 0;
211 const char* last_line_start = 0;
213 if (*pattern == *line) {
214 if (wildcard == 1) last_line_start = line + 1;
219 }
else if (*pattern ==
'?') {
222 if (wildcard == 1) last_line_start = line + 1;
226 }
else if (*pattern ==
'*') {
227 if (*(pattern + 1) ==
'\0') {
231 last_pattern_start = pattern;
236 }
else if (wildcard) {
237 if (*line == *pattern) {
241 last_line_start = line + 1;
246 if ((*pattern) ==
'\0' && (*line) ==
'\0')
249 if (last_pattern_start != 0)
251 pattern = last_pattern_start;
252 line = last_line_start;
262 if (*pattern ==
'\0') {
271 virtual int indexOf(
const char c,
int start = 0) {
272 for (
int j = start; j < len; j++) {
285 virtual int indexOf(
const char* cont,
int start = 0) {
286 if (chars ==
nullptr || cont ==
nullptr)
return -1;
287 int contLen = strlen(cont);
288 for (
int j = start; j < len; j++) {
289 char* pt = chars + j;
290 if (strncmp(pt, cont, contLen) == 0) {
299 if (cont ==
nullptr)
return -1;
300 int contLen = strlen(cont);
301 for (
int j = (len - contLen); j >= 0; j--) {
302 if (strncmp(cont, chars + j, contLen) == 0) {
330 memmove(this->chars, this->chars + n, len + 1);
334 virtual char operator[](
int index) {
return chars[index]; }
350 if (this->len != alt.len)
return false;
351 return strncmp(this->chars, alt.chars, this->len) == 0;
356 return strncmp(this->chars, alt, this->len) == 0;
361 return strncmp(this->chars, alt.chars, this->len) != 0;
366 return strncmp(this->chars, alt, this->len) != 0;
370 virtual const char*
c_str() {
return chars; }
383 virtual bool replace(
const char* toReplace,
const char* replaced) {
385 if (toReplace ==
nullptr || replaced ==
nullptr) {
393 int len_replaced = strlen(replaced);
394 int len_to_replace = strlen(toReplace);
395 insert_len = len_replaced - len_to_replace;
398 memmove(this->chars + pos + len_replaced,
399 this->chars + pos + len_to_replace,
400 old_len - pos - len_to_replace + 1);
402 memmove(this->chars + pos, replaced, len_replaced);
411 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
412 if (
indexOf(toReplace) == -1) {
415 while (
replace(toReplace, replaced));
420 virtual void remove(
const char* toRemove) {
421 if (!
isConst() && chars !=
nullptr) {
422 int removeLen = strlen(toRemove);
425 memmove((
void*)(chars + pos), (
void*)(chars + pos + removeLen),
426 len - (pos + removeLen) + 1);
434 if (!
isConst() && chars !=
nullptr) {
435 int removeLen = strlen(toRemove);
441 memmove((
void*)(chars + pos), (
void*)(chars + pos + removeLen),
442 len - (pos + removeLen) + 1);
451 this->savedChar = chars[len];
452 this->savedLen = len;
461 chars[len] = savedChar;
462 this->len = savedLen;
470 int len = end - start;
472 if (this->chars !=
nullptr) {
473 len = len < this->maxlen ? len : this->maxlen;
474 strncpy(this->chars, from.chars + start, len);
476 this->chars[len] = 0;
482 virtual void substring(
const char* from,
int start,
int end) {
484 int len = end - start;
486 if (this->chars !=
nullptr) {
487 strncpy(this->chars, from + start, len);
488 this->chars[len] = 0;
496 if (chars ==
nullptr)
return;
502 virtual int count(
char c,
int startPos) {
503 for (
int j = startPos; j < len; j++) {
513 if (chars ==
nullptr)
return;
514 int n =
count(
' ', 0);
515 if (n > 0) *
this << n;
520 if (chars ==
nullptr)
return;
522 while (isspace(chars[len])) {
531 if (chars !=
nullptr && !
isConst()) {
544 virtual void insert(
int pos,
const char* str) {
546 int insert_len = strlen(str);
548 int move_len = this->len - pos + 1;
549 memmove(chars + pos + insert_len, chars + pos, move_len);
550 strncpy(chars + pos, str, insert_len);
556 if ((
size_t)len != strlen(alt)) {
559 for (
int j = 0; j < len; j++) {
560 if (tolower(chars[j]) != tolower(alt[j]))
return false;
569 result = atoi(chars);
578 result = atol(chars);
588 result = strtod(chars, &eptr);
595 if (chars !=
nullptr) {
596 for (
int j = 0; j < len; j++) {
597 chars[j] = tolower(chars[j]);
604 if (chars !=
nullptr) {
605 for (
int j = 0; j < len; j++) {
606 chars[j] = toupper(chars[j]);
612 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
613 static char result[160];
614 unsigned char* b = (
unsigned char*)ptr;
618 for (i = size - 1; i >= 0; i--) {
619 for (j = 7; j >= 0; j--) {
620 byte = (b[i] >> j) & 1;
621 result[idx++] =
byte ?
'1' :
'0';
628 bool containsNumber() {
629 for (
int j = 0; j < len; j++) {
630 if (isdigit(chars[j])) {
639 bool result = containsNumber();
641 for (
int j = 0; j < len; j++) {
647 if (minus_count > 1) {
665 for (
int j = pos + 1; j < len; j++) {
666 if (isdigit(chars[j])) {
678 bool result = containsNumber();
681 for (
int j = 0; j < len; j++) {
687 if (minus_count > 1) {
707 char* chars =
nullptr;
708 bool is_const =
false;
715 virtual bool grow(
int newMaxLen) {
return false; }
717 static char* itoa(
int n,
char s[]) {
723 s[i++] = n % 10 +
'0';
724 }
while ((n /= 10) > 0);
725 if (sign < 0) s[i++] =
'-';
731 static void reverse(
char s[]) {
735 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
748 double roundingFactor = 0.5;
749 unsigned long mult = 1;
750 for (i = 0; i < precision; i++) {
751 roundingFactor /= 10.0;
759 strcpy(outstr,
"-\0");
763 val += roundingFactor;
765 strcat(outstr, itoa(
int(val), temp));
767 strcat(outstr,
".\0");
769 unsigned long mult = 1;
770 int padding = precision - 1;
771 while (precision--) mult *= 10;
774 frac = (val - int(val)) * mult;
776 frac = (int(val) - val) * mult;
777 unsigned long frac1 = frac;
779 while (frac1 /= 10) padding--;
781 while (padding--) strcat(outstr,
"0\0");
783 strcat(outstr, itoa(frac, temp));
787 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
789 J = widthp - strlen(outstr);
791 for (i = 0; i < J; i++) {
796 strcat(temp, outstr);
797 strcpy(outstr, temp);
803 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
804 if (n == 0)
return (0);
806 if (tolower(*s1) != tolower(*s2++))
807 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
808 if (*s1++ == 0)
break;