From 7ab7081aeddb23aed98c0f0308d48b8062368e09 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 25 Oct 2011 22:22:30 +0100 Subject: [PATCH 1/4] initialise changeLog of new users applyConcurrentDiff crashes when accessing changeLog[df.uid].length otherwise. --- server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server.js b/server.js index 5212ef4..a163fb1 100644 --- a/server.js +++ b/server.js @@ -20,6 +20,7 @@ var handleReq = function(req, res) { } else if(req.url.match(/^\/getDoc/)) { res.writeHead(200, {'Content-Type': 'text/json'}); res.end(JSON.stringify([doc,++lastUser])); + changeLog[lastUser] = []; } else if(df = req.url.match(/^\/getDiff\?(.*)/)) { var uid = unescape(df[1]); var mydiffs = changeLog[uid]; From 783b04a87cb2cf3be647021fce0170c5d82cd8c6 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 25 Oct 2011 22:47:30 +0100 Subject: [PATCH 2/4] changed Content-type of the answer to putDiff to text/json seems appropriate. --- server.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server.js b/server.js index a163fb1..1b2a05d 100644 --- a/server.js +++ b/server.js @@ -24,7 +24,7 @@ var handleReq = function(req, res) { } else if(df = req.url.match(/^\/getDiff\?(.*)/)) { var uid = unescape(df[1]); var mydiffs = changeLog[uid]; - res.writeHead(200, {'Content-Type': 'text/plain'}); + res.writeHead(200, {'Content-Type': 'text/json'}); res.end(JSON.stringify(mydiffs)); changeLog[uid] = []; } else if(df = req.url.match(/^\/putDiff\?(.*)/)) { @@ -64,5 +64,3 @@ var applyConcurrentDiff = function(df) { } http.createServer(handleReq).listen(4242,"127.0.0.1") - - From 75da69fcb9e7d91c42c345696ff5d739c1793e55 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 25 Oct 2011 22:49:27 +0100 Subject: [PATCH 3/4] remove unused functions from the diff library namely makeDiff, makeDel, and makeInsert --- diffLib.js | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/diffLib.js b/diffLib.js index e60f060..ab967ce 100644 --- a/diffLib.js +++ b/diffLib.js @@ -6,11 +6,6 @@ var applyDiff = function(oldStr, diff) { } } -var makeDiff = function(oldStr, newStr) { - if (oldStr.length > newStr.length) return makeDel(oldStr,newStr); - else return makeInsert(oldStr,newStr); -} - var makeDiffList = function(oldStr, newStr) { if (oldStr === newStr) return []; res = []; @@ -29,29 +24,6 @@ var makeDiffList = function(oldStr, newStr) { return res; } -// Requires that oldStr be the same as newStr, but missing a single contiguous block. -// \exists s1,s2,s3 st oldStr === s1+s3 ^ newStr === s1+s2+s3 -var makeInsert = function(oldStr, newStr) { - var res = {type:"insert", content:""}; - for(var i = 0 ; oldStr[i]===newStr[i] && i Date: Tue, 25 Oct 2011 23:08:10 +0100 Subject: [PATCH 4/4] fixed bug in the diff algorithm The previous algo could fail as i + j could be > oldStr.length. This results in a negative delete (urgh). For instance: oldStr = "aaa" newStr = "aabaaa" gives i = 2 and j = 3. --- diffLib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffLib.js b/diffLib.js index ab967ce..69d4f49 100644 --- a/diffLib.js +++ b/diffLib.js @@ -12,7 +12,7 @@ var makeDiffList = function(oldStr, newStr) { // i := the number of equal chars at the beginning of the strings for(var i = 0 ; i< oldStr.length && i< newStr.length && oldStr[i]===newStr[i] ; i++) {} // j := the number of equal chars at the end of the strings - for(var j = 0 ; j< oldStr.length && j< newStr.length && oldStr[oldStr.length-j-1]===newStr[newStr.length-j-1] ; j++) {} + for(var j = 0 ; j< oldStr.length - i && j< newStr.length - i && oldStr[oldStr.length-j-1]===newStr[newStr.length-j-1] ; j++) {} if(i+j!==oldStr.length) { // Some stuff was deleted from oldStr res.push({type:"delete",point:i,length:(oldStr.length-i-j)});