aboutsummaryrefslogtreecommitdiffstats
path: root/incdec.pm
diff options
context:
space:
mode:
authorTeddy Wing2021-08-15 19:22:28 +0200
committerTeddy Wing2021-08-15 19:22:28 +0200
commitc03cb253bf0400654ea5a1ac9e06be7e6275eab8 (patch)
tree8286924b9d63946564261733745d79bbcb277f05 /incdec.pm
parent12d8440ec63b9d9fdcae337533e78eaaddc054a7 (diff)
downloadreadline-incdec-c03cb253bf0400654ea5a1ac9e06be7e6275eab8.tar.bz2
incdec: Handle point in the middle of subsequent numbers
Previously, we correctly incremented the first number if point was in the middle of that number, but we did not do so for subsequent numbers. This change adds similar handling for all numbers in the line. Got a little lost here trying different loops and changes. I kind of hacked it without heavily planning the algorithm ahead of time. The idea is that if the point position is beyond the right side of the match, we cut off the part of the line from the start to the end of the last match, then try matching again. This allows us to pull out the number around point so that we can increment it with the evaluated regex that follows the loop.
Diffstat (limited to 'incdec.pm')
-rw-r--r--incdec.pm60
1 files changed, 52 insertions, 8 deletions
diff --git a/incdec.pm b/incdec.pm
index f9397b5..5a631c2 100644
--- a/incdec.pm
+++ b/incdec.pm
@@ -1,6 +1,8 @@
use strict;
use warnings;
+# use 5.010;
+
package incdec;
sub incdec {
@@ -13,8 +15,46 @@ sub incdec {
# If point is within a number, move it to ensure we match the whole number
# rather than only part of its digits.
+ my $final_line = '';
my $line_part = $line;
- while (1) {
+ # my $previous_point_position = $point_position;
+ my $previous_point_position = 0;
+ $final_line .= substr($line_part, $previous_point_position);
+ # while (1) {
+ # for (my $i = 0; $i < 3; $i++) {
+ # while ($point_position < length($line_part)) {
+ # # do {
+ # $line_part =~ /(\d+)/;
+ # say "$-[0]:$+[0]...$line_part...";
+ # if ($-[0] <= $point_position && $point_position < $+[0]) {
+ # $point_position = $-[0];
+ #
+ # last;
+ # }
+ #
+ # # if ($point_position >= length($line_part)) {
+ # # last;
+ # # }
+ # # elsif ($point_position >= $+[0]) {
+ # if ($point_position >= $+[0]) {
+ # # repeat loop
+ # $final_line .= substr($line, $previous_point_position, $point_position + 1);
+ # $line_part = substr $line_part, $point_position;
+ # next;
+ # }
+ # else {
+ # last;
+ # }
+ #
+ # # $line_part = substr $line_part, $point_position;
+ # }
+ # } while ($point_position < length($line_part));
+
+ my $line_start = '';
+ # while ($line_part =~ /(\d+)/) {
+ while ($point_position < length($line_part)) {
+ my $len = length($line_part);
+ # print "p[$line_part]p{$point_position:$len}";
$line_part =~ /(\d+)/;
if ($-[0] <= $point_position && $point_position < $+[0]) {
$point_position = $-[0];
@@ -23,27 +63,31 @@ sub incdec {
}
if ($point_position >= $+[0]) {
- # repeat loop
+ $line_start .= substr($line_part, $previous_point_position, $+[0]);
+ $line_part = substr($line_part, $+[0]);
+ $previous_point_position = $point_position;
+ $point_position = 0;
+
next;
}
else {
last;
}
-
- $line_part = substr $line_part, $point_position;
}
# my $line_part = substr $line, $point_position;
$line_part =~ s/(\d+)/$1+1/e;
- my $line_excluded = substr $line, 0, $original_point_position;
+ my $line_excluded = substr $line_start, 0, $original_point_position;
+ # print "x[$line_excluded]x[$line_part]x";
- # $line = $line_excluded . $line_part;
+ $line = $line_excluded . $line_part;
- # return $line;
+ return $line;
- return $line_part;
+ # return $line_part;
+ # return $final_line;
}
1;