适用于 Google diff-match-patch 库的 Swift 包。
原始代码已经多年没有更新,并且 Objective-C 代码必须在禁用 ARC 的情况下构建。 我创建了这个 Swift 包,以便将此配置更改从我的主 Xcode 项目中抽象出来。
在 Xcode 中,在 Project > Package Dependencies 下添加 DiffMatchPatch。
然后如下所示导入它
import diff_match_patch
获取两个 Swift 字符串之间的差异
let dmp = DiffMatchPatch()
// optionally set a timeout to stop very long processing
dmp.diff_Timeout = TimeInterval(1.0)
let array = dmp.diff_main(ofOldString: string1, andNewString: string2)
// optionally perform a semantic cleanup of diffs
dmp.diff_cleanupSemantic(array)
// cast NSMutableArray to [Diff]
guard let diffs = array as? [Diff] else {
return
}
其中包含对 - (NSMutableArray *)diff_bisectOfOldString:(NSString *)_text1 andNewString:(NSString *)_text2 deadline:(NSTimeInterval)deadline;
的一个小改动,该改动将 CFIndex 缓冲区分配到堆而不是堆栈中。 这似乎修复了在比较大型(超过 500kB)字符串时出现的 EXC_BAD_ACCESS 崩溃问题。
该代码采用与 Google 原始代码相同的 Apache 许可证。