Diffitic 2.0.1

Diffitic 2.0.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Hironori Ichimiya.



Diffitic 2.0.1

  • By
  • Hironori Ichimiya






Diffitic is a module for detecting differences between two sequences written in Swift.

Usage

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:

  • 0: (.inserted, 0, 0, 0, 1)
  • 1: (.identical, 0, 3, 1, 3)
  • 2: (.replaced, 3, 1, 4, 1)
  • 3: (.identical, 4, 1, 5, 1)

which means,

  • 0: 1 element from index 0 in the right sequence (that is "horse") is inserted at index 0 in the left sequence.
  • 1: 3 elements from 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.
  • 2: 1 element from index 3 in the left sequence ("sparrow") is replaced with 1 element from index 4 in the right sequence ("koala").
  • 3: 1 element from index 4 in the left sequence ("swift") and 1 element from index 5 in the right sequence ("swift") are same.

Requirements

  • Swift 4.0+
  • iOS 8.0+
  • macOS 10.9+
  • watchOS 2.0+
  • tvOS 9.0+

Installation

Swift Package Manager

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"),
    ],
    ...

Author

Hironori Ichimiya, [email protected]

License

Diffitic is available under the MIT license. See the LICENSE file for more info.