Fix replacement bug in pcre2_substitute().
diff --git a/ChangeLog b/ChangeLog
index 7029ab1..74aeaee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -72,6 +72,10 @@
actually doing anything. Also the fr_CA locale has been added to the list of
locales that can be used.
+14. Fixed a bug in pcre2_substitute(). If a replacement string ended in a
+capturing group number without parentheses, the last character was incorrectly
+literally included at the end of the replacement string.
+
Version 10.00 05-January-2015
-----------------------------
diff --git a/src/pcre2_substitute.c b/src/pcre2_substitute.c
index 98952af..ec00ebb 100644
--- a/src/pcre2_substitute.c
+++ b/src/pcre2_substitute.c
@@ -226,9 +226,9 @@
if (next >= CHAR_0 && next <= CHAR_9)
{
group = next - CHAR_0;
- while (i < rlength - 1)
+ while (++i < rlength)
{
- next = replacement[++i];
+ next = replacement[i];
if (next < CHAR_0 || next > CHAR_9) break;
group = group * 10 + next - CHAR_0;
}
diff --git a/testdata/testinput2 b/testdata/testinput2
index 40a2b4a..e4adf94 100644
--- a/testdata/testinput2
+++ b/testdata/testinput2
@@ -4075,6 +4075,18 @@
/(?<=abc)(|def)/g,replace=<$0>
123abcxyzabcdef789abcpqr
+
+/./replace=$0
+ a
+
+/(.)(.)/replace=$2+$1
+ abc
+
+/(?<A>.)(?<B>.)/replace=$B+$A
+ abc
+
+/(.)(.)/g,replace=$2$1
+ abcdefgh
# End of substitute tests
diff --git a/testdata/testoutput2 b/testdata/testoutput2
index 0b4f7ae..e2fa70e 100644
--- a/testdata/testoutput2
+++ b/testdata/testoutput2
@@ -13721,6 +13721,22 @@
/(?<=abc)(|def)/g,replace=<$0>
123abcxyzabcdef789abcpqr
4: 123abc<>xyzabc<><def>789abc<>pqr
+
+/./replace=$0
+ a
+ 1: a
+
+/(.)(.)/replace=$2+$1
+ abc
+ 1: b+ac
+
+/(?<A>.)(?<B>.)/replace=$B+$A
+ abc
+ 1: b+ac
+
+/(.)(.)/g,replace=$2$1
+ abcdefgh
+ 4: badcfehg
# End of substitute tests