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