diff options
| author | Teddy Wing | 2021-08-28 18:08:30 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-08-28 18:25:23 +0200 |
| commit | 88cc7f664a2c582c4fb08a4c3f927da7863ce9fc (patch) | |
| tree | c11f061db5dc42f5174cb615f330aac2c6f0ae0f | |
| parent | 924e467583354e6faa179518d90e1d4e7ca89743 (diff) | |
| download | readline-incdec-88cc7f664a2c582c4fb08a4c3f927da7863ce9fc.tar.bz2 | |
incdec.pm: Fix an issue where first number wasn't incremented backwards
When `$start_position` was 0 going backwards, the `sed` test command
line failed to increment the first number in the line.
Can't figure out exactly what was wrong, but there seemed to be a
problem with using the `\G` assertion for that test case. Decided to
remove `\G` when `$start_position` position is 0 to work around the
problem. Not sure if there's a more concise solution to this that
wouldn't require me to have two separate subtitution lines.
Also simplified the substitution regular expression. It turns out I
didn't need the first capture group, and it was incorrect in matching
/[^-\d]*/ because we really wanted /(?!-?\d+)/, not either or of the
characters in the group. Completely removing it still allows everything
to work. Don't remember if I added that when I was still using the
substringing algorithm, but whatever happens, it's not necessary now.
| -rw-r--r-- | incdec.bash | 10 | ||||
| -rw-r--r-- | incdec.pm | 10 | ||||
| -rw-r--r-- | t/100-increment-decrement.t | 30 |
3 files changed, 40 insertions, 10 deletions
diff --git a/incdec.bash b/incdec.bash index 9950f33..d00c140 100644 --- a/incdec.bash +++ b/incdec.bash @@ -58,8 +58,14 @@ sub incdec { } } - pos($line) = $start_position; - $line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e; + # Using `\G` when `pos` is 0 seems to cause occasional missed substitutions. + if ($start_position > 0) { + pos($line) = $start_position; + $line =~ s/\G(-?\d+)/$1 + $increment_by/e; + } + else { + $line =~ s/(-?\d+)/$1 + $increment_by/e; + } return $line; } @@ -55,8 +55,14 @@ sub incdec { } } - pos($line) = $start_position; - $line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e; + # Using `\G` when `pos` is 0 seems to cause occasional missed substitutions. + if ($start_position > 0) { + pos($line) = $start_position; + $line =~ s/\G(-?\d+)/$1 + $increment_by/e; + } + else { + $line =~ s/(-?\d+)/$1 + $increment_by/e; + } return $line; } diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index 883a9c2..6fada9b 100644 --- a/t/100-increment-decrement.t +++ b/t/100-increment-decrement.t @@ -104,12 +104,6 @@ is( ); is( - incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 1), - "sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", - 'increments the first integer with point at position 3 backward' -); - -is( incdec::incdec('test -1 ', 1, 7, 1), 'test 0 ', 'increments the negative integer with point at position 7 backward' @@ -169,4 +163,28 @@ is( 'decrements the second integer by 2 with point at position 9 backward' ); +is( + incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 1), + "sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", + 'increments the first integer with point at position 3 backward' +); + +is( + incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 8, 1), + "sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", + 'increments the first integer with point at position 8 backward' +); + +is( + incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", -1, 10, 1), + "sed -n '38,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", + 'decrements the first integer with point at position 10 backward' +); + +is( + incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 0), + "sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", + 'increments the first integer with point at position 3' +); + done_testing; |
