|
| 1 | +/* |
| 2 | + * Copyright 2009-2017 java-diff-utils. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.difflib |
| 17 | + |
| 18 | +import com.github.difflib.algorithm.DiffAlgorithmI |
| 19 | +import com.github.difflib.algorithm.DiffAlgorithmListener |
| 20 | +import com.github.difflib.algorithm.DiffException |
| 21 | +import com.github.difflib.algorithm.myers.MyersDiff |
| 22 | +import com.github.difflib.patch.AbstractDelta |
| 23 | +import com.github.difflib.patch.Patch |
| 24 | +import com.github.difflib.patch.PatchFailedException |
| 25 | + |
| 26 | +internal typealias Predicate<T> = (T) -> Boolean |
| 27 | +internal typealias BiPredicate<T,R> = (T, R) -> Boolean |
| 28 | +internal typealias Consumer<T> = (T) -> Unit |
| 29 | +internal typealias Function<T,R> = (T) -> R |
| 30 | + |
| 31 | +/** |
| 32 | + * Implements the difference and patching engine |
| 33 | + * |
| 34 | + * @author [Dmitry Naumenko]([email protected]) |
| 35 | + */ |
| 36 | +object DiffUtils { |
| 37 | + |
| 38 | + /** |
| 39 | + * Computes the difference between the original and revised list of elements with default diff algorithm |
| 40 | + * |
| 41 | + * @param original The original text. Must not be `null`. |
| 42 | + * @param revised The revised text. Must not be `null`. |
| 43 | + * @param progress progress listener |
| 44 | + * @return The patch describing the difference between the original and revised sequences. Never `null`. |
| 45 | + * @throws com.github.difflib.algorithm.DiffException |
| 46 | + */ |
| 47 | +// @Throws(DiffException::class) |
| 48 | + fun <T> diff(original: List<T>, revised: List<T>, progress: DiffAlgorithmListener): Patch<T> { |
| 49 | + return DiffUtils.diff(original, revised, MyersDiff(), progress) |
| 50 | + } |
| 51 | + |
| 52 | +// @Throws(DiffException::class) |
| 53 | + fun <T> diff(original: List<T>, revised: List<T>): Patch<T> { |
| 54 | + return DiffUtils.diff(original, revised, MyersDiff(), null) |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Computes the difference between the original and revised text. |
| 59 | + */ |
| 60 | +// @Throws(DiffException::class) |
| 61 | + fun diff(sourceText: String, targetText: String, |
| 62 | + progress: DiffAlgorithmListener): Patch<String> { |
| 63 | + return DiffUtils.diff( |
| 64 | + sourceText.split("\n".toRegex()).dropLastWhile { it.isEmpty() }, |
| 65 | + targetText.split("\n".toRegex()).dropLastWhile { it.isEmpty() }, progress) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Computes the difference between the original and revised list of elements with default diff algorithm |
| 70 | + * |
| 71 | + * @param source The original text. Must not be `null`. |
| 72 | + * @param target The revised text. Must not be `null`. |
| 73 | + * |
| 74 | + * @param equalizer the equalizer object to replace the default compare algorithm (Object.equals). If `null` |
| 75 | + * the default equalizer of the default algorithm is used.. |
| 76 | + * @return The patch describing the difference between the original and revised sequences. Never `null`. |
| 77 | + */ |
| 78 | +// @Throws(DiffException::class) |
| 79 | + fun <T> diff(source: List<T>, target: List<T>, |
| 80 | + equalizer: BiPredicate<T, T>?): Patch<T> { |
| 81 | + return if (equalizer != null) { |
| 82 | + DiffUtils.diff(source, target, |
| 83 | + MyersDiff(equalizer)) |
| 84 | + } else DiffUtils.diff(source, target, MyersDiff()) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Computes the difference between the original and revised list of elements with default diff algorithm |
| 89 | + * |
| 90 | + * @param original The original text. Must not be `null`. |
| 91 | + * @param revised The revised text. Must not be `null`. |
| 92 | + * @param algorithm The diff algorithm. Must not be `null`. |
| 93 | + * @param progress The diff algorithm listener. |
| 94 | + * @return The patch describing the difference between the original and revised sequences. Never `null`. |
| 95 | + */ |
| 96 | +// @Throws(DiffException::class) |
| 97 | + fun <T> diff(original: List<T>, revised: List<T>, |
| 98 | + algorithm: DiffAlgorithmI<T>, progress: DiffAlgorithmListener?): Patch<T> { |
| 99 | + return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress)) |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Computes the difference between the original and revised list of elements with default diff algorithm |
| 104 | + * |
| 105 | + * @param original The original text. Must not be `null`. |
| 106 | + * @param revised The revised text. Must not be `null`. |
| 107 | + * @param algorithm The diff algorithm. Must not be `null`. |
| 108 | + * @return The patch describing the difference between the original and revised sequences. Never `null`. |
| 109 | + */ |
| 110 | +// @Throws(DiffException::class) |
| 111 | + fun <T> diff(original: List<T>, revised: List<T>, |
| 112 | + algorithm: DiffAlgorithmI<T>): Patch<T> { |
| 113 | + return diff(original, revised, algorithm, null) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Computes the difference between the given texts inline. This one uses the "trick" to make out of texts lists of |
| 118 | + * characters, like DiffRowGenerator does and merges those changes at the end together again. |
| 119 | + * |
| 120 | + * @param original |
| 121 | + * @param revised |
| 122 | + * @return |
| 123 | + */ |
| 124 | +// @Throws(DiffException::class) |
| 125 | + fun diffInline(original: String, revised: String): Patch<String> { |
| 126 | + val origList = ArrayList<String>() |
| 127 | + val revList = ArrayList<String>() |
| 128 | + for (character in original) { |
| 129 | + origList.add(character.toString()) |
| 130 | + } |
| 131 | + for (character in revised) { |
| 132 | + revList.add(character.toString()) |
| 133 | + } |
| 134 | + val patch = DiffUtils.diff(origList, revList) |
| 135 | + for (delta in patch.deltas) { |
| 136 | + delta.source.lines = compressLines(delta.source.lines!!, "") |
| 137 | + delta.target.lines = compressLines(delta.target.lines!!, "") |
| 138 | + } |
| 139 | + return patch |
| 140 | + } |
| 141 | + |
| 142 | + private fun compressLines(lines: List<String>, delimiter: String): List<String> { |
| 143 | + return if (lines.isEmpty()) { |
| 144 | + emptyList<String>() |
| 145 | + } else listOf<String>(lines.joinToString(delimiter)) |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Patch the original text with given patch |
| 150 | + * |
| 151 | + * @param original the original text |
| 152 | + * @param patch the given patch |
| 153 | + * @return the revised text |
| 154 | + * @throws PatchFailedException if can't apply patch |
| 155 | + */ |
| 156 | +// @Throws(PatchFailedException::class) |
| 157 | + fun <T> patch(original: List<T>, patch: Patch<T>): List<T> { |
| 158 | + return patch.applyTo(original) |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Unpatch the revised text for a given patch |
| 163 | + * |
| 164 | + * @param revised the revised text |
| 165 | + * @param patch the given patch |
| 166 | + * @return the original text |
| 167 | + */ |
| 168 | + fun <T> unpatch(revised: List<T>, patch: Patch<T>): List<T> { |
| 169 | + return patch.restore(revised) |
| 170 | + } |
| 171 | +} |
0 commit comments