34 if (
chars !=
nullptr) {
47 virtual void set(
const char* alt) {
51 int new_len = strlen(alt);
57 this->
chars = (
char*)alt;
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) {
96 char* cpy_chars =
chars;
107 str.
chars = cpy_chars;
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);
149 strncat(
chars, append, n);
156 virtual void add(
const char c) {
166 if (str ==
nullptr)
return isEmpty();
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);
196 if (pattern ==
nullptr)
return false;
201 const char* alt_start = pattern;
204 while (*alt_start ==
',' || *alt_start ==
';' || *alt_start ==
'|' ||
208 if (*alt_start ==
'\0')
return false;
211 const char* alt_end = alt_start;
212 while (*alt_end !=
'\0' && *alt_end !=
',' && *alt_end !=
';' &&
217 const char* trimmed_end = alt_end;
218 while (trimmed_end > alt_start && *(trimmed_end - 1) ==
' ') {
222 size_t alt_len = trimmed_end - alt_start;
225 if (alt_len >=
sizeof(buffer)) alt_len =
sizeof(buffer) - 1;
226 memcpy(buffer, alt_start, alt_len);
227 buffer[alt_len] =
'\0';
231 if (*alt_end ==
'\0')
return false;
232 alt_start = alt_end + 1;
244 const char* last_pattern_start = 0;
245 const char* last_line_start = 0;
247 if (*pattern == *line) {
248 if (wildcard == 1) last_line_start = line + 1;
253 }
else if (*pattern ==
'?') {
256 if (wildcard == 1) last_line_start = line + 1;
260 }
else if (*pattern ==
'*') {
261 if (*(pattern + 1) ==
'\0') {
265 last_pattern_start = pattern;
270 }
else if (wildcard) {
271 if (*line == *pattern) {
275 last_line_start = line + 1;
279 }
else if (last_pattern_start != 0) {
280 pattern = last_pattern_start;
281 line = last_line_start;
289 while (*pattern ==
'*') pattern++;
291 if (*pattern ==
'\0') {
302 virtual int indexOf(
const char c,
int start = 0) {
303 for (
int j = start; j <
len; j++) {
314 for (
int j=0; j < n; j++){
315 result =
indexOf(c, result + 1);
316 if (result < 0)
break;
326 virtual int indexOf(
const char* cont,
int start = 0) {
327 if (
chars ==
nullptr || cont ==
nullptr)
return -1;
328 int contLen = strlen(cont);
329 for (
int j = start; j <
len; j++) {
330 char* pt =
chars + j;
331 if (strncmp(pt, cont, contLen) == 0) {
340 if (cont ==
nullptr)
return -1;
341 int contLen = strlen(cont);
342 for (
int j = (
len - contLen); j >= 0; j--) {
343 if (strncmp(cont,
chars + j, contLen) == 0) {
353 for (
int j=0; j < n; j++){
354 result =
indexOf(cont, result + 1);
355 if (result < 0)
break;
377 if (n <= 0 || n >
len)
return;
402 if (this->
len != alt.
len)
return false;
403 return strncmp(this->
chars, alt.
chars, this->len) == 0;
408 return strncmp(this->
chars, alt, this->
len) == 0;
413 return strncmp(this->
chars, alt.
chars, this->len) != 0;
418 return strncmp(this->
chars, alt, this->
len) != 0;
435 virtual bool replace(
const char* toReplace,
const char* replaced) {
437 if (toReplace ==
nullptr || replaced ==
nullptr) {
445 int len_replaced = strlen(replaced);
446 int len_to_replace = strlen(toReplace);
447 insert_len = len_replaced - len_to_replace;
450 memmove(this->
chars + pos + len_replaced,
451 this->
chars + pos + len_to_replace,
452 old_len - pos - len_to_replace + 1);
454 memmove(this->
chars + pos, replaced, len_replaced);
463 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
464 if (
indexOf(toReplace) == -1) {
467 while (
replace(toReplace, replaced));
472 virtual void remove(
const char* toRemove) {
474 int removeLen = strlen(toRemove);
477 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
478 len - (pos + removeLen) + 1);
487 int removeLen = strlen(toRemove);
493 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
494 len - (pos + removeLen) + 1);
522 int len = end - start;
524 if (this->
chars !=
nullptr) {
534 virtual void substring(
const char* from,
int start,
int end) {
536 int len = end - start;
538 if (this->
chars !=
nullptr) {
539 strncpy(this->
chars, from + start,
len);
548 if (
chars ==
nullptr)
return;
554 virtual int count(
char c,
int startPos) {
555 for (
int j = startPos; j <
len; j++) {
565 if (
chars ==
nullptr)
return;
566 int n =
count(
' ', 0);
567 if (n > 0) *
this << n;
572 if (
chars ==
nullptr ||
len == 0)
return;
596 virtual void insert(
int pos,
const char* str) {
598 int insert_len = strlen(str);
600 int move_len = this->
len - pos + 1;
601 memmove(
chars + pos + insert_len,
chars + pos, move_len);
602 strncpy(
chars + pos, str, insert_len);
608 if (alt ==
nullptr)
return isEmpty();
609 if ((
size_t)
len != strlen(alt)) {
612 for (
int j = 0; j <
len; j++) {
613 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
622 result = atoi(
chars);
631 result = atol(
chars);
641 result = strtod(
chars, &eptr);
652 result = strtof(
chars, &eptr);
654 result = strtod(
chars, &eptr);
662 if (
chars !=
nullptr) {
663 for (
int j = 0; j <
len; j++) {
671 if (
chars !=
nullptr) {
672 for (
int j = 0; j <
len; j++) {
679 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
680 static char result[160];
681 unsigned char* b = (
unsigned char*)ptr;
685 for (i = size - 1; i >= 0; i--) {
686 for (j = 7; j >= 0; j--) {
687 byte = (b[i] >> j) & 1;
688 result[idx++] =
byte ?
'1' :
'0';
696 for (
int j = 0; j <
len; j++) {
697 if (isdigit(
chars[j])) {
708 for (
int j = 0; j <
len; j++) {
714 if (minus_count > 1) {
732 for (
int j = pos + 1; j <
len; j++) {
733 if (isdigit(
chars[j])) {
748 for (
int j = 0; j <
len; j++) {
754 if (minus_count > 1) {
783 virtual bool grow(
int newMaxLen) {
return false; }
785 static char*
itoa(
int n,
char s[]) {
791 s[i++] = n % 10 +
'0';
792 }
while ((n /= 10) > 0);
793 if (sign < 0) s[i++] =
'-';
803 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
816 double roundingFactor = 0.5;
817 unsigned long mult = 1;
818 for (i = 0; i < precision; i++) {
819 roundingFactor /= 10.0;
827 strcpy(outstr,
"-\0");
831 val += roundingFactor;
833 strcat(outstr,
itoa(
int(val), temp));
835 strcat(outstr,
".\0");
837 unsigned long mult = 1;
838 int padding = precision - 1;
839 while (precision--) mult *= 10;
842 frac = (val - int(val)) * mult;
844 frac = (int(val) - val) * mult;
845 unsigned long frac1 = frac;
847 while (frac1 /= 10) padding--;
849 while (padding--) strcat(outstr,
"0\0");
851 strcat(outstr,
itoa(frac, temp));
855 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
857 J = widthp - strlen(outstr);
859 for (i = 0; i < J; i++) {
864 strcat(temp, outstr);
865 strcpy(outstr, temp);
871 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
872 if (n == 0)
return (0);
874 if (tolower(*s1) != tolower(*s2++))
875 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
876 if (*s1++ == 0)
break;