From 829f3a9665c18c38ad9f8c73a868e393ebbb2e59 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Thu, 24 May 2018 14:43:11 +0800 Subject: [PATCH] modification add base --- Sources/Sdifft/Diff.swift | 14 ++++++++++- Tests/SdifftTests/SdifftTests.swift | 37 ++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Sources/Sdifft/Diff.swift b/Sources/Sdifft/Diff.swift index 85f21a0..4dfa06c 100644 --- a/Sources/Sdifft/Diff.swift +++ b/Sources/Sdifft/Diff.swift @@ -134,16 +134,28 @@ extension Array where Element == Int { } public struct Modification { + public enum Base { + case from + case to + } + public let add: [CountableClosedRange] public let delete: [CountableClosedRange] public let same: [CountableClosedRange] + 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() + } } } diff --git a/Tests/SdifftTests/SdifftTests.swift b/Tests/SdifftTests/SdifftTests.swift index 3fa6475..b8c48b5 100644 --- a/Tests/SdifftTests/SdifftTests.swift +++ b/Tests/SdifftTests/SdifftTests.swift @@ -1,6 +1,14 @@ import XCTest @testable import Sdifft +extension String { + subscript(_ range: CountableClosedRange) -> 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( @@ -127,6 +135,13 @@ 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() { @@ -134,12 +149,12 @@ class DiffTests: XCTestCase { 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() { @@ -147,12 +162,12 @@ class DiffTests: XCTestCase { 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" ) }