@@ -50,11 +50,11 @@ where
5050 let ( last_char_index, last_byte_index) =
5151 self . last_char_index_to_byte_index . get ( ) ;
5252 let byte_index = last_byte_index as usize ;
53- let mut char_index = last_char_index as usize ;
5453
5554 if start_char_index >= last_char_index as usize
5655 || end_char_index >= last_char_index as usize
5756 {
57+ let mut char_index = last_char_index as usize ;
5858 #[ allow( unsafe_code) ]
5959 let slice = unsafe {
6060 // SAFETY: Since `indices` iterates over the `CharIndices` of `self`, we can guarantee
7777 }
7878 char_index += 1 ;
7979 }
80- } else {
80+ }
81+
82+ if start_char_index < last_char_index as usize
83+ || end_char_index < last_char_index as usize
84+ {
85+ let mut char_index = last_char_index as usize ;
8186 #[ allow( unsafe_code) ]
8287 let slice = unsafe {
8388 // SAFETY: Since `indices` iterates over the `CharIndices` of `self`, we can guarantee
@@ -162,5 +167,11 @@ mod tests {
162167 assert_eq ! ( rope_with_indices. substring( 10 , 13 ) , "d 你" ) ;
163168 assert_eq ! ( rope_with_indices. substring( 13 , 15 ) , "好世" ) ;
164169 assert_eq ! ( rope_with_indices. substring( 10 , 13 ) , "d 你" ) ;
170+
171+ let rope_with_indices =
172+ WithIndices :: new ( Rope :: from ( "export const answer = 42;\n " ) ) ;
173+ assert_eq ! ( rope_with_indices. substring( 7 , 13 ) , "const " ) ;
174+ assert_eq ! ( rope_with_indices. substring( 13 , 19 ) , "answer" ) ;
175+ assert_eq ! ( rope_with_indices. substring( 7 , 22 ) , "const answer = " ) ;
165176 }
166177}
0 commit comments