So I admit I've got some playing to do, as I've never used Algorithm::Diff, and String::Diff, but here's my current implementation an internal line diff for a text to html diff compare tool I've written.
I wanted to show inline differences. I have two strings that I *know* have some difference in them (thanks to a call to diff).
I put <span> </span> tags around the differences in a string pair. Both in the old string and the new string. (to be used in a 2 column html table showing diffs side by side).
The output from this script looks like the follows.#!/usr/bin/perl use strict; use warnings; while (<DATA>) { my $cmpline = <DATA>; # I always have a pair to compare my ($spanold, $spannew) = spandiffstr ($_, $cmpline); print "$spanold"; print "$spannew"; } sub spandiffstr { my ($old, $new) = @_; my $span = '<span>'; #eventually I'll pass this in as arg my $espan = '</span>'; #ditto my @cold = split //, $old; my @cnew = split //, $new; my $longer = (@cold > @new) ? $#cold : $#cnew; my $subscr; #find first diff in string from front. for (0..$longer) { if ($cold[$_] ne $cnew[$_]) { $subscr = $_; last; } } my $losubscr; #two subscripts because differ len of new and old my $lnsubscr; @cold = reverse @cold; @cnew = reverse @cnew; #we're gonna search these backwards for (0..$longer) { if ($cold[$_] ne $cnew[$_]) { $losubscr = @cold - $_; $lnsubscr = @cnew - $_; last; } } #got pertinent subscripts, now insert span tags substr ($old, $losubscr, 0) = $espan; substr ($new, $lnsubscr, 0) = $espan; substr ($old, $subscr, 0) = $span; substr ($new, $subscr, 0) = $span; return ($old, $new); } __DATA__ I love Perl. I love Your Mom. I love to eat spaghetti. I love to eat spaghetti and meatballs. I love to smell the flowers. I hate to smell the flowers. Stop! That's enough. Not really enough. Blood and Tears are expected. Love and Marriage are exempted.
I love <span>Perl</span>. I love <span>Your Mom</span>. I love to eat spaghetti<span></span>. I love to eat spaghetti<span> and meatballs</span>. I <span>lov</span>e to smell flowers. I <span>hat</span>e to smell flowers. <span>Stop! That's</span> enough. <span>Not really</span> enough. <span>Blood and Tears are expec</span>ted. <span>Love and Marriage are exemp</span>ted.
I guess I'm curious if there's a better way to create spans on a string, and wanted to share this version that was kinda fun, and hear if folks would implement it more idiomatically.
I've been looking at the two modules cited above, it looks might there might be a better way to create multiple spans depending on say multiple changes in the lines, as opposed to the way I do it where I span only from the first and last differences, not taking into account internal matching withing the single string. So that for exampled the last two lines might instead produce multiple spans in a single string: LIke so...
<span>Blood</span> and <span>Te</span>ar<span>s</span> are ex<span>pec +</span>ted. <span>Love</span> and <span>M</span>ar<span>riage</span> are ex<span>e +mp</span>ted.
Of course such an implementation may be a pain... so Then again, my current approach might be good enough. :)
As always I find all you experts and your input invaluable in helping me to think of algorithms and the language differently. Thanks ahead of time for any comments.
So how would you do it? :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: string diff and compare
by jwkrahn (Abbot) on May 04, 2012 at 08:47 UTC | |
by raybies (Chaplain) on May 04, 2012 at 15:36 UTC | |
by Anonymous Monk on May 04, 2012 at 15:54 UTC | |
by Anonymous Monk on May 04, 2012 at 16:13 UTC | |
by jwkrahn (Abbot) on May 04, 2012 at 21:27 UTC | |
by tobyink (Canon) on May 05, 2012 at 07:17 UTC | |
by raybies (Chaplain) on May 04, 2012 at 12:03 UTC | |
by jwkrahn (Abbot) on May 04, 2012 at 21:22 UTC | |
by raybies (Chaplain) on May 07, 2012 at 19:55 UTC | |
by raybies (Chaplain) on May 08, 2012 at 11:45 UTC | |
Re: string diff and compare
by raybies (Chaplain) on May 07, 2012 at 13:48 UTC |