@@ -492,15 +492,17 @@ String operator +(const char *lhs, const String &rhs) {
492492/* ********************************************/
493493
494494int String::compareTo (const char *cstr, size_t length) const {
495- if (!buffer () || !cstr) {
496- if (cstr && length > 0 )
497- return 0 - static_cast <int >(pgm_read_byte (cstr));
498- if (buffer () && len () > 0 )
499- return static_cast <int >(buffer ()[0 ]);
500- return 0 ;
501- }
495+ const auto min_len = std::min (len (), length);
496+ int res = strncmp_P (buffer (), cstr, min_len);
497+ if (!res)
498+ return res;
499+
500+ if (len () < length)
501+ return -1 ;
502+ if (len () > length)
503+ return 1 ;
502504
503- return strncmp_P ( buffer (), cstr, len ()) ;
505+ return 0 ;
504506}
505507
506508int String::compareTo (const String &s) const {
@@ -512,15 +514,23 @@ int String::compareTo(const char *cstr) const {
512514}
513515
514516bool String::equals (const String &s) const {
515- return ( len () == s.len () && compareTo (s) == 0 );
517+ return equals (s. buffer (), s.len ());
516518}
517519
518520bool String::equals (const char *cstr) const {
519- if (len () == 0 )
520- return (cstr == NULL || pgm_read_byte (cstr) == 0 );
521- if (cstr == NULL )
522- return buffer ()[0 ] == 0 ;
523- return strncmp_P (buffer (), cstr, len ()) == 0 ;
521+ return equals (cstr, strlen_P (cstr));
522+ }
523+
524+ bool String::equals (const char *cstr, size_t length) const {
525+ if (!cstr)
526+ return false ;
527+
528+ const auto same_length = len () == length;
529+ if (same_length && length == 0 )
530+ return true ;
531+
532+ return same_length
533+ && strncmp_P (buffer (), cstr, length) == 0 ;
524534}
525535
526536bool String::operator <(const String &rhs) const {
@@ -554,9 +564,9 @@ bool String::operator>=(const char *rhs) const {
554564bool String::equalsIgnoreCase (const char *str, size_t length) const {
555565 if (len () != length)
556566 return false ;
557- if (length == 0 )
567+ if (!cstr || !length )
558568 return true ;
559- return strncasecmp_P (buffer (), str , length) == 0 ;
569+ return strncasecmp_P (buffer (), cstr , length) == 0 ;
560570}
561571
562572bool String::equalsIgnoreCase (const String &s) const {
0 commit comments