6 #include "AudioTools/CoreAudio/AudioLogger.h"
34 if (chars !=
nullptr) {
35 int len = strlen(chars);
36 set((
char*)chars, len, len,
true);
38 this->is_const =
true;
44 StrView(
char chars[],
int maxlen,
int len = 0) {
set(chars, maxlen, len,
false); }
47 virtual void set(
const char* alt) {
51 int new_len = strlen(alt);
56 this->maxlen = this->len;
57 this->chars = (
char*)alt;
60 strncpy(this->chars, alt, this->maxlen);
72 this->chars = alt.chars;
75 strncpy(this->chars, alt.chars, this->maxlen);
80 virtual void set(
const char c) {
85 virtual void set(
int value) {
90 virtual void set(
double value,
int precision = 2,
int withd = 0) {
95 virtual void swap(StrView& str) {
96 char* cpy_chars = chars;
98 bool cpy_is_const = is_const;
100 int cpy_maxlen = maxlen;
103 is_const = str.is_const;
107 str.chars = cpy_chars;
108 str.is_const = cpy_is_const;
110 str.maxlen = cpy_maxlen;
114 virtual void set(
char chars[],
int maxlen,
int len = 0,
117 this->maxlen = maxlen;
126 virtual void add(
int value) {
129 snprintf(this->chars + len, 10,
"%d", value);
135 virtual void add(
double value,
int precision = 2,
int withd = 0) {
144 virtual void add(
const char* append) {
145 if (!
isConst() && append !=
nullptr) {
146 int append_len = strlen(append);
148 int n = (len + append_len) < maxlen - 1 ? append_len : maxlen - len - 1;
149 strncat(chars, append, n);
156 virtual void add(
const char c) {
157 if (!
isConst() && len < maxlen - 1) {
166 if (str ==
nullptr)
return false;
167 return strcmp(this->chars, str) == 0;
172 if (str ==
nullptr)
return false;
173 int len = strlen(str);
174 return strncmp(this->chars, str, len) == 0;
179 if (str ==
nullptr)
return false;
180 int endlen = strlen(str);
181 return strncmp(this->chars + (len - endlen), str, endlen) == 0;
186 if (str ==
nullptr)
return false;
187 int endlen = strlen(str);
188 return strncmp_i(this->chars + (len - endlen), str, endlen) == 0;
197 const char* line = this->chars;
199 const char* last_pattern_start = 0;
200 const char* last_line_start = 0;
202 if (*pattern == *line) {
203 if (wildcard == 1) last_line_start = line + 1;
208 }
else if (*pattern ==
'?') {
211 if (wildcard == 1) last_line_start = line + 1;
215 }
else if (*pattern ==
'*') {
216 if (*(pattern + 1) ==
'\0') {
220 last_pattern_start = pattern;
225 }
else if (wildcard) {
226 if (*line == *pattern) {
230 last_line_start = line + 1;
235 if ((*pattern) ==
'\0' && (*line) ==
'\0')
238 if (last_pattern_start != 0)
240 pattern = last_pattern_start;
241 line = last_line_start;
251 if (*pattern ==
'\0') {
260 virtual int indexOf(
const char c,
int start = 0) {
261 for (
int j = start; j < len; j++) {
272 for (
int j=0; j < n; j++){
273 result =
indexOf(c, result + 1);
274 if (result < 0)
break;
284 virtual int indexOf(
const char* cont,
int start = 0) {
285 if (chars ==
nullptr || cont ==
nullptr)
return -1;
286 int contLen = strlen(cont);
287 for (
int j = start; j < len; j++) {
288 char* pt = chars + j;
289 if (strncmp(pt, cont, contLen) == 0) {
298 if (cont ==
nullptr)
return -1;
299 int contLen = strlen(cont);
300 for (
int j = (len - contLen); j >= 0; j--) {
301 if (strncmp(cont, chars + j, contLen) == 0) {
311 for (
int j=0; j < n; j++){
312 result =
indexOf(cont, result + 1);
313 if (result < 0)
break;
339 memmove(this->chars, this->chars + n, len + 1);
343 virtual char operator[](
int index) {
return chars[index]; }
359 if (this->len != alt.len)
return false;
360 return strncmp(this->chars, alt.chars, this->len) == 0;
365 return strncmp(this->chars, alt, this->len) == 0;
370 return strncmp(this->chars, alt.chars, this->len) != 0;
375 return strncmp(this->chars, alt, this->len) != 0;
379 virtual const char*
c_str() {
return chars; }
392 virtual bool replace(
const char* toReplace,
const char* replaced) {
394 if (toReplace ==
nullptr || replaced ==
nullptr) {
402 int len_replaced = strlen(replaced);
403 int len_to_replace = strlen(toReplace);
404 insert_len = len_replaced - len_to_replace;
407 memmove(this->chars + pos + len_replaced,
408 this->chars + pos + len_to_replace,
409 old_len - pos - len_to_replace + 1);
411 memmove(this->chars + pos, replaced, len_replaced);
420 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
421 if (
indexOf(toReplace) == -1) {
424 while (
replace(toReplace, replaced));
429 virtual void remove(
const char* toRemove) {
430 if (!
isConst() && chars !=
nullptr) {
431 int removeLen = strlen(toRemove);
434 memmove((
void*)(chars + pos), (
void*)(chars + pos + removeLen),
435 len - (pos + removeLen) + 1);
443 if (!
isConst() && chars !=
nullptr) {
444 int removeLen = strlen(toRemove);
450 memmove((
void*)(chars + pos), (
void*)(chars + pos + removeLen),
451 len - (pos + removeLen) + 1);
460 this->savedChar = chars[len];
461 this->savedLen = len;
470 chars[len] = savedChar;
471 this->len = savedLen;
479 int len = end - start;
481 if (this->chars !=
nullptr) {
482 len = len < this->maxlen ? len : this->maxlen;
483 strncpy(this->chars, from.chars + start, len);
485 this->chars[len] = 0;
491 virtual void substring(
const char* from,
int start,
int end) {
493 int len = end - start;
495 if (this->chars !=
nullptr) {
496 strncpy(this->chars, from + start, len);
497 this->chars[len] = 0;
505 if (chars ==
nullptr)
return;
511 virtual int count(
char c,
int startPos) {
512 for (
int j = startPos; j < len; j++) {
522 if (chars ==
nullptr)
return;
523 int n =
count(
' ', 0);
524 if (n > 0) *
this << n;
529 if (chars ==
nullptr)
return;
531 while (isspace(chars[len])) {
540 if (chars !=
nullptr && !
isConst()) {
553 virtual void insert(
int pos,
const char* str) {
555 int insert_len = strlen(str);
557 int move_len = this->len - pos + 1;
558 memmove(chars + pos + insert_len, chars + pos, move_len);
559 strncpy(chars + pos, str, insert_len);
565 if ((
size_t)len != strlen(alt)) {
568 for (
int j = 0; j < len; j++) {
569 if (tolower(chars[j]) != tolower(alt[j]))
return false;
578 result = atoi(chars);
587 result = atol(chars);
597 result = strtod(chars, &eptr);
604 if (chars !=
nullptr) {
605 for (
int j = 0; j < len; j++) {
606 chars[j] = tolower(chars[j]);
613 if (chars !=
nullptr) {
614 for (
int j = 0; j < len; j++) {
615 chars[j] = toupper(chars[j]);
621 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
622 static char result[160];
623 unsigned char* b = (
unsigned char*)ptr;
627 for (i = size - 1; i >= 0; i--) {
628 for (j = 7; j >= 0; j--) {
629 byte = (b[i] >> j) & 1;
630 result[idx++] =
byte ?
'1' :
'0';
637 bool containsNumber() {
638 for (
int j = 0; j < len; j++) {
639 if (isdigit(chars[j])) {
648 bool result = containsNumber();
650 for (
int j = 0; j < len; j++) {
656 if (minus_count > 1) {
674 for (
int j = pos + 1; j < len; j++) {
675 if (isdigit(chars[j])) {
687 bool result = containsNumber();
690 for (
int j = 0; j < len; j++) {
696 if (minus_count > 1) {
716 char* chars =
nullptr;
717 bool is_const =
false;
724 virtual bool grow(
int newMaxLen) {
return false; }
726 static char* itoa(
int n,
char s[]) {
732 s[i++] = n % 10 +
'0';
733 }
while ((n /= 10) > 0);
734 if (sign < 0) s[i++] =
'-';
740 static void reverse(
char s[]) {
744 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
757 double roundingFactor = 0.5;
758 unsigned long mult = 1;
759 for (i = 0; i < precision; i++) {
760 roundingFactor /= 10.0;
768 strcpy(outstr,
"-\0");
772 val += roundingFactor;
774 strcat(outstr, itoa(
int(val), temp));
776 strcat(outstr,
".\0");
778 unsigned long mult = 1;
779 int padding = precision - 1;
780 while (precision--) mult *= 10;
783 frac = (val - int(val)) * mult;
785 frac = (int(val) - val) * mult;
786 unsigned long frac1 = frac;
788 while (frac1 /= 10) padding--;
790 while (padding--) strcat(outstr,
"0\0");
792 strcat(outstr, itoa(frac, temp));
796 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
798 J = widthp - strlen(outstr);
800 for (i = 0; i < J; i++) {
805 strcat(temp, outstr);
806 strcpy(outstr, temp);
812 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
813 if (n == 0)
return (0);
815 if (tolower(*s1) != tolower(*s2++))
816 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
817 if (*s1++ == 0)
break;