Diff-1.0.2: Diff algorithm in pure Haskell

Copyright | (c) David Fox (2015)
License | BSD 3 Clause
Maintainer | s.clover@gmail.com
Stability | experimental
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Algorithm.DiffContext

Description

Generates a grouped diff with merged runs, and outputs them in the manner of diff -u

Synopsis

- type ContextDiff c = [Hunk c]
- type Hunk c = [Diff [c]]
- getContextDiff :: Eq a => Maybe Int -> [a] -> [a] -> ContextDiff (Numbered a)
- prettyContextDiff :: Doc -> Doc -> (Numbered c -> Doc) -> ContextDiff (Numbered c) -> Doc
- prettyContextDiffOld :: Doc -> Doc -> (c -> Doc) -> ContextDiff c -> Doc
- getContextDiffNumbered :: Eq a => Maybe Int -> [Numbered a] -> [Numbered a] -> ContextDiff (Numbered a)
- data Numbered a = Numbered Int a
- numbered :: [a] -> [Numbered a]
- unnumber :: Numbered a -> a
- unNumberContextDiff :: ContextDiff (Numbered a) -> ContextDiff a
- groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]

# Documentation

type ContextDiff c = [Hunk c] Source #

type Hunk c = [Diff [c]] Source #

getContextDiff Source #

Arguments

:: Eq a |
=> Maybe Int | Number of context elements, Nothing means infinite
-> [a] |
-> [a] |
-> ContextDiff (Numbered a) |

    > let textA = ["a","b","c","d","e","f","g","h","i","j","k"]
    > let textB = ["a","b","d","e","f","g","h","i","j"]
    > let diff = getContextDiff (Just 2) textA textB
    > prettyContextDiff (text "file1") (text "file2") (text . unnumber) diff
    --- file1
    +++ file2
    @@ -1,5 +1,4 @@
     a
     b
    -c
     d
     e
    @@ -9,3 +8,2 @@
     i
     j
    -k

prettyContextDiff Source #

Arguments

:: Doc | Document 1 name
-> Doc | Document 2 name
-> (Numbered c -> Doc) | Element pretty printer
-> ContextDiff (Numbered c) |
-> Doc |

Pretty print a ContextDiff in the manner of diff -u.

prettyContextDiffOld Source #

Arguments

:: Doc | Document 1 name
-> Doc | Document 2 name
-> (c -> Doc) | Element pretty printer
-> ContextDiff c |
-> Doc |

Pretty print without line numbers.

getContextDiffNumbered Source #

Arguments

:: Eq a |
=> Maybe Int | Number of context elements, Nothing means infinite
-> [Numbered a] |
-> [Numbered a] |
-> ContextDiff (Numbered a) |

data Numbered a Source #

Constructors

Numbered Int a |

#### Instances

Instances details

Show a => Show (Numbered a) Source # |
Instance details Defined in Data.Algorithm.DiffContext Methods showsPrec :: Int -> Numbered a -> ShowS Source # show :: Numbered a -> String Source # showList :: [Numbered a] -> ShowS Source #
Eq a => Eq (Numbered a) Source # |
Instance details Defined in Data.Algorithm.DiffContext Methods (==) :: Numbered a -> Numbered a -> Bool Source # (/=) :: Numbered a -> Numbered a -> Bool Source #
Ord a => Ord (Numbered a) Source # |

Instance details

Defined in Data.Algorithm.DiffContext

Methods

compare :: Numbered a -> Numbered a -> Ordering Source #

(<) :: Numbered a -> Numbered a -> Bool Source #

(<=) :: Numbered a -> Numbered a -> Bool Source #

(>) :: Numbered a -> Numbered a -> Bool Source #

(>=) :: Numbered a -> Numbered a -> Bool Source #

max :: Numbered a -> Numbered a -> Numbered a Source #

min :: Numbered a -> Numbered a -> Numbered a Source #

numbered :: [a] -> [Numbered a] Source #

unnumber :: Numbered a -> a Source #

unNumberContextDiff :: ContextDiff (Numbered a) -> ContextDiff a Source #

If for some reason you need the line numbers stripped from the result of getContextDiff for backwards compatibility.

groupBy' :: (a -> a -> Bool) -> [a] -> [[a]] Source #

A version of `groupBy` that does not assume the argument function is transitive. This is used to partition the `Diff` list into segments that begin and end with matching (`Both`) text, with and have non-matching (`First` and `Second`) text in the middle.

    let notBoth1 a b = not (a == 1 || b == 1) in

    groupBy' notBoth1 [1,1,2,3,1,1,4,5,6,1]
    [[1],[1,2,3,1],[1,4,5,6,1]]

    groupBy notBoth1 [1,1,2,3,1,1,4,5,6,1]
    [[1],[1,2,3],[1],[1,4,5,6],[1]]

In the first result the list is split anywhere there are two adjacent ones, as desired.