# String Diffing

I’ve wanted to have some wiki-like diffing in my userform textboxes for a while now. Since I’ve been using wikis almost daily, I want the revisioning feature in everything I do. I’m not there yet, but I decided to see what kind of algorithm I would need to do it. I read the Wikipedia article on longest common subsequence and played around with it a little.

This code is called LCSLength in the article. It returns a matrix (2d array) with counts of matching elements at each position. For instance, if you’re diffing “Dick” and “Rick”, they have three letters in common and this table will compute that. It looks like this

 R i c k 0 0 0 0 0 D 0 0 0 0 0 i 0 0 1 1 1 c 0 0 1 2 2 k 0 0 1 2 3

The rest of the functions use this table to figure out what’s what.

This function (called backtrack in the article) traces back through the table and outputs the longest common subsequence. It’s a recursive function (it calls itself) and continually appends letters (or other elements) on to the return string.

When both the i and j counters are zero, it stops calling itself. Otherwise, if the two letters match, it appends the current letter to the end and calls itself using the element up and to the left. If there’s no match, it goes to the larger of the element above (i-1) and the one to the left (j-1). By following the path of the larger numbers through the matrix, it can find the common letters. It’s originally called with the largest i and j – in the above table, it’s called looking at the 3 (the bottom right cell). Here’s how it tracks through the matrix (I’ll use cell references, but it’s not really cells).

1. F6: k=k, so add k to the end of the string.
2. E5: c=c, so add c to the end of the string.
3. D4: i=i, so add i to the end of the string.
4. C3: D <> R so find the larger of C2 or B3
5. C2: i=0 so that’s it.
6. Return “ick”

Thrilling, isn’t it?

This is another recursive function working backward through the matrix. When it finds a match, there’s no prefix. If it’s a new element (in Revised, but not Original) the prefix is a “+”. If it’s a deleted element, you get a “-“. This prints the results to the immediate window. Let’s look at some examples.

This shows the diff on a letter-by-letter basis.

The first line is a printout of the longest common subsequence. The rest is a letter-by-letter diff that shows which elements were added, deleted, and unchanged. We can also diff on words.

Instead of filling the array with letters, I split the string on spaces to get words. Note that I put a leading space in front of each string before the split. The array needs to be 1-based and the Split function is zero based. The array doesn’t actually need to be 1-based, but the first row and column is ignored, so I made sure that it was something I didn’t care about. Once the arrays are filled, everything is the same.

Traditionally, diffing text is done line-by-line. So let’s do that. I found an example essay and made two files; OriginalDiff.txt and RevisedDiff.txt. I changed one thing in Revised and used this code to diff them.

And that’s as far as I got. Next, I need to put the diffs into a database so I can display diffs and revert to prior versions. Or, quite possibly, I’ll lose interest because I don’t have a burning need for this. It’s just something I’ve wanted to do.

## 4 thoughts on “String Diffing”

1. Nigel Heffernan says:

Interesting. You’re looking to identify differences; I’ve had to deal with the related problem of quantifying differences – establishing a numerical ‘edit distance’ between to texts.

The ‘Gold Standard’ for that task is Levenshtein Distance but LCS is a rough-and-ready substitute and a sum-of-common-strings approach works acceptably well in VBA.

Acceptably well is the crux here: VBA doesn’t have a string-builder class and the operations that allocate (and, especially, that concatenate) a string are a serious drag on performance. The perfect approach is to do an element-by-element comparison on the byte arrays and, as you’re already handling files, these are readily to hand:

``` Dim hFile as Long Dim arrBytes() As Byte Dim lngLen as Long Dim strFilePath as String```

``` Open strFilePath For Binary As hFile lngLen = LOF(hFile) ReDim arrBytes(1 To lngLen) Get hFile, , arrBytes Close hFile ```

If you really do want to use the native VBA string-handling – and it’s entirely logical to do this, as it is clearly a string operation – the next-best approach is to leave char-by-char concatenation and comparison to the C community and use the Instr function.

…Which is, of course, all about byte arrays and pointer arithmetic at some deep level. Native string-handling isn’t great, but the VBA.Strings functions are actually very good – straight out of Kernel32 – and letting VBA/Excel act as the co-ordinator and GUI layer for high-performance calculations carried out in other applications is doing what we’re best at.

I digress…

Instr is a faster way of asking “Does this substring exist in the search sample?” and the fastest way of using it to find the longest string by counting down, rather than building up a string from a short fragment.

if anyone is interested in an implementation of the Levenshtein algorithm for VB, here is one take. It probably is not fully optomized but it appears to work.

`Function Damerau_Levenshtein(seq1 As String, seq2 As String, Optional lngLimit As Long = -1) As Double`

``` Dim oneago() As Double Dim twoago() As Double Dim thisrow() As Double Dim iStr1 As Double Dim iStr2 As Double Dim dblDelete As Double Dim dblInsert As Double Dim dblSubsti As Double Dim dblCost As Double Const blnPrint = False 'Create first row ReDim thisrow(0 To Len(seq1)) For iStr1 = 0 To UBound(thisrow) thisrow(iStr1) = iStr1 Next iStr1 'Compare For iStr2 = 1 To Len(seq2) 'Save previous runs twoago = oneago oneago = thisrow 'Generate the current row ReDim thisrow(0 To Len(seq1)) thisrow(0) = iStr2 'Compare For iStr1 = 1 To Len(seq1) dblCost = Abs(Not Mid\$(seq1, iStr1, 1) = Mid\$(seq2, iStr2, 1)) dblDelete = oneago(iStr1) + 1 + 1 dblInsert = thisrow(iStr1 - 1) + 1 + 1 dblSubsti = oneago(iStr1 - 1) + dblCost thisrow(iStr1) = min(dblDelete, dblInsert, dblSubsti) 'Consider transpositions If iStr2 > 1 And iStr1 > 1 Then If Mid\$(seq1, iStr1, 1) = Mid\$(seq2, iStr2 - 1, 1) And Mid\$(seq1, iStr1 - 1, 1) = Mid\$(seq2, iStr2, 1) Then thisrow(iStr1) = min(thisrow(iStr1), twoago(iStr1 - 2) + dblCost, 10000) End If End If Next 'Debugging... If blnPrint Then Dim x As Long For x = 0 To UBound(thisrow) Debug.Print thisrow(x); Next Debug.Print " " End If Next Damerau_Levenshtein = thisrow(UBound(thisrow)) End Function ```

```Function min(a As Double, b As Double, c As Double) As Double If a <= b Then If a <= c Then min = a Else min = c Else If b <= c Then min = b Else min = c End If End Function```

3. Geeko says:

Nice article. Even if I will not use it for this task, it’s interesting regarding recursive functions.

4. Mike says:

I recently implemented Jaro-Winkler distances in VBA, which is a fantastically robust way of doing fuzzy matching. Definitely worth looking into.

Posting code? Use <pre> tags for VBA and <code> tags for inline.