2015-11-03 00:11:55 +03:00
|
|
|
private func unified(patch: Patch<Term>, source: String) -> String {
|
2015-11-02 22:21:34 +03:00
|
|
|
return (patch.state.before.map { "{-\($0)-}" } ?? "")
|
|
|
|
+ (patch.state.after.map { "{+\($0)+}" } ?? "")
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:07:34 +03:00
|
|
|
private func range(patch: Patch<Term>) -> Range<Int>? {
|
2015-11-03 00:07:57 +03:00
|
|
|
return patch.state.after?.extract.range
|
2015-11-03 00:02:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 22:19:10 +03:00
|
|
|
func unified(diff: Diff, before: String, after: String) -> String {
|
2015-11-03 00:02:58 +03:00
|
|
|
return diff.map { (unified($0, source: after), range($0)) }.cata { info, syntax in
|
|
|
|
switch syntax {
|
|
|
|
case .Leaf:
|
|
|
|
return (String(after.utf16[info.1.range]), info.1.range)
|
|
|
|
case let .Indexed(i):
|
|
|
|
var previous = info.1.range.startIndex
|
|
|
|
var out: String = ""
|
|
|
|
for (string, range) in i {
|
2015-11-03 00:07:34 +03:00
|
|
|
if let range = range {
|
|
|
|
out += String(after.utf16[previous..<range.startIndex])
|
|
|
|
previous = range.endIndex
|
|
|
|
}
|
2015-11-03 00:02:58 +03:00
|
|
|
out += string
|
|
|
|
}
|
|
|
|
return (out + String(after.utf16[previous..<info.1.range.endIndex]), info.1.range)
|
|
|
|
case let .Fixed(f):
|
|
|
|
return (f.map { $0.0 }.joinWithSeparator(""), info.1.range)
|
|
|
|
case let .Keyed(k):
|
|
|
|
return (k.values.map { $0.0 }.joinWithSeparator(""), info.1.range)
|
|
|
|
}
|
|
|
|
}.0
|
2015-11-02 22:04:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
import Doubt
|