35 if (
chars !=
nullptr) {
48 virtual void set(
const char* alt) {
52 int new_len = strlen(alt);
58 this->
chars = (
char*)alt;
62 int copy_len = new_len < this->
maxlen ? new_len : this->
maxlen - 1;
63 strncpy(this->
chars, alt, copy_len);
64 this->
chars[copy_len] = 0;
82 this->
chars[copy_len] = 0;
87 virtual void set(
const char c) {
92 virtual void set(
int value) {
97 virtual void set(
double value,
int precision = 2,
int withd = 0) {
103 char* cpy_chars =
chars;
114 str.
chars = cpy_chars;
133 virtual void add(
int value) {
136 snprintf(buffer,
sizeof(buffer),
"%d", value);
142 virtual void add(
double value,
int precision = 2,
int withd = 0) {
151 virtual void add(
const char* append) {
152 if (!
isConst() && append !=
nullptr) {
153 int append_len = strlen(append);
156 strncat(
chars, append, n);
166 virtual const char*
printf(
const char* fmt, ...) {
171 va_copy(args_copy, args);
172 int needed = vsnprintf(
nullptr, 0, fmt, args_copy);
179 if (
chars !=
nullptr) {
188 virtual void add(
const char c) {
198 if (str ==
nullptr)
return isEmpty();
199 return strcmp(this->
chars, str) == 0;
204 if (str ==
nullptr)
return false;
205 int len = strlen(str);
206 return strncmp(this->
chars, str,
len) == 0;
211 if (str ==
nullptr)
return false;
212 int endlen = strlen(str);
213 if (endlen >
len)
return false;
214 return strncmp(this->
chars + (
len - endlen), str, endlen) == 0;
219 if (str ==
nullptr)
return false;
220 int endlen = strlen(str);
221 if (endlen >
len)
return false;
230 if (pattern ==
nullptr)
return false;
235 const char* alt_start = pattern;
238 while (*alt_start ==
',' || *alt_start ==
';' || *alt_start ==
'|' ||
242 if (*alt_start ==
'\0')
return false;
245 const char* alt_end = alt_start;
246 while (*alt_end !=
'\0' && *alt_end !=
',' && *alt_end !=
';' &&
251 const char* trimmed_end = alt_end;
252 while (trimmed_end > alt_start && *(trimmed_end - 1) ==
' ') {
256 size_t alt_len = trimmed_end - alt_start;
259 if (alt_len >=
sizeof(buffer)) alt_len =
sizeof(buffer) - 1;
260 memcpy(buffer, alt_start, alt_len);
261 buffer[alt_len] =
'\0';
265 if (*alt_end ==
'\0')
return false;
266 alt_start = alt_end + 1;
278 const char* last_pattern_start = 0;
279 const char* last_line_start = 0;
281 if (*pattern == *line) {
282 if (wildcard == 1) last_line_start = line + 1;
287 }
else if (*pattern ==
'?') {
290 if (wildcard == 1) last_line_start = line + 1;
294 }
else if (*pattern ==
'*') {
295 if (*(pattern + 1) ==
'\0') {
299 last_pattern_start = pattern;
304 }
else if (wildcard) {
305 if (*line == *pattern) {
309 last_line_start = line + 1;
313 }
else if (last_pattern_start != 0) {
314 pattern = last_pattern_start;
315 line = last_line_start;
323 while (*pattern ==
'*') pattern++;
325 if (*pattern ==
'\0') {
336 virtual int indexOf(
const char c,
int start = 0) {
337 for (
int j = start; j <
len; j++) {
348 for (
int j=0; j < n; j++){
349 result =
indexOf(c, result + 1);
350 if (result < 0)
break;
360 virtual int indexOf(
const char* cont,
int start = 0) {
361 if (
chars ==
nullptr || cont ==
nullptr)
return -1;
362 int contLen = strlen(cont);
363 for (
int j = start; j <
len; j++) {
364 char* pt =
chars + j;
365 if (strncmp(pt, cont, contLen) == 0) {
374 if (cont ==
nullptr)
return -1;
375 int contLen = strlen(cont);
376 for (
int j = (
len - contLen); j >= 0; j--) {
377 if (strncmp(cont,
chars + j, contLen) == 0) {
387 for (
int j=0; j < n; j++){
388 result =
indexOf(cont, result + 1);
389 if (result < 0)
break;
411 if (n <= 0 || n >
len)
return;
436 if (this->
len != alt.
len)
return false;
437 return strncmp(this->
chars, alt.
chars, this->len) == 0;
442 return strncmp(this->
chars, alt, this->
len) == 0;
447 return strncmp(this->
chars, alt.
chars, this->len) != 0;
452 return strncmp(this->
chars, alt, this->
len) != 0;
469 virtual bool replace(
const char* toReplace,
const char* replaced) {
471 if (toReplace ==
nullptr || replaced ==
nullptr) {
479 int len_replaced = strlen(replaced);
480 int len_to_replace = strlen(toReplace);
481 insert_len = len_replaced - len_to_replace;
484 memmove(this->
chars + pos + len_replaced,
485 this->
chars + pos + len_to_replace,
486 old_len - pos - len_to_replace + 1);
488 memmove(this->
chars + pos, replaced, len_replaced);
497 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
498 if (
indexOf(toReplace) == -1) {
501 while (
replace(toReplace, replaced));
506 virtual void remove(
const char* toRemove) {
508 int removeLen = strlen(toRemove);
511 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
512 len - (pos + removeLen) + 1);
521 int removeLen = strlen(toRemove);
527 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
528 len - (pos + removeLen) + 1);
556 int len = end - start;
558 if (this->
chars !=
nullptr) {
568 virtual void substring(
const char* from,
int start,
int end) {
570 int len = end - start;
572 if (this->
chars !=
nullptr) {
574 strncpy(this->
chars, from + start,
len);
583 if (
chars ==
nullptr)
return;
589 virtual int count(
char c,
int startPos) {
590 for (
int j = startPos; j <
len; j++) {
600 if (
chars ==
nullptr)
return;
601 int n =
count(
' ', 0);
602 if (n > 0) *
this << n;
607 if (
chars ==
nullptr ||
len == 0)
return;
631 virtual void insert(
int pos,
const char* str) {
633 int insert_len = strlen(str);
635 int move_len = this->
len - pos + 1;
636 memmove(
chars + pos + insert_len,
chars + pos, move_len);
637 strncpy(
chars + pos, str, insert_len);
643 if (alt ==
nullptr)
return isEmpty();
644 if ((
size_t)
len != strlen(alt)) {
647 for (
int j = 0; j <
len; j++) {
648 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
657 result = atoi(
chars);
666 result = atol(
chars);
676 result = strtod(
chars, &eptr);
687 result = strtof(
chars, &eptr);
689 result = strtod(
chars, &eptr);
697 if (
chars !=
nullptr) {
698 for (
int j = 0; j <
len; j++) {
706 if (
chars !=
nullptr) {
707 for (
int j = 0; j <
len; j++) {
714 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
715 static char result[160];
716 unsigned char* b = (
unsigned char*)ptr;
720 for (i = size - 1; i >= 0; i--) {
721 for (j = 7; j >= 0; j--) {
722 byte = (b[i] >> j) & 1;
723 result[idx++] =
byte ?
'1' :
'0';
731 for (
int j = 0; j <
len; j++) {
732 if (isdigit(
chars[j])) {
743 for (
int j = 0; j <
len; j++) {
749 if (minus_count > 1) {
767 for (
int j = pos + 1; j <
len; j++) {
768 if (isdigit(
chars[j])) {
783 for (
int j = 0; j <
len; j++) {
789 if (minus_count > 1) {
818 virtual bool grow(
int newMaxLen) {
return false; }
820 static char*
itoa(
int n,
char s[]) {
826 s[i++] = n % 10 +
'0';
827 }
while ((n /= 10) > 0);
828 if (sign < 0) s[i++] =
'-';
838 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
851 double roundingFactor = 0.5;
852 unsigned long mult = 1;
853 for (i = 0; i < precision; i++) {
854 roundingFactor /= 10.0;
862 strcpy(outstr,
"-\0");
866 val += roundingFactor;
868 strcat(outstr,
itoa(
int(val), temp));
870 strcat(outstr,
".\0");
872 unsigned long mult = 1;
873 int padding = precision - 1;
874 while (precision--) mult *= 10;
877 frac = (val - int(val)) * mult;
879 frac = (int(val) - val) * mult;
880 unsigned long frac1 = frac;
882 while (frac1 /= 10) padding--;
884 while (padding--) strcat(outstr,
"0\0");
886 strcat(outstr,
itoa(frac, temp));
890 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
892 J = widthp - strlen(outstr);
894 for (i = 0; i < J; i++) {
899 strcat(temp, outstr);
900 strcpy(outstr, temp);
906 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
907 if (n == 0)
return (0);
909 if (tolower(*s1) != tolower(*s2++))
910 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
911 if (*s1++ == 0)
break;