Classlib: fix bug in regexp implementation

This commit is contained in:
Alexey Andreev 2022-02-25 20:32:24 +03:00
parent 19f070840c
commit 393ab3b09e
4 changed files with 12 additions and 3 deletions

View File

@ -700,6 +700,7 @@ public final class TMatcher implements TMatchResult {
this.rightBound = string.length(); this.rightBound = string.length();
matchResult = new TMatchResultImpl(cs, leftBound, rightBound, pat.groupCount(), pat.compCount(), matchResult = new TMatchResultImpl(cs, leftBound, rightBound, pat.groupCount(), pat.compCount(),
pat.consCount()); pat.consCount());
matchResult.useAnchoringBounds(true);
} }
@Override @Override

View File

@ -52,7 +52,7 @@ class TMultiLineEOLSet extends TAbstractSet {
@Override @Override
public int matches(int strIndex, CharSequence testString, TMatchResultImpl matchResult) { public int matches(int strIndex, CharSequence testString, TMatchResultImpl matchResult) {
int strDif = matchResult.hasAnchoringBounds() int strDif = matchResult.hasAnchoringBounds()
? matchResult.getLeftBound() - strIndex ? matchResult.getRightBound() - strIndex
: testString.length() - strIndex; : testString.length() - strIndex;
char ch1; char ch1;
char ch2; char ch2;

View File

@ -47,8 +47,7 @@ final class TSOLSet extends TAbstractSet {
public int matches(int strIndex, CharSequence testString, public int matches(int strIndex, CharSequence testString,
TMatchResultImpl matchResult) { TMatchResultImpl matchResult) {
if (strIndex == 0 if (strIndex == 0
|| (matchResult.hasAnchoringBounds() && strIndex == matchResult || (matchResult.hasAnchoringBounds() && strIndex == matchResult.getLeftBound())) {
.getLeftBound())) {
return next.matches(strIndex, testString, matchResult); return next.matches(strIndex, testString, matchResult);
} }
return -1; return -1;

View File

@ -726,6 +726,15 @@ public class MatcherTest {
assertTrue("The result doesn't contain pattern info", result.contains("(\\d{1,3})")); assertTrue("The result doesn't contain pattern info", result.contains("(\\d{1,3})"));
} }
@Test
public void testSOLRegion() {
Pattern pattern = Pattern.compile("^cd");
String input = "abcde";
Matcher matcher = pattern.matcher(input);
matcher.region(2, input.length());
assertTrue(matcher.lookingAt());
}
private void hitEndTest(boolean callFind, String testNo, String regex, private void hitEndTest(boolean callFind, String testNo, String regex,
String input, boolean hit) { String input, boolean hit) {
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);