tags:

views:

408

answers:

5

How would I go about implementing a diff function, such as stackoverflow's question revision history.

+3  A: 

You have here a javascript example of the implementation of a diff algorithm.

Based on:

P. Heckel, A technique for isolating differences between files Comm. ACM, 21, (4), 264--268 (1978).

The implementation, itself, has two functions, one of which is recommended for use:

diffString( String oldFile, String newFile )

This method takes two strings and calculates the differences in each. The final result is the 'newFile' marked up with HTML (to signify both deletions from the oldFile and additions to the newFile).

VonC
thanks!I'm sure it's a good way by js.
nowa
A: 
Vhaerun
+2  A: 

I would find the code for the FreeBSD diff utility and use that as the baseline. There's no point in re-inventing wheels when the licence allows for this sort of copying.

paxdiablo
That would be an option, but in this particular case (the diff codebase) it would be ten times easier to write it from scratch. The diff codebase is... byzantine, to say the least. It's ancient enough to send you run screaming after looking at it for less than 10 minutes. I know, I've looked :)
Mihai Limbășan
A: 

If what you want is revision history, don't reinvent the wheel starting at diff. Just throw everything into version control and use its diff and logging facilities. For simple, linear history something as simple as RCS will do. Or you can throw the latest cannon at it and use git.

Most diff utilities do a line-by-line diff. Stack overflow does a word-by-word diff. For that something like wdiff is necessary. Most version control systems let you plug in the diff utility. Out of the box, git diff --color-words comes remarkably close to what is done here. With a little fiddling with the settings you can probably get it to spit out something you can then make into a pretty web page.

Schwern
+1  A: 

Most algorithms are based on LCS: Longest common subsequence. It isn't obvious to implement it in an efficient way. You will probably find various implementations on the Net, for various languages.

PhiLho