@@ -11,12 +11,51 @@ class DiffForString(similarityThreshold: Double = 0.5) extends Diff[String] {
11
11
val rows = generator.generateDiffRows(splitIntoLines(left), splitIntoLines(right))
12
12
val lineResults = processLineDiffs(rows)
13
13
if (lineResults.forall(_.isIdentical)) {
14
- IdenticalValue (left)
14
+ if (left == right) {
15
+ IdenticalValue (left)
16
+ } else {
17
+ val resultsByChar =
18
+ generator.generateDiffRows[Int ](left.map(c => c.toInt).toList, right.map(c => c.toInt).toList)
19
+ val resultsAsString : List [DiffRow [String ]] = resultsByChar.map {
20
+ case DiffRow .Insert (newLine) => DiffRow .Insert (newLine.toChar.toString)
21
+ case DiffRow .Delete (oldLine) => DiffRow .Delete (oldLine.toChar.toString)
22
+ case DiffRow .Change (oldLine, newLine) => DiffRow .Change (oldLine.toChar.toString, newLine.toChar.toString)
23
+ case DiffRow .Equal (oldLine, newLine) => DiffRow .Equal (oldLine.toChar.toString, newLine.toChar.toString)
24
+ }
25
+ DiffResultString (mergeIdentical(resultsAsString))
26
+ }
15
27
} else {
16
28
DiffResultString (lineResults)
17
29
}
18
30
}
19
31
32
+ private def mergeIdentical (rows : List [DiffRow [String ]]): List [DiffResult ] = {
33
+ rows
34
+ .foldLeft(List [DiffResult ](IdenticalValue [String ](" " ))) { (acc, item) =>
35
+ acc match {
36
+ case list @ :: (IdenticalValue (head : String ), next) =>
37
+ item match {
38
+ case DiffRow .Insert (" \n " ) => DiffResultMissing (" " ) :: list
39
+ case DiffRow .Insert (newLine) => DiffResultMissing (newLine) :: list
40
+ case DiffRow .Delete (" \n " ) => DiffResultAdditional (" " ) :: list
41
+ case DiffRow .Delete (oldLine) => DiffResultAdditional (oldLine) :: list
42
+ case DiffRow .Change (oldLine, newLine) => DiffResultValue (oldLine, newLine) :: list
43
+ case DiffRow .Equal (oldLine, newLine) => IdenticalValue (head ++ oldLine) :: next
44
+ }
45
+ case other =>
46
+ item match {
47
+ case DiffRow .Insert (" \n " ) => DiffResultMissing (" " ) :: other
48
+ case DiffRow .Insert (newLine) => DiffResultMissing (newLine) :: other
49
+ case DiffRow .Delete (" \n " ) => DiffResultAdditional (" " ) :: other
50
+ case DiffRow .Delete (oldLine) => DiffResultAdditional (oldLine) :: other
51
+ case DiffRow .Change (oldLine, newLine) => DiffResultValue (oldLine, newLine) :: other
52
+ case DiffRow .Equal (oldLine, newLine) => IdenticalValue (oldLine) :: other
53
+ }
54
+ }
55
+ }
56
+ .reverse
57
+ }
58
+
20
59
private def processLineDiffs (rows : List [DiffRow [String ]]) = {
21
60
rows.map {
22
61
case DiffRow .Insert (newLine) => DiffResultMissing (newLine)
@@ -56,8 +95,10 @@ class DiffForString(similarityThreshold: Double = 0.5) extends Diff[String] {
56
95
.map(_.mkString)
57
96
}
58
97
59
- private def processWordDiffs (words : List [DiffRow [String ]]): List [DiffResult ] = {
60
- words.map {
98
+ private def processWordDiffs (words : List [DiffRow [String ]]): List [DiffResult ] = words.map(processWordDiff)
99
+
100
+ private def processWordDiff (wd : DiffRow [String ]) = {
101
+ wd match {
61
102
case DiffRow .Insert (newLine) => DiffResultMissingChunk (newLine)
62
103
case DiffRow .Delete (oldLine) => DiffResultAdditionalChunk (oldLine)
63
104
case DiffRow .Change (" " , newLine) => DiffResultMissingChunk (newLine)
0 commit comments