Tor Valamo
Tor Valamo

Reputation: 33809

diff for single lines

All diff tools I've found are just comparing line by line instead of char by char. Is there any library that gives details on single line strings? Maybe also a percentage difference, though I guess there are separate functions for that?

Upvotes: 10

Views: 3820

Answers (4)

Pierre
Pierre

Reputation: 35306

You can implement a simple Needleman–Wunsch algorithm. The pseudo code is available on Wikipedia: http://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm

Upvotes: 3

slebetman
slebetman

Reputation: 114014

This algorithm diffs word-by-word:

http://github.com/paulgb/simplediff

available in Python and PHP. It can even spit out HTML formatted output using the <ins> and <del> tags.

Upvotes: 5

Michael Williamson
Michael Williamson

Reputation: 11438

I was looking for something similar recently, and came across wdiff. It operates on words, not characters, but is this close to what you're looking for?

Upvotes: 4

JPvdMerwe
JPvdMerwe

Reputation: 3364

What you could try is to split both strings up character by character into lines and then you can use diff on that. It's a dirty hack, but atleast it should work and is quite easy to implement.

Alternately you can split the string up into a list of chars in Python and use difflib. Check Python difflib reference

Upvotes: 3

Related Questions