Skip to content

Commit 509ed70

Browse files
committed
upd
1 parent 9fd7d68 commit 509ed70

File tree

13 files changed

+746
-439
lines changed

13 files changed

+746
-439
lines changed

README.md

Lines changed: 190 additions & 99 deletions
Large diffs are not rendered by default.

examples/test/test.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void testCast(const char* str) {
8282
uint32_t u32 = txt;
8383
// int64_t i64 = txt;
8484
// uint64_t u64 = txt;
85-
bool b = txt;
85+
// bool b = txt;
8686
float f = txt;
8787
double d = txt;
8888
String S = txt;
@@ -95,7 +95,7 @@ void testCast(const char* str) {
9595
Serial.println(String("uint32_t: ") + u32);
9696
// Serial.println(String("int64_t: ") + i64);
9797
// Serial.println(String("uint64_t: ") + u64);
98-
Serial.println(String("bool: ") + b);
98+
// Serial.println(String("bool: ") + b);
9999
Serial.println(String("float: ") + f);
100100
Serial.println(String("double: ") + d);
101101
Serial.println(String("String: ") + S);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=StringUtils
2-
version=1.4.32
2+
version=1.5.0
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
55
sentence=Bunch of converting functions for string data

src/utils/Text.h

Lines changed: 92 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "./convert/convert.h"
77
#include "./convert/unicode.h"
88
#include "./convert/url.h"
9-
#include "hash.h"
9+
#include "./hash.h"
1010

1111
namespace su {
1212

@@ -122,7 +122,7 @@ class Text : public Printable {
122122
if (!length()) return 0;
123123
uint16_t count = 0;
124124
for (uint16_t i = 0; i < _len; i++) {
125-
if ((_charAt(i) & 0xc0) != 0x80) count++;
125+
if ((_charAt(i) & 0xc0) != 0x80) ++count;
126126
}
127127
return count;
128128
}
@@ -134,7 +134,7 @@ class Text : public Printable {
134134
for (uint16_t i = 0; i < _len; i++) {
135135
if ((_charAt(i) & 0xc0) != 0x80) {
136136
if (!upos) return i;
137-
else upos--;
137+
else --upos;
138138
}
139139
}
140140
return 0;
@@ -146,7 +146,7 @@ class Text : public Printable {
146146

147147
uint16_t u = 0;
148148
for (uint16_t i = 0; i < pos; i++) {
149-
if ((_charAt(i) & 0xc0) != 0x80) u++;
149+
if ((_charAt(i) & 0xc0) != 0x80) ++u;
150150
}
151151
return u;
152152
}
@@ -170,7 +170,7 @@ class Text : public Printable {
170170
const char* str() const {
171171
return valid() ? _str : "";
172172
}
173-
173+
174174
// Получить указатель на строку. Всегда вернёт указатель, отличный от nullptr!
175175
const uint8_t* bytes() const {
176176
return (const uint8_t*)(valid() ? _str : "");
@@ -395,7 +395,7 @@ class Text : public Printable {
395395
if (!length()) return 0;
396396
uint16_t sum = 1;
397397
for (uint16_t i = 0; i < _len; i++) {
398-
if (_charAt(i) == sym) sum++;
398+
if (_charAt(i) == sym) ++sum;
399399
}
400400
return sum;
401401
}
@@ -409,7 +409,7 @@ class Text : public Printable {
409409
pos = indexOf(txt, pos);
410410
if (pos < 0) break;
411411
pos += txt._len;
412-
sum++;
412+
++sum;
413413
}
414414
return sum;
415415
}
@@ -429,7 +429,7 @@ class Text : public Printable {
429429
if (end < 0) end = _len;
430430
if (!idx--) return Text(_str + start, end - start, pgm());
431431
if ((uint16_t)end == _len) break;
432-
end++;
432+
++end;
433433
start = end;
434434
}
435435
return Text();
@@ -456,6 +456,43 @@ class Text : public Printable {
456456
return Text();
457457
}
458458

459+
/**
460+
@brief Получить индекс подстроки
461+
462+
@param sub подстрока
463+
@param div разделитель
464+
@return int индекс, -1 если не найдено
465+
*/
466+
int findSub(const Text& sub, char div) const {
467+
if (!length()) return -1;
468+
int16_t start = 0, end = 0, i = 0;
469+
while (1) {
470+
end = indexOf(div, end);
471+
if (end < 0) end = _len;
472+
if (Text(_str + start, end - start, pgm()) == sub) return i;
473+
if ((uint16_t)end == _len) break;
474+
++end;
475+
start = end;
476+
++i;
477+
}
478+
return -1;
479+
}
480+
481+
int findSub(const Text& sub, const Text& div) const {
482+
if (!length() || !div.length() || div._len > _len) return -1;
483+
int16_t start = 0, end = 0, i = 0;
484+
while (1) {
485+
end = indexOf(div, end);
486+
if (end < 0) end = _len;
487+
if (Text(_str + start, end - start, pgm()) == sub) return i;
488+
if ((uint16_t)end == _len) break;
489+
end += div._len;
490+
start = end;
491+
++i;
492+
}
493+
return -1;
494+
}
495+
459496
// ========================== SPLIT ==========================
460497

461498
/**
@@ -482,6 +519,7 @@ class Text : public Printable {
482519
uint16_t split(T** arr, uint16_t len, char div) const {
483520
if (!len || !length()) return 0;
484521
find_t f;
522+
485523
while (!f.last) {
486524
Text txt = _parse(div, 1, len, f);
487525
*(arr[f.count - 1]) = txt;
@@ -528,13 +566,13 @@ class Text : public Printable {
528566
while (txt._len) {
529567
uint8_t sym = txt._charAt(0);
530568
if (sym && (sym <= 0x0F || sym == ' ')) {
531-
txt._str++;
532-
txt._len--;
569+
++txt._str;
570+
--txt._len;
533571
} else break;
534572
}
535573
while (txt._len) {
536574
uint8_t sym = txt._charAt(txt._len - 1);
537-
if (sym <= 0x0F || sym == ' ') txt._len--;
575+
if (sym <= 0x0F || sym == ' ') --txt._len;
538576
else break;
539577
}
540578
return txt;
@@ -704,7 +742,7 @@ class Text : public Printable {
704742

705743
// получить значение как bool
706744
bool toBool() const {
707-
return valid() && (charAt(0) == 't' || charAt(0) == '1');
745+
return length() && (_charAt(0) == 't' || _charAt(0) == '1');
708746
}
709747

710748
// получить значение как int
@@ -772,53 +810,39 @@ class Text : public Printable {
772810
}
773811

774812
// ================= CAST =================
775-
#define T_MAKE_OPERATOR_EXPL(type, func) \
776-
explicit operator type() const { \
777-
return (type)func(); \
778-
} \
779-
bool operator==(const type v) const { \
780-
return (type)func() == v; \
781-
} \
782-
bool operator!=(const type v) const { \
783-
return (type)func() != v; \
784-
} \
785-
bool operator>(const type v) const { \
786-
return (type)func() > v; \
787-
} \
788-
bool operator<(const type v) const { \
789-
return (type)func() < v; \
790-
} \
791-
bool operator>=(const type v) const { \
792-
return (type)func() >= v; \
793-
} \
794-
bool operator<=(const type v) const { \
795-
return (type)func() <= v; \
796-
}
797-
798-
#define T_MAKE_OPERATOR(type, func) \
799-
operator type() const { \
800-
return (type)func(); \
801-
} \
802-
bool operator==(const type v) const { \
803-
return (type)func() == v; \
804-
} \
805-
bool operator!=(const type v) const { \
806-
return (type)func() != v; \
807-
} \
808-
bool operator>(const type v) const { \
809-
return (type)func() > v; \
810-
} \
811-
bool operator<(const type v) const { \
812-
return (type)func() < v; \
813-
} \
814-
bool operator>=(const type v) const { \
815-
return (type)func() >= v; \
816-
} \
817-
bool operator<=(const type v) const { \
818-
return (type)func() <= v; \
819-
}
820-
821-
// T_MAKE_OPERATOR(bool, toBool)
813+
#define T_MAKE_COMPARE(T, func) \
814+
bool operator==(const T v) const { \
815+
return (T)func() == v; \
816+
} \
817+
bool operator!=(const T v) const { \
818+
return (T)func() != v; \
819+
} \
820+
bool operator>(const T v) const { \
821+
return (T)func() > v; \
822+
} \
823+
bool operator<(const T v) const { \
824+
return (T)func() < v; \
825+
} \
826+
bool operator>=(const T v) const { \
827+
return (T)func() >= v; \
828+
} \
829+
bool operator<=(const T v) const { \
830+
return (T)func() <= v; \
831+
}
832+
833+
#define T_MAKE_OPERATOR_EXPL(T, func) \
834+
explicit operator T() const { \
835+
return (T)func(); \
836+
} \
837+
T_MAKE_COMPARE(T, func)
838+
839+
#define T_MAKE_OPERATOR(T, func) \
840+
operator T() const { \
841+
return (T)func(); \
842+
} \
843+
T_MAKE_COMPARE(T, func)
844+
845+
//
822846
T_MAKE_OPERATOR_EXPL(char, toInt16)
823847
T_MAKE_OPERATOR(signed char, toInt16)
824848
T_MAKE_OPERATOR(unsigned char, toInt16)
@@ -833,6 +857,13 @@ class Text : public Printable {
833857
T_MAKE_OPERATOR(float, toFloat)
834858
T_MAKE_OPERATOR(double, toFloat)
835859

860+
bool operator==(const bool v) const {
861+
return length() && (v ? (_charAt(0) == 't' || _charAt(0) == '1') : (_charAt(0) == 'f' || _charAt(0) == '0'));
862+
}
863+
bool operator!=(const bool v) const {
864+
return *this == !v;
865+
}
866+
836867
operator String() const {
837868
return toString();
838869
}
@@ -854,7 +885,7 @@ class Text : public Printable {
854885
if (f.count) f.start = f.end = f.end + divlen;
855886
f.end = indexOf(div, f.end);
856887
if (f.end < 0 || f.count + 1 == len) f.end = _len;
857-
f.count++;
888+
++f.count;
858889
if (f.count == len || f.end == (int16_t)_len) f.last = 1;
859890
return Text(_str + f.start, f.end - f.start, pgm());
860891
}
@@ -863,7 +894,7 @@ class Text : public Printable {
863894
uint16_t _compareN(const char* s1, const char* s2, bool pgm2, uint16_t len) const {
864895
while (len) {
865896
if ((pgm() ? pgm_read_byte(s1++) : *s1++) != (pgm2 ? pgm_read_byte(s2++) : *s2++)) return len;
866-
len--;
897+
--len;
867898
}
868899
return 0;
869900
}

src/utils/Value.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ class Value : public Text {
3333
buf[1] = 0;
3434
_len = 1;
3535
}
36-
Value(unsigned char value, uint8_t base = DEC) : Value() {
36+
Value(signed char value, uint8_t base = DEC) : Value() {
3737
_len = intToStr(value, buf, base);
3838
}
39+
Value(unsigned char value, uint8_t base = DEC) : Value() {
40+
_len = uintToStr(value, buf, base);
41+
}
3942

4043
Value(short value, uint8_t base = DEC) : Value() {
4144
_len = intToStr(value, buf, base);
@@ -92,11 +95,16 @@ class Value : public Text {
9295
_init();
9396
return *this;
9497
}
95-
Value& operator=(unsigned char value) {
98+
Value& operator=(signed char value) {
9699
_len = intToStr(value, buf, DEC);
97100
_init();
98101
return *this;
99102
}
103+
Value& operator=(unsigned char value) {
104+
_len = uintToStr(value, buf, DEC);
105+
_init();
106+
return *this;
107+
}
100108

101109
Value& operator=(short value) {
102110
_len = intToStr(value, buf, DEC);

0 commit comments

Comments
 (0)