25 if (
chars !=
nullptr) {
45 virtual void set(
const char* alt) {
49 int new_len = strlen(alt);
55 this->
chars = (
char*)alt;
79 virtual void set(
const char c) {
84 virtual void set(
int value) {
89 virtual void set(
double value,
int precision = 2,
int withd = 0) {
95 char* cpy_chars =
chars;
105 str.
chars = cpy_chars;
124 virtual void add(
int value) {
127 snprintf((
char*)
c_str()+
len, 11,
"%d", value);
133 virtual void add(
double value,
int precision = 2,
int withd = 0) {
142 virtual void add(
const char* append) {
143 if (!
isConst() && append !=
nullptr) {
144 int append_len = strlen(append);
147 strncat(
chars, append, n);
152 virtual void add(
const uint8_t* append,
int len) {
153 if (!
isConst() && append !=
nullptr) {
154 int append_len =
len;
156 grow(old_len + append_len );
162 virtual void add(
const char c) {
174 if (str ==
nullptr)
return false;
175 return strcmp(this->
chars, str) == 0;
180 if (str ==
nullptr)
return false;
181 if (
chars ==
nullptr)
return false;
182 int len = strlen(str);
183 return strncmp(this->
chars, str,
len) == 0;
188 if (str ==
nullptr)
return false;
189 int endlen = strlen(str);
190 return strncmp(this->
chars + (
len - endlen), str, endlen) == 0;
195 if (str ==
nullptr)
return false;
196 int endlen = strlen(str);
206 const char* line = this->
chars;
208 const char* last_pattern_start = 0;
209 const char* last_line_start = 0;
211 if (*pattern == *line) {
212 if (wildcard == 1) last_line_start = line + 1;
217 }
else if (*pattern ==
'?') {
220 if (wildcard == 1) last_line_start = line + 1;
224 }
else if (*pattern ==
'*') {
225 if (*(pattern + 1) ==
'\0') {
229 last_pattern_start = pattern;
234 }
else if (wildcard) {
235 if (*line == *pattern) {
239 last_line_start = line + 1;
244 if ((*pattern) ==
'\0' && (*line) ==
'\0')
247 if (last_pattern_start != 0)
249 pattern = last_pattern_start;
250 line = last_line_start;
260 if (*pattern ==
'\0') {
269 virtual int indexOf(
const char c,
int start = 0) {
270 for (
int j = start; j <
len; j++) {
283 virtual int indexOf(
const char* cont,
int start = 0) {
284 if (
chars ==
nullptr || cont ==
nullptr)
return -1;
285 int contLen = strlen(cont);
286 for (
int j = start; j <
len; j++) {
287 char* pt =
chars + j;
288 if (strncmp(pt, cont, contLen) == 0) {
297 if (cont ==
nullptr)
return -1;
298 int contLen = strlen(cont);
299 for (
int j = (
len - contLen); j >= 0; j--) {
300 if (strncmp(cont,
chars + j, contLen) == 0) {
348 if (this->
len != alt.
len)
return false;
349 return strncmp(this->
chars, alt.
chars, this->len) == 0;
354 if (alt ==
nullptr)
return len == 0;;
355 return strncmp(this->
chars, alt, this->
len) == 0;
360 return strncmp(this->
chars, alt.
chars, this->len) != 0;
365 return strncmp(this->
chars, alt, this->
len) != 0;
379 if (
len >= 1 &&
chars[0] ==
'\n')
return true;
380 if (
len >= 2 &&
chars[0] ==
'\r' &&
chars[1] ==
'\n')
return true;
388 virtual bool replace(
const char* toReplace,
const int replaced) {
390 snprintf(number, 50,
"%d", replaced);
391 return replace(toReplace, number);
394 virtual bool replace(
const char* toReplace,
const float replaced) {
396 snprintf(number, 50,
"%f", replaced);
397 return replace(toReplace, number);
401 virtual bool replace(
const char* toReplace,
const char* replaced) {
403 if (toReplace ==
nullptr || replaced ==
nullptr) {
411 int len_replaced = strlen(replaced);
412 int len_to_replace = strlen(toReplace);
413 insert_len = len_replaced - len_to_replace;
416 memmove(this->
chars + pos + len_replaced,
417 this->
chars + pos + len_to_replace,
418 old_len - pos - len_to_replace + 1);
420 memmove(this->
chars + pos, replaced, len_replaced);
429 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
430 if (
indexOf(toReplace) == -1) {
433 while (
replace(toReplace, replaced));
438 virtual void remove(
const char* toRemove) {
440 int removeLen = strlen(toRemove);
443 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
444 len - (pos + removeLen) + 1);
453 int removeLen = strlen(toRemove);
459 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
460 len - (pos + removeLen) + 1);
488 int len = end - start;
490 if (this->
chars !=
nullptr) {
500 virtual void substrView(
const char* from,
int start,
int end) {
502 int len = end - start;
504 if (this->
chars !=
nullptr) {
505 strncpy(this->
chars, from + start,
len);
519 virtual int count(
char c,
int startPos) {
520 for (
int j = startPos; j <
len; j++) {
530 int n =
count(
' ', 0);
554 for (
int j=0;j<
maxlen;j++)
567 virtual void insert(
int pos,
const char* str) {
569 int insert_len = strlen(str);
571 int move_len = this->
len - pos + 1;
572 memmove(
chars + pos + insert_len,
chars + pos, move_len);
573 strncpy(
chars + pos, str, insert_len);
579 if ((
size_t)
len != strlen(alt)) {
582 for (
int j = 0; j <
len; j++) {
583 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
592 result = atoi(
chars);
601 result = atol(
chars);
611 result = strtod(
chars, &eptr);
621 result = strtod(
chars, &eptr);
628 if (
chars !=
nullptr) {
629 for (
int j = 0; j <
len; j++) {
637 if (
chars !=
nullptr) {
638 for (
int j = 0; j <
len; j++) {
645 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
646 static char result[160];
647 unsigned char* b = (
unsigned char*)ptr;
651 for (i = size - 1; i >= 0; i--) {
652 for (j = 7; j >= 0; j--) {
653 byte = (b[i] >> j) & 1;
654 result[idx++] =
byte ?
'1' :
'0';
662 for (
int j = 0; j <
len; j++) {
663 if (isdigit(
chars[j])) {
674 for (
int j = 0; j <
len; j++) {
680 if (minus_count > 1) {
698 for (
int j = pos + 1; j <
len; j++) {
699 if (isdigit(
chars[j])) {
714 for (
int j = 0; j <
len; j++) {
720 if (minus_count > 1) {
739 const char*
buildPath(
const char* start,
const char* p1=
nullptr,
const char* p2=
nullptr) {
758 virtual bool grow(
int newMaxLen) {
return false; }
760 static char*
itoa(
int n,
char s[]) {
766 s[i++] = n % 10 +
'0';
767 }
while ((n /= 10) > 0);
768 if (sign < 0) s[i++] =
'-';
778 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
791 double roundingFactor = 0.5;
792 unsigned long mult = 1;
793 for (i = 0; i < precision; i++) {
794 roundingFactor /= 10.0;
802 strcpy(outstr,
"-\0");
806 val += roundingFactor;
808 strcat(outstr,
itoa(
int(val), temp));
810 strcat(outstr,
".\0");
812 unsigned long mult = 1;
813 int padding = precision - 1;
814 while (precision--) mult *= 10;
817 frac = (val - int(val)) * mult;
819 frac = (int(val) - val) * mult;
820 unsigned long frac1 = frac;
822 while (frac1 /= 10) padding--;
824 while (padding--) strcat(outstr,
"0\0");
826 strcat(outstr,
itoa(frac, temp));
830 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
832 J = widthp - strlen(outstr);
834 for (i = 0; i < J; i++) {
839 strcat(temp, outstr);
840 strcpy(outstr, temp);
846 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
847 if (n == 0)
return (0);
849 if (tolower(*s1) != tolower(*s2++))
850 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
851 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:124
virtual void substrView(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:486
virtual void operator=(double val)
we can assign a double
Definition: StrView.h:317
virtual bool equalsIgnoreCase(const char *alt)
Compares the string ignoring the case.
Definition: StrView.h:578
virtual void add(const uint8_t *append, int len)
Definition: StrView.h:152
char * chars
Definition: StrView.h:750
virtual void set(const StrView &alt)
assigs from another StrView value
Definition: StrView.h:65
bool containsNumber()
Definition: StrView.h:661
virtual bool replaceAll(const char *toReplace, const char *replaced)
Replaces all instances of toReplace with replaced.
Definition: StrView.h:429
virtual int indexOf(const char *cont, int start=0)
Definition: StrView.h:283
StrView & operator=(StrView &&)=default
virtual void operator<<(int n)
shift characters to the right -> we just move the pointer
Definition: StrView.h:323
long toLong()
Converts the string to an long.
Definition: StrView.h:598
virtual int maxLength()
provides the maximum length of the string
Definition: StrView.h:385
float toFloat()
Converts the string to a double.
Definition: StrView.h:617
StrView & operator=(const StrView &)=default
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:376
double toDouble()
Converts the string to a double.
Definition: StrView.h:607
virtual bool replace(const char *toReplace, const float replaced)
Definition: StrView.h:394
virtual void clearAll()
Definition: StrView.h:552
virtual void operator+=(double value)
adds a double at the end of the string
Definition: StrView.h:341
virtual void removeAll(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:451
const char * buildPath(const char *start, const char *p1=nullptr, const char *p2=nullptr)
Definition: StrView.h:739
virtual void operator+=(const char value)
adds a character
Definition: StrView.h:344
virtual void substrView(const char *from, int start, int end)
copies a substring into the current string
Definition: StrView.h:500
virtual void operator=(int value)
we can assign an int
Definition: StrView.h:320
virtual void operator=(const char *str)
we can assign a const char*
Definition: StrView.h:308
bool isNumber()
Definition: StrView.h:710
virtual void add(const char c)
adds a character
Definition: StrView.h:162
virtual void setLengthUndo()
undo the last setLength call
Definition: StrView.h:477
static const char * toBinary(void const *const ptr, size_t const size)
provides a binary string represntation
Definition: StrView.h:645
virtual void insert(int pos, const char *str)
inserts a substring into the string
Definition: StrView.h:567
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:269
virtual bool endsWithIgnoreCase(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:194
int savedLen
Definition: StrView.h:754
int numberOfDecimals()
Determines the number of decimals in the number string.
Definition: StrView.h:694
static char * floatToString(char *outstr, double val, int precision, int widthp)
Definition: StrView.h:785
int toInt()
Converts the string to an int.
Definition: StrView.h:589
virtual bool operator==(const StrView &alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:347
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition: StrView.h:179
virtual bool matches(const char *pattern)
Definition: StrView.h:202
virtual bool operator!=(const StrView &alt) const
checks if the indicated string is different from the current string
Definition: StrView.h:359
virtual bool operator==(const char *alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:353
virtual int length()
Definition: StrView.h:373
char savedChar
Definition: StrView.h:755
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:467
int len
Definition: StrView.h:752
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:369
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:187
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:94
virtual void operator=(char *str)
we can assign a char*
Definition: StrView.h:311
virtual bool operator!=(const char *alt) const
checks if the indicated string is different from the current string
Definition: StrView.h:364
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:388
virtual int count(char c, int startPos)
count number of indicated characters as position
Definition: StrView.h:519
StrView(StrView &&)=default
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:513
virtual bool grow(int newMaxLen)
only supported in subclasses
Definition: StrView.h:758
virtual bool replace(const char *toReplace, const char *replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:401
virtual void add(const char *append)
adds a string
Definition: StrView.h:142
int maxlen
Definition: StrView.h:753
virtual void operator+=(int value)
adds a int at the end of the string
Definition: StrView.h:338
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:671
void toLowerCase()
Converts the string to lowercase letters.
Definition: StrView.h:627
static int strncmp_i(const char *s1, const char *s2, int n)
Definition: StrView.h:846
virtual void set(const char c)
Definition: StrView.h:79
static char * itoa(int n, char s[])
Definition: StrView.h:760
static void reverse(char s[])
Definition: StrView.h:774
virtual void operator=(char c)
we can assign a char
Definition: StrView.h:314
virtual char operator[](int index)
Definition: StrView.h:332
virtual void set(int value)
Definition: StrView.h:84
virtual void set(double value, int precision=2, int withd=0)
Definition: StrView.h:89
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:173
virtual void clear()
clears the string by setting the terminating 0 at the beginning
Definition: StrView.h:545
virtual void set(char chars[], int maxlen, int len=0, bool isConst=false)
assigns a memory buffer
Definition: StrView.h:112
virtual void ltrim()
remove leading spaces
Definition: StrView.h:529
virtual void remove(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:438
virtual void add(double value, int precision=2, int withd=0)
adds a double value
Definition: StrView.h:133
virtual void rtrim()
remove trailing spaces
Definition: StrView.h:535
virtual bool isOnHeap()
checks if the string is on the heap
Definition: StrView.h:561
virtual int lastIndexOf(const char *cont)
provides the position of the last occurrence of the indicated substring
Definition: StrView.h:296
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:279
virtual void operator+=(const char *str)
adds a substring at the end of the string
Definition: StrView.h:335
void toUpperCase()
Converts the string to uppercase letters.
Definition: StrView.h:636
virtual bool isConst()
checks if the string is a constant that must not be changed
Definition: StrView.h:564
bool is_const
Definition: StrView.h:751
virtual bool isNewLine()
Definition: StrView.h:378
Definition: Allocator.h:6