Skip to content

Instantly share code, notes, and snippets.

@hiroshi-manabe
Created October 11, 2019 04:24
Show Gist options
  • Save hiroshi-manabe/23ee459e4e51d407150b359443fb6cc2 to your computer and use it in GitHub Desktop.
Save hiroshi-manabe/23ee459e4e51d407150b359443fb6cc2 to your computer and use it in GitHub Desktop.
Characterwise diff
perl -MFile::Temp -Mutf8 -CSD -F/\\t/ -nale 'for $i(0..1) { ($fh[$i], $fname[$i]) = File::Temp::tempfile(UNLINK=>1); binmode $fh[$i], ":utf8"; @{$l[$i]} = split//, $F[$i]; print {$fh[$i]} $_ for @{$l[$i]}; close $fh[$i]; } for (reverse grep { m{^\d}; } map { chomp; $_; } `diff $fname[0] $fname[1]`) { @t = map { m{^(\d+),(\d+)$} ? [$1-1, $2-1] : m{^(\d+)$} ? [$1-1, $1-1] : $_; } split/([acd])/; @new = @{$l[1]}[$t[2]->[0]..$t[2]->[1]]; if ($t[1] eq "a") { splice @{$l[0]}, $t[0]->[0]+1, 0, "<u>", @new, "</u>"; } elsif ($t[1] eq "d") { splice @{$l[0]}, $t[0]->[1]+1, 0, "</s>"; splice @{$l[0]}, $t[0]->[0], 0, "<s>"; } else { splice @{$l[0]}, $t[0]->[1]+1, 0, "</s><u>", @new, "</u>"; splice @{$l[0]}, $t[0]->[0], 0, "<s>"; } } print join("", @{$l[0]});'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment