Skip to content

DiffKotlin is a Kotlin Multiplatform library that provides efficient diffing utilities for comparing lists and collections, powered by the Myers diff algorithm for optimal change detection.

Notifications You must be signed in to change notification settings

baole/diff-kotlin

Repository files navigation

diff-kotlin

A Kotlin Multiplatform (KMP) library for computing text diffs, supporting multiple algorithms including Myers and Myers with linear space optimization. Designed for JVM, Android, iOS, and other Kotlin targets. No additional dependencies required.

This library is a Kotlin port of java-diff-utils.

Features

  • Kotlin Multiplatform (KMP) support: JVM, Android, iOS, and more
  • Efficient diff computation for text and sequences
  • Multiple algorithms: Myers, Myers with linear space
  • Extensible via factory and builder patterns
  • Simple DSL for diff operations
  • Well-tested and production-ready
  • No additional dependencies (other than Kotlin stdlib)

Installation

Add the following to your build.gradle.kts:

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.baole:diff-kotlin:<latest-version>")
}

Replace <latest-version> with the version published on Maven Central.

Usage

Computing Diff

import io.github.diff.generatePatch
import io.github.diff.Delta

val patch = generatePatch {
    this.original = listOf("line1", "line2", "line3", "line4", "line5")
    this.revised = listOf("line1", "line3", "line4 modified", "line5", "line6")
}

for (delta: Delta<String> in patch.getDeltas()) {
    println(delta)
}

Applying Patch

import io.github.diff.PatchFailedException
import io.github.diff.patch

try {
    val result = patch.patch(original)
    println(result.joinToString("\n"))
} catch (e: PatchFailedException) {
    e.printStackTrace()
}

Algorithms

  • MyersDiff: Standard Myers diff algorithm
  • MyersDiffWithLinearSpace: Optimized for linear space usage

You can select the algorithm when generating patches.

Code Quality

This project uses:

  • ktlint for code formatting and style checking
  • Comprehensive test coverage with MockK for mocking

Run code quality checks:

./gradlew ktlintCheck

Auto-fix formatting issues:

./gradlew ktlintFormat

Contributing

Contributions are welcome! Please open issues or submit pull requests.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

About

DiffKotlin is a Kotlin Multiplatform library that provides efficient diffing utilities for comparing lists and collections, powered by the Myers diff algorithm for optimal change detection.

Topics

Resources

Stars

Watchers

Forks