Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Replace nil with explicit sentinel for diff and patch
Nil is a valid value in a sequence, but was used as
an iterator marker, causing program to hang.
Similarly, patch used nil as a deletion-marker.
This commit replaces both nil uses with explicit sentinel values.
  • Loading branch information
pithyless committed Mar 16, 2019
commit 6ec1fbe7a81b1979c3ee9e5443ed38357359d926
2 changes: 1 addition & 1 deletion src/clj/clj_diff/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

(defn patch*
[s edit-script]
(filter #(not (nil? %)) (merge-patch s edit-script nil)))
(filter #(not (= ::delete-sentinel %)) (merge-patch s edit-script ::delete-sentinel)))

(defmulti ^{:arglists '([s edit-script])} patch
"Use the instructions in the edit script to transform the sequence s into
Expand Down
4 changes: 2 additions & 2 deletions src/clj/clj_diff/miller.clj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
(let [d (edit-dist delta p k)
head-x (backtrack-snake a b x (- x k))]
(loop [head-x head-x]
(let [move (first (filter #(and (not (nil? %)) ;; <<<===
(let [move (first (filter #(and (not (= ::sentinel %)) ;; <<<===
(= (:d %) (dec d)))
(map #(% graph delta p head-x k)
[look-left look-up])))]
Expand Down Expand Up @@ -195,7 +195,7 @@
edits))

(defn vectorize [& more]
(map #(vec (cons nil %)) more))
(map #(vec (cons ::sentinel %)) more))

(defn order->ses
[a b]
Expand Down
8 changes: 8 additions & 0 deletions test/clj_diff/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
(let [t (fn [a b] (edit-distance (diff a b)))]
(is (= (t [1 2 3 4 3 2 3 2 1 2 3] [2 3 1 2 3 4 5 4 3])
10))
(is (= (t [nil 1 2] [1 2])
1))
(is (= (t [1 2] [nil 1 2])
1))
(is (= (t [nil 1 2] [nil 1 2])
0))
(is (= (t "abcab" "cbab")
3))
(is (= (t "abcabba" "cbabac")
Expand Down Expand Up @@ -69,6 +75,8 @@
(are [a b]
(= b (patch a (diff a b)))

[nil 42] [42]
[42] [42 nil]
"aba" "aca"
"abcabba" "cbabac"
"FWdRgevf43" "T5C7U3Lz5v"
Expand Down