Understanding how to compare

It is more about the equality than the differences

A good diff algorithm will attempt to identify as much equality as possible. Everything else qualifies as differences. The metric for quality and precision is a smaller count of captured differences. The smaller this number the better, presuming differences aren't escaping undetected.

False negatives, which is allowing differences through without detection, is really bad. This is absolute failure in a diff algorithm. False positives, which is identifying more differences than there actually are is also bad, but a false positive is much better than a false negative. This means it is safer to report more differences than are actually present, which is why a higher number of reported differences is less precise. Maximum precision is reporting differences without any false negatives or false positives.

One way to achieve higher precision is to first capture as much equality as possible. For everything else that is left over prioritize unique items first and report these items as differences. Finally determine the next bit of equality or uniqueness and everything in between is either a change, an insertion, or a deletion.

Algorithm quality

The primary priorities when writing this kind of code are execution speed, precision (as previous described), and code simplicity. In most cases precision is the most important factor in code design, but in some cases speed is more important when processing large input or a batch of thousands of files. Simplicity is necessary so that other people can understand the code and modify it as necessary to improve upon the design and additional features. No algorithm is ever capable of meeting all needs for all use cases, so it is important that other people can understand the code with minimal effort.

Speed

Faster execution is the result of a couple of things. The most important consideration for making an algorithm faster is to reduce the number of passes through data where possible. After that eliminate all costly operations. In most programming languages simple arithmetic and static string comparisons are cheap to execute particularly if the examined strings aren't changing. The theoretical minimum number of data passes is two as you have to read the contents of each submitted sample. Pretty Diff achieves speed in its algorithm by only 3 complete passes through data and taking all possible effort to never repeat a step or loop iteration. The Pretty Diff approach is linear and predictable where the number of interations passing through data is computed as: number of iterations from the first sample + number of iterations from the second sample + the number of iterations from the smallest of those samples. Performance of the mentioned approach is heavily based upon key access in a hash map, which will likely vary by programming language.

The theoretical minimum number of data passes is two as you have to read the contents of each submitted sample. Until we discover a way to perform a comparison without reading from the samples we can safely assume 2 data passes is the fastest possible approach. Between that and the Pretty Diff approach there are two possibilities for increased performance. The first possibility is to make decisions and write output immediately upon reading from the second sample so as to have only two data passes. The challenge with this approach is that analysis occurs in the moment of the current loop interation without knowledge of what comes later in the second sample. A more practical second performance possibility is to write a smaller hash map. Writing a smaller hash map means moving some of the decision tree up front before a separate formal analysis phase. In order for this approach to be viable this early step in logic must be tiny, non-replicating, and a different means of iteration must be devised to account for a data store of unpredictable size.

This page blew up on Hacker News recently and many comments suggested this approach could not possibly be faster than the Myers' O(ND) approach. That may or may not be true and no evidence was provided either way (conjecture is not evidence).

In terms of experimental criteria algorithms are themselves largely irrelevant. More important is the implementation of those algorithms. If exercising the Myers' approach makes fewer decisions and has fewer total data passes, as described in the previous paragraph, then it likely is faster. I am calling out the word total because this makes all the difference. Many of the diff applications I looked at don't provide a complete third data pass. Instead they provide the minimum two complete data passes over the samples and various smaller passes as calculated by block moves and edit distances. If these various fractional passes are non-linear, which means starting and stopping in different places respectively, their performance is less predictable. If these fractional passes are non-linear and touch any given data index more than once they have repetitive logic, and likely are not as fast. To affirmatively guarantee superior performance over the Pretty Diff approach there needs to be fewer passes over data, which means no repetition and a smaller number of iterations. Preditability ensures the performance of an application scales roughtly proportionately to the size of the provided samples, where an unpredictable approach would scale disproportionately (slower over time). I say roughtly because things in physical reality always mess this up like: solar flares, memory block limitations, CPU heat, and so forth.

I believe the approach taken here is fast. I honestly cannot say, scientifically, it is the fastest ever (or slowest) approach for its level of accuracy without also writing alternate algorithms into applications with identical application constraints. Neither can anyone else. I can safely say this approach is the fastest ever comparative algorithm for its level of predictability and precision.

Output format

The Pretty Diff algorithm inherits its output format from the application jsdifflib. The output is a big array containing child arrays at each index. The child arrays each contain 5 indexes in the format: type, start in first sample, end in first sample, start in second sample, end in second sample. There are 4 types defined in the output: "equal", "replace", "insert", "delete". The "equal" type means both array index items are identical at the indexes compared. The "replace" type means that changes are present in the indexes compared. The "insert" type means the index of the second sample is unique to the second sample. The "delete" type means the index of the first sample is unique to the first sample.

How the algorithm works at a high level

Getting by with a hash map and some counts

The Pretty Diff algorithm takes after the Paul Heckel algorithm. First thing is to transform the string input into arrays. The most primative way to do this is to split the input by lines where each line of input is an index in an arry. Once the two input samples are converted to arrays they will each have to be examined.

The first loop will walk through an array representing one of the code samples and make a decision for a hash map which I call table in the code. I simply named the two arrays one and two. Each index of the array, by default a line of code, will serve as a key in the hash map named table. If this key does not exist then we will add it. The value for each key is an object with two properites named one and two. These properties are simply a count indicating the number of times the key is encountered in the two arrays. If the key is already present then we will simply increase the value of properties one or two respective of the current array.

Here is an example of this step in code:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  1. do {
  2. if (table[two[b]] === undefined) {
  3. table[two[b]] = {
  4. one: 0,
  5. two: 1
  6. };
  7. } else {
  8. table[two[b]].two += 1;
  9. }
  10. b += 1;
  11. } while (b < lenb);

The third and final pass through the data

Once the table is fully populated with all indexes from both arrays one and two we need a third and final loop to walk across the data and make some simple decisions. Here is this complete step in code.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  1. do {
  2. c = a;
  3. d = b;
  4. if (one[a] === two[b]) {
  5. equality();
  6. } else if (table[one[a]][1] < 1 && table[two[b]][0] < 1) {
  7. replaceUniques();
  8. } else if (table[one[a]][1] < 1 && one[a + 1] !== two[b + 2]) {
  9. deletion();
  10. } else if (table[two[b]][0] < 1 && one[a + 2] !== two[b + 1]) {
  11. insertion();
  12. } else if (table[one[a]][0] - table[one[a]][1] === 1 && one[a + 1] !== two[b + 2]) {
  13. deletionStatic();
  14. } else if (table[two[b]][1] - table[two[b]][0] === 1 && one[a + 2] !== two[b + 1]) {
  15. insertionStatic();
  16. } else if (one[a + 1] === two[b]) {
  17. deletion();
  18. } else if (one[a] === two[b + 1]) {
  19. insertion();
  20. } else {
  21. replacement();
  22. }
  23. a = a + 1;
  24. b = b + 1;
  25. } while (a < lena && b < lenb);

Before I go any further I want to be clear this logic is fast and simple, but it isn't precise at all. We will fix this later with a child function named fix. I chose to make corrections for precision as a secondary step so as to not disrupt performance and isolate complexity into a separate single location.

In this code references a and b are simply positive integer incrementors. The a reference is always associated with the one array while the b reference is always associated with the two array. This is necessary so that each array may increment independently without collision. The c and d references are secondary incrementors used as closures in the child functions. These secondary incrementors allow for child loops to occur without mutation to the primary incrementors.

The first thing that happens in this loop is an attempt to discover equality. Everything else is less important and so happens later in the decision tree.

The second rule is to identify items that occur only one more time in each array. If the next item to occur in the arrays is not equal but occurs just once more then we know the item is a unique change.

The third rule is to determine if the current item is a deletion. If the current item, and possibly additional following items, is present in the first sample but no longer exists in the second sample then it is a dynamic deletion.

The fourth rule is the same as the third rule but in reverse to determine dynamic insertions.

The fifth rule is to determine if the current occurs exactly one more time in the first sample than in the second sample and yet is not a match for the next item in the first sample with two items up in the second sample. This is a static deletion, or rather a deletion of a fixed number of items.

The sixth rule is the same as the fifth rule but in reverse to determine static insertions.

The seventh and final rule determines that the current items in the samples are non-unique changes.

The child functions are as follows:

  1. - 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. - 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. - 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. - 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. - 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. - 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. - 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  1. equality = function diffview__opcodes_equality() {
  2. do {
  3. table[one[c]].one -= 1;
  4. table[one[c]].two -= 1;
  5. c += 1;
  6. d += 1;
  7. } while (c < lena && d < lenb && one[c] === two[d]);
  8. fix(["equal", a, c, b, d]);
  9. b = d - 1;
  10. a = c - 1;
  11. },
  12. deletion = function diffview__opcodes_deletion() {
  13. do {
  14. table[one[c]].one -= 1;
  15. c += 1;
  16. } while (c < lena && table[one[c]].two < 1);
  17. fix(["delete", a, c, -1, -1]);
  18. a = c - 1;
  19. b = d - 1;
  20. },
  21. deletionStatic = function diffview__opcodes_deletionStatic() {
  22. table[one[a]].one -= 1;
  23. fix([
  24. "delete", a, a + 1,
  25. -1,
  26. -1
  27. ]);
  28. a = c;
  29. b = d - 1;
  30. },
  31. insertion = function diffview__opcodes_insertion() {
  32. do {
  33. table[two[d]].two -= 1;
  34. d += 1;
  35. } while (d < lenb && table[two[d]].one < 1);
  36. fix(["insert", -1, -1, b, d]);
  37. a = c - 1;
  38. b = d - 1;
  39. },
  40. insertionStatic = function diffview__opcodes_insertionStatic() {
  41. table[two[b]].two -= 1;
  42. fix([
  43. "insert", -1, -1, b, b + 1
  44. ]);
  45. a = c - 1;
  46. b = d;
  47. },
  48. replacement = function diffview__opcodes_replacement() {
  49. do {
  50. table[one[c]].one -= 1;
  51. table[two[d]].two -= 1;
  52. c += 1;
  53. d += 1;
  54. } while (c < lena && d < lenb && table[one[c]].two > 0 && table[two[d]].one > 0);
  55. fix(["replace", a, c, b, d]);
  56. a = c - 1;
  57. b = d - 1;
  58. },
  59. replaceUniques = function diffview__opcodes_replaceUniques() {
  60. do {
  61. table[one[c]].one -= 1;
  62. c += 1;
  63. d += 1;
  64. } while (c < lena && d < lenb && table[one[c]].two < 1 && table[two[d]].one < 1);
  65. fix(["replace", a, c, b, d]);
  66. a = c - 1;
  67. b = d - 1;
  68. }

It must be noted that most of the these child functions do contain their own loops, but these loops are not duplicates. The primary parent loop, the so called third and final loop, is adjusted forward by the distance these smaller loops increment so that there is no repetition or lose of execution speed.

The fix function

We can see from the child function code that arrays are generated which appear to be the format described as the child arrays of the output. Instead of pushing this data to the output array directly instead we are passing it through a function named fix which serves as a sort of quality control filter. This function checks for things like:

  • A diff type immediately following the same diff type, which can be combined into a single child array for the output
  • An insertion immediately following a deletion, or vise versa, which should likely be a replacement
  • Whether false positives generated from replacements immediately following an insertion or deletion can be eliminated

The general idea is to only accept a child array of the output as an argument and the make a decision after comparing it against the previous child array of the output. Aside from two narrow edge cases there is no analysis of the original data. Think of it like an appeals court in that no new evidence is allowed, because only the laws under scrutiny.

The code for the fix function is as follows:

  1. - 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  1. fix = function diffview__opcodes_fix(code) {
  2. var prior = codes[codes.length - 1];
  3. if (prior !== undefined) {
  4. if (prior[0] === code[0]) {
  5. if (code[0] === "replace" || code[0] === "equal") {
  6. prior[2] = code[2];
  7. prior[4] = code[4];
  8. } else if (code[0] === "delete") {
  9. prior[2] = code[2];
  10. } else if (code[0] === "insert") {
  11. prior[4] = code[4];
  12. }
  13. return;
  14. }
  15. if (prior[0] === "insert" && prior[4] - prior[3] === 1) {
  16. if (code[2] - code[1] === 1) {
  17. if (code[0] === "replace") {
  18. prior[0] = "replace";
  19. prior[1] = code[1];
  20. prior[2] = code[2];
  21. code[0] = "insert";
  22. code[1] = -1;
  23. code[2] = -1;
  24. } else if (code[0] === "delete") {
  25. code[0] = "replace";
  26. code[3] = prior[3];
  27. code[4] = prior[4];
  28. codes.pop();
  29. prior = codes[codes.length - 1];
  30. if (prior[0] === "replace") {
  31. prior[2] = code[2];
  32. prior[4] = code[4];
  33. return;
  34. }
  35. }
  36. } else if (code[0] === "delete") {
  37. prior[0] = "replace";
  38. prior[1] = code[1];
  39. prior[2] = code[1] + 1;
  40. code[1] = code[1] + 1;
  41. } else if (code[0] === "replace") {
  42. prior[0] = "replace";
  43. prior[1] = code[1];
  44. prior[2] = code[1] + 1;
  45. c = prior[2];
  46. d = prior[4];
  47. return;
  48. }
  49. } else if (prior[0] === "insert" && code[0] === "delete" && code[2] - code[1] === 1) {
  50. prior[4] = prior[4] - 1;
  51. code[0] = "replace";
  52. code[3] = prior[4];
  53. code[4] = prior[4] + 1;
  54. } else if (prior[0] === "delete" && prior[2] - prior[1] === 1) {
  55. if (code[4] - code[3] === 1) {
  56. if (code[0] === "replace") {
  57. prior[0] = "replace";
  58. prior[3] = code[3];
  59. prior[4] = code[4];
  60. code[0] = "delete";
  61. code[3] = -1;
  62. code[4] = -1;
  63. } else if (code[0] === "insert") {
  64. code[0] = "replace";
  65. code[1] = prior[1];
  66. code[2] = prior[2];
  67. codes.pop();
  68. prior = codes[codes.length - 1];
  69. if (prior[0] === "replace") {
  70. prior[2] = code[2];
  71. prior[4] = code[4];
  72. return;
  73. }
  74. }
  75. } else if (code[0] === "insert") {
  76. prior[0] = "replace";
  77. prior[3] = code[3];
  78. prior[4] = code[3] + 1;
  79. code[3] = code[3] + 1;
  80. } else if (code[0] === "replace") {
  81. prior[0] = "replace";
  82. prior[3] = code[3];
  83. prior[4] = code[4] + 1;
  84. c = prior[2];
  85. d = prior[4];
  86. return;
  87. }
  88. } else if (prior[0] === "delete" && code[0] === "insert" && code[4] - code[3] === 1) {
  89. prior[2] = prior[2] - 1;
  90. code[0] = "replace";
  91. code[1] = prior[2];
  92. code[2] = prior[2] + 1;
  93. } else if (prior[0] === "replace") {
  94. if (code[0] === "delete") {
  95. if (one[code[2] - 1] === two[prior[4] - 1]) {
  96. if (prior[2] - prior[1] > 1) {
  97. prior[4] = prior[4] - 1;
  98. }
  99. c = c - 1;
  100. d = d - 1;
  101. return;
  102. }
  103. if (one[code[2]] === two[prior[4] - 1]) {
  104. if (prior[2] - prior[1] > 1) {
  105. prior[2] = prior[2] - 1;
  106. prior[4] = prior[4] - 11;
  107. table[one[c - 1]][0] = table[one[c - 1]][0] - 1;
  108. }
  109. }
  110. } else if (code[0] === "insert") {
  111. if (one[prior[2] - 1] === two[code[4] - 1]) {
  112. if (prior[2] - prior[1] > 1) {
  113. prior[2] = prior[2] - 1;
  114. }
  115. c = c - 1;
  116. d = d - 1;
  117. return;
  118. }
  119. if (one[code[2] - 1] === two[prior[4]]) {
  120. if (prior[4] - prior[3] > 1) {
  121. prior[2] = prior[2] - 1;
  122. prior[4] = prior[4] - 1;
  123. table[two[d - 1]][1] = table[two[d - 1]][1] - 1;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. codes.push(code);
  130. };

Done!

That is the entire diff algorithm. Comments, feedback, and pull requests are welcome. Here is the complete function used in the Pretty Diff application:

  1. - 1
  2. 2
  3. - 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. - 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. - 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. - 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. - 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. - 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. - 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. - 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. - 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. - 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. - 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. - 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  1. function diffview__opcodes() {
  2. var table = {},
  3. lines = function diff_opcodes_lines(str) {
  4. str = str
  5. .replace(/\r\n/g, "\n")
  6. .replace(/\r/g, "\n");
  7. return str.split("\n");
  8. },
  9. one = (typeof options.source === "string")
  10. ? lines(options.source)
  11. : options.source,
  12. two = (typeof options.diff === "string")
  13. ? lines(options.diff)
  14. : options.diff,
  15. lena = one.length,
  16. lenb = two.length,
  17. a = 0,
  18. b = 0,
  19. c = 0,
  20. d = 0,
  21. codes = [],
  22. fix = function diffview__opcodes_fix(code) {
  23. var prior = codes[codes.length - 1];
  24. if (prior !== undefined) {
  25. if (prior[0] === code[0]) {
  26. if (code[0] === "replace" || code[0] === "equal") {
  27. prior[2] = code[2];
  28. prior[4] = code[4];
  29. } else if (code[0] === "delete") {
  30. prior[2] = code[2];
  31. } else if (code[0] === "insert") {
  32. prior[4] = code[4];
  33. }
  34. return;
  35. }
  36. if (prior[0] === "insert" && prior[4] - prior[3] === 1) {
  37. if (code[2] - code[1] === 1) {
  38. if (code[0] === "replace") {
  39. prior[0] = "replace";
  40. prior[1] = code[1];
  41. prior[2] = code[2];
  42. code[0] = "insert";
  43. code[1] = -1;
  44. code[2] = -1;
  45. } else if (code[0] === "delete") {
  46. code[0] = "replace";
  47. code[3] = prior[3];
  48. code[4] = prior[4];
  49. codes.pop();
  50. prior = codes[codes.length - 1];
  51. if (prior[0] === "replace") {
  52. prior[2] = code[2];
  53. prior[4] = code[4];
  54. return;
  55. }
  56. }
  57. } else if (code[0] === "delete") {
  58. prior[0] = "replace";
  59. prior[1] = code[1];
  60. prior[2] = code[1] + 1;
  61. code[1] = code[1] + 1;
  62. } else if (code[0] === "replace") {
  63. prior[0] = "replace";
  64. prior[1] = code[1];
  65. prior[2] = code[1] + 1;
  66. c = prior[2];
  67. d = prior[4];
  68. return;
  69. }
  70. } else if (prior[0] === "insert" && code[0] === "delete" && code[2] - code[1] === 1) {
  71. prior[4] = prior[4] - 1;
  72. code[0] = "replace";
  73. code[3] = prior[4];
  74. code[4] = prior[4] + 1;
  75. } else if (prior[0] === "delete" && prior[2] - prior[1] === 1) {
  76. if (code[4] - code[3] === 1) {
  77. if (code[0] === "replace") {
  78. prior[0] = "replace";
  79. prior[3] = code[3];
  80. prior[4] = code[4];
  81. code[0] = "delete";
  82. code[3] = -1;
  83. code[4] = -1;
  84. } else if (code[0] === "insert") {
  85. code[0] = "replace";
  86. code[1] = prior[1];
  87. code[2] = prior[2];
  88. codes.pop();
  89. prior = codes[codes.length - 1];
  90. if (prior[0] === "replace") {
  91. prior[2] = code[2];
  92. prior[4] = code[4];
  93. return;
  94. }
  95. }
  96. } else if (code[0] === "insert") {
  97. prior[0] = "replace";
  98. prior[3] = code[3];
  99. prior[4] = code[3] + 1;
  100. code[3] = code[3] + 1;
  101. } else if (code[0] === "replace") {
  102. prior[0] = "replace";
  103. prior[3] = code[3];
  104. prior[4] = code[4] + 1;
  105. c = prior[2];
  106. d = prior[4];
  107. return;
  108. }
  109. } else if (prior[0] === "delete" && code[0] === "insert" && code[4] - code[3] === 1) {
  110. prior[2] = prior[2] - 1;
  111. code[0] = "replace";
  112. code[1] = prior[2];
  113. code[2] = prior[2] + 1;
  114. } else if (prior[0] === "replace") {
  115. if (code[0] === "delete") {
  116. if (one[code[2] - 1] === two[prior[4] - 1]) {
  117. if (prior[2] - prior[1] > 1) {
  118. prior[4] = prior[4] - 1;
  119. }
  120. c = c - 1;
  121. d = d - 1;
  122. return;
  123. }
  124. if (one[code[2]] === two[prior[4] - 1]) {
  125. if (prior[2] - prior[1] > 1) {
  126. prior[2] = prior[2] - 1;
  127. prior[4] = prior[4] - 11;
  128. table[one[c - 1]][0] = table[one[c - 1]][0] - 1;
  129. }
  130. }
  131. } else if (code[0] === "insert") {
  132. if (one[prior[2] - 1] === two[code[4] - 1]) {
  133. if (prior[2] - prior[1] > 1) {
  134. prior[2] = prior[2] - 1;
  135. }
  136. c = c - 1;
  137. d = d - 1;
  138. return;
  139. }
  140. if (one[code[2] - 1] === two[prior[4]]) {
  141. if (prior[4] - prior[3] > 1) {
  142. prior[2] = prior[2] - 1;
  143. prior[4] = prior[4] - 1;
  144. table[two[d - 1]][1] = table[two[d - 1]][1] - 1;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. codes.push(code);
  151. },
  152. equality = function diffview__opcodes_equality() {
  153. do {
  154. table[one[c]][0] = table[one[c]][0] - 1;
  155. table[one[c]][1] = table[one[c]][1] - 1;
  156. c = c + 1;
  157. d = d + 1;
  158. } while (c < lena && d < lenb && one[c] === two[d]);
  159. fix(["equal", a, c, b, d]);
  160. b = d - 1;
  161. a = c - 1;
  162. },
  163. deletion = function diffview__opcodes_deletion() {
  164. do {
  165. table[one[c]][0] = table[one[c]][0] - 1;
  166. c = c + 1;
  167. } while (c < lena && table[one[c]][1] < 1);
  168. fix(["delete", a, c, -1, -1]);
  169. a = c - 1;
  170. b = d - 1;
  171. },
  172. deletionStatic = function diffview__opcodes_deletionStatic() {
  173. table[one[a]][0] = table[one[a]][0] - 1;
  174. fix([
  175. "delete", a, a + 1,
  176. -1,
  177. -1
  178. ]);
  179. a = c;
  180. b = d - 1;
  181. },
  182. insertion = function diffview__opcodes_insertion() {
  183. do {
  184. table[two[d]][1] = table[two[d]][1] - 1;
  185. d = d + 1;
  186. } while (d < lenb && table[two[d]][0] < 1);
  187. fix(["insert", -1, -1, b, d]);
  188. a = c - 1;
  189. b = d - 1;
  190. },
  191. insertionStatic = function diffview__opcodes_insertionStatic() {
  192. table[two[b]][1] = table[two[b]][1] - 1;
  193. fix([
  194. "insert", -1, -1, b, b + 1
  195. ]);
  196. a = c - 1;
  197. b = d;
  198. },
  199. replacement = function diffview__opcodes_replacement() {
  200. do {
  201. table[one[c]][0] = table[one[c]][0] - 1;
  202. table[two[d]][1] = table[two[d]][1] - 1;
  203. c = c + 1;
  204. d = d + 1;
  205. } while (c < lena && d < lenb && table[one[c]][1] > 0 && table[two[d]][0] > 0);
  206. fix(["replace", a, c, b, d]);
  207. a = c - 1;
  208. b = d - 1;
  209. },
  210. replaceUniques = function diffview__opcodes_replaceUniques() {
  211. do {
  212. table[one[c]][0] = table[one[c]][0] - 1;
  213. c = c + 1;
  214. d = d + 1;
  215. } while (c < lena && d < lenb && table[one[c]][1] < 1 && table[two[d]][0] < 1);
  216. fix(["replace", a, c, b, d]);
  217. a = c - 1;
  218. b = d - 1;
  219. };
  220. // * First Pass, account for lines from first file
  221. // * build the table from the second file
  222. do {
  223. if (options.diffspaceignore === true) {
  224. two[b] = two[b].replace(/\s+/g, "");
  225. }
  226. if (table[two[b]] === undefined) {
  227. table[two[b]] = [0, 1];
  228. } else {
  229. table[two[b]][1] = table[two[b]][1] + 1;
  230. }
  231. b = b + 1;
  232. } while (b < lenb);
  233. // * Second Pass, account for lines from second file
  234. // * build the table from the first file
  235. lena = one.length;
  236. a = 0;
  237. do {
  238. if (options.diffspaceignore === true) {
  239. one[a] = one[a].replace(/\s+/g, "");
  240. }
  241. if (table[one[a]] === undefined) {
  242. table[one[a]] = [1, 0];
  243. } else {
  244. table[one[a]][0] = table[one[a]][0] + 1;
  245. }
  246. a = a + 1;
  247. } while (a < lena);
  248. a = 0;
  249. b = 0;
  250. // find all equality... differences are what's left over solve only for the
  251. // second set test removing reverse test removing undefined checks for table
  252. // refs
  253. do {
  254. c = a;
  255. d = b;
  256. if (one[a] === two[b]) {
  257. equality();
  258. } else if (table[one[a]][1] < 1 && table[two[b]][0] < 1) {
  259. replaceUniques();
  260. } else if (table[one[a]][1] < 1 && one[a + 1] !== two[b + 2]) {
  261. deletion();
  262. } else if (table[two[b]][0] < 1 && one[a + 2] !== two[b + 1]) {
  263. insertion();
  264. } else if (table[one[a]][0] - table[one[a]][1] === 1 && one[a + 1] !== two[b + 2]) {
  265. deletionStatic();
  266. } else if (table[two[b]][1] - table[two[b]][0] === 1 && one[a + 2] !== two[b + 1]) {
  267. insertionStatic();
  268. } else if (one[a + 1] === two[b]) {
  269. deletion();
  270. } else if (one[a] === two[b + 1]) {
  271. insertion();
  272. } else {
  273. replacement();
  274. }
  275. a = a + 1;
  276. b = b + 1;
  277. } while (a < lena && b < lenb);
  278. if (lena - a === lenb - b) {
  279. if (one[a] === two[b]) {
  280. fix(["equal", a, lena, b, lenb]);
  281. } else {
  282. fix(["replace", a, lena, b, lenb]);
  283. }
  284. } else if (a < lena) {
  285. fix(["delete", a, lena, -1, -1]);
  286. } else if (b < lenb) {
  287. fix(["insert", -1, -1, b, lenb]);
  288. }
  289. return codes;
  290. }