-2

Can anyone put the code for delta encoding for strings.

for example, If the initial string is

"Cat and Dogs"

and later the string is

"Cats and Dogs"

I should be able to get the difference between the string versions very efficiently. If anyone can put the code here so that very big paragraphs can be sent with delta encoding with very less data.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Venu prasad H S
  • 231
  • 1
  • 8
  • No idea what you are asking. Maybe you should Google `'diff'` .. If that is what you mean..well, that is anything but trivial, even if it __looks so__ for the human eye! See [here](http://stackoverflow.com/questions/24887238/how-to-compare-two-rich-text-box-contents-and-highlight-the-characters-that-are/24970638?s=1|0.2755#24970638) for in implementation that should make it clear that there is nothing 'very efficient' about diffing and nobody could or will 'put code here' .. – TaW Jul 04 '16 at 13:27
  • I'm looking for a logic to construct the final string with the old string and the delta(difference between the old and the new version). where delta must be possibly small in size so that I can transfer large paragraphs with ease. – Venu prasad H S Jul 04 '16 at 13:37
  • I found the answer Taw, Look into the answer. – Venu prasad H S Jul 04 '16 at 17:53

1 Answers1

1

I found the answer, I wanted to share it to all.

var oldString = "This is just a sample of string to test the delta encoding. Infact, with my own login.dsfdsfdsfdsfsdfsdfdsfdsfsdfsdfds  dsfds fds fddsf";
var newString = "dsfdsThissdf is just a sample of string to test X the dedsfdsflta encoding sdfds decoding. Infact, with my 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;
    for (var i = changes.length - 1; i >= 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);
Venu prasad H S
  • 231
  • 1
  • 8