Skip to content

Commit

Permalink
* Update cabal synopsis, description, and homepage
Browse files Browse the repository at this point in the history
* Add missing let statements for some example code
* Add a couple more examples
  • Loading branch information
ddssff committed Oct 23, 2023
1 parent 026aef2 commit 936a2b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
8 changes: 6 additions & 2 deletions Diff.cabal
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
name: Diff
version: 0.5
synopsis: O(ND) diff algorithm in haskell.
description: Implementation of the standard diff algorithm, and utilities for pretty printing.
synopsis: Diff algorithm in pure Haskell
description: Implementation of the standard diff algorithm in Haskell.
.
Time complexity is O(ND) (input length * number of differences).
Space complexity is O(D^2). Includes utilities for pretty printing.
category: Algorithms
homepage: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927
license: BSD3
license-file: LICENSE
author: Sterling Clover
Expand Down
10 changes: 9 additions & 1 deletion src/Data/Algorithm/Diff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-- Portability : portable
--
-- This is an implementation of the diff algorithm as described in
-- \"An \( O(ND) \) Difference Algorithm and Its Variations (1986)\"
-- /An \( O(ND) \) Difference Algorithm and Its Variations (1986)/
-- <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927>.
-- For inputs of size \( O(N) \) with the number of differences \( D \)
-- it has \( O(ND) \) time and \( O(D^2) \) space complexity.
Expand Down Expand Up @@ -92,11 +92,19 @@ lcs eq as bs = path . head . dropWhile (\dl -> poi dl /= lena || poj dl /= lenb)

-- | Takes two lists and returns a list of differences between them. This is
-- 'getDiffBy' with '==' used as predicate.
--
-- > > getDiff ["a","b","c","d","e"] ["a","c","d","f"]
-- > [Both "a" "a",First "b",Both "c" "c",Both "d" "d",First "e",Second "f"]
-- > > getDiff "abcde" "acdf"
-- > [Both 'a' 'a',First 'b',Both 'c' 'c',Both 'd' 'd',First 'e',Second 'f']
getDiff :: (Eq a) => [a] -> [a] -> [Diff a]
getDiff = getDiffBy (==)

-- | Takes two lists and returns a list of differences between them, grouped
-- into chunks. This is 'getGroupedDiffBy' with '==' used as predicate.
--
-- > > getGroupedDiff "abcde" "acdf"
-- > [Both "a" "a",First "b",Both "cd" "cd",First "e",Second "f"]
getGroupedDiff :: (Eq a) => [a] -> [a] -> [Diff [a]]
getGroupedDiff = getGroupedDiffBy (==)

Expand Down
58 changes: 30 additions & 28 deletions src/Data/Algorithm/DiffContext.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,36 @@ groupBy' eq (x : xs) = go [x] xs
-- This new one corrects the issue. Here is the example from the test
-- suite:
--
-- > prettyContextDiff (text "file1") (text "file2") text (getContextDiffOld 2 (lines textA) (lines textB))
-- --- file1
-- +++ file2
-- @@
-- a
-- b
-- -c
-- @@
-- d
-- e
-- @@
-- i
-- j
-- -k
--
-- > prettyContextDiff (text "file1") (text "file2") text (getContextDiff 2 (lines textA) (lines textB))
-- --- file1
-- +++ file2
-- @@
-- a
-- b
-- -c
-- d
-- e
-- @@
-- i
-- j
-- -k
-- > > let textA = unlines ["a","b","c","d","e","f","g","h","i","j","k"]
-- > > let textB = unlines ["a","b","d","e","f","g","h","i","j"]
-- > > prettyContextDiff (text "file1") (text "file2") text (getContextDiffOld 2 (lines textA) (lines textB))
-- > --- file1
-- > +++ file2
-- > @@
-- > a
-- > b
-- > -c
-- > @@
-- > d
-- > e
-- > @@
-- > i
-- > j
-- > -k
-- >
-- > > prettyContextDiff (text "file1") (text "file2") text (getContextDiff 2 (lines textA) (lines textB))
-- > --- file1
-- > +++ file2
-- > @@
-- > a
-- > b
-- > -c
-- > d
-- > e
-- > @@
-- > i
-- > j
-- > -k
getContextDiffNew ::
Eq a
=> Maybe Int -- ^ Number of context elements, Nothing means infinite
Expand Down
8 changes: 8 additions & 0 deletions src/Data/Algorithm/DiffOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ diffToLineRanges = toLineRange 1 1
: toLineRange (leftLine+linesF) (rightLine+linesS) rs

-- | pretty print the differences. The output is similar to the output of the diff utility
--
-- > > putStr (ppDiff (getGroupedDiff ["a","b","c","d","e"] ["a","c","d","f"]))
-- > 2d1
-- > < b
-- > 5c4
-- > < e
-- > ---
-- > > f
ppDiff :: [Diff [String]] -> String
ppDiff gdiff =
let diffLineRanges = diffToLineRanges gdiff
Expand Down

0 comments on commit 936a2b5

Please sign in to comment.