Skip to content

Character-level diff misalignment in DiffRowGenerator for single-line strings​ #213

@dcsmf

Description

@dcsmf

When using DiffRowGenerator with inlineDiffByWord(false) for character-level diff, the marked differences are not correctly aligned. Specifically, when comparing "aaaab"and "bbbbb", the output is:

Old: aaaab(correct)
New: bbbbb(incorrect, expected bbbbb)

This suggests that the diff algorithm is not strictly aligning changes by character position.

Demo code:

import com.github.difflib.DiffUtils;
import com.github.difflib.algorithm.myers.MyersDiff;
import com.github.difflib.text.DiffRow;
import com.github.difflib.text.DiffRowGenerator;
import java.util.List;

public class DiffExample {
  public static void main(String[] args) {
      DiffRowGenerator generator = DiffRowGenerator.create()
              .showInlineDiffs(true)
              .inlineDiffByWord(false)
              .ignoreWhiteSpaces(false)
              .oldTag(f -> "~")
              .newTag(f -> "**")
              .build();

      List<DiffRow> rows = generator.generateDiffRows(
              List.of("aaaab"),
              List.of("bbbbb")
      );

      System.out.println("Old: " + rows.get(0).getOldLine()); // Actual: ~aaaa~b
      System.out.println("New: " + rows.get(0).getNewLine()); // Actual: b**bbbb** (Expected: **bbbb**b)
  }
}

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