TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Oct 2017 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✓ |
Maintained by Hironori Ichimiya.
Diffitic is a module for detecting differences between two sequences written in Swift.
This module provides a function diff
.
public func diff(leftCount: Int,
rightCount: Int,
equalityChecker: (Int, Int) -> Bool)
-> [(DiffType, Int, Int, Int, Int)]
It takes two integer parameters and a closure parameter.
Two integers indicate the length of the "left" and "right" sequences, respectively.
A closure takes two indices of the "left" and "right" sequence, and then it returns whether the two elements specified by indices is same or not.
The diff
doesn't care what type of data the both sequences actually are.
Here is an example that sequences are arrays of strings.
import Foundation
import Diffitic
let left = ["dog", "cat", "cow", "sparrow", "swift"]
let right = ["horse", "dog", "cat", "cow", "koala", "swift"]
let result = diff(leftCount: left.count, rightCount: right.count) { (leftIndex, rightIndex) in
return left[leftIndex] == right[rightIndex]
}
The return value of the diff
function is an array of segments.
One segment is represented as a tuple of five values: type
, leftIndex
, leftCount
, rightIndex
and rightCount
.
The leftIndex
and the leftCount
point to a part of the left sequence.
The same applies to the rightIndex
and the rightCount
.
The type
represents a type of this segment.
type |
Description |
---|---|
.identical |
The both parts of the sequences are same. |
.inserted |
The right part is inserted. |
.deleted |
The left part is deleted. |
.replaced |
The left part is replaced with the right part. |
In this example, the result
consists of these four segments:
(.inserted, 0, 0, 0, 1)
(.identical, 0, 3, 1, 3)
(.replaced, 3, 1, 4, 1)
(.identical, 4, 1, 5, 1)
which means,
"horse"
) is inserted at index 0 in the left sequence."dog"
, "cat"
and "cow"
) and 3 elements from index 1 in the right sequence (also "dog"
, "cat"
and "cow"
) are same."sparrow"
) is replaced with 1 element from index 4 in the right sequence ("koala"
)."swift"
) and 1 element from index 5 in the right sequence ("swift"
) are same.Diffitic is available through Swift Package Manager.
To install it, add dependency to your Package.swift
file like following:
import PackageDescription
let package = Package(
name: "Hello",
dependencies: [
.package(url: "https://github.com/hironytic/Diffitic.git", from: "2.0.0"),
],
...
Hironori Ichimiya, [email protected]
Diffitic is available under the MIT license. See the LICENSE file for more info.