Created
July 12, 2018 17:00
-
-
Save vorobeij/7ef286ae06d92ec5ddbeafa6a7302fa6 to your computer and use it in GitHub Desktop.
Diff string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class StringDifference(sw: String, sn: String, cursorStartBefore: Int = 0, cursorEndBefore: Int = 0, cursorStartAfter: Int = 0, cursorEndAfter: Int = 0) { | |
| var start: Int = 0 | |
| var count: Int = 0 | |
| var after: Int = 0 | |
| var added: Int = 0 | |
| get() = if (count == 0) after else 0 | |
| var removed = 0 | |
| get() = if (after == 0) count else 0 | |
| var replaced = 0 | |
| get() = if (count != 0 && after != 0) after else 0 | |
| var noDiff = false | |
| get() = count == 0 && after == 0 | |
| init { | |
| var end = 0 | |
| var lw = sw.length - 1 | |
| var ln = sn.length - 1 | |
| val min = Math.min(lw, ln) + 1 | |
| for (i in cursor..min) { | |
| start = i | |
| if (i >= min) break | |
| if (sw[i] != sn[i]) { | |
| break | |
| } | |
| } | |
| for (i in 0..min) { | |
| end = i | |
| if (i >= min) break | |
| if (sw[lw - i] != sn[ln - i]) break | |
| } | |
| // "x", "xxx" | |
| if (ln > lw) { | |
| val endFromStart = ln - end + 1 | |
| if (endFromStart < start) { | |
| val s = Math.min(endFromStart, start) | |
| val e = Math.max(endFromStart, start) | |
| start = s | |
| end = e | |
| } | |
| } | |
| count = Math.max(lw - ln, 0) | |
| after = Math.max(ln - lw, 0) | |
| val lenDiff = ln - lw | |
| // if (after < lenDiff) after = lenDiff | |
| // if (count < -lenDiff) count = -lenDiff | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| fun addition_isCorrect() { | |
| testDiff("x", "xxx", 1, 2, 0) | |
| testDiff("jj", "jjj", 1, 2, 0) | |
| testDiff("xxxxxxxxx", "xxxxxxxxxxxxxx", 3, 5, 0) | |
| testDiff("ttttt", "tttttttt", 1, 3, 0) | |
| testDiff("xxx", "x", 1, 0, 2) | |
| testDiff("f", "fafa", 1, 3, 0) | |
| testDiff("@Serg", "@@Serg", 0, 1, 0) | |
| testDiff("@Serg", " @@Serg", 0, 2, 0) | |
| testDiff("@S", "@S @", 2, 2, 0) | |
| testDiff("x @Serg", "x @@Serg", 2, 1, 0) | |
| testDiff("@Sergey Petrov ", "@Sergey Petrov g", 15, 1, 0) | |
| } | |
| fun testDiff(s1: String, s2: String, strt: Int, add: Int, rem: Int) { | |
| println("add \"$s1\" \"$s2\"") | |
| val diff = StringDifference(s1, s2) | |
| with(diff) { println("added=${added}($add), removed=${removed}($rem), replaced=${replaced}, start=${start}($strt), count=${count}, after=${after}") } | |
| // assertTrue(strt == diff.start) | |
| // assertTrue(diff.count >= 0) | |
| // assertTrue(diff.after >= 0) | |
| // assertTrue(diff.added == add) | |
| // assertTrue(diff.removed == rem) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment