Giter Club home page Giter Club logo

swiftdiff's Introduction

SwiftDiff

SwiftDiff is a (partial) port of the Google Diff, Match and Patch Library (google-diff-match-patch) to Swift. The Google Diff, Match and Patch Library was originally written by Neil Fraser.

So far only the diff algorithm has been ported. It allows comparing two blocks of plain text and efficiently returning a list of their differences. It supports detecting in-line text differences.

SwiftDiff was updated to Swift 5 and SPM 5.1 by Cyberfun. It was updated to Swift and SPM 5.7 and improved by niklhut.

To use SwiftDiff, add the following package dependency:

.package(url: "https://github.com/niklhut/SwiftDiff.git", from: "1.0.0")

Usage

let text1 = "The quick brown fox jumps over the lazy dog."
let text2 = "That quick brown fox jumped over a lazy dog."

let myDiff = diff(text1: text1, text2: text2)

The diff would look like the following:

[
    .equal(text: "Th"), 
    .delete(text: "e"), 
    .insert(text: "at"), 
    .equal(text: " quick brown fox jump"), 
    .delete(text: "s"), 
    .insert(text: "ed"), 
    .equal(text: " over "), 
    .delete(text: "the"), 
    .insert(text: "a"), 
    .equal(text: " lazy dog.")
]

To find any overlaps between deletions and insertions you can use cleanupSemantic(diffs: [Diff]) or directly call .cleaningUpSemantics() on an array of Diff:

let text3 = "The quick brown fox goes through the forest."
let text4 = "The brown fox quickly goes home."

let myDiff2 = diff(text1: text3, text2: text4).cleaningUpSemantics()

So the diff is cleaned up and looks like the following

[
    .equal(text: "The "), 
    .delete(text: "quick "), 
    .equal(text: "brown fox "), 
    .delete(text: "goes through the forest"), 
    .insert(text: "quickly goes home"), 
    .equal(text: ".")
]

instead of

[
    .equal(text: "The "), 
    .delete(text: "quick "),
    .equal(text: "brown fox "), 
    .insert(text: "quickly "), 
    .equal(text: "goes "), 
    .delete(text: "t"), 
    .equal(text: "h"), 
    .delete(text: "r"), 
    .equal(text: "o"),
    .delete(text: "ugh th"),
    .insert(text: "m"), 
    .equal(text: "e"), 
    .delete(text: " forest"),
    .equal(text: ".")
]

Codable

Since the Diff is Codable it can easily be converted to, for example, JSON:

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted // only for nicer displaying
let jsonData = try jsonEncoder.encode(myDiff2)
let json = String(data: jsonData, encoding: .utf8)
print(json!)

Output:

[
  {
    "equal" : {
      "text" : "The "
    }
  },
  {
    "delete" : {
      "text" : "quick "
    }
  },
  {
    "equal" : {
      "text" : "brown fox "
    }
  },
  {
    "delete" : {
      "text" : "goes through the forest"
    }
  },
  {
    "insert" : {
      "text" : "quickly goes home"
    }
  },
  {
    "equal" : {
      "text" : "."
    }
  }
]

License

SwiftDiff is licensed under the Apache License 2.0 โ€“ see the LICENSE file for details.

The original Google Diff, Match and Patch Library is also licensed under the same license and Copyright (c) 2006 Google Inc.

swiftdiff's People

Contributors

turbolent avatar niklhut avatar atacan avatar aaron-- 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.