From aff27d201c023747cc2b99f853ecb82e4f5b609b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 19 Aug 2021 21:43:12 +0200 Subject: incdec: Get backward incrementing working in all cases Set up the proper condition to get the start index of the number just before point. --- incdec.pm | 12 +++++++----- t/100-increment-decrement.t | 6 ++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/incdec.pm b/incdec.pm index b05c547..a473e30 100644 --- a/incdec.pm +++ b/incdec.pm @@ -13,15 +13,17 @@ sub incdec { my @points = (); my $previous_match_start = 0; my $previous_match_end = 0; + print "$line\n"; while ($line =~ /(\d+)/g) { if ($is_backward) { - # print "pos[$point_position] -[$-[0]]- +[$+[0]]+\n"; + print "pos[$point_position] -[$-[0]]- +[$+[0]]+\n"; + print "pos[$point_position] -[$previous_match_start]- +[$previous_match_end]+\n"; # if $point_position < $-[0] # && previous $+[0] <= $point_position - # if ($previous_match_end <= $point_position && $point_position < $-[0]) { - if ($-[0] <= $point_position && $point_position < $+[0]) { - # print "matched"; - $start_position = $-[0]; + if ($previous_match_end - 1 <= $point_position && $point_position < $-[0]) { + # if ($-[0] <= $point_position && $point_position < $+[0]) { + print "matched at $-[0];$previous_match_end\n"; + $start_position = $previous_match_start; last; } diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index 1345dfe..dd0df5c 100644 --- a/t/100-increment-decrement.t +++ b/t/100-increment-decrement.t @@ -67,4 +67,10 @@ is( 'increments the third triple-digit integer with point at position 13 backward' ); +is( + incdec::incdec('test 12 19 555 64', 14, 1), + 'test 12 19 556 64', + 'increments the third triple-digit integer with point at position 14 backward' +); + done_testing; -- cgit v1.2.3