Skip to content

Conversation

@wmccrthy
Copy link
Contributor

@wmccrthy wmccrthy commented Nov 26, 2025

Adds a diffText battery, that implements a myers-diff esque algorithm using the notion of an edit graph. An edit graph is can be described as:

  • src string along x axis
  • destination string along y axis
  • each edge in the graph represents a diff operation:
    • (n, n) -> (n+1, n) represents a deletion of src[n+1]
    • (n, n) -> (n, n+1) represents an addition of destination[n+1]
    • (n, n) -> (n+1, n+1) exists only if src[n+1] == destination[n+1]
  • The shortest path from (0, 0) -> (len(src), len(destination)) represents the shortest-edit-sequence.

Myers-diff works by greedily following diagonal edges (n,n -> n+1,n+1) but the algorithm is not super intuitive / readable imo.

This implementation stays consistent with the greedy nature but varies slightly in how the graph is traversed. We use a deque to BFS the edit graph in a greedy fashion: whenever a node has a diagonal edge, we push it to the front of the deque, while other edges are always pushed to the back.

I included the deque implementation as a separate battery, since I figure it could provide other use down the line.

Both diffText and deque have solid test coverage.

Here is what the output of examples/diffText.luau looks like:

Screenshot 2025-11-25 at 5 47 41 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant