aboutsummaryrefslogtreecommitdiffstats
path: root/incdec.pl
diff options
context:
space:
mode:
authorTeddy Wing2021-08-22 23:03:19 +0200
committerTeddy Wing2021-08-22 23:03:19 +0200
commit1d27b8f1769c010fa097bee11cb9236b243e720c (patch)
treeeb2cf1d17575c633dbd8d222d8ebe0e5aef21e4d /incdec.pl
parent392554df943a23099d788d0f04e1cf13b66e910f (diff)
downloadreadline-incdec-1d27b8f1769c010fa097bee11cb9236b243e720c.tar.bz2
Add incdec.pl
This was from when I extracted the `incdec` function to a standalone Perl script to test what was wrong with negative numbers in the second position.
Diffstat (limited to 'incdec.pl')
-rwxr-xr-xincdec.pl56
1 files changed, 56 insertions, 0 deletions
diff --git a/incdec.pl b/incdec.pl
new file mode 100755
index 0000000..e60922e
--- /dev/null
+++ b/incdec.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl -s
+#
+use strict;
+use warnings;
+
+sub incdec {
+ my ($line, $increment_by, $point_position, $is_backward) = @_;
+
+ $point_position ||= 0;
+ $is_backward ||= 0;
+
+ my $start_position = 0;
+ my $previous_match_start = 0;
+ while ($line =~ /\b(-?\d+)\b/g) {
+ if ($is_backward) {
+ if ($point_position < $-[0]) {
+ $start_position = $previous_match_start;
+
+ last;
+ }
+
+ $previous_match_start = $-[0];
+ }
+ else {
+ if ($point_position < $+[0]) {
+ $start_position = $-[0];
+
+ last;
+ }
+ }
+ }
+
+ if ($is_backward && $point_position == length $line) {
+ $start_position = $previous_match_start;
+ }
+
+ pos($line) = $start_position;
+ $line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e;
+
+ return $line;
+}
+
+# my $line = 'test 1 0';
+# my ${"increment-by"} = -1;
+# my ${"point-position"} = 8;
+# my $backward = 1;
+
+my $line = 'test 1 -2';
+my $increment_by = -1;
+my $point_position = 9;
+my $backward = 1;
+
+print "$line, $increment_by, $point_position, $backward\n";
+my $output = incdec($line, $increment_by, $point_position, $backward);
+print $output;
+print "\n";