6
6
#include " ./convert/convert.h"
7
7
#include " ./convert/unicode.h"
8
8
#include " ./convert/url.h"
9
- #include " hash.h"
9
+ #include " ./ hash.h"
10
10
11
11
namespace su {
12
12
@@ -122,7 +122,7 @@ class Text : public Printable {
122
122
if (!length ()) return 0 ;
123
123
uint16_t count = 0 ;
124
124
for (uint16_t i = 0 ; i < _len; i++) {
125
- if ((_charAt (i) & 0xc0 ) != 0x80 ) count++ ;
125
+ if ((_charAt (i) & 0xc0 ) != 0x80 ) ++count ;
126
126
}
127
127
return count;
128
128
}
@@ -134,7 +134,7 @@ class Text : public Printable {
134
134
for (uint16_t i = 0 ; i < _len; i++) {
135
135
if ((_charAt (i) & 0xc0 ) != 0x80 ) {
136
136
if (!upos) return i;
137
- else upos-- ;
137
+ else --upos ;
138
138
}
139
139
}
140
140
return 0 ;
@@ -146,7 +146,7 @@ class Text : public Printable {
146
146
147
147
uint16_t u = 0 ;
148
148
for (uint16_t i = 0 ; i < pos; i++) {
149
- if ((_charAt (i) & 0xc0 ) != 0x80 ) u++ ;
149
+ if ((_charAt (i) & 0xc0 ) != 0x80 ) ++u ;
150
150
}
151
151
return u;
152
152
}
@@ -170,7 +170,7 @@ class Text : public Printable {
170
170
const char * str () const {
171
171
return valid () ? _str : " " ;
172
172
}
173
-
173
+
174
174
// Получить указатель на строку. Всегда вернёт указатель, отличный от nullptr!
175
175
const uint8_t * bytes () const {
176
176
return (const uint8_t *)(valid () ? _str : " " );
@@ -395,7 +395,7 @@ class Text : public Printable {
395
395
if (!length ()) return 0 ;
396
396
uint16_t sum = 1 ;
397
397
for (uint16_t i = 0 ; i < _len; i++) {
398
- if (_charAt (i) == sym) sum++ ;
398
+ if (_charAt (i) == sym) ++sum ;
399
399
}
400
400
return sum;
401
401
}
@@ -409,7 +409,7 @@ class Text : public Printable {
409
409
pos = indexOf (txt, pos);
410
410
if (pos < 0 ) break ;
411
411
pos += txt._len ;
412
- sum++ ;
412
+ ++sum ;
413
413
}
414
414
return sum;
415
415
}
@@ -429,7 +429,7 @@ class Text : public Printable {
429
429
if (end < 0 ) end = _len;
430
430
if (!idx--) return Text (_str + start, end - start, pgm ());
431
431
if ((uint16_t )end == _len) break ;
432
- end++ ;
432
+ ++end ;
433
433
start = end;
434
434
}
435
435
return Text ();
@@ -456,6 +456,43 @@ class Text : public Printable {
456
456
return Text ();
457
457
}
458
458
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
+
459
496
// ========================== SPLIT ==========================
460
497
461
498
/* *
@@ -482,6 +519,7 @@ class Text : public Printable {
482
519
uint16_t split (T** arr, uint16_t len, char div) const {
483
520
if (!len || !length ()) return 0 ;
484
521
find_t f;
522
+
485
523
while (!f.last ) {
486
524
Text txt = _parse (div , 1 , len, f);
487
525
*(arr[f.count - 1 ]) = txt;
@@ -528,13 +566,13 @@ class Text : public Printable {
528
566
while (txt._len ) {
529
567
uint8_t sym = txt._charAt (0 );
530
568
if (sym && (sym <= 0x0F || sym == ' ' )) {
531
- txt._str ++ ;
532
- txt._len -- ;
569
+ ++ txt._str ;
570
+ -- txt._len ;
533
571
} else break ;
534
572
}
535
573
while (txt._len ) {
536
574
uint8_t sym = txt._charAt (txt._len - 1 );
537
- if (sym <= 0x0F || sym == ' ' ) txt._len -- ;
575
+ if (sym <= 0x0F || sym == ' ' ) -- txt._len ;
538
576
else break ;
539
577
}
540
578
return txt;
@@ -704,7 +742,7 @@ class Text : public Printable {
704
742
705
743
// получить значение как bool
706
744
bool toBool () const {
707
- return valid () && (charAt (0 ) == ' t' || charAt (0 ) == ' 1' );
745
+ return length () && (_charAt (0 ) == ' t' || _charAt (0 ) == ' 1' );
708
746
}
709
747
710
748
// получить значение как int
@@ -772,53 +810,39 @@ class Text : public Printable {
772
810
}
773
811
774
812
// ================= 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
+ //
822
846
T_MAKE_OPERATOR_EXPL (char , toInt16)
823
847
T_MAKE_OPERATOR (signed char , toInt16)
824
848
T_MAKE_OPERATOR (unsigned char , toInt16)
@@ -833,6 +857,13 @@ class Text : public Printable {
833
857
T_MAKE_OPERATOR (float , toFloat)
834
858
T_MAKE_OPERATOR (double , toFloat)
835
859
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
+
836
867
operator String () const {
837
868
return toString ();
838
869
}
@@ -854,7 +885,7 @@ class Text : public Printable {
854
885
if (f.count ) f.start = f.end = f.end + divlen;
855
886
f.end = indexOf (div , f.end );
856
887
if (f.end < 0 || f.count + 1 == len) f.end = _len;
857
- f.count ++ ;
888
+ ++ f.count ;
858
889
if (f.count == len || f.end == (int16_t )_len) f.last = 1 ;
859
890
return Text (_str + f.start , f.end - f.start , pgm ());
860
891
}
@@ -863,7 +894,7 @@ class Text : public Printable {
863
894
uint16_t _compareN (const char * s1, const char * s2, bool pgm2, uint16_t len) const {
864
895
while (len) {
865
896
if ((pgm () ? pgm_read_byte (s1++) : *s1++) != (pgm2 ? pgm_read_byte (s2++) : *s2++)) return len;
866
- len-- ;
897
+ --len ;
867
898
}
868
899
return 0 ;
869
900
}
0 commit comments