aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2022-05-26 16:55:58 +0200
committerTeddy Wing2022-05-26 16:55:58 +0200
commitbf6544d5be555686c63f3da3fa500a1f8b8d5fe9 (patch)
tree00e338a64ebc408508aa2665846ffbfcf0bdd226
parent419f4cdec4c13c62c8c365a24ee5c59b59c66fdd (diff)
parent1cb9ad7286b87c348861dafd112a73901e4d6358 (diff)
downloadreadline-incdec-bf6544d5be555686c63f3da3fa500a1f8b8d5fe9.tar.bz2
Merge branch 'keep-leading-zeros'
-rw-r--r--CHANGELOG12
-rw-r--r--Makefile2
-rw-r--r--README.md4
-rw-r--r--incdec.bash8
-rw-r--r--incdec.m4.bash2
-rw-r--r--incdec.pm8
-rw-r--r--t/100-increment-decrement.t26
7 files changed, 51 insertions, 11 deletions
diff --git a/CHANGELOG b/CHANGELOG
new file mode 100644
index 0000000..4374165
--- /dev/null
+++ b/CHANGELOG
@@ -0,0 +1,12 @@
+CHANGELOG
+=========
+
+v0.0.2 (2022-05-26):
+ Breaking changes:
+
+ * Keep leading zeros when incrementing and decrementing.
+
+v0.0.1 (2021-08-29):
+ First release.
+
+ vim:tw=80:comments=:fo+=n:formatlistpat=^\\s*\\*\\s*
diff --git a/Makefile b/Makefile
index 921fb82..9ef647c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# Copyright (c) 2021 Teddy Wing
+# Copyright (c) 2021–2022 Teddy Wing
#
# This file is part of Incdec.
#
diff --git a/README.md b/README.md
index 986e195..765191c 100644
--- a/README.md
+++ b/README.md
@@ -54,5 +54,5 @@ bindings for `C-x -` and `C-x +`:
## License
-Copyright © 2021 Teddy Wing. Licensed under the GNU GPLv3+ (see the included
-COPYING file).
+Copyright © 2021–2022 Teddy Wing. Licensed under the GNU GPLv3+ (see the
+included COPYING file).
diff --git a/incdec.bash b/incdec.bash
index 338afae..f364c0f 100644
--- a/incdec.bash
+++ b/incdec.bash
@@ -1,4 +1,4 @@
-# Copyright (c) 2021 Teddy Wing
+# Copyright (c) 2021–2022 Teddy Wing
#
# This file is part of Incdec.
#
@@ -30,10 +30,12 @@ sub incdec {
$point_position ||= 0;
$is_backward ||= 0;
+ my $number_regex = '-?([1-9]\d*|0\D|0$)';
+
my $start_position = 0;
my $previous_match_start = 0;
my $i = 0;
- while ($line =~ /(-?\d+)/g) {
+ while ($line =~ /($number_regex)/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
@@ -67,7 +69,7 @@ sub incdec {
}
pos($line) = $start_position;
- $line =~ s/\G(-?\d+)/$1 + $increment_by/e;
+ $line =~ s/\G($number_regex)/$1 + $increment_by/e;
return ($line, $start_position);
}
diff --git a/incdec.m4.bash b/incdec.m4.bash
index ffa9854..113cfc6 100644
--- a/incdec.m4.bash
+++ b/incdec.m4.bash
@@ -1,4 +1,4 @@
-# Copyright (c) 2021 Teddy Wing
+# Copyright (c) 2021–2022 Teddy Wing
#
# This file is part of Incdec.
#
diff --git a/incdec.pm b/incdec.pm
index a979cf7..6e1d83f 100644
--- a/incdec.pm
+++ b/incdec.pm
@@ -1,4 +1,4 @@
-# Copyright (c) 2021 Teddy Wing
+# Copyright (c) 2021–2022 Teddy Wing
#
# This file is part of Incdec.
#
@@ -27,10 +27,12 @@ sub incdec {
$point_position ||= 0;
$is_backward ||= 0;
+ my $number_regex = '-?([1-9]\d*|0\D|0$)';
+
my $start_position = 0;
my $previous_match_start = 0;
my $i = 0;
- while ($line =~ /(-?\d+)/g) {
+ while ($line =~ /($number_regex)/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 +66,7 @@ sub incdec {
}
pos($line) = $start_position;
- $line =~ s/\G(-?\d+)/$1 + $increment_by/e;
+ $line =~ s/\G($number_regex)/$1 + $increment_by/e;
return ($line, $start_position);
}
diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t
index eda27ca..cba66e8 100644
--- a/t/100-increment-decrement.t
+++ b/t/100-increment-decrement.t
@@ -1,6 +1,6 @@
#!/usr/bin/env perl -w
-# Copyright (c) 2021 Teddy Wing
+# Copyright (c) 2021–2022 Teddy Wing
#
# This file is part of Incdec.
#
@@ -44,6 +44,22 @@ is_deeply(
'increments the first integer'
);
+@got = incdec::incdec('test 012 0', 1);
+@want = ('test 013 0', 6);
+is_deeply(
+ \@got,
+ \@want,
+ 'increments an integer with a leading zero'
+);
+
+@got = incdec::incdec('test A-02 0', -1);
+@want = ('test A-01 0', 8);
+is_deeply(
+ \@got,
+ \@want,
+ 'increments a negative integer with a leading zero'
+);
+
@got = incdec::incdec('test 12 0', 1, 6);
@want = ('test 13 0', 5);
is_deeply(
@@ -212,6 +228,14 @@ is_deeply(
'decrements the second integer by 2 with point at position 9 backward'
);
+@got = incdec::incdec('test_99_A_-_03_[a30df7cf]', 1, 15, 1);
+@want = ('test_99_A_-_04_[a30df7cf]', 13);
+is_deeply(
+ \@got,
+ \@want,
+ 'increments the second zero-prefixed integer by 1 with point at position 15 backward'
+);
+
@got = incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 1);
@want = ("sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 8);
is_deeply(