diff options
| author | Teddy Wing | 2021-08-18 20:41:38 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-08-18 20:41:38 +0200 |
| commit | 9abbafff1df5dc60e773c7a96f4c6299f63d377b (patch) | |
| tree | 890bccbc26f374886b617d935fe5e2bbc7a056bd /incdec.pm | |
| parent | 7b6610832b2a4244b048e0565ef2d299495012c3 (diff) | |
| download | readline-incdec-9abbafff1df5dc60e773c7a96f4c6299f63d377b.tar.bz2 | |
incdec: Switch to the new incrementing algorithm
Still a bunch of old code to clean up, but this is a lot shorter than
what I had before, and I think it will be easier to extend it for
backwards searching.
Diffstat (limited to 'incdec.pm')
| -rw-r--r-- | incdec.pm | 78 |
1 files changed, 41 insertions, 37 deletions
@@ -9,42 +9,42 @@ sub incdec { $point_position ||= 0; $is_backward ||= 0; - my $original_point_position = $point_position; + # my $original_point_position = $point_position; # If point is within a number, move it to ensure we match the whole number # rather than only part of its digits. - my $line_start = ''; - my $line_part = $line; - while ($point_position < length($line_part)) { - $line_part =~ /(\d+)/; - - # If `$point_position` is within the matched number, we can stop. - if ($-[0] <= $point_position && $point_position < $+[0]) { - last; - } - - # Continue the loop if point is further right than the end of the match. - if ($point_position >= $+[0]) { - # Store the line part that we don't increment so we can include it at the end. - $line_start .= substr($line_part, 0, $+[0]); - - # Match starting in the next part of the string next iteration. - $line_part = substr($line_part, $+[0]); - - # Adjust point position according to the new line part. - $point_position -= $+[0]; - - next; - } - else { - last; - } - } + # my $line_start = ''; + # my $line_part = $line; + # while ($point_position < length($line_part)) { + # $line_part =~ /(\d+)/; + # + # # If `$point_position` is within the matched number, we can stop. + # if ($-[0] <= $point_position && $point_position < $+[0]) { + # last; + # } + # + # # Continue the loop if point is further right than the end of the match. + # if ($point_position >= $+[0]) { + # # Store the line part that we don't increment so we can include it at the end. + # $line_start .= substr($line_part, 0, $+[0]); + # + # # Match starting in the next part of the string next iteration. + # $line_part = substr($line_part, $+[0]); + # + # # Adjust point position according to the new line part. + # $point_position -= $+[0]; + # + # next; + # } + # else { + # last; + # } + # } # Idea: Loop through @- and @+ to find @- <= point < @+ - $point_position = $original_point_position; - my @matches = $line =~ /(\d+)/g; - print "[[@matches]]"; + # $point_position = $original_point_position; + # my @matches = $line =~ /(\d+)/g; + # print "[[@matches]]"; # TODO: @- and @+ hold a list of captured groups. You need to loop through matches to get positions for each match with $-+[0]. It doesn't give you a list of positions of all matches. # print "-[[@-]]-.+[[@+]]+"; # for (my $i = 0; $i < scalar @+ - 1; $i++) { @@ -70,10 +70,10 @@ sub incdec { my $start_position = 0; while ($line =~ /(\d+)/g) { my $pos = pos $line; - print "\n-[$-[0]]-+[$+[0]]+\n"; + # print "\n-[$-[0]]-+[$+[0]]+\n"; # print "..+[@+]..perlpos[:$pos]..pos[$point_position].."; if ($point_position < $+[0]) { - print "//[$matches[$i]]//"; + # print "//[$matches[$i]]//"; $start_position = $-[0]; last; @@ -81,15 +81,19 @@ sub incdec { $i++; } - print "\npos[$start_position]\n"; + # print "\npos[$start_position]\n"; + + pos($line) = $start_position; + + $line =~ s/\G([^\d]*)(\d+)/$1 . ($2 + 1)/e; # Final match, final match before point - $line_part =~ s/(\d+)/$1+1/e; + # $line_part =~ s/(\d+)/$1+1/e; - my $line_excluded = substr $line_start, 0, $original_point_position; + # my $line_excluded = substr $line_start, 0, $original_point_position; - $line = $line_excluded . $line_part; + # $line = $line_excluded . $line_part; return $line; } |
