#!/usr/bin/perl -w use strict; use Carp; use Data::Dumper; my @d = <>; unshift @d, "# dummy line to make line 1 index 1 in \@d\n"; our $i_last_l_ok = -1; our $count_i_last_l_ok; sub l_ok ($) { my ($i) = @_; if ($i == $i_last_l_ok) { confess $i if $count_i_last_l_ok++ > 50; } else { $count_i_last_l_ok = 0; $i_last_l_ok = $i; } return unless $i < @d; $_ = $d[$i]; #print STDERR "L $i\n"; 1; } sub l ($) { my ($i) = @_; confess $i unless l_ok $i; }; our ($ifilehead, $ihunkhead, $ichunkstart, $ichunkend); our ($before, $after); sub analyse_chunk () { print Dumper($ichunkstart, $ichunkend, $before, $after); flush STDOUT; } for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) { m{^diff} or next; while (l_ok $ifilehead and m{^diff|^index|^---|^\Q+++\E}) { $ifilehead++ } $ihunkhead = $ifilehead; while (l_ok $ihunkhead) { m{^\@\@} or confess "$ihunkhead $_ ?"; my $i = $ihunkhead + 1; for (; ; $i++) { 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; } elsif (m{^[-+]}) { my $which = $& eq '-' ? \$before : \$after; $ichunkstart //= $i; $$which //= ''; $$which .= $'; } else { confess "$i $_ ?"; } } $ichunkend = $i; $ihunkhead = $i; } }