From 88cc7f664a2c582c4fb08a4c3f927da7863ce9fc Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 28 Aug 2021 18:08:30 +0200 Subject: 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. --- incdec.bash | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'incdec.bash') 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; } -- cgit v1.2.3