Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
/ Sdifft Public archive
forked from wzxha/Sdifft

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

License

Notifications You must be signed in to change notification settings

alibaba-archive/Sdifft

 
 

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.8%
  • Ruby 4.2%