aboutsummaryrefslogtreecommitdiffstats
path: root/incdec.pm
blob: bcf62e0f87fa8614468a9457dee5a389b12d7e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use strict;
use warnings;

package incdec;

sub incdec {
	my ($line, $point_position, $is_backward) = @_;

	$point_position ||= 0;
	$is_backward ||= 0;

	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;
	my $previous_point_position = 0;
	while ($point_position < length($line_part)) {
		$line_part =~ /(\d+)/;
		if ($-[0] <= $point_position && $point_position < $+[0]) {
			$point_position = $-[0];

			last;
		}

		if ($point_position >= $+[0]) {
			$line_start .= substr($line_part, 0, $+[0]);
			$line_part = substr($line_part, $+[0]);
			$previous_point_position = $point_position;
			$point_position -= $+[0];

			next;
		}
		else {
			last;
		}
	}

	$line_part =~ s/(\d+)/$1+1/e;

	my $line_excluded = substr $line_start, 0, $original_point_position;

	$line = $line_excluded . $line_part;

	return $line;
}

1;