22
33use std:: {
44 borrow:: Cow ,
5- cell:: RefCell ,
65 collections:: VecDeque ,
76 hash:: Hash ,
87 ops:: { Bound , RangeBounds } ,
@@ -734,6 +733,18 @@ impl PartialEq<Rope<'_>> for Rope<'_> {
734733 return false ;
735734 }
736735
736+ if let (
737+ Rope {
738+ repr : Repr :: Light ( s) ,
739+ } ,
740+ Rope {
741+ repr : Repr :: Light ( other) ,
742+ } ,
743+ ) = ( self , other)
744+ {
745+ return s == other;
746+ }
747+
737748 let chunks = match & self . repr {
738749 Repr :: Light ( s) => & [ ( * s, 0 ) ] [ ..] ,
739750 Repr :: Full ( data) => & data[ ..] ,
@@ -743,22 +754,63 @@ impl PartialEq<Rope<'_>> for Rope<'_> {
743754 Repr :: Full ( data) => & data[ ..] ,
744755 } ;
745756
746- let mut cur = 0 ;
747- let other_chunk_index = RefCell :: new ( 0 ) ;
748- let mut other_chunk_byte_index = 0 ;
749- let other_chunk = || other_chunks[ * other_chunk_index. borrow ( ) ] . 0 . as_bytes ( ) ;
750- for ( chunk, start_pos) in chunks. iter ( ) {
751- let chunk = chunk. as_bytes ( ) ;
752- while ( cur - start_pos) < chunk. len ( ) {
753- if other_chunk_byte_index >= other_chunk ( ) . len ( ) {
754- other_chunk_byte_index = 0 ;
755- * other_chunk_index. borrow_mut ( ) += 1 ;
757+ let total_bytes = self . len ( ) ;
758+ let mut byte_idx = 0 ;
759+
760+ let mut chunks_idx = 0 ;
761+ let mut in_chunk_byte_idx = 0 ;
762+
763+ let mut other_chunks_idx = 0 ;
764+ let mut in_other_chunk_byte_idx = 0 ;
765+
766+ loop {
767+ if byte_idx == total_bytes {
768+ break ;
769+ }
770+
771+ let & ( chunk, _) = & chunks[ chunks_idx] ;
772+ let chunk_len = chunk. len ( ) ;
773+ let & ( other_chunk, _) = & other_chunks[ other_chunks_idx] ;
774+ let other_chunk_len = other_chunk. len ( ) ;
775+
776+ let chunk_remaining = chunk_len - in_chunk_byte_idx;
777+ let other_chunk_remaining = other_chunk_len - in_other_chunk_byte_idx;
778+
779+ match chunk_remaining. cmp ( & other_chunk_remaining) {
780+ std:: cmp:: Ordering :: Less => {
781+ if other_chunk
782+ [ in_other_chunk_byte_idx..in_other_chunk_byte_idx + chunk_remaining]
783+ != chunk[ in_chunk_byte_idx..]
784+ {
785+ return false ;
786+ }
787+ in_other_chunk_byte_idx += chunk_remaining;
788+ chunks_idx += 1 ;
789+ in_chunk_byte_idx = 0 ;
790+ byte_idx += chunk_remaining;
756791 }
757- if chunk[ cur - start_pos] == other_chunk ( ) [ other_chunk_byte_index] {
758- cur += 1 ;
759- other_chunk_byte_index += 1 ;
760- } else {
761- return false ;
792+ std:: cmp:: Ordering :: Equal => {
793+ if chunk[ in_chunk_byte_idx..]
794+ != other_chunk[ in_other_chunk_byte_idx..]
795+ {
796+ return false ;
797+ }
798+ chunks_idx += 1 ;
799+ other_chunks_idx += 1 ;
800+ in_chunk_byte_idx = 0 ;
801+ in_other_chunk_byte_idx = 0 ;
802+ byte_idx += chunk_remaining;
803+ }
804+ std:: cmp:: Ordering :: Greater => {
805+ if chunk[ in_chunk_byte_idx..in_chunk_byte_idx + other_chunk_remaining]
806+ != other_chunk[ in_other_chunk_byte_idx..]
807+ {
808+ return false ;
809+ }
810+ in_chunk_byte_idx += other_chunk_remaining;
811+ other_chunks_idx += 1 ;
812+ in_other_chunk_byte_idx = 0 ;
813+ byte_idx += other_chunk_remaining;
762814 }
763815 }
764816 }
@@ -1061,6 +1113,16 @@ mod tests {
10611113 b. add ( "fghi" ) ;
10621114
10631115 assert_eq ! ( a, b) ;
1116+
1117+ let mut a = Rope :: new ( ) ;
1118+ a. add ( "abc" ) ;
1119+
1120+ let mut b = Rope :: new ( ) ;
1121+ b. add ( "a" ) ;
1122+ b. add ( "b" ) ;
1123+ b. add ( "c" ) ;
1124+
1125+ assert_eq ! ( a, b) ;
10641126 }
10651127
10661128 #[ test]
0 commit comments