25 if (
chars !=
nullptr) {
45 virtual void set(
const char* alt) {
49 int new_len = strlen(alt);
55 this->
chars = (
char*)alt;
75 if (this->
chars ==
nullptr &&
len == 0) {
84 virtual void set(
const char c) {
89 virtual void set(
int value) {
94 virtual void set(
double value,
int precision = 2,
int withd = 0) {
100 char* cpy_chars =
chars;
110 str.
chars = cpy_chars;
129 virtual void add(
int value) {
132 snprintf((
char*)
c_str()+
len, 11,
"%d", value);
138 virtual void add(
double value,
int precision = 2,
int withd = 0) {
147 virtual void add(
const char* append) {
148 if (!
isConst() && append !=
nullptr) {
149 int append_len = strlen(append);
152 strncat(
chars, append, n);
157 virtual void add(
const uint8_t* append,
int len) {
158 if (!
isConst() && append !=
nullptr) {
159 int append_len =
len;
161 grow(old_len + append_len );
167 virtual void add(
const char c) {
179 if (str ==
nullptr)
return false;
180 return strcmp(this->
chars, str) == 0;
185 if (str ==
nullptr)
return false;
186 if (
chars ==
nullptr)
return false;
187 int len = strlen(str);
188 return strncmp(this->
chars, str,
len) == 0;
193 if (str ==
nullptr)
return false;
194 if (this->
chars ==
nullptr)
return false;
195 int endlen = strlen(str);
196 return strncmp(this->
chars + (
len - endlen), str, endlen) == 0;
201 if (str ==
nullptr)
return false;
202 int endlen = strlen(str);
212 const char* line = this->
chars;
214 const char* last_pattern_start = 0;
215 const char* last_line_start = 0;
217 if (*pattern == *line) {
218 if (wildcard == 1) last_line_start = line + 1;
223 }
else if (*pattern ==
'?') {
226 if (wildcard == 1) last_line_start = line + 1;
230 }
else if (*pattern ==
'*') {
231 if (*(pattern + 1) ==
'\0') {
235 last_pattern_start = pattern;
240 }
else if (wildcard) {
241 if (*line == *pattern) {
245 last_line_start = line + 1;
250 if ((*pattern) ==
'\0' && (*line) ==
'\0')
253 if (last_pattern_start != 0)
255 pattern = last_pattern_start;
256 line = last_line_start;
266 if (*pattern ==
'\0') {
275 virtual int indexOf(
const char c,
int start = 0) {
276 for (
int j = start; j <
len; j++) {
289 virtual int indexOf(
const char* cont,
int start = 0) {
290 if (
chars ==
nullptr || cont ==
nullptr)
return -1;
291 int contLen = strlen(cont);
292 for (
int j = start; j <
len; j++) {
293 char* pt =
chars + j;
294 if (strncmp(pt, cont, contLen) == 0) {
303 if (cont ==
nullptr)
return -1;
304 int contLen = strlen(cont);
305 for (
int j = (
len - contLen); j >= 0; j--) {
306 if (strncmp(cont,
chars + j, contLen) == 0) {
334 if (this->
chars ==
nullptr)
return;
355 if (this->
len != alt.
len)
return false;
356 return strncmp(this->
chars, alt.
chars, this->len) == 0;
361 if (alt ==
nullptr)
return len == 0;;
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) {
410 if (toReplace ==
nullptr || replaced ==
nullptr) {
418 int len_replaced = strlen(replaced);
419 int len_to_replace = strlen(toReplace);
420 insert_len = len_replaced - len_to_replace;
423 memmove(this->
chars + pos + len_replaced,
424 this->
chars + pos + len_to_replace,
425 old_len - pos - len_to_replace + 1);
427 memmove(this->
chars + pos, replaced, len_replaced);
436 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
437 if (
indexOf(toReplace) == -1) {
440 while (
replace(toReplace, replaced));
445 virtual void remove(
const char* toRemove) {
447 int removeLen = strlen(toRemove);
450 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
451 len - (pos + removeLen) + 1);
460 int removeLen = strlen(toRemove);
466 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
467 len - (pos + removeLen) + 1);
495 int len = end - start;
497 if (this->
chars !=
nullptr) {
507 virtual void substrView(
const char* from,
int start,
int end) {
509 int len = end - start;
511 if (this->
chars !=
nullptr) {
512 strncpy(this->
chars, from + start,
len);
526 virtual int count(
char c,
int startPos) {
527 for (
int j = startPos; j <
len; j++) {
537 int n =
count(
' ', 0);
561 for (
int j=0;j<
maxlen;j++)
574 virtual void insert(
int pos,
const char* str) {
576 int insert_len = strlen(str);
578 int move_len = this->
len - pos + 1;
579 memmove(
chars + pos + insert_len,
chars + pos, move_len);
580 strncpy(
chars + pos, str, insert_len);
586 if ((
size_t)
len != strlen(alt)) {
589 for (
int j = 0; j <
len; j++) {
590 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
599 result = atoi(
chars);
608 result = atol(
chars);
618 result = strtod(
chars, &eptr);
628 result = strtod(
chars, &eptr);
635 if (
chars !=
nullptr) {
636 for (
int j = 0; j <
len; j++) {
644 if (
chars !=
nullptr) {
645 for (
int j = 0; j <
len; j++) {
652 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
653 static char result[160];
654 unsigned char* b = (
unsigned char*)ptr;
658 for (i = size - 1; i >= 0; i--) {
659 for (j = 7; j >= 0; j--) {
660 byte = (b[i] >> j) & 1;
661 result[idx++] =
byte ?
'1' :
'0';
669 for (
int j = 0; j <
len; j++) {
670 if (isdigit(
chars[j])) {
681 for (
int j = 0; j <
len; j++) {
687 if (minus_count > 1) {
705 for (
int j = pos + 1; j <
len; j++) {
706 if (isdigit(
chars[j])) {
721 for (
int j = 0; j <
len; j++) {
727 if (minus_count > 1) {
746 const char*
buildPath(
const char* start,
const char* p1=
nullptr,
const char* p2=
nullptr) {
764 virtual bool grow(
int newMaxLen) {
return false; }
766 static char*
itoa(
int n,
char s[]) {
772 s[i++] = n % 10 +
'0';
773 }
while ((n /= 10) > 0);
774 if (sign < 0) s[i++] =
'-';
784 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
797 double roundingFactor = 0.5;
798 unsigned long mult = 1;
799 for (i = 0; i < precision; i++) {
800 roundingFactor /= 10.0;
808 strcpy(outstr,
"-\0");
812 val += roundingFactor;
814 strcat(outstr,
itoa(
int(val), temp));
816 strcat(outstr,
".\0");
818 unsigned long mult = 1;
819 int padding = precision - 1;
820 while (precision--) mult *= 10;
823 frac = (val - int(val)) * mult;
825 frac = (int(val) - val) * mult;
826 unsigned long frac1 = frac;
828 while (frac1 /= 10) padding--;
830 while (padding--) strcat(outstr,
"0\0");
832 strcat(outstr,
itoa(frac, temp));
836 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
838 J = widthp - strlen(outstr);
840 for (i = 0; i < J; i++) {
845 strcat(temp, outstr);
846 strcpy(outstr, temp);
852 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
853 if (n == 0)
return (0);
855 if (tolower(*s1) != tolower(*s2++))
856 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
857 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:19
virtual void add(int value)
adds a int value
Definition: StrView.h:129
virtual void substrView(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:493
virtual void operator=(double val)
we can assign a double
Definition: StrView.h:323
virtual bool equalsIgnoreCase(const char *alt)
Compares the string ignoring the case.
Definition: StrView.h:585
virtual void add(const uint8_t *append, int len)
Definition: StrView.h:157
char * chars
Definition: StrView.h:756
virtual void set(const StrView &alt)
assigs from another StrView value
Definition: StrView.h:67
bool containsNumber()
Definition: StrView.h:668
virtual bool replaceAll(const char *toReplace, const char *replaced)
Replaces all instances of toReplace with replaced.
Definition: StrView.h:436
virtual int indexOf(const char *cont, int start=0)
Definition: StrView.h:289
virtual void operator<<(int n)
shift characters to the right -> we just move the pointer
Definition: StrView.h:329
long toLong()
Converts the string to an long.
Definition: StrView.h:605
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:624
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:383
double toDouble()
Converts the string to a double.
Definition: StrView.h:614
virtual bool replace(const char *toReplace, const float replaced)
Definition: StrView.h:401
virtual void clearAll()
Definition: StrView.h:559
virtual void operator+=(double value)
adds a double at the end of the string
Definition: StrView.h:348
virtual void removeAll(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:458
static char * itoa(int n, char s[])
Definition: StrView.h:766
virtual void operator+=(const char value)
adds a character
Definition: StrView.h:351
virtual void substrView(const char *from, int start, int end)
copies a substring into the current string
Definition: StrView.h:507
virtual void operator=(int value)
we can assign an int
Definition: StrView.h:326
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:746
virtual void operator=(const char *str)
we can assign a const char*
Definition: StrView.h:314
bool isNumber()
Definition: StrView.h:717
virtual void add(const char c)
adds a character
Definition: StrView.h:167
virtual void setLengthUndo()
undo the last setLength call
Definition: StrView.h:484
virtual void insert(int pos, const char *str)
inserts a substring into the string
Definition: StrView.h:574
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:275
virtual bool endsWithIgnoreCase(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:200
int savedLen
Definition: StrView.h:760
int numberOfDecimals()
Determines the number of decimals in the number string.
Definition: StrView.h:701
int toInt()
Converts the string to an int.
Definition: StrView.h:596
virtual bool operator==(const StrView &alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:354
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition: StrView.h:184
virtual bool matches(const char *pattern)
Definition: StrView.h:208
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:360
virtual int length()
Definition: StrView.h:380
char savedChar
Definition: StrView.h:761
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:474
int len
Definition: StrView.h:758
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:192
StrView(char chars[], int maxlen, int len=0)
Creates a StrView with the indicated buffer.
Definition: StrView.h:35
virtual void swap(StrView &str)
Definition: StrView.h:99
virtual void operator=(char *str)
we can assign a char*
Definition: StrView.h:317
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:24
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:526
StrView(StrView &&)=default
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:520
virtual bool grow(int newMaxLen)
only supported in subclasses
Definition: StrView.h:764
virtual bool replace(const char *toReplace, const char *replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:408
virtual void add(const char *append)
adds a string
Definition: StrView.h:147
int maxlen
Definition: StrView.h:759
static const char * toBinary(void const *const ptr, size_t const size)
provides a binary string represntation
Definition: StrView.h:652
virtual void operator+=(int value)
adds a int at the end of the string
Definition: StrView.h:345
virtual void set(const char *alt)
assigs a value
Definition: StrView.h:45
bool isInteger()
Returns true if the string is an integer.
Definition: StrView.h:678
void toLowerCase()
Converts the string to lowercase letters.
Definition: StrView.h:634
StrView & operator=(StrView &&)=default
static int strncmp_i(const char *s1, const char *s2, int n)
Definition: StrView.h:852
virtual void set(const char c)
Definition: StrView.h:84
static void reverse(char s[])
Definition: StrView.h:780
virtual void operator=(char c)
we can assign a char
Definition: StrView.h:320
virtual char operator[](int index)
Definition: StrView.h:339
virtual void set(int value)
Definition: StrView.h:89
virtual void set(double value, int precision=2, int withd=0)
Definition: StrView.h:94
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:178
virtual void clear()
clears the string by setting the terminating 0 at the beginning
Definition: StrView.h:552
virtual void set(char chars[], int maxlen, int len=0, bool isConst=false)
assigns a memory buffer
Definition: StrView.h:117
virtual void ltrim()
remove leading spaces
Definition: StrView.h:536
virtual void remove(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:445
virtual void add(double value, int precision=2, int withd=0)
adds a double value
Definition: StrView.h:138
virtual void rtrim()
remove trailing spaces
Definition: StrView.h:542
virtual bool isOnHeap()
checks if the string is on the heap
Definition: StrView.h:568
static char * floatToString(char *outstr, double val, int precision, int widthp)
Definition: StrView.h:791
virtual int lastIndexOf(const char *cont)
provides the position of the last occurrence of the indicated substring
Definition: StrView.h:302
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:285
virtual void operator+=(const char *str)
adds a substring at the end of the string
Definition: StrView.h:342
void toUpperCase()
Converts the string to uppercase letters.
Definition: StrView.h:643
virtual bool isConst()
checks if the string is a constant that must not be changed
Definition: StrView.h:571
bool is_const
Definition: StrView.h:757
virtual bool isNewLine()
Definition: StrView.h:385
Definition: Allocator.h:6