Mark Jason Dominus writes:
[...]
> I thought last week's intro quiz was unusually cool, and I was
> disappointed that so few people seemed to have done it.
So did I - ;-), but didn't feel up to tackling it.
> Michael Toren's sample solution to the 'expert' quiz shows that it
> doesn't have to be difficult to write a useful implementation of
> 'patch', and I was hoping that the space of possible versions would
> have been more thoroughly explored. Alas.
Having been guilted (just kidding) into actually trying something, I tackled
the expert (patch) quiz this evening. I did first look at Michael Toren's
to get me started.
What I've worked on so far is trying to get the diff file into a suitable
data structure. I want to process the file being patched one line at a
time, so I thought it would be easier to first load the patchfile into a
useable data structure. What I've got so far...
# get BEGIN and END and COMMAND
open PATCH, $patchfile or die "Couldn't read $patchfile: $!\n";
my ($begin, $end, $command, @line);
while (<PATCH>) {
if (/^(\d+),?(\d+)?([adc]).*/) {
($begin, $end, $command) = ($1, $2, $3);
$end ||= $begin;
for ($begin .. $end) {
push @{ $line[$_] }, $command;
}
}
# get new lines to add
if (/^>\s(.*)/) {
push @{ $line[$begin] }, $1 ;
}
}
>From this:
1,2d0
< The Way that can be told of is not the eternal Way;
< The name that can be named is not the eternal name.
4c2,3
< The Named is the mother of all things.
---
> The named is the mother of all things.
>
11a11,13
> They both may be called deep and profound.
> Deeper and more profound,
> The door of all subtleties!
Data::Dumper gives me:
$VAR1 = [
undef,
[
'd'
],
[
'd'
],
${\$VAR1->[0]},
[
'c',
'The named is the mother of all things.',
''
],
${\$VAR1->[0]},
${\$VAR1->[0]},
${\$VAR1->[0]},
${\$VAR1->[0]},
${\$VAR1->[0]},
${\$VAR1->[0]},
[
'a',
'They both may be called deep and profound.',
'Deeper and more profound,',
'The door of all subtleties!'
]
];
So $line[line_number]->[0] gives me the command for each line number and if
there are new lines to add they follow in the same array.
This seems like a useful start to me, but I should probably make this
'two-sided' so that a reverse patch would also be possible.
Advice/comments/suggestions welcome.
-K
--
Kevin Pfeiffer <pfeiffer-Is5wZoSgCRJn68oJJulU0Q@xxxxxxxxxxxxxxxx> -
www.iu-bremen.de
|