Skip to content

Commit 4600887

Browse files
committed
refactor: do a general cleanup
1 parent 2e5d985 commit 4600887

File tree

11 files changed

+53
-73
lines changed

11 files changed

+53
-73
lines changed

src/commonMain/kotlin/io/github/petertrr/diffutils/text/StringUtils.kt renamed to src/commonMain/kotlin/io/github/petertrr/diffutils/StringUtils.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,57 +18,57 @@
1818
*/
1919
@file:JvmName("StringUtils")
2020

21-
package io.github.petertrr.diffutils.text
21+
package io.github.petertrr.diffutils
2222

2323
import kotlin.jvm.JvmName
2424

2525
/**
2626
* Replaces all opening and closing tags (`<` and `>`)
2727
* with their escaped sequences (`&lt;` and `&gt;`).
2828
*/
29-
internal fun htmlEntities(str: String): String =
30-
str.replace("<", "&lt;").replace(">", "&gt;")
29+
internal fun String.htmlEntities(): String =
30+
replace("<", "&lt;").replace(">", "&gt;")
3131

3232
/**
3333
* Normalizes a string by escaping some HTML meta characters
3434
* and replacing tabs with 4 spaces each.
3535
*/
36-
internal fun normalize(str: String): String =
37-
htmlEntities(str).replace("\t", " ")
36+
internal fun String.normalize(): String =
37+
htmlEntities().replace("\t", " ")
3838

3939
/**
40-
* Wrap the text with the given column width
40+
* Wrap the text with the given column width.
4141
*/
42-
internal fun wrapText(line: String, columnWidth: Int): String {
42+
internal fun String.wrapText(columnWidth: Int): String {
4343
require(columnWidth >= 0) { "Column width must be greater than or equal to 0" }
4444

4545
if (columnWidth == 0) {
46-
return line
46+
return this
4747
}
4848

49-
val length = line.length
50-
val delimiter = "<br/>".length
49+
val length = length
50+
val delimiterLength = "<br/>".length
5151
var widthIndex = columnWidth
52-
val b = StringBuilder(line)
52+
val sb = StringBuilder(this)
5353
var count = 0
5454

5555
while (length > widthIndex) {
56-
var breakPoint = widthIndex + delimiter * count
56+
var breakPoint = widthIndex + delimiterLength * count
5757

58-
if (b[breakPoint - 1].isHighSurrogate() && b[breakPoint].isLowSurrogate()) {
58+
if (sb[breakPoint - 1].isHighSurrogate() && sb[breakPoint].isLowSurrogate()) {
5959
// Shift a breakpoint that would split a supplemental code-point.
6060
breakPoint += 1
6161

62-
if (breakPoint == b.length) {
62+
if (breakPoint == sb.length) {
6363
// Break before instead of after if this is the last code-point.
6464
breakPoint -= 2
6565
}
6666
}
6767

68-
b.insert(breakPoint, "<br/>")
68+
sb.insert(breakPoint, "<br/>")
6969
widthIndex += columnWidth
7070
count++
7171
}
7272

73-
return b.toString()
73+
return sb.toString()
7474
}

src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class MyersDiff<T>(private val equalizer: DiffEqualizer<T> = EqualsDiffEq
5858
val max = origSize + revSize + 1
5959
val size = 1 + 2 * max
6060
val middle = size / 2
61-
val diagonal: Array<PathNode?> = arrayOfNulls(size)
61+
val diagonal = arrayOfNulls<PathNode>(size)
6262
diagonal[middle + 1] = PathNode(0, -1, snake = true, bootstrap = true, prev = null)
6363

6464
for (d in 0..<max) {

src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiffWithLinearSpace.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public class MyersDiffWithLinearSpace<T>(
5050
progress: DiffAlgorithmListener,
5151
) {
5252
progress.diffStep((end1 - start1) / 2 + (end2 - start2) / 2, -1)
53-
5453
val middle = getMiddleSnake(data, start1, end1, start2, end2)
5554

5655
if (middle == null ||

src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/PathNode.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ internal class PathNode(
5555
return null
5656
}
5757

58-
return if (!snake && prev != null) {
59-
prev.previousSnake()
60-
} else {
61-
this
62-
}
58+
return if (!snake && prev != null) prev.previousSnake() else this
6359
}
6460

6561
override fun toString(): String {

src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import io.github.petertrr.diffutils.patch.Delta
2929
import io.github.petertrr.diffutils.patch.DeltaType
3030
import io.github.petertrr.diffutils.patch.InsertDelta
3131
import io.github.petertrr.diffutils.patch.Patch
32+
import io.github.petertrr.diffutils.wrapText
3233
import kotlin.math.max
3334
import kotlin.math.min
3435

@@ -84,8 +85,8 @@ public class DiffRowGenerator(
8485
inlineDiffByWord: Boolean = false,
8586
private val inlineDiffSplitter: DiffSplitter = if (inlineDiffByWord) WordDiffSplitter() else CharDiffSplitter(),
8687
private val mergeOriginalRevised: Boolean = false,
87-
private val newTag: DiffTagGenerator = NewDiffTagGenerator(),
88-
private val oldTag: DiffTagGenerator = OldDiffTagGenerator(),
88+
private val newTag: DiffTagGenerator = HtmlDiffTagGenerator("editNewInline"),
89+
private val oldTag: DiffTagGenerator = HtmlDiffTagGenerator("editOldInline"),
8990
private val reportLinesUnchanged: Boolean = false,
9091
private val lineNormalizer: DiffLineNormalizer = HtmlLineNormalizer(),
9192
private val processDiffs: DiffLineProcessor? = null,
@@ -138,8 +139,8 @@ public class DiffRowGenerator(
138139
return diffRows
139140
}
140141

141-
internal fun normalizeLines(list: List<String>): List<String> =
142-
if (reportLinesUnchanged) list else list.map { lineNormalizer.normalize(it) }
142+
private fun normalizeLines(list: List<String>): List<String> =
143+
if (reportLinesUnchanged) list else list.map(lineNormalizer::normalize)
143144

144145
/**
145146
* Transforms one patch delta into a [DiffRow] object.
@@ -262,7 +263,7 @@ public class DiffRowGenerator(
262263
}
263264

264265
private fun buildDiffRowWithoutNormalizing(type: DiffRow.Tag, oldLine: String, newLine: String): DiffRow =
265-
DiffRow(type, wrapText(oldLine, columnWidth), wrapText(newLine, columnWidth))
266+
DiffRow(type, oldLine.wrapText(columnWidth), newLine.wrapText(columnWidth))
266267

267268
/**
268269
* Add the inline diffs for given delta
@@ -364,8 +365,8 @@ public class DiffRowGenerator(
364365
}
365366
}
366367

367-
val origResult = StringBuilder()
368-
val revResult = StringBuilder()
368+
val origResult = StringBuilder(origList.size)
369+
val revResult = StringBuilder(revList.size)
369370

370371
for (character in origList) {
371372
origResult.append(character)
@@ -379,9 +380,11 @@ public class DiffRowGenerator(
379380
// trailing empty string by default
380381
val original = origResult.split("\n").dropLastWhile(String::isEmpty)
381382
val revised = revResult.split("\n").dropLastWhile(String::isEmpty)
382-
val diffRows = ArrayList<DiffRow>()
383383

384-
for (j in 0..<max(original.size, revised.size)) {
384+
val size = max(original.size, revised.size)
385+
val diffRows = ArrayList<DiffRow>(size)
386+
387+
for (j in 0..<size) {
385388
diffRows.add(
386389
buildDiffRowWithoutNormalizing(
387390
type = DiffRow.Tag.CHANGE,
@@ -396,7 +399,7 @@ public class DiffRowGenerator(
396399

397400
private fun preprocessLine(line: String): String {
398401
val normalized = lineNormalizer.normalize(line)
399-
return if (columnWidth == 0) normalized else wrapText(normalized, columnWidth)
402+
return if (columnWidth == 0) normalized else normalized.wrapText(columnWidth)
400403
}
401404

402405
/**

src/commonMain/kotlin/io/github/petertrr/diffutils/text/NewDiffTagGenerator.kt renamed to src/commonMain/kotlin/io/github/petertrr/diffutils/text/HtmlDiffTagGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package io.github.petertrr.diffutils.text
1717

18-
internal class NewDiffTagGenerator : DiffTagGenerator {
18+
internal class HtmlDiffTagGenerator(private val className: String) : DiffTagGenerator {
1919
override fun generateOpen(tag: DiffRow.Tag): String =
20-
"<span class=\"editNewInline\">"
20+
"<span class=\"$className\">"
2121

2222
override fun generateClose(tag: DiffRow.Tag): String =
2323
"</span>"

src/commonMain/kotlin/io/github/petertrr/diffutils/text/HtmlLineNormalizer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package io.github.petertrr.diffutils.text
1717

18-
import io.github.petertrr.diffutils.text.normalize as normalizeFn
18+
import io.github.petertrr.diffutils.normalize
1919

2020
internal class HtmlLineNormalizer : DiffLineNormalizer {
2121
override fun normalize(line: String): String =
22-
normalizeFn(line)
22+
line.normalize()
2323
}

src/commonMain/kotlin/io/github/petertrr/diffutils/text/OldDiffTagGenerator.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/commonMain/kotlin/io/github/petertrr/diffutils/text/WordDiffSplitter.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616
package io.github.petertrr.diffutils.text
1717

18+
// As a "global" variable to avoid re-compiling the regex each time
19+
private val defaultPattern = Regex("\\s+|[,.\\[\\](){}/\\\\*+\\-#]")
20+
1821
/**
1922
* Splitting lines by word to achieve word by word diff checking.
2023
*/
21-
internal class WordDiffSplitter(private val pattern: Regex = Regex("\\s+|[,.\\[\\](){}/\\\\*+\\-#]")) : DiffSplitter {
24+
internal class WordDiffSplitter(private val pattern: Regex = defaultPattern) : DiffSplitter {
2225
override fun split(line: String): MutableList<String> {
2326
val matchResults = pattern.findAll(line)
2427
val list = ArrayList<String>()

src/commonTest/kotlin/io/github/petertrr/diffutils/text/DiffRowGeneratorTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class DiffRowGeneratorTest {
4040
*/
4141
@Test
4242
fun testNormalize_List() {
43-
val generator = DiffRowGenerator()
44-
assertEquals(listOf(" test"), generator.normalizeLines(listOf("\ttest")))
43+
val normalizer = HtmlLineNormalizer()
44+
assertEquals(" test", normalizer.normalize("\ttest"))
4545
}
4646

4747
@Test

src/commonTest/kotlin/io/github/petertrr/diffutils/text/StringUtilsTest.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package io.github.petertrr.diffutils.text
2020

21+
import io.github.petertrr.diffutils.htmlEntities
22+
import io.github.petertrr.diffutils.normalize
23+
import io.github.petertrr.diffutils.wrapText
2124
import kotlin.test.Test
2225
import kotlin.test.assertEquals
2326
import kotlin.test.assertFailsWith
@@ -27,32 +30,32 @@ class StringUtilsTest {
2730
* Test of htmlEntities method, of class StringUtils.
2831
*/
2932
@Test
30-
fun testHtmlEntites() {
31-
assertEquals("&lt;test&gt;", htmlEntities("<test>"))
33+
fun testHtmlEntities() {
34+
assertEquals("&lt;test&gt;", "<test>".htmlEntities())
3235
}
3336

3437
/**
3538
* Test of normalize method, of class StringUtils.
3639
*/
3740
@Test
3841
fun testNormalize_String() {
39-
assertEquals(" test", normalize("\ttest"))
42+
assertEquals(" test", "\ttest".normalize())
4043
}
4144

4245
/**
4346
* Test of wrapText method, of class
4447
*/
4548
@Test
4649
fun testWrapText_String_int() {
47-
assertEquals("te<br/>st", wrapText("test", 2))
48-
assertEquals("tes<br/>t", wrapText("test", 3))
49-
assertEquals("test", wrapText("test", 10))
50-
assertEquals(".\uD800\uDC01<br/>.", wrapText(".\uD800\uDC01.", 2))
51-
assertEquals("..<br/>\uD800\uDC01", wrapText("..\uD800\uDC01", 3))
50+
assertEquals("te<br/>st", "test".wrapText(2))
51+
assertEquals("tes<br/>t", "test".wrapText(3))
52+
assertEquals("test", "test".wrapText(10))
53+
assertEquals(".\uD800\uDC01<br/>.", ".\uD800\uDC01.".wrapText(2))
54+
assertEquals("..<br/>\uD800\uDC01", "..\uD800\uDC01".wrapText(3))
5255
}
5356

5457
@Test
5558
fun testWrapText_String_int_zero() {
56-
assertFailsWith<IllegalArgumentException> { wrapText("test", -1) }
59+
assertFailsWith<IllegalArgumentException> { "test".wrapText(-1) }
5760
}
5861
}

0 commit comments

Comments
 (0)