Giter Site home page Giter Site logo

diff's Introduction

diff

Nim implementation of Python difflib's sequence matcher

diff is a library for finding the differences between two sequences.

The sequences can be of lines, strings (e.g., words), characters, bytes, or of any custom β€œitem” type so long as it implements == and hash().

For other Nim code see http://www.qtrac.eu/sitemap.html#foss

Examples

For example, this code:

let a = ("Tulips are yellow,\nViolets are blue,\nAgar is sweet,\n" &
         "As are you.").split('\n')
let b = ("Roses are red,\nViolets are blue,\nSugar is sweet,\n" &
         "And so are you.").split('\n')
for span in spanSlices(a, b):
  case span.tag
  of tagReplace:
    for text in span.a:
      echo("- ", text)
    for text in span.b:
      echo("+ ", text)
  of tagDelete:
    for text in span.a:
      echo("- ", text)
  of tagInsert:
    for text in span.b:
      echo("+ ", text)
  of tagEqual:
    for text in span.a:
      echo("= ", text)

produces this output:

- Tulips are yellow,
+ Roses are red,
= Violets are blue,
- Agar is sweet,
- As are you.
+ Sugar is sweet,
+ And so are you.

If you need indexes rather than subsequences themselves, use spans(a, b).

To skip the same subsequences pass skipEqual = true and for tagEqual use: of tagEqual: doAssert(false).

See also tests/test.nim.

License

diff is free open source software (FOSS) licensed under the Apache License, Version 2.0.

diff's People

Contributors

mark-summerfield avatar ynfle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.