#!/usr/bin/perl -w use strict; use Carp; my @d = <>; sub l_ok ($) { my ($i) = @_; return unless $i < @d; $_ = $d[$i]; 1; } sub l ($) { my ($i) = @_; confess $i unless l_ok $i; }; our ($ifilehead, $ihunkhead, $ichunkstart, $ichunkend); our ($before, $after); sub analyse_chunk () { print Dumper($before, $after); } for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) { m{^diff} or next; while (l_ok $ifilehead and m{^index|^---|^\Q+++\E}) { } $ihunkhead = $ifilehead; while (l_ok $ihunkhead) { m{^\@\@} or confess "$_ ?"; my $i = $ihunkhead + 1; for (;;) { if (!l_ok $i or m{^ } or m{^\@\@}) { if (defined $ichunkstart) { $ichunkend = $i; analyse_chunk(); $ichunkstart = $ichunkend = $before = $after = undef; } l_ok $i or last; m{^\@\@} and last; } elif (m{^[-+]}) { my $which = $& eq '-' ? \$before : \$after; $ichunkfirst //= $i; $$which //= ''; $$which .= $'; } else { confess "$_ ?"; } } $ihunkhead = $i; } }