Skip to content

Instantly share code, notes, and snippets.

@aboisvert
Created September 28, 2017 14:57
Show Gist options
  • Select an option

  • Save aboisvert/2daf9ed487214d810e0689d3e3a3714f to your computer and use it in GitHub Desktop.

Select an option

Save aboisvert/2daf9ed487214d810e0689d3e3a3714f to your computer and use it in GitHub Desktop.

Revisions

  1. aboisvert created this gist Sep 28, 2017.
    45 changes: 45 additions & 0 deletions fastdiff.nim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    import os, sets, hashes, strutils, sequtils


    type
    HString = object
    str: string
    hash: Hash

    proc hash*(str: HString): Hash =
    str.hash

    proc toHString*(str: string): HString =
    result.hash = hash(str)
    shallowCopy(result.str, str)

    converter toString*(str: HString): string =
    str.str

    if paramCount() != 2:
    echo "mydiff file1 file2"
    quit()

    proc main(): void =
    let old_lines = readFile(paramStr(1)).splitLines().mapIt(it.toHString)
    let new_lines = readFile(paramStr(2)).splitLines().mapIt(it.toHString)

    let old_lines_set = toSet(old_lines)
    let new_lines_set = toSet(new_lines)

    let old_added = old_lines_set - new_lines_set
    let old_removed = new_lines_set - old_lines_set

    for line in old_lines:
    if line in old_added:
    echo "-", line.strip()
    elif line in old_removed:
    echo "+", line.strip()

    for line in new_lines:
    if line in old_added:
    echo "-", line.strip()
    elif line in old_removed:
    echo "+", line.strip()

    main()