PDA

View Full Version : Align and pad two similar text files for text comparison



stanlymt
6th May 2016, 06:13
I've to compare and highlight differences between two similar text files. I'm using QT 4.8 on VC 2010 and C++ as the programming language. After researching, I've found the google-diff-match-patch library which is available in QT platform.
https://code.google.com/p/google-diff-match-patch/

Using the library, I've been trying to align both the text files so that similar lines appear on the same line.
I don't find any good way to add newline wherever appropriate on either left or right text so that similar text appear on the same line for side-by-side comparison.

Please suggest the best way based on QT & diff match patch QT library.

for eg:-


QString leftText = "Hello World\nTest1 Wo23\n\nTest4";
QString rightText = "Goodmorning World\n\nTest2 Wo33\nTest5";


After alignment the result should be


leftText = "Hello World\n\nTest1 Wo23\n\nTest4"; //added '\n' in second line to align with similar line in 3rd line
rightText = "Goodmorning World\n\nTest2 Wo33\n\nTest5"; //added '\n' in 5th line to align with similar line in 7th line


Above is simple example, but in real-world I'm dealing with complex left text and right text of two files where similar line may be few lines below or above and empty lines needs to be created in between.