Simple diffing library in pure Swift.
You can use Carthage or Swift Package Manager to install Diff.
Start by importing the package:
import DiffIf there is no difference, the diff will be nil.
diff("Hello", "Hello") // nilFor the sake of brevity, we'll ! the rest of the examples since we know they're different.
let (range, string) = diff("Hello world", "Hello there world")! // range: 6..<6 // string: "there "let (range, string) = diff("Hello there world", "Hello world")! // range: 6..<12 // string: ""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: []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 to Jonathan Clem for the original algorithm and Caleb Davenport for inspiration for the generics implementation and help debugging a few edge cases!