Skip to content

Commit 1922876

Browse files
committed
Merge pull request livecode#3810 from thierrydouez/bugfix-17245
[ BUG 17245 ] Fix matchChunk err with optional captured group
2 parents a30bf7d + 6afc296 commit 1922876

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

docs/notes/bugfix-17245.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix matchChunk error with optional captured group

engine/src/exec-strings.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,15 +862,16 @@ void MCStringsEvalMatchChunk(MCExecContext& ctxt, MCStringRef p_string, MCString
862862
t_success = MCStringFormat(r_results[i], "%d", t_compiled->matchinfo[t_match_index].rm_so + 1);
863863
if (t_success)
864864
t_success = MCStringFormat(r_results[i + 1], "%d", t_compiled->matchinfo[t_match_index].rm_eo);
865-
866-
if (++t_match_index >= NSUBEXP)
867-
t_match_index = 0;
868865
}
869866
else
870867
{
871868
r_results[i] = MCValueRetain(kMCEmptyString);
872869
r_results[i + 1] = MCValueRetain(kMCEmptyString);
873870
}
871+
// matchChunk did set empty values of positionVarsList with regex "(a)?(B)" applied on "B"
872+
// if an optional capture group didn't match (which is valid), index wasn't updated.
873+
if (++t_match_index >= NSUBEXP)
874+
t_match_index = 0;
874875
}
875876

876877
if (t_success)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
script "CoreStringsRegexOptCapturedGroup"
2+
3+
/*
4+
Copyright (C) 2015 LiveCode Ltd.
5+
6+
This file is part of LiveCode.
7+
8+
LiveCode is free software; you can redistribute it and/or modify it under
9+
the terms of the GNU General Public License v3 as published by the Free
10+
Software Foundation.
11+
12+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
13+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
19+
20+
21+
on TestRegexWithOptionalCapturedGroup
22+
local aRegex, aText
23+
local p0, p1, p2, p3
24+
local v1, v2
25+
26+
-- see Bug 17245
27+
-- check that second group is not empty when 1st is.
28+
29+
put "(a)?(B)" into aRegex
30+
put "B" into aText
31+
32+
TestAssert "matchChunk should match", matchChunk( aText, aRegex, p0,p1,p2,p3) is true
33+
34+
TestAssert "... and get empty for p0 and p1", ( p0 & p1 ) is empty
35+
36+
TestAssert "... and get 1 for p2 and p3", ( p2 is 1) and (p3 is 1)
37+
38+
TestAssert "matchText should match", matchText( aText, aRegex, v0, v1) is true
39+
40+
TestAssert "... and get empty for v0", v0 is empty
41+
42+
TestAssert "... and get B for v1", v1 is "B"
43+
44+
45+
end TestRegexWithOptionalCapturedGroup

0 commit comments

Comments
 (0)