@@ -52,22 +52,28 @@ defmodule TextDelta.Iterator do
5252 end
5353
5454 def next ( { [ head_a | _ ] , [ head_b | _ ] } = sets , skip_type ) do
55- comparison = Operation . compare ( head_a , head_b )
5655 skip = Operation . type ( head_a ) == skip_type
57- do_next ( sets , comparison , skip )
56+ len_a = Operation . length ( head_a )
57+ len_b = Operation . length ( head_b )
58+
59+ cond do
60+ len_a > len_b -> do_next ( sets , :gt , len_b , skip )
61+ len_a < len_b -> do_next ( sets , :lt , len_a , skip )
62+ true -> do_next ( sets , :eq , 0 , skip )
63+ end
5864 end
5965
60- defp do_next ( { [ head_a | tail_a ] , [ head_b | tail_b ] } , :gt , false ) do
61- { head_a , remainder_a } = Operation . slice ( head_a , Operation . length ( head_b ) )
66+ defp do_next ( { [ head_a | tail_a ] , [ head_b | tail_b ] } , :gt , len , false ) do
67+ { head_a , remainder_a } = Operation . slice ( head_a , len )
6268 { { head_a , [ remainder_a | tail_a ] } , { head_b , tail_b } }
6369 end
6470
65- defp do_next ( { [ head_a | tail_a ] , [ head_b | tail_b ] } , :lt , _ ) do
66- { head_b , remainder_b } = Operation . slice ( head_b , Operation . length ( head_a ) )
71+ defp do_next ( { [ head_a | tail_a ] , [ head_b | tail_b ] } , :lt , len , _ ) do
72+ { head_b , remainder_b } = Operation . slice ( head_b , len )
6773 { { head_a , tail_a } , { head_b , [ remainder_b | tail_b ] } }
6874 end
6975
70- defp do_next ( { [ head_a | tail_a ] , [ head_b | tail_b ] } , _ , _ ) do
76+ defp do_next ( { [ head_a | tail_a ] , [ head_b | tail_b ] } , _ , _ , _ ) do
7177 { { head_a , tail_a } , { head_b , tail_b } }
7278 end
7379end
0 commit comments