0

If I have two strings: "Hello, this is a string!" and "Hello world, this is a string!"

And I track a highlighted word with { index: 13, length: 4 }, how can I diff the two strings such that I can know to add 6 to the index since it's moved over 6 characters? I have a basic diffing algorithm that tells me that ADD: ' world' takes place at count 5.

The diff function I have would output:

[
  { count: 5, value: 'Hello' },
  { count: 6, added: true, value: ' world' },
  ...
]

It is as simple as tracking those changes and adding it to the index, or am I missing something?

Everett Glovier
  • 328
  • 3
  • 15
  • Show your code of your `I have a basic diffing algorithm`. – GetSet Apr 16 '20 at 03:06
  • Do you mean you are feeding both values into a function to get a difference? Or that you are trying to keep track of changes made through time (event sourcing)? – Philip Rollins Apr 16 '20 at 03:08
  • If you know the location of the addition and its length, then just check if it was added before the highlight index, and if so increment the highlight index by the length of the addition. Also have you considered the case where something is added in the middle of a highlight (highlight index <= addition index < highlight index + highlight length)? – Shivashriganesh Mahato Apr 16 '20 at 03:21
  • @ShivashriganeshMahato i would have to add to the length or remove the highlight completely. At least that was my thought? – Everett Glovier Apr 16 '20 at 03:23
  • Completely up to your preference how you want to implement your application. If you want to add to the length, then your logic comes down 3 cases: addition is before highlight, addition is between highlight, and addition is after highlight. If the first, add to the highlight index. If the second, add to the highlight length. If neither, do nothing. – Shivashriganesh Mahato Apr 16 '20 at 03:28
  • @EverettGlovier not sure of your exact use case, but might be worthwhile to take a look at https://stackoverflow.com/questions/57102484, as this shows the use of a diff function ( PatienceDiff and PatienceDiffPlus in particular ) which provides a result that already indicates where to insert or remove characters in order to achieve the result of the second string. – Trentium Apr 17 '20 at 18:07

0 Answers0