Skip to content

DiffRow alignment does not use the optimal path #227

@zhaohuihua

Description

@zhaohuihua

Describe the bug
During usage, I found that when displaying comparison results in a left‑right structure, DiffRow merges non‑optimally matched OldLine and NewLine into the same row.

To Reproduce

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import com.github.difflib.text.DiffRow;
import com.github.difflib.text.DiffRowGenerator;

/**
 * Line text compare test
 * 
 * @author zhaohuihua
 * @version 20260316
 */
public class LineCompareTest {

    public static void main(String[] args) throws IOException {
        List<String> aTexts = Arrays.asList(
                "Chapter 1 Definitions",
                "Chapter 2 Risk Warnings and Explanations",
                "Chapter 3 Issuance Terms",
                "Chapter 4 Use of Raised Funds",
                "Chapter 5 Basic Information of Issuer",
                "Chapter 6 Financial Status of Issuer",
                "Chapter 7 Credit Status of Issuer",
                "Chapter 8 Credit Enhancement of Debt Financing Instruments",
                "Chapter 9 Tax Items",
                "Chapter 10 Active Debt Management"
        );
        List<String> bTexts = Arrays.asList(
                "Chapter 1 Definitions",
                "Chapter 2 Risk Warnings and Explanations",
                "Chapter 3 Issuance Terms",
                "Chapter 4 Basic Information of the Company",
                "Chapter 5 Financial Status of the Company",
                "Chapter 6 Credit Status of the Company",
                "Chapter 7 Credit Enhancement of Debt Financing Instruments",
                "Chapter 8 Holder Meeting Mechanism",
                "Chapter 9 Use of Raised Funds",
                "Chapter 10 Tax Items"
        );
        System.out.println("[aTexts] compare to [bTexts]:");
        textLineCompare(aTexts, bTexts);
//        System.out.println();
//        System.out.println();
//        System.out.println("[bTexts] compare to [aTexts]:");
//        textLineCompare(bTexts, aTexts);
    }
    
    private static void textLineCompare(List<String> oldTexts, List<String> newTexts) {
        DiffRowGenerator generator = DiffRowGenerator.create()
                .showInlineDiffs(true)
                .inlineDiffByWord(true)
                // .mergeOriginalRevised(true)
                // .reportLinesUnchanged(true)
                .oldTag((tag, f) -> f ? "<old>" : "</old>")
                .newTag((tag, f) -> f ? "<new>" : "</new>")
                .build();

        List<DiffRow> rows = generator.generateDiffRows(oldTexts, newTexts);

        System.out.println("------------------------------------------------------------");
        int li = 0, ri = 0;
        for (DiffRow row : rows) {
            String left = row.getOldLine();
            String right = row.getNewLine();
            if (row.getTag() == DiffRow.Tag.EQUAL) {
                li++;
                ri++;
                System.out.printf("%-6s %4s %4s    %s\n", row.getTag(), li, ri, left);
            } else if (row.getTag() == DiffRow.Tag.INSERT) {
                ri++;
                System.out.printf("%-6s      %4s    %s\n", row.getTag(), ri, right);
            } else if (row.getTag() == DiffRow.Tag.DELETE) {
                li++;
                System.out.printf("%-6s %4s         %s\n", row.getTag(), li, left);
            } else {
                li++;
                ri++;
                System.out.printf("%-6s %4s         %s\n", row.getTag(), li, left);
                System.out.printf("%16s    %s\n", ri, right);
            }
            System.out.println("------------------------------------------------------------");
        }
    }
}

Expected behavior
[aTexts] compare to [bTexts]:
Image
[bTexts] compare to [aTexts]:
Image

System

  • Java version 8
  • Version 4.16

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions