3

In Google's diff_match_patch I found the following code:

best_common = shorttext.substring(j - suffixLength, j) +
            shorttext.substring(j, j + prefixLength);

But isn't this the same as:

best_common = shorttext.substring(j - suffixLength, j + prefixLength);

?

If you want to see this code in it's natural habitat, please look at the source:

https://code.google.com/p/google-diff-match-patch/source/browse/trunk/javascript/diff_match_patch_uncompressed.js

Look for line 673 and 674.

4

2 回答 2

1

是的,它们将与您的情况相同suffixLength并且prefixLength保证是非负的。

于 2013-06-05T20:35:57.087 回答
1

对于大多数情况,这些是等效的。但是,有一个边缘情况需要考虑。

对于以下示例,我们假设shorttext == "abcdefghijklmnopqrstuvwxyz".

prefixLengthsuffixLength为负

让和。j = 12_ 然后prefixLength = -4suffixLength = -5

shorttext.substring(j - suffixLength, j) + shorttext.substring(j, j + prefixLength)
// == "mnopq" + "jkl"
shorttext.substring(j - suffixLength, j + prefixLength)
// == "jklmnopq"
于 2013-06-05T20:52:43.850 回答