[php] diff

Viewer

  1. <?php 
  2. $var = 'var diff_match_patch = function() {
  3.         this.Diff_Timeout = 1;
  4.         this.Diff_EditCost = 4;
  5.         this.Match_Threshold = .5;
  6.         this.Match_Distance = 1E3;
  7.         this.Patch_DeleteThreshold = .5;
  8.         this.Patch_Margin = 4;
  9.         this.Match_MaxBits = 32
  10.     },
  11.     DIFF_DELETE = -1,
  12.     DIFF_INSERT = 1,
  13.     DIFF_EQUAL = 0;
  14. diff_match_patch.Diff = function(a, b) {
  15.     this[0] = a;
  16.     this[1] = b
  17. };
  18. diff_match_patch.Diff.prototype.length = 2;
  19. diff_match_patch.Diff.prototype.toString = function() {
  20.     return this[0] + "," + this[1]
  21. };
  22. diff_match_patch.prototype.diff_main = function(a, b, c, d) {
  23.     "undefined" == typeof d && (d = 0 >= this.Diff_Timeout ? Number.MAX_VALUE : (new Date).getTime() + 1E3 * this.Diff_Timeout);
  24.     if (null == a || null == b) throw Error("Null input. (diff_main)");
  25.     if (a == b) return a ? [new diff_match_patch.Diff(DIFF_EQUAL, a)] : [];
  26.     "undefined" == typeof c && (c = !0);
  27.     var e = c,
  28.         f = this.diff_commonPrefix(a, b);
  29.     c = a.substring(0, f);
  30.     a = a.substring(f);
  31.     b = b.substring(f);
  32.     f = this.diff_commonSuffix(a, b);
  33.     var g = a.substring(a.length - f);
  34.     a = a.substring(0, a.length - f);
  35.     b = b.substring(0,
  36.         b.length - f);
  37.     a = this.diff_compute_(a, b, e, d);
  38.     c && a.unshift(new diff_match_patch.Diff(DIFF_EQUAL, c));
  39.     g && a.push(new diff_match_patch.Diff(DIFF_EQUAL, g));
  40.     this.diff_cleanupMerge(a);
  41.     return a
  42. };
  43. diff_match_patch.prototype.diff_compute_ = function(a, b, c, d) {
  44.     if (!a) return [new diff_match_patch.Diff(DIFF_INSERT, b)];
  45.     if (!b) return [new diff_match_patch.Diff(DIFF_DELETE, a)];
  46.     var e = a.length > b.length ? a : b,
  47.         f = a.length > b.length ? b : a,
  48.         g = e.indexOf(f);
  49.     return -1 != g ? (c = [new diff_match_patch.Diff(DIFF_INSERT, e.substring(0, g)), new diff_match_patch.Diff(DIFF_EQUAL, f), new diff_match_patch.Diff(DIFF_INSERT, e.substring(g + f.length))], a.length > b.length && (c[0][0] = c[2][0] = DIFF_DELETE), c) : 1 == f.length ? [new diff_match_patch.Diff(DIFF_DELETE,
  50.         a), new diff_match_patch.Diff(DIFF_INSERT, b)] : (e = this.diff_halfMatch_(a, b)) ? (b = e[1], f = e[3], a = e[4], e = this.diff_main(e[0], e[2], c, d), c = this.diff_main(b, f, c, d), e.concat([new diff_match_patch.Diff(DIFF_EQUAL, a)], c)) : c && 100 < a.length && 100 < b.length ? this.diff_lineMode_(a, b, d) : this.diff_bisect_(a, b, d)
  51. };
  52. diff_match_patch.prototype.diff_lineMode_ = function(a, b, c) {
  53.     var d = this.diff_linesToChars_(a, b);
  54.     a = d.chars1;
  55.     b = d.chars2;
  56.     d = d.lineArray;
  57.     a = this.diff_main(a, b, !1, c);
  58.     this.diff_charsToLines_(a, d);
  59.     this.diff_cleanupSemantic(a);
  60.     a.push(new diff_match_patch.Diff(DIFF_EQUAL, ""));
  61.     for (var e = d = b = 0, f = "", g = ""; b < a.length;) {
  62.         switch (a[b][0]) {
  63.             case DIFF_INSERT:
  64.                 e++;
  65.                 g += a[b][1];
  66.                 break;
  67.             case DIFF_DELETE:
  68.                 d++;
  69.                 f += a[b][1];
  70.                 break;
  71.             case DIFF_EQUAL:
  72.                 if (1 <= d && 1 <= e) {
  73.                     a.splice(b - d - e, d + e);
  74.                     b = b - d - e;
  75.                     d = this.diff_main(f, g, !1, c);
  76.                     for (e = d.length - 1; 0 <= e; e--) a.splice(b,
  77.                         0, d[e]);
  78.                     b += d.length
  79.                 }
  80.                 d = e = 0;
  81.                 g = f = ""
  82.         }
  83.         b++
  84.     }
  85.     a.pop();
  86.     return a
  87. };
  88. diff_match_patch.prototype.diff_bisect_ = function(a, b, c) {
  89.     for (var d = a.length, e = b.length, f = Math.ceil((d + e) / 2), g = 2 * f, h = Array(g), l = Array(g), k = 0; k < g; k++) h[k] = -1, l[k] = -1;
  90.     h[f + 1] = 0;
  91.     l[f + 1] = 0;
  92.     k = d - e;
  93.     for (var m = 0 != k % 2, p = 0, x = 0, w = 0, q = 0, t = 0; t < f && !((new Date).getTime() > c); t++) {
  94.         for (var v = -t + p; v <= t - x; v += 2) {
  95.             var n = f + v;
  96.             var r = v == -t || v != t && h[n - 1] < h[n + 1] ? h[n + 1] : h[n - 1] + 1;
  97.             for (var y = r - v; r < d && y < e && a.charAt(r) == b.charAt(y);) r++, y++;
  98.             h[n] = r;
  99.             if (r > d) x += 2;
  100.             else if (y > e) p += 2;
  101.             else if (m && (n = f + k - v, 0 <= n && n < g && -1 != l[n])) {
  102.                 var u = d - l[n];
  103.                 if (r >=
  104.                     u) return this.diff_bisectSplit_(a, b, r, y, c)
  105.             }
  106.         }
  107.         for (v = -t + w; v <= t - q; v += 2) {
  108.             n = f + v;
  109.             u = v == -t || v != t && l[n - 1] < l[n + 1] ? l[n + 1] : l[n - 1] + 1;
  110.             for (r = u - v; u < d && r < e && a.charAt(d - u - 1) == b.charAt(e - r - 1);) u++, r++;
  111.             l[n] = u;
  112.             if (u > d) q += 2;
  113.             else if (r > e) w += 2;
  114.             else if (!m && (n = f + k - v, 0 <= n && n < g && -1 != h[n] && (r = h[n], y = f + r - n, u = d - u, r >= u))) return this.diff_bisectSplit_(a, b, r, y, c)
  115.         }
  116.     }
  117.     return [new diff_match_patch.Diff(DIFF_DELETE, a), new diff_match_patch.Diff(DIFF_INSERT, b)]
  118. };
  119. diff_match_patch.prototype.diff_bisectSplit_ = function(a, b, c, d, e) {
  120.     var f = a.substring(0, c),
  121.         g = b.substring(0, d);
  122.     a = a.substring(c);
  123.     b = b.substring(d);
  124.     f = this.diff_main(f, g, !1, e);
  125.     e = this.diff_main(a, b, !1, e);
  126.     return f.concat(e)
  127. };
  128. diff_match_patch.prototype.diff_linesToChars_ = function(a, b) {
  129.     function c(a) {
  130.         for (var b = "", c = 0, g = -1, h = d.length; g < a.length - 1;) {
  131.             g = a.indexOf("\\n", c); - 1 == g && (g = a.length - 1);
  132.             var l = a.substring(c, g + 1);
  133.             (e.hasOwnProperty ? e.hasOwnProperty(l) : void 0 !== e[l]) ? b += String.fromCharCode(e[l]): (h == f && (l = a.substring(c), g = a.length), b += String.fromCharCode(h), e[l] = h, d[h++] = l);
  134.             c = g + 1
  135.         }
  136.         return b
  137.     }
  138.     var d = [],
  139.         e = {};
  140.     d[0] = "";
  141.     var f = 4E4,
  142.         g = c(a);
  143.     f = 65535;
  144.     var h = c(b);
  145.     return {
  146.         chars1: g,
  147.         chars2: h,
  148.         lineArray: d
  149.     }
  150. };
  151. diff_match_patch.prototype.diff_charsToLines_ = function(a, b) {
  152.     for (var c = 0; c < a.length; c++) {
  153.         for (var d = a[c][1], e = [], f = 0; f < d.length; f++) e[f] = b[d.charCodeAt(f)];
  154.         a[c][1] = e.join("")
  155.     }
  156. };
  157. diff_match_patch.prototype.diff_commonPrefix = function(a, b) {
  158.     if (!a || !b || a.charAt(0) != b.charAt(0)) return 0;
  159.     for (var c = 0, d = Math.min(a.length, b.length), e = d, f = 0; c < e;) a.substring(f, e) == b.substring(f, e) ? f = c = e : d = e, e = Math.floor((d - c) / 2 + c);
  160.     return e
  161. };
  162. diff_match_patch.prototype.diff_commonSuffix = function(a, b) {
  163.     if (!a || !b || a.charAt(a.length - 1) != b.charAt(b.length - 1)) return 0;
  164.     for (var c = 0, d = Math.min(a.length, b.length), e = d, f = 0; c < e;) a.substring(a.length - e, a.length - f) == b.substring(b.length - e, b.length - f) ? f = c = e : d = e, e = Math.floor((d - c) / 2 + c);
  165.     return e
  166. };
  167. diff_match_patch.prototype.diff_commonOverlap_ = function(a, b) {
  168.     var c = a.length,
  169.         d = b.length;
  170.     if (0 == c || 0 == d) return 0;
  171.     c > d ? a = a.substring(c - d) : c < d && (b = b.substring(0, c));
  172.     c = Math.min(c, d);
  173.     if (a == b) return c;
  174.     d = 0;
  175.     for (var e = 1;;) {
  176.         var f = a.substring(c - e);
  177.         f = b.indexOf(f);
  178.         if (-1 == f) return d;
  179.         e += f;
  180.         if (0 == f || a.substring(c - e) == b.substring(0, e)) d = e, e++
  181.     }
  182. };
  183. diff_match_patch.prototype.diff_halfMatch_ = function(a, b) {
  184.     function c(a, b, c) {
  185.         for (var d = a.substring(c, c + Math.floor(a.length / 4)), e = -1, g = "", h, k, l, m; - 1 != (e = b.indexOf(d, e + 1));) {
  186.             var p = f.diff_commonPrefix(a.substring(c), b.substring(e)),
  187.                 u = f.diff_commonSuffix(a.substring(0, c), b.substring(0, e));
  188.             g.length < u + p && (g = b.substring(e - u, e) + b.substring(e, e + p), h = a.substring(0, c - u), k = a.substring(c + p), l = b.substring(0, e - u), m = b.substring(e + p))
  189.         }
  190.         return 2 * g.length >= a.length ? [h, k, l, m, g] : null
  191.     }
  192.     if (0 >= this.Diff_Timeout) return null;
  193.     var d = a.length > b.length ? a : b,
  194.         e = a.length > b.length ? b : a;
  195.     if (4 > d.length || 2 * e.length < d.length) return null;
  196.     var f = this,
  197.         g = c(d, e, Math.ceil(d.length / 4));
  198.     d = c(d, e, Math.ceil(d.length / 2));
  199.     if (g || d) g = d ? g ? g[4].length > d[4].length ? g : d : d : g;
  200.     else return null;
  201.     if (a.length > b.length) {
  202.         d = g[0];
  203.         e = g[1];
  204.         var h = g[2];
  205.         var l = g[3]
  206.     } else h = g[0], l = g[1], d = g[2], e = g[3];
  207.     return [d, e, h, l, g[4]]
  208. };
  209. diff_match_patch.prototype.diff_cleanupSemantic = function(a) {
  210.     for (var b = !1, c = [], d = 0, e = null, f = 0, g = 0, h = 0, l = 0, k = 0; f < a.length;) a[f][0] == DIFF_EQUAL ? (c[d++] = f, g = l, h = k, k = l = 0, e = a[f][1]) : (a[f][0] == DIFF_INSERT ? l += a[f][1].length : k += a[f][1].length, e && e.length <= Math.max(g, h) && e.length <= Math.max(l, k) && (a.splice(c[d - 1], 0, new diff_match_patch.Diff(DIFF_DELETE, e)), a[c[d - 1] + 1][0] = DIFF_INSERT, d--, d--, f = 0 < d ? c[d - 1] : -1, k = l = h = g = 0, e = null, b = !0)), f++;
  211.     b && this.diff_cleanupMerge(a);
  212.     this.diff_cleanupSemanticLossless(a);
  213.     for (f = 1; f <
  214.         a.length;) {
  215.         if (a[f - 1][0] == DIFF_DELETE && a[f][0] == DIFF_INSERT) {
  216.             b = a[f - 1][1];
  217.             c = a[f][1];
  218.             d = this.diff_commonOverlap_(b, c);
  219.             e = this.diff_commonOverlap_(c, b);
  220.             if (d >= e) {
  221.                 if (d >= b.length / 2 || d >= c.length / 2) a.splice(f, 0, new diff_match_patch.Diff(DIFF_EQUAL, c.substring(0, d))), a[f - 1][1] = b.substring(0, b.length - d), a[f + 1][1] = c.substring(d), f++
  222.             } else if (e >= b.length / 2 || e >= c.length / 2) a.splice(f, 0, new diff_match_patch.Diff(DIFF_EQUAL, b.substring(0, e))), a[f - 1][0] = DIFF_INSERT, a[f - 1][1] = c.substring(0, c.length - e), a[f + 1][0] = DIFF_DELETE,
  223.                 a[f + 1][1] = b.substring(e), f++;
  224.             f++
  225.         }
  226.         f++
  227.     }
  228. };
  229. diff_match_patch.prototype.diff_cleanupSemanticLossless = function(a) {
  230.     function b(a, b) {
  231.         if (!a || !b) return 6;
  232.         var c = a.charAt(a.length - 1),
  233.             d = b.charAt(0),
  234.             e = c.match(diff_match_patch.nonAlphaNumericRegex_),
  235.             f = d.match(diff_match_patch.nonAlphaNumericRegex_),
  236.             g = e && c.match(diff_match_patch.whitespaceRegex_),
  237.             h = f && d.match(diff_match_patch.whitespaceRegex_);
  238.         c = g && c.match(diff_match_patch.linebreakRegex_);
  239.         d = h && d.match(diff_match_patch.linebreakRegex_);
  240.         var k = c && a.match(diff_match_patch.blanklineEndRegex_),
  241.             l = d && b.match(diff_match_patch.blanklineStartRegex_);
  242.         return k || l ? 5 : c || d ? 4 : e && !g && h ? 3 : g || h ? 2 : e || f ? 1 : 0
  243.     }
  244.     for (var c = 1; c < a.length - 1;) {
  245.         if (a[c - 1][0] == DIFF_EQUAL && a[c + 1][0] == DIFF_EQUAL) {
  246.             var d = a[c - 1][1],
  247.                 e = a[c][1],
  248.                 f = a[c + 1][1],
  249.                 g = this.diff_commonSuffix(d, e);
  250.             if (g) {
  251.                 var h = e.substring(e.length - g);
  252.                 d = d.substring(0, d.length - g);
  253.                 e = h + e.substring(0, e.length - g);
  254.                 f = h + f
  255.             }
  256.             g = d;
  257.             h = e;
  258.             for (var l = f, k = b(d, e) + b(e, f); e.charAt(0) === f.charAt(0);) {
  259.                 d += e.charAt(0);
  260.                 e = e.substring(1) + f.charAt(0);
  261.                 f = f.substring(1);
  262.                 var m = b(d, e) + b(e, f);
  263.                 m >= k && (k = m, g = d, h = e, l = f)
  264.             }
  265.             a[c - 1][1] != g && (g ? a[c - 1][1] = g : (a.splice(c -
  266.                 1, 1), c--), a[c][1] = h, l ? a[c + 1][1] = l : (a.splice(c + 1, 1), c--))
  267.         }
  268.         c++
  269.     }
  270. };
  271. diff_match_patch.nonAlphaNumericRegex_ = /[^a-zA-Z0-9]/;
  272. diff_match_patch.whitespaceRegex_ = /\\s/;
  273. diff_match_patch.linebreakRegex_ = /[\\r\\n]/;
  274. diff_match_patch.blanklineEndRegex_ = /\\n\\r?\\n$/;
  275. diff_match_patch.blanklineStartRegex_ = /^\\r?\\n\\r?\\n/;
  276. diff_match_patch.prototype.diff_cleanupEfficiency = function(a) {
  277.     for (var b = !1, c = [], d = 0, e = null, f = 0, g = !1, h = !1, l = !1, k = !1; f < a.length;) a[f][0] == DIFF_EQUAL ? (a[f][1].length < this.Diff_EditCost && (l || k) ? (c[d++] = f, g = l, h = k, e = a[f][1]) : (d = 0, e = null), l = k = !1) : (a[f][0] == DIFF_DELETE ? k = !0 : l = !0, e && (g && h && l && k || e.length < this.Diff_EditCost / 2 && 3 == g + h + l + k) && (a.splice(c[d - 1], 0, new diff_match_patch.Diff(DIFF_DELETE, e)), a[c[d - 1] + 1][0] = DIFF_INSERT, d--, e = null, g && h ? (l = k = !0, d = 0) : (d--, f = 0 < d ? c[d - 1] : -1, l = k = !1), b = !0)), f++;
  278.     b && this.diff_cleanupMerge(a)
  279. };
  280. diff_match_patch.prototype.diff_cleanupMerge = function(a) {
  281.     a.push(new diff_match_patch.Diff(DIFF_EQUAL, ""));
  282.     for (var b = 0, c = 0, d = 0, e = "", f = "", g; b < a.length;) switch (a[b][0]) {
  283.         case DIFF_INSERT:
  284.             d++;
  285.             f += a[b][1];
  286.             b++;
  287.             break;
  288.         case DIFF_DELETE:
  289.             c++;
  290.             e += a[b][1];
  291.             b++;
  292.             break;
  293.         case DIFF_EQUAL:
  294.             1 < c + d ? (0 !== c && 0 !== d && (g = this.diff_commonPrefix(f, e), 0 !== g && (0 < b - c - d && a[b - c - d - 1][0] == DIFF_EQUAL ? a[b - c - d - 1][1] += f.substring(0, g) : (a.splice(0, 0, new diff_match_patch.Diff(DIFF_EQUAL, f.substring(0, g))), b++), f = f.substring(g), e = e.substring(g)),
  295.                 g = this.diff_commonSuffix(f, e), 0 !== g && (a[b][1] = f.substring(f.length - g) + a[b][1], f = f.substring(0, f.length - g), e = e.substring(0, e.length - g))), b -= c + d, a.splice(b, c + d), e.length && (a.splice(b, 0, new diff_match_patch.Diff(DIFF_DELETE, e)), b++), f.length && (a.splice(b, 0, new diff_match_patch.Diff(DIFF_INSERT, f)), b++), b++) : 0 !== b && a[b - 1][0] == DIFF_EQUAL ? (a[b - 1][1] += a[b][1], a.splice(b, 1)) : b++, c = d = 0, f = e = ""
  296.     }
  297.     "" === a[a.length - 1][1] && a.pop();
  298.     c = !1;
  299.     for (b = 1; b < a.length - 1;) a[b - 1][0] == DIFF_EQUAL && a[b + 1][0] == DIFF_EQUAL && (a[b][1].substring(a[b][1].length -
  300.         a[b - 1][1].length) == a[b - 1][1] ? (a[b][1] = a[b - 1][1] + a[b][1].substring(0, a[b][1].length - a[b - 1][1].length), a[b + 1][1] = a[b - 1][1] + a[b + 1][1], a.splice(b - 1, 1), c = !0) : a[b][1].substring(0, a[b + 1][1].length) == a[b + 1][1] && (a[b - 1][1] += a[b + 1][1], a[b][1] = a[b][1].substring(a[b + 1][1].length) + a[b + 1][1], a.splice(b + 1, 1), c = !0)), b++;
  301.     c && this.diff_cleanupMerge(a)
  302. };
  303. diff_match_patch.prototype.diff_xIndex = function(a, b) {
  304.     var c = 0,
  305.         d = 0,
  306.         e = 0,
  307.         f = 0,
  308.         g;
  309.     for (g = 0; g < a.length; g++) {
  310.         a[g][0] !== DIFF_INSERT && (c += a[g][1].length);
  311.         a[g][0] !== DIFF_DELETE && (d += a[g][1].length);
  312.         if (c > b) break;
  313.         e = c;
  314.         f = d
  315.     }
  316.     return a.length != g && a[g][0] === DIFF_DELETE ? f : f + (b - e)
  317. };
  318. diff_match_patch.prototype.diff_prettyHtml = function(a) {
  319.     for (var b = [], c = /&/g, d = /</g, e = />/g, f = /\\n/g, g = 0; g < a.length; g++) {
  320.         var h = a[g][0],
  321.             l = a[g][1].replace(c, "&").replace(d, "<").replace(e, ">").replace(f, "¶<br>");
  322.         switch (h) {
  323.             case DIFF_INSERT:
  324.                 b[g] = \'<ins style="background:#e6ffe6;">\' + l + "</ins>";
  325.                 break;
  326.             case DIFF_DELETE:
  327.                 b[g] = \'<del style="background:#ffe6e6;">\' + l + "</del>";
  328.                 break;
  329.             case DIFF_EQUAL:
  330.                 b[g] = "<span>" + l + "</span>"
  331.         }
  332.     }
  333.     return b.join("")
  334. };
  335. diff_match_patch.prototype.diff_text1 = function(a) {
  336.     for (var b = [], c = 0; c < a.length; c++) a[c][0] !== DIFF_INSERT && (b[c] = a[c][1]);
  337.     return b.join("")
  338. };
  339. diff_match_patch.prototype.diff_text2 = function(a) {
  340.     for (var b = [], c = 0; c < a.length; c++) a[c][0] !== DIFF_DELETE && (b[c] = a[c][1]);
  341.     return b.join("")
  342. };
  343. diff_match_patch.prototype.diff_levenshtein = function(a) {
  344.     for (var b = 0, c = 0, d = 0, e = 0; e < a.length; e++) {
  345.         var f = a[e][1];
  346.         switch (a[e][0]) {
  347.             case DIFF_INSERT:
  348.                 c += f.length;
  349.                 break;
  350.             case DIFF_DELETE:
  351.                 d += f.length;
  352.                 break;
  353.             case DIFF_EQUAL:
  354.                 b += Math.max(c, d), d = c = 0
  355.         }
  356.     }
  357.     return b += Math.max(c, d)
  358. };
  359. diff_match_patch.prototype.diff_toDelta = function(a) {
  360.     for (var b = [], c = 0; c < a.length; c++) switch (a[c][0]) {
  361.         case DIFF_INSERT:
  362.             b[c] = "+" + encodeURI(a[c][1]);
  363.             break;
  364.         case DIFF_DELETE:
  365.             b[c] = "-" + a[c][1].length;
  366.             break;
  367.         case DIFF_EQUAL:
  368.             b[c] = "=" + a[c][1].length
  369.     }
  370.     return b.join("\\t").replace(/%20/g, " ")
  371. };
  372. diff_match_patch.prototype.diff_fromDelta = function(a, b) {
  373.     for (var c = [], d = 0, e = 0, f = b.split(/\\t/g), g = 0; g < f.length; g++) {
  374.         var h = f[g].substring(1);
  375.         switch (f[g].charAt(0)) {
  376.             case "+":
  377.                 try {
  378.                     c[d++] = new diff_match_patch.Diff(DIFF_INSERT, decodeURI(h))
  379.                 } catch (k) {
  380.                     throw Error("Illegal escape in diff_fromDelta: " + h);
  381.                 }
  382.                 break;
  383.             case "-":
  384.             case "=":
  385.                 var l = parseInt(h, 10);
  386.                 if (isNaN(l) || 0 > l) throw Error("Invalid number in diff_fromDelta: " + h);
  387.                 h = a.substring(e, e += l);
  388.                 "=" == f[g].charAt(0) ? c[d++] = new diff_match_patch.Diff(DIFF_EQUAL, h) : c[d++] =
  389.                     new diff_match_patch.Diff(DIFF_DELETE, h);
  390.                 break;
  391.             default:
  392.                 if (f[g]) throw Error("Invalid diff operation in diff_fromDelta: " + f[g]);
  393.         }
  394.     }
  395.     if (e != a.length) throw Error("Delta length (" + e + ") does not equal source text length (" + a.length + ").");
  396.     return c
  397. };
  398. diff_match_patch.prototype.match_main = function(a, b, c) {
  399.     if (null == a || null == b || null == c) throw Error("Null input. (match_main)");
  400.     c = Math.max(0, Math.min(c, a.length));
  401.     return a == b ? 0 : a.length ? a.substring(c, c + b.length) == b ? c : this.match_bitap_(a, b, c) : -1
  402. };
  403. diff_match_patch.prototype.match_bitap_ = function(a, b, c) {
  404.     function d(a, d) {
  405.         var e = a / b.length,
  406.             g = Math.abs(c - d);
  407.         return f.Match_Distance ? e + g / f.Match_Distance : g ? 1 : e
  408.     }
  409.     if (b.length > this.Match_MaxBits) throw Error("Pattern too long for this browser.");
  410.     var e = this.match_alphabet_(b),
  411.         f = this,
  412.         g = this.Match_Threshold,
  413.         h = a.indexOf(b, c); - 1 != h && (g = Math.min(d(0, h), g), h = a.lastIndexOf(b, c + b.length), -1 != h && (g = Math.min(d(0, h), g)));
  414.     var l = 1 << b.length - 1;
  415.     h = -1;
  416.     for (var k, m, p = b.length + a.length, x, w = 0; w < b.length; w++) {
  417.         k = 0;
  418.         for (m = p; k < m;) d(w,
  419.             c + m) <= g ? k = m : p = m, m = Math.floor((p - k) / 2 + k);
  420.         p = m;
  421.         k = Math.max(1, c - m + 1);
  422.         var q = Math.min(c + m, a.length) + b.length;
  423.         m = Array(q + 2);
  424.         for (m[q + 1] = (1 << w) - 1; q >= k; q--) {
  425.             var t = e[a.charAt(q - 1)];
  426.             m[q] = 0 === w ? (m[q + 1] << 1 | 1) & t : (m[q + 1] << 1 | 1) & t | (x[q + 1] | x[q]) << 1 | 1 | x[q + 1];
  427.             if (m[q] & l && (t = d(w, q - 1), t <= g))
  428.                 if (g = t, h = q - 1, h > c) k = Math.max(1, 2 * c - h);
  429.                 else break
  430.         }
  431.         if (d(w + 1, c) > g) break;
  432.         x = m
  433.     }
  434.     return h
  435. };
  436. diff_match_patch.prototype.match_alphabet_ = function(a) {
  437.     for (var b = {}, c = 0; c < a.length; c++) b[a.charAt(c)] = 0;
  438.     for (c = 0; c < a.length; c++) b[a.charAt(c)] |= 1 << a.length - c - 1;
  439.     return b
  440. };
  441. diff_match_patch.prototype.patch_addContext_ = function(a, b) {
  442.     if (0 != b.length) {
  443.         if (null === a.start2) throw Error("patch not initialized");
  444.         for (var c = b.substring(a.start2, a.start2 + a.length1), d = 0; b.indexOf(c) != b.lastIndexOf(c) && c.length < this.Match_MaxBits - this.Patch_Margin - this.Patch_Margin;) d += this.Patch_Margin, c = b.substring(a.start2 - d, a.start2 + a.length1 + d);
  445.         d += this.Patch_Margin;
  446.         (c = b.substring(a.start2 - d, a.start2)) && a.diffs.unshift(new diff_match_patch.Diff(DIFF_EQUAL, c));
  447.         (d = b.substring(a.start2 + a.length1,
  448.             a.start2 + a.length1 + d)) && a.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL, d));
  449.         a.start1 -= c.length;
  450.         a.start2 -= c.length;
  451.         a.length1 += c.length + d.length;
  452.         a.length2 += c.length + d.length
  453.     }
  454. };
  455. diff_match_patch.prototype.patch_make = function(a, b, c) {
  456.     if ("string" == typeof a && "string" == typeof b && "undefined" == typeof c) {
  457.         var d = a;
  458.         b = this.diff_main(d, b, !0);
  459.         2 < b.length && (this.diff_cleanupSemantic(b), this.diff_cleanupEfficiency(b))
  460.     } else if (a && "object" == typeof a && "undefined" == typeof b && "undefined" == typeof c) b = a, d = this.diff_text1(b);
  461.     else if ("string" == typeof a && b && "object" == typeof b && "undefined" == typeof c) d = a;
  462.     else if ("string" == typeof a && "string" == typeof b && c && "object" == typeof c) d = a, b = c;
  463.     else throw Error("Unknown call format to patch_make.");
  464.     if (0 === b.length) return [];
  465.     c = [];
  466.     a = new diff_match_patch.patch_obj;
  467.     for (var e = 0, f = 0, g = 0, h = d, l = 0; l < b.length; l++) {
  468.         var k = b[l][0],
  469.             m = b[l][1];
  470.         e || k === DIFF_EQUAL || (a.start1 = f, a.start2 = g);
  471.         switch (k) {
  472.             case DIFF_INSERT:
  473.                 a.diffs[e++] = b[l];
  474.                 a.length2 += m.length;
  475.                 d = d.substring(0, g) + m + d.substring(g);
  476.                 break;
  477.             case DIFF_DELETE:
  478.                 a.length1 += m.length;
  479.                 a.diffs[e++] = b[l];
  480.                 d = d.substring(0, g) + d.substring(g + m.length);
  481.                 break;
  482.             case DIFF_EQUAL:
  483.                 m.length <= 2 * this.Patch_Margin && e && b.length != l + 1 ? (a.diffs[e++] = b[l], a.length1 += m.length, a.length2 += m.length) :
  484.                     m.length >= 2 * this.Patch_Margin && e && (this.patch_addContext_(a, h), c.push(a), a = new diff_match_patch.patch_obj, e = 0, h = d, f = g)
  485.         }
  486.         k !== DIFF_INSERT && (f += m.length);
  487.         k !== DIFF_DELETE && (g += m.length)
  488.     }
  489.     e && (this.patch_addContext_(a, h), c.push(a));
  490.     return c
  491. };
  492. diff_match_patch.prototype.patch_deepCopy = function(a) {
  493.     for (var b = [], c = 0; c < a.length; c++) {
  494.         var d = a[c],
  495.             e = new diff_match_patch.patch_obj;
  496.         e.diffs = [];
  497.         for (var f = 0; f < d.diffs.length; f++) e.diffs[f] = new diff_match_patch.Diff(d.diffs[f][0], d.diffs[f][1]);
  498.         e.start1 = d.start1;
  499.         e.start2 = d.start2;
  500.         e.length1 = d.length1;
  501.         e.length2 = d.length2;
  502.         b[c] = e
  503.     }
  504.     return b
  505. };
  506. diff_match_patch.prototype.patch_apply = function(a, b) {
  507.     if (0 == a.length) return [b, []];
  508.     a = this.patch_deepCopy(a);
  509.     var c = this.patch_addPadding(a);
  510.     b = c + b + c;
  511.     this.patch_splitMax(a);
  512.     for (var d = 0, e = [], f = 0; f < a.length; f++) {
  513.         var g = a[f].start2 + d,
  514.             h = this.diff_text1(a[f].diffs),
  515.             l = -1;
  516.         if (h.length > this.Match_MaxBits) {
  517.             var k = this.match_main(b, h.substring(0, this.Match_MaxBits), g); - 1 != k && (l = this.match_main(b, h.substring(h.length - this.Match_MaxBits), g + h.length - this.Match_MaxBits), -1 == l || k >= l) && (k = -1)
  518.         } else k = this.match_main(b, h,
  519.             g);
  520.         if (-1 == k) e[f] = !1, d -= a[f].length2 - a[f].length1;
  521.         else if (e[f] = !0, d = k - g, g = -1 == l ? b.substring(k, k + h.length) : b.substring(k, l + this.Match_MaxBits), h == g) b = b.substring(0, k) + this.diff_text2(a[f].diffs) + b.substring(k + h.length);
  522.         else if (g = this.diff_main(h, g, !1), h.length > this.Match_MaxBits && this.diff_levenshtein(g) / h.length > this.Patch_DeleteThreshold) e[f] = !1;
  523.         else {
  524.             this.diff_cleanupSemanticLossless(g);
  525.             h = 0;
  526.             var m;
  527.             for (l = 0; l < a[f].diffs.length; l++) {
  528.                 var p = a[f].diffs[l];
  529.                 p[0] !== DIFF_EQUAL && (m = this.diff_xIndex(g, h));
  530.                 p[0] ===
  531.                     DIFF_INSERT ? b = b.substring(0, k + m) + p[1] + b.substring(k + m) : p[0] === DIFF_DELETE && (b = b.substring(0, k + m) + b.substring(k + this.diff_xIndex(g, h + p[1].length)));
  532.                 p[0] !== DIFF_DELETE && (h += p[1].length)
  533.             }
  534.         }
  535.     }
  536.     b = b.substring(c.length, b.length - c.length);
  537.     return [b, e]
  538. };
  539. diff_match_patch.prototype.patch_addPadding = function(a) {
  540.     for (var b = this.Patch_Margin, c = "", d = 1; d <= b; d++) c += String.fromCharCode(d);
  541.     for (d = 0; d < a.length; d++) a[d].start1 += b, a[d].start2 += b;
  542.     d = a[0];
  543.     var e = d.diffs;
  544.     if (0 == e.length || e[0][0] != DIFF_EQUAL) e.unshift(new diff_match_patch.Diff(DIFF_EQUAL, c)), d.start1 -= b, d.start2 -= b, d.length1 += b, d.length2 += b;
  545.     else if (b > e[0][1].length) {
  546.         var f = b - e[0][1].length;
  547.         e[0][1] = c.substring(e[0][1].length) + e[0][1];
  548.         d.start1 -= f;
  549.         d.start2 -= f;
  550.         d.length1 += f;
  551.         d.length2 += f
  552.     }
  553.     d = a[a.length - 1];
  554.     e = d.diffs;
  555.     0 == e.length || e[e.length - 1][0] != DIFF_EQUAL ? (e.push(new diff_match_patch.Diff(DIFF_EQUAL, c)), d.length1 += b, d.length2 += b) : b > e[e.length - 1][1].length && (f = b - e[e.length - 1][1].length, e[e.length - 1][1] += c.substring(0, f), d.length1 += f, d.length2 += f);
  556.     return c
  557. };
  558. diff_match_patch.prototype.patch_splitMax = function(a) {
  559.     for (var b = this.Match_MaxBits, c = 0; c < a.length; c++)
  560.         if (!(a[c].length1 <= b)) {
  561.             var d = a[c];
  562.             a.splice(c--, 1);
  563.             for (var e = d.start1, f = d.start2, g = ""; 0 !== d.diffs.length;) {
  564.                 var h = new diff_match_patch.patch_obj,
  565.                     l = !0;
  566.                 h.start1 = e - g.length;
  567.                 h.start2 = f - g.length;
  568.                 "" !== g && (h.length1 = h.length2 = g.length, h.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL, g)));
  569.                 for (; 0 !== d.diffs.length && h.length1 < b - this.Patch_Margin;) {
  570.                     g = d.diffs[0][0];
  571.                     var k = d.diffs[0][1];
  572.                     g === DIFF_INSERT ? (h.length2 +=
  573.                         k.length, f += k.length, h.diffs.push(d.diffs.shift()), l = !1) : g === DIFF_DELETE && 1 == h.diffs.length && h.diffs[0][0] == DIFF_EQUAL && k.length > 2 * b ? (h.length1 += k.length, e += k.length, l = !1, h.diffs.push(new diff_match_patch.Diff(g, k)), d.diffs.shift()) : (k = k.substring(0, b - h.length1 - this.Patch_Margin), h.length1 += k.length, e += k.length, g === DIFF_EQUAL ? (h.length2 += k.length, f += k.length) : l = !1, h.diffs.push(new diff_match_patch.Diff(g, k)), k == d.diffs[0][1] ? d.diffs.shift() : d.diffs[0][1] = d.diffs[0][1].substring(k.length))
  574.                 }
  575.                 g = this.diff_text2(h.diffs);
  576.                 g = g.substring(g.length - this.Patch_Margin);
  577.                 k = this.diff_text1(d.diffs).substring(0, this.Patch_Margin);
  578.                 "" !== k && (h.length1 += k.length, h.length2 += k.length, 0 !== h.diffs.length && h.diffs[h.diffs.length - 1][0] === DIFF_EQUAL ? h.diffs[h.diffs.length - 1][1] += k : h.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL, k)));
  579.                 l || a.splice(++c, 0, h)
  580.             }
  581.         }
  582. };
  583. diff_match_patch.prototype.patch_toText = function(a) {
  584.     for (var b = [], c = 0; c < a.length; c++) b[c] = a[c];
  585.     return b.join("")
  586. };
  587. diff_match_patch.prototype.patch_fromText = function(a) {
  588.     var b = [];
  589.     if (!a) return b;
  590.     a = a.split("\\n");
  591.     for (var c = 0, d = /^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$/; c < a.length;) {
  592.         var e = a[c].match(d);
  593.         if (!e) throw Error("Invalid patch string: " + a[c]);
  594.         var f = new diff_match_patch.patch_obj;
  595.         b.push(f);
  596.         f.start1 = parseInt(e[1], 10);
  597.         "" === e[2] ? (f.start1--, f.length1 = 1) : "0" == e[2] ? f.length1 = 0 : (f.start1--, f.length1 = parseInt(e[2], 10));
  598.         f.start2 = parseInt(e[3], 10);
  599.         "" === e[4] ? (f.start2--, f.length2 = 1) : "0" == e[4] ? f.length2 = 0 : (f.start2--, f.length2 =
  600.             parseInt(e[4], 10));
  601.         for (c++; c < a.length;) {
  602.             e = a[c].charAt(0);
  603.             try {
  604.                 var g = decodeURI(a[c].substring(1))
  605.             } catch (h) {
  606.                 throw Error("Illegal escape in patch_fromText: " + g);
  607.             }
  608.             if ("-" == e) f.diffs.push(new diff_match_patch.Diff(DIFF_DELETE, g));
  609.             else if ("+" == e) f.diffs.push(new diff_match_patch.Diff(DIFF_INSERT, g));
  610.             else if (" " == e) f.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL, g));
  611.             else if ("@" == e) break;
  612.             else if ("" !== e) throw Error(\'Invalid patch mode "\' + e + \'" in: \' + g);
  613.             c++
  614.         }
  615.     }
  616.     return b
  617. };
  618. diff_match_patch.patch_obj = function() {
  619.     this.diffs = [];
  620.     this.start2 = this.start1 = null;
  621.     this.length2 = this.length1 = 0
  622. };
  623. diff_match_patch.patch_obj.prototype.toString = function() {
  624.     for (var a = ["@@ -" + (0 === this.length1 ? this.start1 + ",0" : 1 == this.length1 ? this.start1 + 1 : this.start1 + 1 + "," + this.length1) + " +" + (0 === this.length2 ? this.start2 + ",0" : 1 == this.length2 ? this.start2 + 1 : this.start2 + 1 + "," + this.length2) + " @@\\n"], b, c = 0; c < this.diffs.length; c++) {
  625.         switch (this.diffs[c][0]) {
  626.             case DIFF_INSERT:
  627.                 b = "+";
  628.                 break;
  629.             case DIFF_DELETE:
  630.                 b = "-";
  631.                 break;
  632.             case DIFF_EQUAL:
  633.                 b = " "
  634.         }
  635.         a[c + 1] = b + encodeURI(this.diffs[c][1]) + "\\n"
  636.     }
  637.     return a.join("").replace(/%20/g, " ")
  638. };
  639. this.diff_match_patch = diff_match_patch;
  640. this.DIFF_DELETE = DIFF_DELETE;
  641. this.DIFF_INSERT = DIFF_INSERT;
  642. this.DIFF_EQUAL = DIFF_EQUAL;';

Editor

You can edit this paste and save as new: