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);
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;
335 if (n <= 0 || n >
len)
return;
360 if (this->
len != alt.
len)
return false;
361 return strncmp(this->
chars, alt.
chars, this->len) == 0;
366 return strncmp(this->
chars, alt, this->
len) == 0;
371 return strncmp(this->
chars, alt.
chars, this->len) != 0;
376 return strncmp(this->
chars, alt, this->
len) != 0;
393 virtual bool replace(
const char* toReplace,
const char* replaced) {
395 if (toReplace ==
nullptr || replaced ==
nullptr) {
403 int len_replaced = strlen(replaced);
404 int len_to_replace = strlen(toReplace);
405 insert_len = len_replaced - len_to_replace;
408 memmove(this->
chars + pos + len_replaced,
409 this->
chars + pos + len_to_replace,
410 old_len - pos - len_to_replace + 1);
412 memmove(this->
chars + pos, replaced, len_replaced);
421 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
422 if (
indexOf(toReplace) == -1) {
425 while (
replace(toReplace, replaced));
430 virtual void remove(
const char* toRemove) {
432 int removeLen = strlen(toRemove);
435 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
436 len - (pos + removeLen) + 1);
445 int removeLen = strlen(toRemove);
451 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
452 len - (pos + removeLen) + 1);
480 int len = end - start;
482 if (this->
chars !=
nullptr) {
492 virtual void substring(
const char* from,
int start,
int end) {
494 int len = end - start;
496 if (this->
chars !=
nullptr) {
497 strncpy(this->
chars, from + start,
len);
506 if (
chars ==
nullptr)
return;
512 virtual int count(
char c,
int startPos) {
513 for (
int j = startPos; j <
len; j++) {
523 if (
chars ==
nullptr)
return;
524 int n =
count(
' ', 0);
525 if (n > 0) *
this << n;
530 if (
chars ==
nullptr ||
len == 0)
return;
554 virtual void insert(
int pos,
const char* str) {
556 int insert_len = strlen(str);
558 int move_len = this->
len - pos + 1;
559 memmove(
chars + pos + insert_len,
chars + pos, move_len);
560 strncpy(
chars + pos, str, insert_len);
566 if (alt ==
nullptr)
return isEmpty();
567 if ((
size_t)
len != strlen(alt)) {
570 for (
int j = 0; j <
len; j++) {
571 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
580 result = atoi(
chars);
589 result = atol(
chars);
599 result = strtod(
chars, &eptr);
610 result = strtof(
chars, &eptr);
612 result = strtod(
chars, &eptr);
620 if (
chars !=
nullptr) {
621 for (
int j = 0; j <
len; j++) {
629 if (
chars !=
nullptr) {
630 for (
int j = 0; j <
len; j++) {
637 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
638 static char result[160];
639 unsigned char* b = (
unsigned char*)ptr;
643 for (i = size - 1; i >= 0; i--) {
644 for (j = 7; j >= 0; j--) {
645 byte = (b[i] >> j) & 1;
646 result[idx++] =
byte ?
'1' :
'0';
654 for (
int j = 0; j <
len; j++) {
655 if (isdigit(
chars[j])) {
666 for (
int j = 0; j <
len; j++) {
672 if (minus_count > 1) {
690 for (
int j = pos + 1; j <
len; j++) {
691 if (isdigit(
chars[j])) {
706 for (
int j = 0; j <
len; j++) {
712 if (minus_count > 1) {
741 virtual bool grow(
int newMaxLen) {
return false; }
743 static char*
itoa(
int n,
char s[]) {
749 s[i++] = n % 10 +
'0';
750 }
while ((n /= 10) > 0);
751 if (sign < 0) s[i++] =
'-';
761 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
774 double roundingFactor = 0.5;
775 unsigned long mult = 1;
776 for (i = 0; i < precision; i++) {
777 roundingFactor /= 10.0;
785 strcpy(outstr,
"-\0");
789 val += roundingFactor;
791 strcat(outstr,
itoa(
int(val), temp));
793 strcat(outstr,
".\0");
795 unsigned long mult = 1;
796 int padding = precision - 1;
797 while (precision--) mult *= 10;
800 frac = (val - int(val)) * mult;
802 frac = (int(val) - val) * mult;
803 unsigned long frac1 = frac;
805 while (frac1 /= 10) padding--;
807 while (padding--) strcat(outstr,
"0\0");
809 strcat(outstr,
itoa(frac, temp));
813 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
815 J = widthp - strlen(outstr);
817 for (i = 0; i < J; i++) {
822 strcat(temp, outstr);
823 strcpy(outstr, temp);
829 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
830 if (n == 0)
return (0);
832 if (tolower(*s1) != tolower(*s2++))
833 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
834 if (*s1++ == 0)
break;