From 678852785ef2b3002a05d7ea144836ba66785634 Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Mon, 20 Jan 2025 19:06:08 +0100 Subject: [PATCH 1/2] refactor: port changes up to version 4.15 --- .../github/petertrr/diffutils/patch/Patch.kt | 42 ++++++++++++++----- .../diffutils/text/WordDiffSplitter.kt | 3 +- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt index e3c5075..cb5d585 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt @@ -37,40 +37,62 @@ public class Patch(private var conflictOutput: ConflictOutput = ExceptionP } /** - * Apply this patch to the given target. + * Apply this patch to the given target list, returning a new list. * * @return The patched text * @throws PatchFailedException If the patch cannot be applied */ public fun applyTo(target: List): List { val result = target.toMutableList() + applyToExisting(result) + return result + } + + /** + * Apply this patch to the given target list, directly modifying it. + * + * @return The patched text + * @throws PatchFailedException If the patch cannot be applied + */ + @Suppress("MemberVisibilityCanBePrivate") + public fun applyToExisting(target: MutableList) { val it = deltas.listIterator(deltas.size) while (it.hasPrevious()) { val delta = it.previous() - val verifyChunk = delta.verifyAndApplyTo(result) - conflictOutput.processConflict(verifyChunk, delta, result) + val verifyChunk = delta.verifyAndApplyTo(target) + conflictOutput.processConflict(verifyChunk, delta, target) } - - return result } /** - * Restore the text to its original form. Opposite of the [applyTo] method. + * Creates a new list, containing the restored state of the given target list. + * Opposite of the [applyTo] method. * * @param target The given target * @return The restored text */ public fun restore(target: List): List { val result = target.toMutableList() + restoreToExisting(result) + return result + } + + /** + * Restores all changes within the given target list. + * Opposite of the [applyToExisting] method. + * + * @param target The given target + * @return The restored text + */ + @Suppress("MemberVisibilityCanBePrivate") + public fun restoreToExisting(target: MutableList) { val it = deltas.listIterator(deltas.size) while (it.hasPrevious()) { val delta = it.previous() - delta.restore(result) + delta.restore(target) } - - return result } /** @@ -102,7 +124,7 @@ public class Patch(private var conflictOutput: ConflictOutput = ExceptionP var startRevised = 0 val adjustedChanges = if (includeEquals) { - changes.sortedBy { it.startOriginal } + changes.sortedBy(Change::startOriginal) } else { changes } diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/text/WordDiffSplitter.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/text/WordDiffSplitter.kt index bfa249b..65a70f9 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/text/WordDiffSplitter.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/text/WordDiffSplitter.kt @@ -16,7 +16,8 @@ package io.github.petertrr.diffutils.text // As a "global" variable to avoid re-compiling the regex each time -private val defaultPattern = Regex("\\s+|[,.\\[\\](){}/\\\\*+\\-#]") +@Suppress("RegExpRedundantEscape") // To be PCRE compliant! +private val defaultPattern = Regex("""\s+|[,.\[\](){}\/\\*+\-#<>;:&']+""") /** * Splitting lines by word to achieve word by word diff checking. From a480252c9d8c1dd836a624be3b1490224248d5fc Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Mon, 20 Jan 2025 19:08:12 +0100 Subject: [PATCH 2/2] docs: update and cleanup the readme --- README.md | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 1b7b235..1a64e04 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,38 @@ # kotlin-multiplatform-diff ![Build and test](https://github.com/petertrr/kotlin-multiplatform-diff/workflows/Build%20and%20test/badge.svg) +[![Codecov](https://codecov.io/gh/petertrr/kotlin-multiplatform-diff/branch/main/graph/badge.svg)](https://codecov.io/gh/petertrr/kotlin-multiplatform-diff) [![License](https://img.shields.io/github/license/petertrr/kotlin-multiplatform-diff)](https://github.com/petertrr/kotlin-multiplatform-diff/blob/main/LICENSE) -[![codecov](https://codecov.io/gh/petertrr/kotlin-multiplatform-diff/branch/main/graph/badge.svg)](https://codecov.io/gh/petertrr/kotlin-multiplatform-diff) - -[![Releases](https://img.shields.io/github/v/release/petertrr/kotlin-multiplatform-diff)](https://github.com/petertrr/kotlin-multiplatform-diff/releases) [![Maven Central](https://img.shields.io/maven-central/v/io.github.petertrr/kotlin-multiplatform-diff)](https://mvnrepository.com/artifact/io.github.petertrr) [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) +[![Kotlin](https://img.shields.io/badge/kotlin-1.9.24-blue.svg?logo=kotlin)](http://kotlinlang.org) This is a port of [java-diff-utils](https://github.com/java-diff-utils/java-diff-utils) to Kotlin with multiplatform support. -All credit for the implementation goes to original authors. +All credit for the implementation goes to the original authors. ## Features -All features from version `4.12` of the original library are present, except for: +All features from version `4.15` of the original library are present, except for: - fuzzy patches - unified diff, which heavily uses file read/write and therefore needs a more complicated rewrite - diff-utils-jgit, which uses JVM-only JGit -Please refer to the original guides for more information. - -## Supported Platforms +Refer to the [original wiki][1] for more information. -Currently, artifacts for the following platforms are supported: +## Supported platforms - JVM -- JS (both browser and Node.js) -- WebAssembly (JS and WASI) +- JS (browser and Node.js) +- WebAssembly (WASM/JS and WASM/WASI) - Native -The supported Native targets are (following the Kotlin/Native [target support guidelines][1]): +Supported Native targets are (following the Kotlin/Native [target support guidelines][2]): -| Tier 1 | Tier 2 | Tier 3 | -|:------------------|:------------------|:---------| -| macosX64 | linuxX64 | mingwX64 | -| macosArm64 | linuxArm64 | | +| Tier 1 | Tier 2 | Tier 3 | +|:-----------|:-----------|:---------| +| macosX64 | linuxX64 | mingwX64 | +| macosArm64 | linuxArm64 | | -[1]: https://kotlinlang.org/docs/native-target-support.html +[1]: https://github.com/java-diff-utils/java-diff-utils/wiki +[2]: https://kotlinlang.org/docs/native-target-support.html