memo.xight.org

日々のメモ

String::Diff - 行内差分取得モジュール

Summary

Perlの行内差分取得モジュール.

文字列同士の差分を作って変更無い場所,変更が合った場所で配列を分けて作成.

my $diff = String::Diff::diff_fully('this is Perl', 'this is Ruby');
for my $line (@{ $diff->[0] }) {
    print "$line->[0]: '$line->[1]'\n";
}
# u: 'this is '
# -: 'Perl'

for my $line (@{ $diff->[1] }) {
    print "$line->[0]: '$line->[1]'\n";
}
# u: 'this is '
# +: 'Ruby'


差分の合った部分にマークを付ける.

my $diff = String::Diff::diff('this is Perl', 'this is Ruby');

print "$diff->[0]\n";# this is [Perl]
print "$diff->[1]\n";# this is {Ruby}

my $diff = String::Diff::diff('this is Perl', 'this is Ruby',{
    remove_open => '<del>',
    remove_close => '</del>',
    append_open => '<ins>',
    append_close => '</ins>',
});

print "$diff->[0]\n";# this is <del>Perl</del>
print "$diff->[1]\n";# this is <ins>Ruby</ins>


差分同士の結果をマージして一つの文字列にする.

my $diff = String::Diff::diff_merge('this is Perl', 'this is Ruby'{
    remove_open => '<del>',
    remove_close => '</del>',
    append_open => '<ins>',
    append_close => '</ins>',
});
int "$diff\n";# this is <del>Perl</del><ins>Ruby</ins>


正規表現のパターンにする.

my $diff = String::Diff::diff_regexp('this is Perl', 'this is Ruby');
print "$diff\n";# this\ is\ (?:Perl|Ruby)


利用例

Hatena::Wiki - Kwiki mutual diffs
http://hatenawiki.blogdb.jp/?action=diff&page_name=HomePage&revision_id=2&current_revision_id=3

Reference

YappoLogs: String::Diff - 行内差分取得モジュール
http://blog.yappo.jp/yappo/archives/000479.html

String::Diff