Delta encoding - strings (Programming logic - no specific language) -
can put code delta encoding strings.
for example, if initial string
"cat , dogs"
and later string
"cats , dogs"
i should able difference between string versions efficiently. if can put code here big paragraphs can sent delta encoding less data.
i found answer, wanted share all.
var oldstring = "this sample of string test delta encoding. infact, own login.dsfdsfdsfdsfsdfsdfdsfdsfsdfsdfds dsfds fds fddsf"; var newstring = "dsfdsthissdf sample of string test x dedsfdsflta encoding sdfds decoding. infact, own sadsadsadsad" var result = ""; var changes = getchanges(oldstring, newstring); var string = getoriginal(oldstring, changes); function getchanges(os, ns) { var addedindex = false; var changes = []; var obj = []; var oi = 0, ni = 0; while (oi < oldstring.length && ni < newstring.length) { if (newstring.charat(ni) != oldstring.charat(oi)) { if (!addedindex) { obj.push(oi); obj.push(newstring.charat(ni)); addedindex = true; } else { obj[1] += newstring.charat(ni); } ni++; } else { if (addedindex) { changes.push(obj); obj = []; addedindex = false; } oi++; ni++; } }; if (addedindex) { changes.push(obj); obj = []; addedindex = false; } obj = []; if (ni == newstring.length) { obj.push(-1 * oi); changes.push(obj); } if (oi == oldstring.length) { obj.push(ni); obj.push(newstring.substring(ni)); changes.push(obj); } return changes; } function getoriginal(os, changes) { var result = os; (var = changes.length - 1; >= 0 ; i--) { if (changes[i][0] < 0) { result = result.substring(0, -1 * changes[i][0]); } else { result = result.substring(0, changes[i][0]) + changes[i][1] + result.substring(changes[i][0]); } }; return result; } console.log(string); console.log(newstring);
Comments
Post a Comment