Skip to content

Commit

Permalink
modification add base
Browse files Browse the repository at this point in the history
  • Loading branch information
wzxha committed May 24, 2018
1 parent 4f19826 commit 829f3a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
14 changes: 13 additions & 1 deletion Sources/Sdifft/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,28 @@ extension Array where Element == Int {
}

public struct Modification {
public enum Base {
case from
case to
}

public let add: [CountableClosedRange<Int>]
public let delete: [CountableClosedRange<Int>]
public let same: [CountableClosedRange<Int>]
public let base: Base

init(from: String, to: String, matrix: Matrix) {
let same =
lcs(from: from, to: to, position: (from.count, to.count), matrix: matrix, same: ([], []))
add = same.to.getChangeRanges(max: to.count - 1)
delete = same.from.getChangeRanges(max: from.count - 1)
self.same = same.to.getSameRanges()
if add.isEmpty {
base = .from
self.same = same.from.getSameRanges()
} else {
base = .to
self.same = same.to.getSameRanges()
}
}
}

Expand Down
37 changes: 26 additions & 11 deletions Tests/SdifftTests/SdifftTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import XCTest
@testable import Sdifft

extension String {
subscript(_ range: CountableClosedRange<Int>) -> String {
let start = index(startIndex, offsetBy: range.lowerBound)
let end = index(startIndex, offsetBy: range.upperBound)
return String(self[start...end])
}
}

class DiffTests: XCTestCase {
func testMatrix() {
assert(
Expand Down Expand Up @@ -127,32 +135,39 @@ class DiffTests: XCTestCase {
assert(diff2.modification.add == [0...0, 2...3])
assert(diff2.modification.delete == [1...1])
assert(diff2.modification.same == [1...1])

let to3 = "A\r\nB\r\nC"
let from3 = "A\r\n\r\nB\r\n\r\nC"
let diff3 = Diff(from: from3, to: to3)
assert(diff3.modification.add == [])
assert(diff3.modification.delete == [1...1, 4...4])
assert(diff3.modification.same == [0...0, 2...3, 5...6])
}

func testString1() {
let to = "abcdhijk"
let from = "bj"
let diff = Diff(from: from, to: to)
assert(
(to as NSString).substring(with: NSRange(diff.modification.add[0])) == "a" &&
(to as NSString).substring(with: NSRange(diff.modification.add[1])) == "cdhi" &&
(to as NSString).substring(with: NSRange(diff.modification.add[2])) == "k" &&
to[diff.modification.add[0]] == "a" &&
to[diff.modification.add[1]] == "cdhi" &&
to[diff.modification.add[2]] == "k" &&
diff.modification.delete.count == 0 &&
(to as NSString).substring(with: NSRange(diff.modification.same[0])) == "b" &&
(to as NSString).substring(with: NSRange(diff.modification.same[1])) == "j")
to[diff.modification.same[0]] == "b" &&
to[diff.modification.same[1]] == "j")
}

func testString2() {
let to = "abcdhijk"
let from = "bexj"
let diff = Diff(from: from, to: to)
assert(
(to as NSString).substring(with: NSRange(diff.modification.add[0])) == "a" &&
(to as NSString).substring(with: NSRange(diff.modification.add[1])) == "cdhi" &&
(to as NSString).substring(with: NSRange(diff.modification.add[2])) == "k" &&
(from as NSString).substring(with: NSRange(diff.modification.delete[0])) == "ex" &&
(to as NSString).substring(with: NSRange(diff.modification.same[0])) == "b" &&
(to as NSString).substring(with: NSRange(diff.modification.same[1])) == "j"
to[diff.modification.add[0]] == "a" &&
to[diff.modification.add[1]] == "cdhi" &&
to[diff.modification.add[2]] == "k" &&
from[diff.modification.delete[0]] == "ex" &&
to[diff.modification.same[0]] == "b" &&
to[diff.modification.same[1]] == "j"
)
}

Expand Down

0 comments on commit 829f3a9

Please sign in to comment.