@@ -56,6 +56,7 @@ struct charset : std::bitset<256>
5656 // }
5757};
5858
59+ class estring ;
5960class estring_view : public std ::string_view
6061{
6162public:
@@ -141,30 +142,23 @@ class estring_view : public std::string_view
141142 return substr (start, end - start + 1 );
142143 }
143144#if __cplusplus < 202000L
144- bool starts_with (estring_view x)
145- {
145+ bool starts_with (std::string_view x) const {
146146 auto len = x.size ();
147147 return length () >= len &&
148148 memcmp (data (), x.data (), len) == 0 ;
149149 }
150150
151- bool ends_with (estring_view x)
152- {
151+ bool ends_with (std::string_view x) const {
153152 auto len = x.size ();
154153 return length () >= len &&
155154 memcmp (&*end () - len, x.data (), len) == 0 ;
156155 }
157156#endif
158157
159- bool istarts_with (estring_view x) {
160- return strncasecmp (data (), x.data (), x.size ()) == 0 ;
161- }
162-
163- int icmp (estring_view x) {
164- auto ret = strncasecmp (data (), x.data (), std::min (size (), x.size ()));
165- if (ret != 0 ) return ret;
166- return size () - x.size ();
167- }
158+ bool istarts_with (std::string_view x) const ;
159+ int icmp (std::string_view x) const ;
160+ estring tolower_fast () const ;
161+ estring toupper_fast () const ;
168162
169163 template <typename Separator>
170164 struct _split
@@ -461,7 +455,6 @@ class estring : public std::string
461455
462456 estring () = default ;
463457 estring (const std::string& v) : std::string(v) {}
464- estring (estring_view sv) : std::string(sv) { }
465458 estring (std::string_view sv) : std::string(sv) { }
466459
467460 estring_view view () const
@@ -473,12 +466,10 @@ class estring : public std::string
473466 return view ().trim (spaces);
474467 }
475468#if __cplusplus < 202000L
476- bool starts_with (estring_view x)
477- {
469+ bool starts_with (std::string_view x) const {
478470 return view ().starts_with (x);
479471 }
480- bool ends_with (estring_view x)
481- {
472+ bool ends_with (std::string_view x) const {
482473 return view ().ends_with (x);
483474 }
484475#endif
@@ -705,32 +696,44 @@ inline char toupper_fast(char c) {
705696 return c - (' a' - ' A' ) * (' a' <= c && c <= ' z' );
706697}
707698
708- inline uint64_t tolower_fast8 (uint64_t x) {
709- uint64_t all_bytes = 0x0101010101010101 ;
710- uint64_t heptets = x & (0x7f * all_bytes);
711- uint64_t is_ascii = ~x & (0x80 * all_bytes);
712- uint64_t is_gt_Z = heptets + (0x7f - ' Z' ) * all_bytes;
713- uint64_t is_ge_A = heptets + (0x80 - ' A' ) * all_bytes;
714- uint64_t is_upper = (is_ge_A ^ is_gt_Z) & is_ascii;
715- return x | (is_upper >> 2 );
716- }
717-
718- inline uint64_t toupper_fast8 (uint64_t x) {
719- uint64_t all_bytes = 0x0101010101010101 ;
720- uint64_t heptets = x & (0x7f * all_bytes);
721- uint64_t is_ascii = ~x & (0x80 * all_bytes);
722- uint64_t is_gt_z = heptets + (0x7f - ' z' ) * all_bytes;
723- uint64_t is_ge_a = heptets + (0x80 - ' a' ) * all_bytes;
724- uint64_t is_lower = (is_ge_a ^ is_gt_z) & is_ascii;
725- return x ^ (is_lower >> 2 );
726- }
727-
728- // convert string to lower or upper, the storage of out must be >= len + 1
699+ // convert string to lower or upper, if len == 0 then len = strlen(in)
700+ // the storage of out must be >= len + 1
729701// it's possible that out == in
730702void tolower_fast (char * out, const char * in, size_t len);
731703void toupper_fast (char * out, const char * in, size_t len);
704+ inline void tolower_fast (char * out, std::string_view in) {
705+ tolower_fast (out, in.data (), in.size ());
706+ }
707+ inline void toupper_fast (char * out, std::string_view in) {
708+ toupper_fast (out, in.data (), in.size ());
709+ }
710+ inline estring tolower_fast (std::string_view in) {
711+ estring out (in.size (), ' \0 ' );
712+ tolower_fast (&out[0 ], in);
713+ return out;
714+ }
715+ inline estring toupper_fast (std::string_view in) {
716+ estring out (in.size (), ' \0 ' );
717+ toupper_fast (&out[0 ], in);
718+ return out;
719+ }
732720
733- // compare 2 strings without case sensitive
721+ // compare 2 strings ignoring cases
734722int stricmp_fast (std::string_view a, std::string_view b);
735723
736724} // namespace photon
725+
726+ inline bool estring_view::istarts_with (std::string_view x) const {
727+ return size () >= x.size () && photon::stricmp_fast (*this , x) == 0 ;
728+ }
729+
730+ inline int estring_view::icmp (std::string_view x) const {
731+ return photon::stricmp_fast (*this , x);
732+ }
733+
734+ inline estring estring_view::tolower_fast () const {
735+ return photon::toupper_fast (*this );
736+ }
737+ inline estring estring_view::toupper_fast () const {
738+ return photon::toupper_fast (*this );
739+ }
0 commit comments