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 /incdec.bash | |
| 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.
Diffstat (limited to 'incdec.bash')
| -rw-r--r-- | incdec.bash | 10 |
1 files changed, 8 insertions, 2 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; } |
