24 if (
chars !=
nullptr) {
44 virtual void set(
const char* alt) {
48 int new_len = strlen(alt);
54 this->
chars = (
char*)alt;
74 if (this->
chars ==
nullptr &&
len == 0) {
83 virtual void set(
const char c) {
88 virtual void set(
int value) {
93 virtual void set(
double value,
int precision = 2,
int withd = 0) {
99 char* cpy_chars =
chars;
109 str.
chars = cpy_chars;
128 virtual void add(
int value) {
131 snprintf((
char*)
c_str() +
len, 11,
"%d", value);
137 virtual void add(
double value,
int precision = 2,
int withd = 0) {
146 virtual void add(
const char* append) {
147 if (!
isConst() && append !=
nullptr) {
148 int append_len = strlen(append);
151 strncat(
chars, append, n);
156 virtual void add(
const uint8_t* append,
int len) {
157 if (!
isConst() && append !=
nullptr) {
158 int append_len =
len;
160 grow(old_len + append_len);
161 memcpy(
chars + old_len, append,
len);
166 virtual void add(
const char c) {
178 if (str ==
nullptr)
return false;
179 return strcmp(this->
chars, str) == 0;
184 if (str ==
nullptr)
return false;
185 if (
chars ==
nullptr)
return false;
186 int len = strlen(str);
187 return strncmp(this->
chars, str,
len) == 0;
192 if (str ==
nullptr)
return false;
193 if (this->
chars ==
nullptr)
return false;
194 int endlen = strlen(str);
195 return strncmp(this->
chars + (
len - endlen), str, endlen) == 0;
200 if (str ==
nullptr)
return false;
201 int endlen = strlen(str);
211 const char* line = this->
chars;
213 const char* last_pattern_start = 0;
214 const char* last_line_start = 0;
216 if (*pattern == *line) {
217 if (wildcard == 1) last_line_start = line + 1;
222 }
else if (*pattern ==
'?') {
225 if (wildcard == 1) last_line_start = line + 1;
229 }
else if (*pattern ==
'*') {
230 if (*(pattern + 1) ==
'\0') {
234 last_pattern_start = pattern;
239 }
else if (wildcard) {
240 if (*line == *pattern) {
244 last_line_start = line + 1;
249 if ((*pattern) ==
'\0' && (*line) ==
'\0')
252 if (last_pattern_start != 0)
254 pattern = last_pattern_start;
255 line = last_line_start;
265 if (*pattern ==
'\0') {
274 virtual int indexOf(
const char c,
int start = 0) {
275 for (
int j = start; j <
len; j++) {
288 virtual int indexOf(
const char* cont,
int start = 0) {
289 if (
chars ==
nullptr || cont ==
nullptr)
return -1;
290 int contLen = strlen(cont);
291 for (
int j = start; j <
len; j++) {
292 char* pt =
chars + j;
293 if (strncmp(pt, cont, contLen) == 0) {
302 if (cont ==
nullptr)
return -1;
303 int contLen = strlen(cont);
304 for (
int j = (
len - contLen); j >= 0; j--) {
305 if (strncmp(cont,
chars + j, contLen) == 0) {
333 if (this->
chars ==
nullptr)
return;
354 if (this->
len != alt.
len)
return false;
355 return strncmp(this->
chars, alt.
chars, this->len) == 0;
360 if (alt ==
nullptr)
return len == 0;
361 if (strlen(alt) != this->
len)
return false;
362 return strncmp(this->
chars, alt, this->
len) == 0;
367 return strncmp(this->
chars, alt.
chars, this->len) != 0;
372 return strncmp(this->
chars, alt, this->
len) != 0;
386 if (
len >= 1 &&
chars[0] ==
'\n')
return true;
387 if (
len >= 2 &&
chars[0] ==
'\r' &&
chars[1] ==
'\n')
return true;
395 virtual bool replace(
const char* toReplace,
const int replaced) {
397 snprintf(number, 50,
"%d", replaced);
398 return replace(toReplace, number);
401 virtual bool replace(
const char* toReplace,
const float replaced) {
403 snprintf(number, 50,
"%f", replaced);
404 return replace(toReplace, number);
408 virtual bool replace(
const char* toReplace,
const char* replaced,
411 if (toReplace ==
nullptr || replaced ==
nullptr) {
415 int pos =
indexOf(toReplace, startPos);
419 int len_replaced = strlen(replaced);
420 int len_to_replace = strlen(toReplace);
421 insert_len = len_replaced - len_to_replace;
424 memmove(this->
chars + pos + len_replaced,
425 this->
chars + pos + len_to_replace,
426 old_len - pos - len_to_replace + 1);
428 memmove(this->
chars + pos, replaced, len_replaced);
437 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
450 replace(toReplace, replaced, pos);
457 virtual void remove(
const char* toRemove) {
459 int removeLen = strlen(toRemove);
462 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
463 len - (pos + removeLen) + 1);
472 int removeLen = strlen(toRemove);
478 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
479 len - (pos + removeLen) + 1);
507 int len = end - start;
509 if (this->
chars !=
nullptr) {
519 virtual void substrView(
const char* from,
int start,
int end) {
521 int len = end - start;
523 if (this->
chars !=
nullptr) {
524 strncpy(this->
chars, from + start,
len);
538 virtual int count(
char c,
int startPos) {
539 for (
int j = startPos; j <
len; j++) {
549 int n =
count(
' ', 0);
585 virtual void insert(
int pos,
const char* str) {
587 int insert_len = strlen(str);
589 int move_len = this->
len - pos + 1;
590 memmove(
chars + pos + insert_len,
chars + pos, move_len);
591 strncpy(
chars + pos, str, insert_len);
597 if ((
size_t)
len != strlen(alt)) {
600 for (
int j = 0; j <
len; j++) {
601 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
610 result = atoi(
chars);
619 result = atol(
chars);
629 result = strtod(
chars, &eptr);
639 result = strtod(
chars, &eptr);
646 if (
chars !=
nullptr) {
647 for (
int j = 0; j <
len; j++) {
655 if (
chars !=
nullptr) {
656 for (
int j = 0; j <
len; j++) {
663 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
664 static char result[160];
665 unsigned char* b = (
unsigned char*)ptr;
669 for (i = size - 1; i >= 0; i--) {
670 for (j = 7; j >= 0; j--) {
671 byte = (b[i] >> j) & 1;
672 result[idx++] =
byte ?
'1' :
'0';
680 for (
int j = 0; j <
len; j++) {
681 if (isdigit(
chars[j])) {
692 for (
int j = 0; j <
len; j++) {
698 if (minus_count > 1) {
716 for (
int j = pos + 1; j <
len; j++) {
717 if (isdigit(
chars[j])) {
732 for (
int j = 0; j <
len; j++) {
738 if (minus_count > 1) {
757 const char*
buildPath(
const char* start,
const char* p1 =
nullptr,
758 const char* p2 =
nullptr) {
760 if (p1 !=
nullptr)
add(p1);
761 if (p2 !=
nullptr)
add(p2);
774 virtual bool grow(
int newMaxLen) {
return false; }
776 static char*
itoa(
int n,
char s[]) {
782 s[i++] = n % 10 +
'0';
783 }
while ((n /= 10) > 0);
784 if (sign < 0) s[i++] =
'-';
794 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
807 double roundingFactor = 0.5;
808 unsigned long mult = 1;
809 for (i = 0; i < precision; i++) {
810 roundingFactor /= 10.0;
818 strcpy(outstr,
"-\0");
822 val += roundingFactor;
824 strcat(outstr,
itoa(
int(val), temp));
826 strcat(outstr,
".\0");
828 unsigned long mult = 1;
829 int padding = precision - 1;
830 while (precision--) mult *= 10;
833 frac = (val - int(val)) * mult;
835 frac = (int(val) - val) * mult;
836 unsigned long frac1 = frac;
838 while (frac1 /= 10) padding--;
840 while (padding--) strcat(outstr,
"0\0");
842 strcat(outstr,
itoa(frac, temp));
846 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
848 J = widthp - strlen(outstr);
850 for (i = 0; i < J; i++) {
855 strcat(temp, outstr);
856 strcpy(outstr, temp);
862 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
863 if (n == 0)
return (0);
865 if (tolower(*s1) != tolower(*s2++))
866 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
867 if (*s1++ == 0)
break;
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
virtual void add(int value)
adds a int value
Definition: StrView.h:128
virtual void substrView(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:505
virtual void operator=(double val)
we can assign a double
Definition: StrView.h:322
virtual bool equalsIgnoreCase(const char *alt)
Compares the string ignoring the case.
Definition: StrView.h:596
virtual void add(const uint8_t *append, int len)
Definition: StrView.h:156
char * chars
Definition: StrView.h:766
virtual void set(const StrView &alt)
assigs from another StrView value
Definition: StrView.h:66
bool containsNumber()
Definition: StrView.h:679
virtual bool replaceAll(const char *toReplace, const char *replaced)
Replaces all instances of toReplace with replaced.
Definition: StrView.h:437
virtual int indexOf(const char *cont, int start=0)
Definition: StrView.h:288
virtual void operator<<(int n)
shift characters to the right -> we just move the pointer
Definition: StrView.h:328
long toLong()
Converts the string to an long.
Definition: StrView.h:616
virtual int maxLength()
provides the maximum length of the string
Definition: StrView.h:392
float toFloat()
Converts the string to a double.
Definition: StrView.h:635
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:383
double toDouble()
Converts the string to a double.
Definition: StrView.h:625
virtual bool replace(const char *toReplace, const float replaced)
Definition: StrView.h:401
virtual void clearAll()
Definition: StrView.h:571
virtual void operator+=(double value)
adds a double at the end of the string
Definition: StrView.h:347
virtual void removeAll(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:470
static char * itoa(int n, char s[])
Definition: StrView.h:776
virtual void operator+=(const char value)
adds a character
Definition: StrView.h:350
virtual void substrView(const char *from, int start, int end)
copies a substring into the current string
Definition: StrView.h:519
virtual void operator=(int value)
we can assign an int
Definition: StrView.h:325
StrView & operator=(const StrView &)=default
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:376
const char * buildPath(const char *start, const char *p1=nullptr, const char *p2=nullptr)
Definition: StrView.h:757
virtual void operator=(const char *str)
we can assign a const char*
Definition: StrView.h:313
bool isNumber()
Definition: StrView.h:728
virtual void add(const char c)
adds a character
Definition: StrView.h:166
virtual void setLengthUndo()
undo the last setLength call
Definition: StrView.h:496
virtual void insert(int pos, const char *str)
inserts a substring into the string
Definition: StrView.h:585
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:274
virtual bool endsWithIgnoreCase(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:199
int savedLen
Definition: StrView.h:770
int numberOfDecimals()
Determines the number of decimals in the number string.
Definition: StrView.h:712
int toInt()
Converts the string to an int.
Definition: StrView.h:607
virtual bool operator==(const StrView &alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:353
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition: StrView.h:183
virtual bool matches(const char *pattern)
Definition: StrView.h:207
virtual bool operator!=(const StrView &alt) const
checks if the indicated string is different from the current string
Definition: StrView.h:366
virtual bool operator==(const char *alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:359
virtual int length()
Definition: StrView.h:380
char savedChar
Definition: StrView.h:771
StrView(const StrView &)=default
virtual void setLength(int len, bool addZero=true)
limits the length of the string (by adding a delimiting 0)
Definition: StrView.h:486
int len
Definition: StrView.h:768
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:191
StrView(char chars[], int maxlen, int len=0)
Creates a StrView with the indicated buffer.
Definition: StrView.h:34
virtual void swap(StrView &str)
Definition: StrView.h:98
virtual void operator=(char *str)
we can assign a char*
Definition: StrView.h:316
virtual bool operator!=(const char *alt) const
checks if the indicated string is different from the current string
Definition: StrView.h:371
StrView(const char *chars)
Creates a StrView for string constant.
Definition: StrView.h:23
virtual bool replace(const char *toReplace, const int replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:395
virtual int count(char c, int startPos)
count number of indicated characters as position
Definition: StrView.h:538
StrView(StrView &&)=default
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:532
virtual bool grow(int newMaxLen)
only supported in subclasses
Definition: StrView.h:774
virtual void add(const char *append)
adds a string
Definition: StrView.h:146
int maxlen
Definition: StrView.h:769
static const char * toBinary(void const *const ptr, size_t const size)
provides a binary string represntation
Definition: StrView.h:663
virtual void operator+=(int value)
adds a int at the end of the string
Definition: StrView.h:344
virtual void set(const char *alt)
assigs a value
Definition: StrView.h:44
bool isInteger()
Returns true if the string is an integer.
Definition: StrView.h:689
void toLowerCase()
Converts the string to lowercase letters.
Definition: StrView.h:645
StrView & operator=(StrView &&)=default
static int strncmp_i(const char *s1, const char *s2, int n)
Definition: StrView.h:862
virtual void set(const char c)
Definition: StrView.h:83
static void reverse(char s[])
Definition: StrView.h:790
virtual void operator=(char c)
we can assign a char
Definition: StrView.h:319
virtual bool replace(const char *toReplace, const char *replaced, int startPos=0)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:408
virtual char operator[](int index)
Definition: StrView.h:338
virtual void set(int value)
Definition: StrView.h:88
virtual void set(double value, int precision=2, int withd=0)
Definition: StrView.h:93
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:177
virtual void clear()
clears the string by setting the terminating 0 at the beginning
Definition: StrView.h:564
virtual void set(char chars[], int maxlen, int len=0, bool isConst=false)
assigns a memory buffer
Definition: StrView.h:116
virtual void ltrim()
remove leading spaces
Definition: StrView.h:548
virtual void remove(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:457
virtual void add(double value, int precision=2, int withd=0)
adds a double value
Definition: StrView.h:137
virtual void rtrim()
remove trailing spaces
Definition: StrView.h:554
virtual bool isOnHeap()
checks if the string is on the heap
Definition: StrView.h:579
static char * floatToString(char *outstr, double val, int precision, int widthp)
Definition: StrView.h:801
virtual int lastIndexOf(const char *cont)
provides the position of the last occurrence of the indicated substring
Definition: StrView.h:301
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:284
virtual void operator+=(const char *str)
adds a substring at the end of the string
Definition: StrView.h:341
void toUpperCase()
Converts the string to uppercase letters.
Definition: StrView.h:654
virtual bool isConst()
checks if the string is a constant that must not be changed
Definition: StrView.h:582
bool is_const
Definition: StrView.h:767
virtual bool isNewLine()
Definition: StrView.h:385
Definition: Allocator.h:13