@@ -75,6 +75,9 @@ defmodule Dmp.Patch do
7575
7676 :equal ->
7777 " "
78+
79+ _ ->
80+ raise RuntimeError , "Invalid operation #{ inspect ( op ) } "
7881 end
7982
8083 text = uri_encode ( text )
@@ -534,7 +537,9 @@ defmodule Dmp.Patch do
534537 opts
535538 )
536539
537- if start_loc != - 1 do
540+ if start_loc == - 1 do
541+ { - 1 , - 1 }
542+ else
538543 end_loc =
539544 Match . main (
540545 text ,
@@ -549,8 +554,6 @@ defmodule Dmp.Patch do
549554 else
550555 { start_loc , end_loc }
551556 end
552- else
553- { start_loc , - 1 }
554557 end
555558 else
556559 { Match . main ( text , text1 , expected_loc , opts ) , - 1 }
@@ -586,11 +589,14 @@ defmodule Dmp.Patch do
586589
587590 # Returns true if the end points match, but the content is unacceptably bad.
588591 def poor_match ( diffs , text1 , opts ) do
589- lev = Diff . levenshtein ( diffs )
590592 text1_length = String . length ( text1 )
591593
592- text1_length > Keyword . fetch! ( opts , :match_max_bits ) &&
593- lev / text1_length > Keyword . fetch! ( opts , :patch_delete_threshold )
594+ if text1_length > Keyword . fetch! ( opts , :match_max_bits ) do
595+ normalized_lev = Diff . levenshtein ( diffs ) / text1_length
596+ normalized_lev > Keyword . fetch! ( opts , :patch_delete_threshold )
597+ else
598+ false
599+ end
594600 end
595601
596602 def apply_match_diff ( { op , first_text } , acc_text , index1 , diffs , start_loc ) do
@@ -621,6 +627,9 @@ defmodule Dmp.Patch do
621627 substring ( acc_text , start_loc + index3 )
622628
623629 { acc_text , index1 }
630+
631+ _ ->
632+ raise RuntimeError , "Invalid operation #{ inspect ( op ) } "
624633 end
625634 end
626635
@@ -680,19 +689,9 @@ defmodule Dmp.Patch do
680689 padding_length = String . length ( null_padding )
681690 first_diff = List . first ( first_patch . diffs )
682691
683- if is_nil ( first_diff ) || elem ( first_diff , 0 ) != :equal do
684- # Add null_padding equality.
685- # start1 and start2 should be 0.
686- % Patch {
687- first_patch
688- | diffs: [ { :equal , null_padding } | first_patch . diffs ] ,
689- start1: first_patch . start1 - padding_length ,
690- start2: first_patch . start2 - padding_length ,
691- length1: first_patch . length1 + padding_length ,
692- length2: first_patch . length2 + padding_length
693- }
694- else
695- { op , text } = first_diff
692+ { op , text } = Diff . undiff ( first_diff )
693+
694+ if op == :equal do
696695 text_length = String . length ( text )
697696
698697 if padding_length > text_length do
@@ -712,6 +711,18 @@ defmodule Dmp.Patch do
712711 else
713712 first_patch
714713 end
714+ else
715+ # :nil, :insert, or :delete
716+ # Add null_padding equality.
717+ # start1 and start2 should be 0.
718+ % Patch {
719+ first_patch
720+ | diffs: [ { :equal , null_padding } | first_patch . diffs ] ,
721+ start1: first_patch . start1 - padding_length ,
722+ start2: first_patch . start2 - padding_length ,
723+ length1: first_patch . length1 + padding_length ,
724+ length2: first_patch . length2 + padding_length
725+ }
715726 end
716727 end
717728
@@ -720,16 +731,9 @@ defmodule Dmp.Patch do
720731 padding_length = String . length ( null_padding )
721732 last_diff = List . last ( last_patch . diffs )
722733
723- if is_nil ( last_diff ) || elem ( last_diff , 0 ) != :equal do
724- # Add null_padding equality.
725- % Patch {
726- last_patch
727- | diffs: last_patch . diffs ++ [ { :equal , null_padding } ] ,
728- length1: last_patch . length1 + padding_length ,
729- length2: last_patch . length2 + padding_length
730- }
731- else
732- { op , text } = last_diff
734+ { op , text } = Diff . undiff ( last_diff )
735+
736+ if op == :equal do
733737 text_length = String . length ( text )
734738
735739 if padding_length > text_length do
@@ -746,6 +750,15 @@ defmodule Dmp.Patch do
746750 else
747751 last_patch
748752 end
753+ else
754+ # :nil, :insert, or :delete
755+ # Add null_padding equality.
756+ % Patch {
757+ last_patch
758+ | diffs: last_patch . diffs ++ [ { :equal , null_padding } ] ,
759+ length1: last_patch . length1 + padding_length ,
760+ length2: last_patch . length2 + padding_length
761+ }
749762 end
750763 end
751764
@@ -837,11 +850,13 @@ defmodule Dmp.Patch do
837850 if postcontext != "" do
838851 postcontext_length = String . length ( postcontext )
839852 last_diff = List . last ( patch . diffs )
853+ { op , text } = Diff . undiff ( last_diff )
840854
841855 patch_diffs =
842- if is_tuple ( last_diff ) && elem ( last_diff , 0 ) == :equal do
843- Enum . drop ( patch . diffs , - 1 ) ++ [ { :equal , elem ( last_diff , 1 ) <> postcontext } ]
856+ if op == :equal do
857+ Enum . drop ( patch . diffs , - 1 ) ++ [ { :equal , text <> postcontext } ]
844858 else
859+ # :nil, :insert, or :delete
845860 patch . diffs ++ [ { :equal , postcontext } ]
846861 end
847862
0 commit comments