diff options
| -rw-r--r-- | incdec.bash | 10 | ||||
| -rw-r--r-- | incdec.pm | 10 | ||||
| -rw-r--r-- | t/100-increment-decrement.t | 30 | 
3 files changed, 40 insertions, 10 deletions
| diff --git a/incdec.bash b/incdec.bash index 9950f33..d00c140 100644 --- a/incdec.bash +++ b/incdec.bash @@ -58,8 +58,14 @@ sub incdec {  		}  	} -	pos($line) = $start_position; -	$line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e; +	# Using `\G` when `pos` is 0 seems to cause occasional missed substitutions. +	if ($start_position > 0) { +		pos($line) = $start_position; +		$line =~ s/\G(-?\d+)/$1 + $increment_by/e; +	} +	else { +		$line =~ s/(-?\d+)/$1 + $increment_by/e; +	}  	return $line;  } @@ -55,8 +55,14 @@ sub incdec {  		}  	} -	pos($line) = $start_position; -	$line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e; +	# Using `\G` when `pos` is 0 seems to cause occasional missed substitutions. +	if ($start_position > 0) { +		pos($line) = $start_position; +		$line =~ s/\G(-?\d+)/$1 + $increment_by/e; +	} +	else { +		$line =~ s/(-?\d+)/$1 + $increment_by/e; +	}  	return $line;  } diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index 883a9c2..6fada9b 100644 --- a/t/100-increment-decrement.t +++ b/t/100-increment-decrement.t @@ -104,12 +104,6 @@ is(  );  is( -	incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 1), -	"sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", -	'increments the first integer with point at position 3 backward' -); - -is(  	incdec::incdec('test -1 ', 1, 7, 1),  	'test 0 ',  	'increments the negative integer with point at position 7 backward' @@ -169,4 +163,28 @@ is(  	'decrements the second integer by 2 with point at position 9 backward'  ); +is( +	incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 1), +	"sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", +	'increments the first integer with point at position 3 backward' +); + +is( +	incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 8, 1), +	"sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", +	'increments the first integer with point at position 8 backward' +); + +is( +	incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", -1, 10, 1), +	"sed -n '38,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", +	'decrements the first integer with point at position 10 backward' +); + +is( +	incdec::incdec("sed -n '39,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", 1, 3, 0), +	"sed -n '40,54p' Alice\'s\ Adventures\ in\ Wonderland.txt ", +	'increments the first integer with point at position 3' +); +  done_testing; | 
