### Diff Result Example Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Illustrates the structure of the diff output, showing equal, deleted, and inserted text segments. ```swift [ .equal(text: "Th"), .delete(text: "e"), .insert(text: "at"), .equal(text: " quick brown fox jump"), .delete(text: "s"), .insert(text: "ed"), .equal(text: " over "), .delete(text: "the"), .insert(text: "a"), .equal(text: " lazy dog.") ] ``` -------------------------------- ### Cleaned Diff Result Example Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Shows the result of a semantically cleaned diff, presenting more coherent text changes. ```swift [ .equal(text: "The "), .delete(text: "quick "), .equal(text: "brown fox "), .delete(text: "goes through the forest"), .insert(text: "quickly goes home"), .equal(text: ".") ] ``` -------------------------------- ### JSON Output of Diff Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Example JSON representation of a semantically cleaned diff, showing the structure of encoded diff data. ```json [ { "equal" : { "text" : "The " } }, { "delete" : { "text" : "quick " } }, { "equal" : { "text" : "brown fox " } }, { "delete" : { "text" : "goes through the forest" } }, { "insert" : { "text" : "quickly goes home" } }, { "equal" : { "text" : "." } } ] ``` -------------------------------- ### Original Diff Before Cleanup Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Displays the raw diff output before semantic cleanup, highlighting finer-grained, less readable changes. ```swift [ .equal(text: "The "), .delete(text: "quick "), .equal(text: "brown fox "), .insert(text: "quickly "), .equal(text: "goes "), .delete(text: "t"), .equal(text: "h"), .delete(text: "r"), .equal(text: "o"), .delete(text: "ugh th"), .insert(text: "m"), .equal(text: "e"), .delete(text: " forest"), .equal(text: ".") ] ``` -------------------------------- ### Basic Text Diff Calculation Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Compares two strings and returns a list of differences. This is the primary function for finding text variations. ```swift let text1 = "The quick brown fox jumps over the lazy dog." let text2 = "That quick brown fox jumped over a lazy dog." let myDiff = diff(text1: text1, text2: text2) ``` -------------------------------- ### Add SwiftDiff Package Dependency Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Add this package dependency to your Swift project to use SwiftDiff. ```swift .package(url: "https://github.com/niklhut/SwiftDiff.git", from: "1.0.0") ``` -------------------------------- ### Encode Diff to JSON Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Converts a `Diff` object to JSON format using `JSONEncoder`. This is useful for storing or transmitting diff data. ```swift let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .prettyPrinted // only for nicer displaying let jsonData = try jsonEncoder.encode(myDiff2) let json = String(data: jsonData, encoding: .utf8) print(json!) ``` -------------------------------- ### Semantic Diff Cleanup Source: https://github.com/niklhut/swiftdiff/blob/master/README.md Cleans up diffs to better represent semantic changes by merging adjacent deletions and insertions. Use `.cleaningUpSemantics()` for this. ```swift let text3 = "The quick brown fox goes through the forest." let text4 = "The brown fox quickly goes home." let myDiff2 = diff(text1: text3, text2: text4).cleaningUpSemantics() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.