|
| 1 | +type change = { |
| 2 | + (* number of lines in new data changed/inserted here. *) |
| 3 | + inserted: int; |
| 4 | + (* number of lines in old data changed/deleted here. *) |
| 5 | + deleted: int; |
| 6 | + (* line number of 1st deleted line. *) |
| 7 | + firstDeletedLine: int; |
| 8 | + (* line number of 1st inserted line. *) |
| 9 | + firstInsertedLine: int; |
| 10 | +} |
| 11 | + |
| 12 | +(* A diff is a chain of changes *) |
| 13 | +type diff = change list |
| 14 | + |
| 15 | +(* 'a' stands for added, 'd' for deleted and 'c' for changed *) |
| 16 | +let changeLetter inserts deletes = |
| 17 | + match inserts, deletes with |
| 18 | + | 0, _ -> 'd' |
| 19 | + | _, 0 -> 'a' |
| 20 | + | _ -> 'c' |
| 21 | + |
| 22 | +let translateLineNumber lnum = lnum + 1 |
| 23 | + |
| 24 | +let numberRange a b = |
| 25 | + let a = translateLineNumber a in |
| 26 | + let b = translateLineNumber b in |
| 27 | + if b > a then |
| 28 | + Format.sprintf "%d,%d" a b |
| 29 | + else |
| 30 | + string_of_int b |
| 31 | + |
| 32 | +let printChange change oldArr newArr = |
| 33 | + (* No insertions or deletions means same line *) |
| 34 | + if change.inserted = 0 && change.deleted == 0 then () |
| 35 | + else |
| 36 | + let lastDeletedLine = (change.firstDeletedLine + change.deleted - 1) in |
| 37 | + let lastInsertedLine = (change.firstInsertedLine + change.inserted - 1) in |
| 38 | + Format.sprintf "%s%c%s" |
| 39 | + (numberRange change.firstDeletedLine lastDeletedLine) |
| 40 | + (changeLetter change.inserted change.deleted) |
| 41 | + (numberRange change.firstInsertedLine lastInsertedLine) |
| 42 | + |> print_endline; |
| 43 | + |
| 44 | + if change.deleted > 0 then |
| 45 | + for i = change.firstDeletedLine to lastDeletedLine do |
| 46 | + Format.sprintf "< %s" (Array.get oldArr i) |> print_endline; |
| 47 | + done; |
| 48 | + if change.deleted > 0 && change.inserted > 0 then |
| 49 | + print_endline "---"; |
| 50 | + if change.inserted > 0 then |
| 51 | + for i = change.firstInsertedLine to lastInsertedLine do |
| 52 | + Format.sprintf "> %s" (Array.get newArr i) |> print_endline; |
| 53 | + done |
| 54 | + |
| 55 | +(* Compute longest common subsequence between oldArr and newArr |
| 56 | + * Uses the dynamic programming approach described on: |
| 57 | + * https://en.wikipedia.org/wiki/Longest_common_subsequence_problem *) |
| 58 | +let computeMatrix ~compareFn oldArr oldLen newArr newLen = |
| 59 | + let matrix = Array.make_matrix (oldLen + 1) (newLen + 1) 0 in |
| 60 | + |
| 61 | + for i = 1 to oldLen do |
| 62 | + for j = 1 to newLen do |
| 63 | + let c = compareFn (Array.unsafe_get oldArr (i - 1)) (Array.unsafe_get newArr (j - 1)) in |
| 64 | + if c = 0 then |
| 65 | + let northEast = Array.unsafe_get (Array.unsafe_get matrix (i - 1)) (j - 1) in |
| 66 | + Array.unsafe_set (Array.unsafe_get matrix i) j (northEast + 1) |
| 67 | + else |
| 68 | + let north = Array.unsafe_get (Array.unsafe_get matrix (i - 1)) j in |
| 69 | + let east = Array.unsafe_get (Array.unsafe_get matrix i) (j - 1) in |
| 70 | + Array.unsafe_set (Array.unsafe_get matrix i) j (max east north) |
| 71 | + done; |
| 72 | + done; |
| 73 | + |
| 74 | + matrix |
| 75 | + |
| 76 | +(* emits differences between `oldArr` and `newArr` through the `onChange` callback *) |
| 77 | +let diff ~onChange ~compareFn oldArr newArr = |
| 78 | + let oldLen = Array.length oldArr in |
| 79 | + let newLen = Array.length newArr in |
| 80 | + |
| 81 | + (* compute the longest common subsequence *) |
| 82 | + let matrix = computeMatrix ~compareFn oldArr oldLen newArr newLen in |
| 83 | + |
| 84 | + (* Read the longest common subsequence matrix and compute changes while working |
| 85 | + * backwards through the matrix *) |
| 86 | + let rec walkMatrix i j firstDeletedLine firstInsertedLine = |
| 87 | + if i > 0 && j > 0 then ( |
| 88 | + if (Array.unsafe_get oldArr (i - 1)) = (Array.unsafe_get newArr (j - 1)) then ( |
| 89 | + onChange { |
| 90 | + firstDeletedLine = i; |
| 91 | + firstInsertedLine = j; |
| 92 | + deleted = firstDeletedLine - i; |
| 93 | + inserted = firstInsertedLine - j; |
| 94 | + }; |
| 95 | + let nextI = i - 1 in |
| 96 | + let nextJ = j - 1 in |
| 97 | + walkMatrix nextI nextJ nextI nextJ |
| 98 | + ) else if (Array.unsafe_get (Array.unsafe_get matrix (i - 1)) j) >= (Array.unsafe_get (Array.unsafe_get matrix i) (j - 1)) then ( |
| 99 | + walkMatrix (i - 1) j firstDeletedLine firstInsertedLine |
| 100 | + ) else ( |
| 101 | + walkMatrix i (j - 1) firstDeletedLine firstInsertedLine |
| 102 | + ) |
| 103 | + ) else if j > 0 && i = 0 then ( |
| 104 | + walkMatrix i (j - 1) firstDeletedLine firstInsertedLine |
| 105 | + ) else if i > 0 && j = 0 then ( |
| 106 | + walkMatrix (i - 1) j firstDeletedLine firstInsertedLine |
| 107 | + ) else ( |
| 108 | + onChange { |
| 109 | + firstDeletedLine = i; |
| 110 | + firstInsertedLine = j; |
| 111 | + deleted = firstDeletedLine - i; |
| 112 | + inserted = firstInsertedLine - j |
| 113 | + } |
| 114 | + ) |
| 115 | + in |
| 116 | + walkMatrix oldLen newLen oldLen newLen |
| 117 | + |
| 118 | +(* splits both strings in lines and does a line-based diff *) |
| 119 | +let diffTwoStrings oldString newString = |
| 120 | + let oldArr = oldString |> String.split_on_char '\n' |> Array.of_list in |
| 121 | + let newArr = newString |> String.split_on_char '\n' |> Array.of_list in |
| 122 | + let script = ref [] in |
| 123 | + diff |
| 124 | + ~onChange:(fun change -> script.contents <- change::script.contents) |
| 125 | + ~compareFn:String.compare |
| 126 | + oldArr |
| 127 | + newArr; |
| 128 | + List.iter (fun change -> printChange change oldArr newArr) script.contents |
0 commit comments