Skip to content

Fix #3280: Count capturing groups independently on find. #3468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions javalib/src/main/scala/java/util/regex/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ final class Matcher private[regex] (
private var lastMatchIsValid = false
private var canStillFind = true

// Group count
private var lastGroupCount: Option[Int] = None

// Append state (updated by replacement methods)
private var appendPos: Int = 0

Expand Down Expand Up @@ -160,6 +163,7 @@ final class Matcher private[regex] (
regexp = pattern.newJSRegExp()
regexp.lastIndex = prevLastIndex
lastMatch = null
lastGroupCount = None
startOfGroupCache = None
this
}
Expand All @@ -172,7 +176,21 @@ final class Matcher private[regex] (
lastMatch
}

def groupCount(): Int = ensureLastMatch.length-1
def groupCount(): Int = {
if (lastMatch != null) {
lastMatch.length-1
} else {
lastGroupCount match {
case Some(n) => n

case None =>
val groupCountRegex = new js.RegExp("|" + pattern0.jsPattern)
val newGroupCount = groupCountRegex.exec("").length-1
lastGroupCount = Some(newGroupCount)
newGroupCount
}
}
}

def start(): Int = ensureLastMatch.index
def end(): Int = start() + group().length
Expand All @@ -198,7 +216,7 @@ final class Matcher private[regex] (

// Seal the state

def toMatchResult(): MatchResult = new SealedResult(inputstr, lastMatch, pattern())
def toMatchResult(): MatchResult = new SealedResult(inputstr, lastMatch, pattern(), groupCount())

// Other query state methods

Expand Down Expand Up @@ -249,10 +267,11 @@ object Matcher {
}

private final class SealedResult(inputstr: String,
lastMatch: js.RegExp.ExecResult, pattern: Pattern)
lastMatch: js.RegExp.ExecResult, pattern: Pattern,
lastGroupCount: Int)
extends MatchResult {

def groupCount(): Int = ensureLastMatch.length-1
def groupCount(): Int = lastGroupCount

def start(): Int = ensureLastMatch.index
def end(): Int = start() + group().length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class RegexMatcherTest {

def parseExpect(regex: String, str: String, pos: (Int, Int)*): Unit = {
val matcher = Pattern.compile(regex).matcher(str)
assertEquals(pos.length - 1, matcher.groupCount)
assertTrue(matcher.find())
assertEquals(pos.length - 1, matcher.groupCount)
var i = 0
Expand Down Expand Up @@ -159,6 +160,8 @@ class RegexMatcherTest {
}

def checkGroups(matcher: Matcher, startEndMatch: (Int, Int, String)*): Unit = {
assertEquals(startEndMatch.size - 1, matcher.groupCount)

assertTrue(matcher.find())

assertEquals(startEndMatch(0)._1, matcher.start)
Expand Down Expand Up @@ -233,6 +236,13 @@ class RegexMatcherTest {
assertTrue(matcher1.matches())
matcher1.usePattern(patternNoDots)
assertFalse(matcher1.matches())

val patternWithOneGroup = Pattern.compile("ab(cd)efg")
val patternWithTwoGroups = Pattern.compile("ab(cd)(ef)g")
val matcher2 = patternWithOneGroup.matcher("Scala.js")
assertEquals(1, matcher2.groupCount())
matcher2.usePattern(patternWithTwoGroups)
assertEquals(2, matcher2.groupCount())
}

@Test def lookingAt(): Unit = {
Expand Down