Sign in

    diff

    A diff algorithm library for MoonBit

    diff
    Download zip
    Author
    Version
    0.2.1
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    38

    No dependencies

    #Diff

    A diffing library for MoonBit.

    #Features

    This library provides several diffing algorithms and hooks to generate and process differences between sequences.

    #Core Algorithms

    • Myers' Algorithm: A classic and efficient diffing algorithm with a time complexity of O((N+M)D) and space complexity of O(N+M), where N and M are the lengths of the sequences and D is the number of differences. It finds a guaranteed shortest edit script.
    • Patience Algorithm: An algorithm designed to produce more human-readable diffs, especially for code or text with a lot of noise or moved blocks. It works by finding unique common elements (like a Longest Common Subsequence) to anchor the diff, and then recursively diffs the sections between these anchors.

    #Diff Hooks

    Diff hooks are used to process the raw output of the diffing algorithms. You can chain them to achieve different output formats.

    • Capture: A basic hook that captures the raw sequence of Equal, Insert, and Delete operations generated by a diffing algorithm.
    • Replace: A hook that builds upon Capture. It processes the operation list to merge adjacent Delete and Insert operations into a more semantic Replace operation.
    • Compact: An advanced hook that produces a more concise and readable diff. It intelligently shifts Insert and Delete operations to expand adjacent Equal blocks, making the final output as clean and minimal as possible. This is especially effective for line-by-line diffs of text files where changes might be slightly offset.

    #High-Level API

    The library also exposes simple-to-use functions for common use cases, such as diff_line for comparing arrays of strings (lines of a file).

    #Examples

    // diff array let a = [1, 2, 3, 4, 5, 8, 9] let b = [1, 2, 0, 4, 5, 6, 7] let diffs = diff(a, b) println(diffs) // diff lines let old = ["0", "1", "2", "3", "4", "5"] let new = ["9", "1", "2", "5", "6", "7"] print_diff_line(old, new)

    #Reference

    diff

    fn[T : Eq] diff(old : Array[T], new : Array[T]) -> Array[
    DiffOp
    ]
    Main entry point for standard diffing logic using Myers algorithm with Replace hook.

    diff_line

    fn diff_line(old : Array[String], new : Array[String]) -> Array[
    DiffOp
    ]
    Computes the difference between two arrays of strings, line by line.

    diff_str

    fn diff_str(old : String, new : String) -> Array[
    DiffOp
    ]
    Computes the difference between two strings, character by character.

    diff_with_compact

    fn[T : Eq] diff_with_compact(old : Array[T], new : Array[T]) -> Array[
    DiffOp
    ]
    to produce a more readable and concise result.
    fn[T : Eq + Show] print_diff_line(old : Array[T], new : Array[T]) -> Unit
    Computes the diff between two arrays and prints the result in a line-by-line format.
    fn[T : Show] print_diff_ops(old : Array[T], new : Array[T], ops : Array[
    DiffOp
    ]) -> Unit
    Prints a formatted diff to the console based on a list of DiffOp.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io