Giter Club home page Giter Club logo

diff's Introduction

Diff

Simple diffing library in pure Swift.

Installing

You should use Swift Package Manager to install Diff. You will need snapshot 2016-04-12 or later to use Diff.

Usage

Start by importing the package:

import Diff

Same

If there is no difference, the diff will be nil.

diff("Hello", "Hello") // nil

For the sake of brevity, we'll ! the rest of the examples since we know they're different.

Insert

let (range, string) = diff("Hello world", "Hello there world")!
// range: 6..<6
// string: "there "

Remove

let (range, string) = diff("Hello there world", "Hello world")!
// range: 6..<12
// string: ""

Other Types

Diff can diff any array. Here's an array of things that conform to Equatable:

let (range, replacement) = diff([1, 2, 3], [1, 2, 3, 4])!
// range: 3..<3
// replacement: [4]

You can even use arrays of anything as long as you can compare them:

let before: [Foo] = [a, b]
let after: [Foo] = [b]
let (range, replacement) = diff(before, after, compare: Foo.compare)!
// range: 0..<1
// replacement: []

Development

If you want to contribute to Diff, please write a test.

Building and running the tests locally with SPM is easy:

$ git clone https://github.com/soffes/diff
$ cd diff
$ swift build
$ swift test

Thanks

Thanks to Jonathan Clem for the original algorithm and Caleb Davenport for inspiration for the generics implementation and help debugging a few edge cases!

diff's People

Contributors

soffes avatar

Watchers

 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.