diff options
| author | Teddy Wing | 2022-05-26 16:55:58 +0200 |
|---|---|---|
| committer | Teddy Wing | 2022-05-26 16:55:58 +0200 |
| commit | bf6544d5be555686c63f3da3fa500a1f8b8d5fe9 (patch) | |
| tree | 00e338a64ebc408508aa2665846ffbfcf0bdd226 | |
| parent | 419f4cdec4c13c62c8c365a24ee5c59b59c66fdd (diff) | |
| parent | 1cb9ad7286b87c348861dafd112a73901e4d6358 (diff) | |
| download | readline-incdec-bf6544d5be555686c63f3da3fa500a1f8b8d5fe9.tar.bz2 | |
Merge branch 'keep-leading-zeros'
| -rw-r--r-- | CHANGELOG | 12 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | incdec.bash | 8 | ||||
| -rw-r--r-- | incdec.m4.bash | 2 | ||||
| -rw-r--r-- | incdec.pm | 8 | ||||
| -rw-r--r-- | t/100-increment-decrement.t | 26 |
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* @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Teddy Wing +# Copyright (c) 2021–2022 Teddy Wing # # This file is part of Incdec. # @@ -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. # @@ -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( |
