From ea3cd1980b86d647fb4a1d771489c93ebd1d77e0 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 25 May 2022 01:29:05 +0200 Subject: incdec.pm: Idea to keep leading zeros in numbers --- incdec.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/incdec.pm b/incdec.pm index a979cf7..39eaba4 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 =~ /(-?\d+)/g) { + while ($line =~ /(-?[1-9]\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(-?\d+)/$1 + $increment_by/e; + $line =~ s/\G(-?[1-9]\d*)/$1 + $increment_by/e; return ($line, $start_position); } -- cgit v1.2.3 From 1c7000e9e25aba7406a09d3530e756cfc8e52386 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 25 May 2022 01:32:20 +0200 Subject: incdec.pm: Fix leading-zero handling for single-digit "0" Incrementing or decrementing "0" didn't work with the leading-zero idea I had. --- incdec.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/incdec.pm b/incdec.pm index 39eaba4..2c4bdef 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*)/g) { + while ($line =~ /(-?([1-9]\d*|\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*)/$1 + $increment_by/e; + $line =~ s/\G(-?([1-9]\d*|\d))/$1 + $increment_by/e; return ($line, $start_position); } -- cgit v1.2.3 From ac4b0dc07c3b2ac710f51fa804068ee7a76f916f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 25 May 2022 01:39:35 +0200 Subject: 100-increment-decrement: Add tests for integers with leading zero --- t/100-increment-decrement.t | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index eda27ca..50d76c5 100644 --- a/t/100-increment-decrement.t +++ b/t/100-increment-decrement.t @@ -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-03 0', 7); +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( -- cgit v1.2.3 From 3831218d3271133e176b07e0f19e0b16dd907491 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 25 May 2022 01:48:50 +0200 Subject: 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. --- incdec.pm | 4 ++-- t/100-increment-decrement.t | 2 +- 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, -- cgit v1.2.3 From b42dc03982cd87ed10dbbc6d339004805fc39fd7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 25 May 2022 01:52:05 +0200 Subject: incdec.pm: Fix increment for single-digit zero at the end of the line This just adds another "or" option to match a zero at the end of the string. Wonder if there's a nicer way to do this. --- incdec.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/incdec.pm b/incdec.pm index 8cc0b32..f35c963 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*|0\D))/g) { + while ($line =~ /(-?([1-9]\d*|0\D|0$))/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*|0\D))/$1 + $increment_by/e; + $line =~ s/\G(-?([1-9]\d*|0\D|0$))/$1 + $increment_by/e; return ($line, $start_position); } -- cgit v1.2.3 From 48fefa2bee2e67743f8056dea93eed2ccf28653a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 25 May 2022 02:22:51 +0200 Subject: incdec.pm: Extract number matching regex to a variable So it can be re-used in both the find and the replace steps without being duplicated now that it's become more complicated. --- incdec.pm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/incdec.pm b/incdec.pm index f35c963..0ec0ab5 100644 --- a/incdec.pm +++ b/incdec.pm @@ -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 =~ /(-?([1-9]\d*|0\D|0$))/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(-?([1-9]\d*|0\D|0$))/$1 + $increment_by/e; + $line =~ s/\G($number_regex)/$1 + $increment_by/e; return ($line, $start_position); } -- cgit v1.2.3 From c74280c3c3f15915fbcb3f326a86633c92cd805c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 26 May 2022 00:34:51 +0200 Subject: 100-increment-decrement: Add a more complex leading zero test --- t/100-increment-decrement.t | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index d83a35a..ee5a849 100644 --- a/t/100-increment-decrement.t +++ b/t/100-increment-decrement.t @@ -228,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( -- cgit v1.2.3 From 48356a6720b735f86f930e7a41c7b0f297042a51 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 26 May 2022 16:50:43 +0200 Subject: incdec.bash: Recompile with latest changes --- incdec.bash | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/incdec.bash b/incdec.bash index 338afae..5c8f401 100644 --- a/incdec.bash +++ b/incdec.bash @@ -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); } -- cgit v1.2.3 From 7d45241d3e42e425afd579d1e93758d0c6b8c886 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 26 May 2022 16:51:00 +0200 Subject: Add CHANGELOG --- CHANGELOG | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 CHANGELOG 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* -- cgit v1.2.3 From 1cb9ad7286b87c348861dafd112a73901e4d6358 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 26 May 2022 16:52:36 +0200 Subject: Update copyright year --- Makefile | 2 +- README.md | 4 ++-- incdec.bash | 2 +- incdec.m4.bash | 2 +- incdec.pm | 2 +- t/100-increment-decrement.t | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) 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 5c8f401..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. # 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 0ec0ab5..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. # diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index ee5a849..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. # -- cgit v1.2.3