gliff

gliff — A text diffing library for Gleam.

Provides Myers and Patience diff algorithms, unified diff formatting, patch application, semantic cleanup, and inline highlighting.

Values

pub fn apply_patch(
  text: String,
  edits: List(types.Edit),
) -> Result(String, String)

Apply edit operations to a text string to produce the target text.

The fundamental property is: apply_patch(old, diff(old, new)) == Ok(new)

Returns an error if the text doesn’t match the expected content described by the Equal and Delete operations.

pub fn cleanup_semantic(
  edits: List(types.Edit),
) -> List(types.Edit)

Eliminate trivial equalities and merge adjacent edits.

Removes small Equal sections that are surrounded by larger changes, absorbing them into the Delete and Insert operations. This produces fewer, larger edit blocks that are easier to read.

pub fn cleanup_semantic_lossless(
  edits: List(types.Edit),
) -> List(types.Edit)

Shift edit boundaries to natural positions without changing semantics.

Moves edit boundaries to align with word boundaries, sentence endings, or blank lines based on a scoring system. The resulting diff applies identically but reads more naturally.

pub fn default_config() -> types.DiffConfig

Create a default diff configuration.

Returns DiffConfig(algorithm: Myers, cleanup: NoCleanup, max_iterations: 0) where max_iterations: 0 means unlimited computation.

pub fn diff(old: String, new: String) -> List(types.Edit)

Compute a line-level diff between two strings using the Myers algorithm.

Returns a list of edit operations (Equal, Insert, Delete) where each operation contains one or more contiguous lines.

pub fn diff_chars(old: String, new: String) -> List(types.Edit)

Compute a character-level diff between two strings using the Myers algorithm.

Each element in the returned edit list represents one or more contiguous grapheme clusters that share the same edit type.

pub fn diff_chars_with(
  old: String,
  new: String,
  config: types.DiffConfig,
) -> types.DiffResult

Compute a character-level diff with full configuration.

Same as diff_with but operates on grapheme clusters instead of lines.

pub fn diff_myers(old: String, new: String) -> List(types.Edit)

Compute a line-level diff using the Myers algorithm (explicit alias for diff).

pub fn diff_patience(
  old: String,
  new: String,
) -> List(types.Edit)

Compute a line-level diff using the Patience algorithm.

Patience diff anchors on lines that appear exactly once in both texts, then recursively diffs the gaps. This often produces more readable output for code changes where blocks are reordered.

pub fn diff_with(
  old: String,
  new: String,
  config: types.DiffConfig,
) -> types.DiffResult

Compute a line-level diff with full configuration.

Allows selecting the algorithm, cleanup mode, and iteration budget. Returns Complete if the diff finished normally, or Truncated if the iteration budget was exceeded (in which case a crude but correct fallback diff is returned).

pub fn diff_words(old: String, new: String) -> List(types.Edit)

Compute a word-level diff between two strings.

Tokenizes input by whitespace boundaries (preserving whitespace as separate tokens), then diffs the token sequences. Each edit contains one or more word/whitespace tokens.

pub fn from_unified(
  input: String,
) -> Result(List(types.Hunk), String)

Parse a unified diff string into a list of hunks.

Accepts the standard format produced by diff -u or to_unified. Returns an error if the input is malformed.

pub fn inline_highlight(
  edits: List(types.Edit),
) -> List(types.InlineEdit)

Enrich line-level edits with character-level change highlighting.

For adjacent Delete/Insert pairs, computes a character-level sub-diff and returns spans marking which characters actually changed. Equal edits pass through as InlineEqual.

pub fn similarity(edits: List(types.Edit)) -> Float

Compute the similarity ratio between two texts from their diff result.

Returns a value between 0.0 (completely different) and 1.0 (identical). Calculated as 2 * matching_elements / total_elements.

pub fn to_unified(
  edits: List(types.Edit),
  old_name old_name: String,
  new_name new_name: String,
) -> String

Format a list of edits as a unified diff string.

The output matches the format produced by diff -u, with 3 lines of context around each change. old_name and new_name appear in the --- and +++ header lines.

Search Document