Swiftpack.co - Package - Wzxhaha/Sdifft

Sdifft

swift version travis-ci codecov codebeat badge

Using the Myers's Difference Algorithm to compare differences between two equatable element

Example(String)

impoort Sdifft

let source = "b"
let target = "abcd"
let diff = Diff(source: from, target: to)
diff.scripts // [.insert(into: 3), .insert(into: 2), .same(into: 1), .insert(into: 0)]

/// Get diff attributedString
let diffAttributes = 
    DiffAttributes(
        insert: [.backgroundColor: UIColor.green]], 
        delete: [.backgroundColor: UIColor.red], 
        same: [.backgroundColor: UIColor.white]
    )
let attributedString = NSAttributedString(source: source, target: target, attributes: diffAttributes) 

// output ->
// a{green}b{black}cd{green}

Example(Line)

impoort Sdifft
let source = ["Hello"]
let target = ["Hello", "World", "!"]
let attributedString = 
    NSAttributedString(source: source, target: target, attributes: diffAttributes) {
        let string = NSMutableAttributedString(attributedString: string)
        string.append(NSAttributedString(string: "\n"))
        switch script {
        case .delete:
            string.insert(NSAttributedString(string: "- "), at: 0)
        case .insert:
            string.insert(NSAttributedString(string: "+ "), at: 0)
        case .same:
            string.insert(NSAttributedString(string: " "), at: 0)
        }
        return string
    }

// output ->
//    Hello 
//  + World{green}
//  + !{green}

Installation

Swift Package Manager

// Package.swift
let package = Package(
    name: "XXX",
    dependencies: [
        .Package(url: "https://github.com/Wzxhaha/Sdifft", majorVersion: 2)
    ]
)

Carthage

// Cartfile
github "Wzxhaha/Sdifft"

License

Sdifft is released under the MIT license. See LICENSE for details.

Github

link
Stars: 1
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

2.0.0 - Dec 14, 2018

1.4.1 - Jun 7, 2018

1.4.0 - May 30, 2018

  1. add NSAttributedString+Diff.swift
  2. remove enumBase
  3. update Modification

1.3.0 - May 25, 2018

Because LCS is 'bottom-up', so them need be reversed to get the normal sequence

1.2.2 - May 24, 2018