Simple two-string merge

120 views
Skip to first unread message

Christopher G. Prince

unread,
May 20, 2016, 12:42:57 PM5/20/16
to Diff Match Patch
For my purposes, I was wanting a textual merge where two strings were intelligently merged together. Seemingly simple enough, but it took me some thinking on how to apply the DiffMatchPatch to achieve this, so thought I'd share. This is my code in Swift:

//

//  DiffMatchPatch+Extras.swift

//  SharedNotes

//

//  Created by Christopher Prince on 5/20/16.

//  Copyright © 2016 Spastic Muffin, LLC. All rights reserved.

//


import Foundation


extension DiffMatchPatch {

    /* Example of the result of calling diff_mainOfOldString:

    diff objects: (

        "Diff(DIFF_EQUAL,\"Hello friend, \")",

        "Diff(DIFF_DELETE,\"there \")",

        "Diff(DIFF_EQUAL,\"is my world\")",

        "Diff(DIFF_INSERT,\"\U00b6What's going on\")"

    )

    

    This method just concatenates all of the diff results, equal, delete, insert etc. together, which creates a simple merge of the two strings.

    */

    func diff_simpleMerge(firstString firstString:String, secondString: String) -> String {

        let diffs = self.diff_mainOfOldString(firstString, andNewString: secondString)

        print("diff objects: \(diffs)")

        

        var concatenatedDiffText:String = ""

        for obj in diffs {

            let diff = obj as! Diff

            concatenatedDiffText += diff.text

        }

        

        return concatenatedDiffText

    }

}

Christopher G. Prince

unread,
May 20, 2016, 12:44:33 PM5/20/16
to Diff Match Patch
Didn't mean to put the "All rights reserved." part in there. Consider that removed.
Reply all
Reply to author
Forward
0 new messages