Skip to content

Commit

Permalink
fix to work properly even if Object.prototype has been extended (than…
Browse files Browse the repository at this point in the history
…ks to Max Al-Shawabkeh!)
  • Loading branch information
cemerick committed Jan 25, 2012
1 parent d1f0bcf commit 03a300c
Showing 1 changed file with 93 additions and 81 deletions.
174 changes: 93 additions & 81 deletions difflib.js
100644 → 100755
Original file line number Original file line Diff line number Diff line change
@@ -1,37 +1,38 @@
/* /***
This is part of jsdifflib v1.0. <http://github.com/cemerick/jsdifflib> This is part of jsdifflib v1.0. <http://snowtide.com/jsdifflib>
Copyright 2007 - 2011 Chas Emerick <cemerick@snowtide.com>. All rights reserved. Copyright (c) 2007, Snowtide Informatics Systems, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are Redistribution and use in source and binary forms, with or without modification,
permitted provided that the following conditions are met: are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of * Redistributions of source code must retain the above copyright notice, this
conditions and the following disclaimer. list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Snowtide Informatics Systems nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
2. Redistributions in binary form must reproduce the above copyright notice, this list THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
of conditions and the following disclaimer in the documentation and/or other materials EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
provided with the distribution. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
THIS SOFTWARE IS PROVIDED BY Chas Emerick ``AS IS'' AND ANY EXPRESS OR IMPLIED INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Chas Emerick OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON DAMAGE.
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ***/
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF /* Author: Chas Emerick <cemerick@snowtide.com> */
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Chas Emerick.
*/
__whitespace = {" ":true, "\t":true, "\n":true, "\f":true, "\r":true}; __whitespace = {" ":true, "\t":true, "\n":true, "\f":true, "\r":true};


difflib = { difflib = {
defaultJunkFunction: function (c) { defaultJunkFunction: function (c) {
return c in __whitespace; return __whitespace.hasOwnProperty(c);
}, },


stripLinebreaks: function (str) { return str.replace(/^[\n\r]*|[\n\r]*$/g, ""); }, stripLinebreaks: function (str) { return str.replace(/^[\n\r]*|[\n\r]*$/g, ""); },
Expand Down Expand Up @@ -87,12 +88,12 @@ difflib = {
// is in the dict (js object) provided to this function; replaces being able to // is in the dict (js object) provided to this function; replaces being able to
// carry around dict.has_key in python... // carry around dict.has_key in python...
__isindict: function (dict) { __isindict: function (dict) {
return function (key) { return key in dict; }; return function (key) { return dict.hasOwnProperty(key); };
}, },


// replacement for python's dict.get function -- need easy default values // replacement for python's dict.get function -- need easy default values
__dictget: function (dict, key, defaultValue) { __dictget: function (dict, key, defaultValue) {
return key in dict ? dict[key] : defaultValue; return dict.hasOwnProperty(key) ? dict[key] : defaultValue;
}, },


SequenceMatcher: function (a, b, isjunk) { SequenceMatcher: function (a, b, isjunk) {
Expand Down Expand Up @@ -121,7 +122,7 @@ difflib = {
var populardict = {}; var populardict = {};
for (var i = 0; i < b.length; i++) { for (var i = 0; i < b.length; i++) {
var elt = b[i]; var elt = b[i];
if (elt in b2j) { if (b2j.hasOwnProperty(elt)) {
var indices = b2j[elt]; var indices = b2j[elt];
if (n >= 200 && indices.length * 100 > n) { if (n >= 200 && indices.length * 100 > n) {
populardict[elt] = 1; populardict[elt] = 1;
Expand All @@ -134,20 +135,23 @@ difflib = {
} }
} }


for (var elt in populardict) for (var elt in populardict) {
delete b2j[elt]; if (populardict.hasOwnProperty(elt)) {
delete b2j[elt];
}
}


var isjunk = this.isjunk; var isjunk = this.isjunk;
var junkdict = {}; var junkdict = {};
if (isjunk) { if (isjunk) {
for (var elt in populardict) { for (var elt in populardict) {
if (isjunk(elt)) { if (populardict.hasOwnProperty(elt) && isjunk(elt)) {
junkdict[elt] = 1; junkdict[elt] = 1;
delete populardict[elt]; delete populardict[elt];
} }
} }
for (var elt in b2j) { for (var elt in b2j) {
if (isjunk(elt)) { if (b2j.hasOwnProperty(elt) && isjunk(elt)) {
junkdict[elt] = 1; junkdict[elt] = 1;
delete b2j[elt]; delete b2j[elt];
} }
Expand All @@ -174,14 +178,16 @@ difflib = {
var newj2len = {}; var newj2len = {};
var jdict = difflib.__dictget(b2j, a[i], nothing); var jdict = difflib.__dictget(b2j, a[i], nothing);
for (var jkey in jdict) { for (var jkey in jdict) {
j = jdict[jkey]; if (jdict.hasOwnProperty(jkey)) {
if (j < blo) continue; j = jdict[jkey];
if (j >= bhi) break; if (j < blo) continue;
newj2len[j] = k = difflib.__dictget(j2len, j - 1, 0) + 1; if (j >= bhi) break;
if (k > bestsize) { newj2len[j] = k = difflib.__dictget(j2len, j - 1, 0) + 1;
besti = i - k + 1; if (k > bestsize) {
bestj = j - k + 1; besti = i - k + 1;
bestsize = k; bestj = j - k + 1;
bestsize = k;
}
} }
} }
j2len = newj2len; j2len = newj2len;
Expand All @@ -206,7 +212,7 @@ difflib = {
} }


while (besti + bestsize < ahi && bestj + bestsize < bhi && isbjunk(b[bestj + bestsize]) && while (besti + bestsize < ahi && bestj + bestsize < bhi && isbjunk(b[bestj + bestsize]) &&
a[besti + bestsize] == b[bestj + bestsize]) { a[besti + bestsize] == b[bestj + bestsize]) {
bestsize++; bestsize++;
} }


Expand Down Expand Up @@ -246,17 +252,19 @@ difflib = {
var i1 = j1 = k1 = block = 0; var i1 = j1 = k1 = block = 0;
var non_adjacent = []; var non_adjacent = [];
for (var idx in matching_blocks) { for (var idx in matching_blocks) {
block = matching_blocks[idx]; if (matching_blocks.hasOwnProperty(idx)) {
i2 = block[0]; block = matching_blocks[idx];
j2 = block[1]; i2 = block[0];
k2 = block[2]; j2 = block[1];
if (i1 + k1 == i2 && j1 + k1 == j2) { k2 = block[2];
k1 += k2; if (i1 + k1 == i2 && j1 + k1 == j2) {
} else { k1 += k2;
if (k1) non_adjacent.push([i1, j1, k1]); } else {
i1 = i2; if (k1) non_adjacent.push([i1, j1, k1]);
j1 = j2; i1 = i2;
k1 = k2; j1 = j2;
k1 = k2;
}
} }
} }


Expand All @@ -276,23 +284,25 @@ difflib = {
var block, ai, bj, size, tag; var block, ai, bj, size, tag;
var blocks = this.get_matching_blocks(); var blocks = this.get_matching_blocks();
for (var idx in blocks) { for (var idx in blocks) {
block = blocks[idx]; if (blocks.hasOwnProperty(idx)) {
ai = block[0]; block = blocks[idx];
bj = block[1]; ai = block[0];
size = block[2]; bj = block[1];
tag = ''; size = block[2];
if (i < ai && j < bj) { tag = '';
tag = 'replace'; if (i < ai && j < bj) {
} else if (i < ai) { tag = 'replace';
tag = 'delete'; } else if (i < ai) {
} else if (j < bj) { tag = 'delete';
tag = 'insert'; } else if (j < bj) {
tag = 'insert';
}
if (tag) answer.push([tag, i, ai, j, bj]);
i = ai + size;
j = bj + size;

if (size) answer.push(['equal', ai, i, bj, j]);
} }
if (tag) answer.push([tag, i, ai, j, bj]);
i = ai + size;
j = bj + size;

if (size) answer.push(['equal', ai, i, bj, j]);
} }


return answer; return answer;
Expand Down Expand Up @@ -327,19 +337,21 @@ difflib = {
var nn = n + n; var nn = n + n;
var groups = []; var groups = [];
for (var idx in codes) { for (var idx in codes) {
code = codes[idx]; if (codes.hasOwnProperty(idx)) {
tag = code[0]; code = codes[idx];
i1 = code[1]; tag = code[0];
i2 = code[2]; i1 = code[1];
j1 = code[3]; i2 = code[2];
j2 = code[4]; j1 = code[3];
if (tag == 'equal' && i2 - i1 > nn) { j2 = code[4];
groups.push([tag, i1, Math.min(i2, i1 + n), j1, Math.min(j2, j1 + n)]); if (tag == 'equal' && i2 - i1 > nn) {
i1 = Math.max(i1, i2-n); groups.push([tag, i1, Math.min(i2, i1 + n), j1, Math.min(j2, j1 + n)]);
j1 = Math.max(j1, j2-n); i1 = Math.max(i1, i2-n);
j1 = Math.max(j1, j2-n);
}

groups.push([tag, i1, i2, j1, j2]);
} }

groups.push([tag, i1, i2, j1, j2]);
} }


if (groups && groups[groups.length - 1][0] == 'equal') groups.pop(); if (groups && groups[groups.length - 1][0] == 'equal') groups.pop();
Expand Down

0 comments on commit 03a300c

Please sign in to comment.