10 static const char *toStr(IPAddress address) {
11 static char result[20];
12 sprintf(result,
"%d.%d.%d.%d", address[0], address[1], address[2],
31 if (
chars !=
nullptr) {
51 virtual void set(
const char* alt) {
55 int new_len = strlen(alt);
61 this->
chars = (
char*)alt;
85 virtual void set(
const char c) {
90 virtual void set(
int value) {
95 virtual void set(
double value,
int precision = 2,
int withd = 0) {
101 char* cpy_chars =
chars;
111 str.
chars = cpy_chars;
130 virtual void add(
int value) {
133 snprintf((
char*)
c_str()+
len, 11,
"%d", value);
139 virtual void add(
double value,
int precision = 2,
int withd = 0) {
148 virtual void add(
const char* append) {
149 if (!
isConst() && append !=
nullptr) {
150 int append_len = strlen(append);
153 strncat(
chars, append, n);
160 virtual void add(
const char c) {
172 if (str ==
nullptr)
return false;
173 return strcmp(this->
chars, str) == 0;
178 if (str ==
nullptr)
return false;
179 if (
chars ==
nullptr)
return false;
180 int len = strlen(str);
181 return strncmp(this->
chars, str,
len) == 0;
186 if (str ==
nullptr)
return false;
187 int endlen = strlen(str);
188 return strncmp(this->
chars + (
len - endlen), str, endlen) == 0;
193 if (str ==
nullptr)
return false;
194 int endlen = strlen(str);
204 const char* line = this->
chars;
206 const char* last_pattern_start = 0;
207 const char* last_line_start = 0;
209 if (*pattern == *line) {
210 if (wildcard == 1) last_line_start = line + 1;
215 }
else if (*pattern ==
'?') {
218 if (wildcard == 1) last_line_start = line + 1;
222 }
else if (*pattern ==
'*') {
223 if (*(pattern + 1) ==
'\0') {
227 last_pattern_start = pattern;
232 }
else if (wildcard) {
233 if (*line == *pattern) {
237 last_line_start = line + 1;
242 if ((*pattern) ==
'\0' && (*line) ==
'\0')
245 if (last_pattern_start != 0)
247 pattern = last_pattern_start;
248 line = last_line_start;
258 if (*pattern ==
'\0') {
267 virtual int indexOf(
const char c,
int start = 0) {
268 for (
int j = start; j <
len; j++) {
281 virtual int indexOf(
const char* cont,
int start = 0) {
282 if (
chars ==
nullptr || cont ==
nullptr)
return -1;
283 int contLen = strlen(cont);
284 for (
int j = start; j <
len; j++) {
285 char* pt =
chars + j;
286 if (strncmp(pt, cont, contLen) == 0) {
295 if (cont ==
nullptr)
return -1;
296 int contLen = strlen(cont);
297 for (
int j = (
len - contLen); j >= 0; j--) {
298 if (strncmp(cont,
chars + j, contLen) == 0) {
346 if (this->
len != alt.
len)
return false;
347 return strncmp(this->
chars, alt.
chars, this->len) == 0;
352 return strncmp(this->
chars, alt, this->
len) == 0;
357 return strncmp(this->
chars, alt.
chars, this->len) != 0;
362 return strncmp(this->
chars, alt, this->
len) != 0;
376 if (
len >= 1 &&
chars[0] ==
'\n')
return true;
377 if (
len >= 2 &&
chars[0] ==
'\r' &&
chars[1] ==
'\n')
return true;
385 virtual bool replace(
const char* toReplace,
const int replaced) {
387 snprintf(number, 50,
"%d", replaced);
388 return replace(toReplace, number);
391 virtual bool replace(
const char* toReplace,
const float replaced) {
393 snprintf(number, 50,
"%f", replaced);
394 return replace(toReplace, number);
398 virtual bool replace(
const char* toReplace,
const char* replaced) {
400 if (toReplace ==
nullptr || replaced ==
nullptr) {
408 int len_replaced = strlen(replaced);
409 int len_to_replace = strlen(toReplace);
410 insert_len = len_replaced - len_to_replace;
413 memmove(this->
chars + pos + len_replaced,
414 this->
chars + pos + len_to_replace,
415 old_len - pos - len_to_replace + 1);
417 memmove(this->
chars + pos, replaced, len_replaced);
426 virtual bool replaceAll(
const char* toReplace,
const char* replaced) {
427 if (
indexOf(toReplace) == -1) {
430 while (
replace(toReplace, replaced));
435 virtual void remove(
const char* toRemove) {
437 int removeLen = strlen(toRemove);
440 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
441 len - (pos + removeLen) + 1);
450 int removeLen = strlen(toRemove);
456 memmove((
void*)(
chars + pos), (
void*)(
chars + pos + removeLen),
457 len - (pos + removeLen) + 1);
485 int len = end - start;
487 if (this->
chars !=
nullptr) {
497 virtual void substring(
const char* from,
int start,
int end) {
499 int len = end - start;
501 if (this->
chars !=
nullptr) {
502 strncpy(this->
chars, from + start,
len);
516 virtual int count(
char c,
int startPos) {
517 for (
int j = startPos; j <
len; j++) {
527 int n =
count(
' ', 0);
551 for (
int j=0;j<
maxlen;j++)
564 virtual void insert(
int pos,
const char* str) {
566 int insert_len = strlen(str);
568 int move_len = this->
len - pos + 1;
569 memmove(
chars + pos + insert_len,
chars + pos, move_len);
570 strncpy(
chars + pos, str, insert_len);
576 if ((
size_t)
len != strlen(alt)) {
579 for (
int j = 0; j <
len; j++) {
580 if (tolower(
chars[j]) != tolower(alt[j]))
return false;
589 result = atoi(
chars);
598 result = atol(
chars);
608 result = strtod(
chars, &eptr);
618 result = strtod(
chars, &eptr);
625 if (
chars !=
nullptr) {
626 for (
int j = 0; j <
len; j++) {
634 if (
chars !=
nullptr) {
635 for (
int j = 0; j <
len; j++) {
642 static const char*
toBinary(
void const*
const ptr,
size_t const size) {
643 static char result[160];
644 unsigned char* b = (
unsigned char*)ptr;
648 for (i = size - 1; i >= 0; i--) {
649 for (j = 7; j >= 0; j--) {
650 byte = (b[i] >> j) & 1;
651 result[idx++] =
byte ?
'1' :
'0';
659 for (
int j = 0; j <
len; j++) {
660 if (isdigit(
chars[j])) {
671 for (
int j = 0; j <
len; j++) {
677 if (minus_count > 1) {
695 for (
int j = pos + 1; j <
len; j++) {
696 if (isdigit(
chars[j])) {
711 for (
int j = 0; j <
len; j++) {
717 if (minus_count > 1) {
736 const char*
buildPath(
const char* start,
const char* p1=
nullptr,
const char* p2=
nullptr) {
755 virtual bool grow(
int newMaxLen) {
return false; }
757 static char*
itoa(
int n,
char s[]) {
763 s[i++] = n % 10 +
'0';
764 }
while ((n /= 10) > 0);
765 if (sign < 0) s[i++] =
'-';
775 for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
788 double roundingFactor = 0.5;
789 unsigned long mult = 1;
790 for (i = 0; i < precision; i++) {
791 roundingFactor /= 10.0;
799 strcpy(outstr,
"-\0");
803 val += roundingFactor;
805 strcat(outstr,
itoa(
int(val), temp));
807 strcat(outstr,
".\0");
809 unsigned long mult = 1;
810 int padding = precision - 1;
811 while (precision--) mult *= 10;
814 frac = (val - int(val)) * mult;
816 frac = (int(val) - val) * mult;
817 unsigned long frac1 = frac;
819 while (frac1 /= 10) padding--;
821 while (padding--) strcat(outstr,
"0\0");
823 strcat(outstr,
itoa(frac, temp));
827 if ((widthp != 0) && ((
size_t)widthp >= strlen(outstr))) {
829 J = widthp - strlen(outstr);
831 for (i = 0; i < J; i++) {
836 strcat(temp, outstr);
837 strcpy(outstr, temp);
843 static int strncmp_i(
const char* s1,
const char* s2,
int n) {
844 if (n == 0)
return (0);
846 if (tolower(*s1) != tolower(*s2++))
847 return (*(
unsigned char*)s1 - *(
unsigned char*)--s2);
848 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:25
virtual void add(int value)
adds a int value
Definition: StrView.h:130
virtual void operator=(double val)
we can assign a double
Definition: StrView.h:315
virtual bool equalsIgnoreCase(const char *alt)
Compares the string ignoring the case.
Definition: StrView.h:575
char * chars
Definition: StrView.h:747
virtual void set(const StrView &alt)
assigs from another StrView value
Definition: StrView.h:71
bool containsNumber()
Definition: StrView.h:658
virtual bool replaceAll(const char *toReplace, const char *replaced)
Replaces all instances of toReplace with replaced.
Definition: StrView.h:426
virtual int indexOf(const char *cont, int start=0)
Definition: StrView.h:281
StrView & operator=(StrView &&)=default
virtual void operator<<(int n)
shift characters to the right -> we just move the pointer
Definition: StrView.h:321
long toLong()
Converts the string to an long.
Definition: StrView.h:595
virtual int maxLength()
provides the maximum length of the string
Definition: StrView.h:382
float toFloat()
Converts the string to a double.
Definition: StrView.h:614
StrView & operator=(const StrView &)=default
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:373
double toDouble()
Converts the string to a double.
Definition: StrView.h:604
virtual bool replace(const char *toReplace, const float replaced)
Definition: StrView.h:391
virtual void clearAll()
Definition: StrView.h:549
virtual void operator+=(double value)
adds a double at the end of the string
Definition: StrView.h:339
virtual void removeAll(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:448
const char * buildPath(const char *start, const char *p1=nullptr, const char *p2=nullptr)
Definition: StrView.h:736
virtual void operator+=(const char value)
adds a character
Definition: StrView.h:342
virtual void operator=(int value)
we can assign an int
Definition: StrView.h:318
virtual void operator=(const char *str)
we can assign a const char*
Definition: StrView.h:306
bool isNumber()
Definition: StrView.h:707
virtual void add(const char c)
adds a character
Definition: StrView.h:160
virtual void setLengthUndo()
undo the last setLength call
Definition: StrView.h:474
static const char * toBinary(void const *const ptr, size_t const size)
provides a binary string represntation
Definition: StrView.h:642
virtual void insert(int pos, const char *str)
inserts a substring into the string
Definition: StrView.h:564
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:267
virtual bool endsWithIgnoreCase(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:192
int savedLen
Definition: StrView.h:751
int numberOfDecimals()
Determines the number of decimals in the number string.
Definition: StrView.h:691
static char * floatToString(char *outstr, double val, int precision, int widthp)
Definition: StrView.h:782
int toInt()
Converts the string to an int.
Definition: StrView.h:586
virtual bool operator==(const StrView &alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:345
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition: StrView.h:177
virtual bool matches(const char *pattern)
Definition: StrView.h:200
virtual bool operator!=(const StrView &alt) const
checks if the indicated string is different from the current string
Definition: StrView.h:356
virtual bool operator==(const char *alt) const
checks if the indicated string is equal to the current string
Definition: StrView.h:351
virtual int length()
Definition: StrView.h:370
char savedChar
Definition: StrView.h:752
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:464
int len
Definition: StrView.h:749
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:366
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:185
StrView(char chars[], int maxlen, int len=0)
Creates a StrView with the indicated buffer.
Definition: StrView.h:41
virtual void swap(StrView &str)
Definition: StrView.h:100
virtual void operator=(char *str)
we can assign a char*
Definition: StrView.h:309
virtual bool operator!=(const char *alt) const
checks if the indicated string is different from the current string
Definition: StrView.h:361
StrView(const char *chars)
Creates a StrView for string constant.
Definition: StrView.h:30
virtual bool replace(const char *toReplace, const int replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:385
virtual int count(char c, int startPos)
count number of indicated characters as position
Definition: StrView.h:516
StrView(StrView &&)=default
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:510
virtual bool grow(int newMaxLen)
only supported in subclasses
Definition: StrView.h:755
virtual bool replace(const char *toReplace, const char *replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:398
virtual void add(const char *append)
adds a string
Definition: StrView.h:148
int maxlen
Definition: StrView.h:750
virtual void operator+=(int value)
adds a int at the end of the string
Definition: StrView.h:336
virtual void set(const char *alt)
assigs a value
Definition: StrView.h:51
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:483
bool isInteger()
Returns true if the string is an integer.
Definition: StrView.h:668
virtual void substring(const char *from, int start, int end)
copies a substring into the current string
Definition: StrView.h:497
void toLowerCase()
Converts the string to lowercase letters.
Definition: StrView.h:624
static int strncmp_i(const char *s1, const char *s2, int n)
Definition: StrView.h:843
virtual void set(const char c)
Definition: StrView.h:85
static char * itoa(int n, char s[])
Definition: StrView.h:757
static void reverse(char s[])
Definition: StrView.h:771
virtual void operator=(char c)
we can assign a char
Definition: StrView.h:312
virtual char operator[](int index)
Definition: StrView.h:330
virtual void set(int value)
Definition: StrView.h:90
virtual void set(double value, int precision=2, int withd=0)
Definition: StrView.h:95
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:171
virtual void clear()
clears the string by setting the terminating 0 at the beginning
Definition: StrView.h:542
virtual void set(char chars[], int maxlen, int len=0, bool isConst=false)
assigns a memory buffer
Definition: StrView.h:118
virtual void ltrim()
remove leading spaces
Definition: StrView.h:526
virtual void remove(const char *toRemove)
removes the indicated substring from the string
Definition: StrView.h:435
virtual void add(double value, int precision=2, int withd=0)
adds a double value
Definition: StrView.h:139
virtual void rtrim()
remove trailing spaces
Definition: StrView.h:532
virtual bool isOnHeap()
checks if the string is on the heap
Definition: StrView.h:558
virtual int lastIndexOf(const char *cont)
provides the position of the last occurrence of the indicated substring
Definition: StrView.h:294
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:277
virtual void operator+=(const char *str)
adds a substring at the end of the string
Definition: StrView.h:333
void toUpperCase()
Converts the string to uppercase letters.
Definition: StrView.h:633
virtual bool isConst()
checks if the string is a constant that must not be changed
Definition: StrView.h:561
bool is_const
Definition: StrView.h:748
virtual bool isNewLine()
Definition: StrView.h:375
Definition: Allocator.h:6