You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

411 line
11 KiB

3 年之前
  1. /***
  2. This is part of jsdifflib v1.0. <http://snowtide.com/jsdifflib>
  3. Copyright (c) 2007, Snowtide Informatics Systems, Inc.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. * Neither the name of the Snowtide Informatics Systems nor the names of its
  13. contributors may be used to endorse or promote products derived from this
  14. software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  16. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  18. SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  20. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  21. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  23. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  24. DAMAGE.
  25. ***/
  26. /* Author: Chas Emerick <cemerick@snowtide.com> */
  27. __whitespace = {" ":true, "\t":true, "\n":true, "\f":true, "\r":true};
  28. difflib = {
  29. defaultJunkFunction: function (c) {
  30. return __whitespace.hasOwnProperty(c);
  31. },
  32. stripLinebreaks: function (str) { return str.replace(/^[\n\r]*|[\n\r]*$/g, ""); },
  33. stringAsLines: function (str) {
  34. var lfpos = str.indexOf("\n");
  35. var crpos = str.indexOf("\r");
  36. var linebreak = ((lfpos > -1 && crpos > -1) || crpos < 0) ? "\n" : "\r";
  37. var lines = str.split(linebreak);
  38. for (var i = 0; i < lines.length; i++) {
  39. lines[i] = difflib.stripLinebreaks(lines[i]);
  40. }
  41. return lines;
  42. },
  43. // iteration-based reduce implementation
  44. __reduce: function (func, list, initial) {
  45. if (initial != null) {
  46. var value = initial;
  47. var idx = 0;
  48. } else if (list) {
  49. var value = list[0];
  50. var idx = 1;
  51. } else {
  52. return null;
  53. }
  54. for (; idx < list.length; idx++) {
  55. value = func(value, list[idx]);
  56. }
  57. return value;
  58. },
  59. // comparison function for sorting lists of numeric tuples
  60. __ntuplecomp: function (a, b) {
  61. var mlen = Math.max(a.length, b.length);
  62. for (var i = 0; i < mlen; i++) {
  63. if (a[i] < b[i]) return -1;
  64. if (a[i] > b[i]) return 1;
  65. }
  66. return a.length == b.length ? 0 : (a.length < b.length ? -1 : 1);
  67. },
  68. __calculate_ratio: function (matches, length) {
  69. return length ? 2.0 * matches / length : 1.0;
  70. },
  71. // returns a function that returns true if a key passed to the returned function
  72. // is in the dict (js object) provided to this function; replaces being able to
  73. // carry around dict.has_key in python...
  74. __isindict: function (dict) {
  75. return function (key) { return dict.hasOwnProperty(key); };
  76. },
  77. // replacement for python's dict.get function -- need easy default values
  78. __dictget: function (dict, key, defaultValue) {
  79. return dict.hasOwnProperty(key) ? dict[key] : defaultValue;
  80. },
  81. SequenceMatcher: function (a, b, isjunk) {
  82. this.set_seqs = function (a, b) {
  83. this.set_seq1(a);
  84. this.set_seq2(b);
  85. }
  86. this.set_seq1 = function (a) {
  87. if (a == this.a) return;
  88. this.a = a;
  89. this.matching_blocks = this.opcodes = null;
  90. }
  91. this.set_seq2 = function (b) {
  92. if (b == this.b) return;
  93. this.b = b;
  94. this.matching_blocks = this.opcodes = this.fullbcount = null;
  95. this.__chain_b();
  96. }
  97. this.__chain_b = function () {
  98. var b = this.b;
  99. var n = b.length;
  100. var b2j = this.b2j = {};
  101. var populardict = {};
  102. for (var i = 0; i < b.length; i++) {
  103. var elt = b[i];
  104. if (b2j.hasOwnProperty(elt)) {
  105. var indices = b2j[elt];
  106. if (n >= 200 && indices.length * 100 > n) {
  107. populardict[elt] = 1;
  108. delete b2j[elt];
  109. } else {
  110. indices.push(i);
  111. }
  112. } else {
  113. b2j[elt] = [i];
  114. }
  115. }
  116. for (var elt in populardict) {
  117. if (populardict.hasOwnProperty(elt)) {
  118. delete b2j[elt];
  119. }
  120. }
  121. var isjunk = this.isjunk;
  122. var junkdict = {};
  123. if (isjunk) {
  124. for (var elt in populardict) {
  125. if (populardict.hasOwnProperty(elt) && isjunk(elt)) {
  126. junkdict[elt] = 1;
  127. delete populardict[elt];
  128. }
  129. }
  130. for (var elt in b2j) {
  131. if (b2j.hasOwnProperty(elt) && isjunk(elt)) {
  132. junkdict[elt] = 1;
  133. delete b2j[elt];
  134. }
  135. }
  136. }
  137. this.isbjunk = difflib.__isindict(junkdict);
  138. this.isbpopular = difflib.__isindict(populardict);
  139. }
  140. this.find_longest_match = function (alo, ahi, blo, bhi) {
  141. var a = this.a;
  142. var b = this.b;
  143. var b2j = this.b2j;
  144. var isbjunk = this.isbjunk;
  145. var besti = alo;
  146. var bestj = blo;
  147. var bestsize = 0;
  148. var j = null;
  149. var j2len = {};
  150. var nothing = [];
  151. for (var i = alo; i < ahi; i++) {
  152. var newj2len = {};
  153. var jdict = difflib.__dictget(b2j, a[i], nothing);
  154. for (var jkey in jdict) {
  155. if (jdict.hasOwnProperty(jkey)) {
  156. j = jdict[jkey];
  157. if (j < blo) continue;
  158. if (j >= bhi) break;
  159. newj2len[j] = k = difflib.__dictget(j2len, j - 1, 0) + 1;
  160. if (k > bestsize) {
  161. besti = i - k + 1;
  162. bestj = j - k + 1;
  163. bestsize = k;
  164. }
  165. }
  166. }
  167. j2len = newj2len;
  168. }
  169. while (besti > alo && bestj > blo && !isbjunk(b[bestj - 1]) && a[besti - 1] == b[bestj - 1]) {
  170. besti--;
  171. bestj--;
  172. bestsize++;
  173. }
  174. while (besti + bestsize < ahi && bestj + bestsize < bhi &&
  175. !isbjunk(b[bestj + bestsize]) &&
  176. a[besti + bestsize] == b[bestj + bestsize]) {
  177. bestsize++;
  178. }
  179. while (besti > alo && bestj > blo && isbjunk(b[bestj - 1]) && a[besti - 1] == b[bestj - 1]) {
  180. besti--;
  181. bestj--;
  182. bestsize++;
  183. }
  184. while (besti + bestsize < ahi && bestj + bestsize < bhi && isbjunk(b[bestj + bestsize]) &&
  185. a[besti + bestsize] == b[bestj + bestsize]) {
  186. bestsize++;
  187. }
  188. return [besti, bestj, bestsize];
  189. }
  190. this.get_matching_blocks = function () {
  191. if (this.matching_blocks != null) return this.matching_blocks;
  192. var la = this.a.length;
  193. var lb = this.b.length;
  194. var queue = [[0, la, 0, lb]];
  195. var matching_blocks = [];
  196. var alo, ahi, blo, bhi, qi, i, j, k, x;
  197. while (queue.length) {
  198. qi = queue.pop();
  199. alo = qi[0];
  200. ahi = qi[1];
  201. blo = qi[2];
  202. bhi = qi[3];
  203. x = this.find_longest_match(alo, ahi, blo, bhi);
  204. i = x[0];
  205. j = x[1];
  206. k = x[2];
  207. if (k) {
  208. matching_blocks.push(x);
  209. if (alo < i && blo < j)
  210. queue.push([alo, i, blo, j]);
  211. if (i+k < ahi && j+k < bhi)
  212. queue.push([i + k, ahi, j + k, bhi]);
  213. }
  214. }
  215. matching_blocks.sort(difflib.__ntuplecomp);
  216. var i1 = j1 = k1 = block = 0;
  217. var non_adjacent = [];
  218. for (var idx in matching_blocks) {
  219. if (matching_blocks.hasOwnProperty(idx)) {
  220. block = matching_blocks[idx];
  221. i2 = block[0];
  222. j2 = block[1];
  223. k2 = block[2];
  224. if (i1 + k1 == i2 && j1 + k1 == j2) {
  225. k1 += k2;
  226. } else {
  227. if (k1) non_adjacent.push([i1, j1, k1]);
  228. i1 = i2;
  229. j1 = j2;
  230. k1 = k2;
  231. }
  232. }
  233. }
  234. if (k1) non_adjacent.push([i1, j1, k1]);
  235. non_adjacent.push([la, lb, 0]);
  236. this.matching_blocks = non_adjacent;
  237. return this.matching_blocks;
  238. }
  239. this.get_opcodes = function () {
  240. if (this.opcodes != null) return this.opcodes;
  241. var i = 0;
  242. var j = 0;
  243. var answer = [];
  244. this.opcodes = answer;
  245. var block, ai, bj, size, tag;
  246. var blocks = this.get_matching_blocks();
  247. for (var idx in blocks) {
  248. if (blocks.hasOwnProperty(idx)) {
  249. block = blocks[idx];
  250. ai = block[0];
  251. bj = block[1];
  252. size = block[2];
  253. tag = '';
  254. if (i < ai && j < bj) {
  255. tag = 'replace';
  256. } else if (i < ai) {
  257. tag = 'delete';
  258. } else if (j < bj) {
  259. tag = 'insert';
  260. }
  261. if (tag) answer.push([tag, i, ai, j, bj]);
  262. i = ai + size;
  263. j = bj + size;
  264. if (size) answer.push(['equal', ai, i, bj, j]);
  265. }
  266. }
  267. return answer;
  268. }
  269. // this is a generator function in the python lib, which of course is not supported in javascript
  270. // the reimplementation builds up the grouped opcodes into a list in their entirety and returns that.
  271. this.get_grouped_opcodes = function (n) {
  272. if (!n) n = 3;
  273. var codes = this.get_opcodes();
  274. if (!codes) codes = [["equal", 0, 1, 0, 1]];
  275. var code, tag, i1, i2, j1, j2;
  276. if (codes[0][0] == 'equal') {
  277. code = codes[0];
  278. tag = code[0];
  279. i1 = code[1];
  280. i2 = code[2];
  281. j1 = code[3];
  282. j2 = code[4];
  283. codes[0] = [tag, Math.max(i1, i2 - n), i2, Math.max(j1, j2 - n), j2];
  284. }
  285. if (codes[codes.length - 1][0] == 'equal') {
  286. code = codes[codes.length - 1];
  287. tag = code[0];
  288. i1 = code[1];
  289. i2 = code[2];
  290. j1 = code[3];
  291. j2 = code[4];
  292. codes[codes.length - 1] = [tag, i1, Math.min(i2, i1 + n), j1, Math.min(j2, j1 + n)];
  293. }
  294. var nn = n + n;
  295. var group = [];
  296. var groups = [];
  297. for (var idx in codes) {
  298. if (codes.hasOwnProperty(idx)) {
  299. code = codes[idx];
  300. tag = code[0];
  301. i1 = code[1];
  302. i2 = code[2];
  303. j1 = code[3];
  304. j2 = code[4];
  305. if (tag == 'equal' && i2 - i1 > nn) {
  306. group.push([tag, i1, Math.min(i2, i1 + n), j1, Math.min(j2, j1 + n)]);
  307. groups.push(group);
  308. group = [];
  309. i1 = Math.max(i1, i2-n);
  310. j1 = Math.max(j1, j2-n);
  311. }
  312. group.push([tag, i1, i2, j1, j2]);
  313. }
  314. }
  315. if (group && !(group.length == 1 && group[0][0] == 'equal')) groups.push(group)
  316. return groups;
  317. }
  318. this.ratio = function () {
  319. matches = difflib.__reduce(
  320. function (sum, triple) { return sum + triple[triple.length - 1]; },
  321. this.get_matching_blocks(), 0);
  322. return difflib.__calculate_ratio(matches, this.a.length + this.b.length);
  323. }
  324. this.quick_ratio = function () {
  325. var fullbcount, elt;
  326. if (this.fullbcount == null) {
  327. this.fullbcount = fullbcount = {};
  328. for (var i = 0; i < this.b.length; i++) {
  329. elt = this.b[i];
  330. fullbcount[elt] = difflib.__dictget(fullbcount, elt, 0) + 1;
  331. }
  332. }
  333. fullbcount = this.fullbcount;
  334. var avail = {};
  335. var availhas = difflib.__isindict(avail);
  336. var matches = numb = 0;
  337. for (var i = 0; i < this.a.length; i++) {
  338. elt = this.a[i];
  339. if (availhas(elt)) {
  340. numb = avail[elt];
  341. } else {
  342. numb = difflib.__dictget(fullbcount, elt, 0);
  343. }
  344. avail[elt] = numb - 1;
  345. if (numb > 0) matches++;
  346. }
  347. return difflib.__calculate_ratio(matches, this.a.length + this.b.length);
  348. }
  349. this.real_quick_ratio = function () {
  350. var la = this.a.length;
  351. var lb = this.b.length;
  352. return _calculate_ratio(Math.min(la, lb), la + lb);
  353. }
  354. this.isjunk = isjunk ? isjunk : difflib.defaultJunkFunction;
  355. this.a = this.b = null;
  356. this.set_seqs(a, b);
  357. }
  358. };