Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can just concatenate your changes and keep track of when they were inserted/removed. Note that the numbers give the index within the string (and note that chars removed do not increase index).</p> <p><em>Step 1:</em> <code>0-1) &lt;insert 'cat' at 0&gt;</code></p> <ul> <li><code>[0] c inserted at step 1</code></li> <li><code>[1] a inserted at step 1</code></li> <li><code>[2] t inserted at step 1</code></li> </ul> <p><em>Step 2:</em> <code>1-2) &lt;insert 'r' at 1&gt; &lt;insert 'e' at 3&gt;</code></p> <ul> <li><code>[0] c inserted at step 1</code></li> <li><code>[1] r inserted at step 2</code> &lt;= this was inserted here in this step at position 1</li> <li><code>[2] a inserted at step 1</code></li> <li><code>[3] t inserted at step 1</code></li> <li><code>[4] e inserted at step 2</code> &lt;= this was inserted here in this step at position 3</li> </ul> <p>Note that the position of 'e' was actually shifted to 4 due to the other insertion.</p> <p><em>Step 3:</em> <code>2-3) &lt;remove from 3&gt; &lt;insert 'n' at 3&gt;</code> &lt;= I changed this one to the minimal diff</p> <ul> <li><code>[0] c inserted at step 1</code></li> <li><code>[1] r inserted at step 2</code></li> <li><code>[2] a inserted at step 1</code></li> <li><code>[3] t inserted at step 1, removed at step 3</code> &lt;= does no longer count, thus the next index is the same</li> <li><code>[3] n inserted at step 3</code> &lt;= this was inserted here in this step at position 3</li> <li><code>[4] e inserted at step 2</code></li> </ul> <p>So the basic algorithm is:</p> <ul> <li>maintain a list of characters, together with step of insertion and step of removal</li> <li>for each step do <ul> <li>decompose your diff from this step into single char inserts and removals</li> <li>for a insert of a new char X at position P do the following: <ul> <li>insert the new char X after the latest char in your list with index P, set step of insertion to current step and adjust indices of following items (i.e. add one).</li> </ul></li> <li>for a deletion at a position P do <ul> <li>mark the char in your list with index P (there is only one of them which still is present, i.e. where the removal step is not set) by setting the removal step to the current step and adjust later indices (i.e. subtract one)</li> </ul></li> </ul></li> </ul> <p>In both cases note that previous insertions/removals within this step may shift the position of current action (one way to easily work around this is to do insertions/removals backwards from end of string to start).</p> <p>The result will be a list of changes as you specified in your question. For lots of changes it might become quite unreadable but it will nevertheless describe the complete history of your text.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload