# diff

Dark Mode

- Manual
- Standard library
- Index

Search:
Group by:

This module implements an algorithm to compute the diff between two sequences of lines.

A basic example of `diffInt` on 2 arrays of integers:

    import experimental/diff
    echo diffInt([0, 1, 2, 3, 4, 5, 6, 7, 8], [-1, 1, 2, 3, 4, 5, 666, 7, 42])

Another short example of `diffText` to diff strings:

    import experimental/diff
    # 2 samples of text for testing (from "The Call of Cthulhu" by Lovecraft)
    let txt0 = """I have looked upon all the universe has to hold of horror,
    even skies of spring and flowers of summer must ever be poison to me."""
    let txt1 = """I have looked upon all your code has to hold of bugs,
    even skies of spring and flowers of summer must ever be poison to me."""

    echo diffText(txt0, txt1)

- To learn more see Diff on Wikipedia.

# Imports

tables, strutils

# Types

    Item = object
      startA*: int               ## Start Line number in Data A.
      startB*: int               ## Start Line number in Data B.
      deletedA*: int             ## Number of changes in Data A.
      insertedB*: int            ## Number of changes in Data B.

  An Item in the list of differences. Source Edit

# Procs

    proc diffInt(arrayA, arrayB: openArray[int]): seq[Item] {...}{.raises: [], tags: [].}

  Find the difference in 2 arrays of integers.

  `arrayA` A-version of the numbers (usually the old one)

  `arrayB` B-version of the numbers (usually the new one)

  Returns a sequence of Items that describe the differences.

  Source Edit

    proc diffText(textA, textB: string): seq[Item] {...}{.raises: [KeyError], tags: [].}

  Find the difference in 2 text documents, comparing by textlines.

  The algorithm itself is comparing 2 arrays of numbers so when comparing 2 text documents each line is converted into a (hash) number. This hash-value is computed by storing all textlines into a common hashtable so i can find duplicates in there, and generating a new number each time a new textline is inserted.

  `textA` A-version of the text (usually the old one)

  `textB` B-version of the text (usually the new one)

  Returns a seq of Items that describe the differences.

  Source Edit