aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2022-05-25 01:48:50 +0200
committerTeddy Wing2022-05-25 01:48:50 +0200
commit3831218d3271133e176b07e0f19e0b16dd907491 (patch)
tree6b8706a06738e881f1ea4ad1f12d7edee65f88b9
parentac4b0dc07c3b2ac710f51fa804068ee7a76f916f (diff)
downloadreadline-incdec-3831218d3271133e176b07e0f19e0b16dd907491.tar.bz2
incdec.pm: Idea for correctly excluding leading zero
This passes the two leading zero tests, but fails the test that tries to increment a single-digit "0" at the last position in the string. Also fix the test's expected start position.
-rw-r--r--incdec.pm4
-rw-r--r--t/100-increment-decrement.t2
2 files changed, 3 insertions, 3 deletions
diff --git a/incdec.pm b/incdec.pm
index 2c4bdef..8cc0b32 100644
--- a/incdec.pm
+++ b/incdec.pm
@@ -30,7 +30,7 @@ sub incdec {
my $start_position = 0;
my $previous_match_start = 0;
my $i = 0;
- while ($line =~ /(-?([1-9]\d*|\d))/g) {
+ while ($line =~ /(-?([1-9]\d*|0\D))/g) {
if ($is_backward) {
# Set start position to the current match start. This gives us the
# correct start position when incrementing the last number in a
@@ -64,7 +64,7 @@ sub incdec {
}
pos($line) = $start_position;
- $line =~ s/\G(-?([1-9]\d*|\d))/$1 + $increment_by/e;
+ $line =~ s/\G(-?([1-9]\d*|0\D))/$1 + $increment_by/e;
return ($line, $start_position);
}
diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t
index 50d76c5..d83a35a 100644
--- a/t/100-increment-decrement.t
+++ b/t/100-increment-decrement.t
@@ -53,7 +53,7 @@ is_deeply(
);
@got = incdec::incdec('test A-02 0', -1);
-@want = ('test A-03 0', 7);
+@want = ('test A-01 0', 8);
is_deeply(
\@got,
\@want,