Skip to content
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

Fix #3631: Regex now handles OR alternatives with more than two clauses #3642

Merged
merged 1 commit into from
Jan 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions nativelib/src/main/scala/scala/scalanative/regex/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,13 @@ class Parser(wholeRegexp: String, _flags: Int) {
val old = re
re = re.subs(0)
this.reuse(old)

/* Scala Native Issue #3631
* This Scala Native port must follow the Java switch
* statement semantics used in the RE2J original code.
* That is, return the now shortened re if there is no explicit case.
*/
case _ =>
}
re
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package scala.issues

import org.junit.Test
import org.junit.Assert._

class RegexIssuesTest:

/* Issue 3631 describes a parse failue in a complicated regex.
* To increase confidence, the Test should stay as close as feasible
* to the original report.
*
* The complication is that Scala 2.12 regex class does not have
* the "matches" method used in the Issue. That method was introduced
* in Scala 2.13.
*
* To reduce duplication & confusion, this Test is run only on Scala 3.
*
* This test should be run on both Scala and JVM to ensure that the regex
* from the Issue continues to parse on the latter.
*/

@Test def test_Issue3631(): Unit = {
// Use the full regex from the Issue, which is _not_ the minimal reproducer.
val pattern = "^(\\-|\\+)?(0\\.[0-9]+|[1-9][0-9]*\\.[0-9]+|[1-9][0-9]*|0)$"
val expected = "1"

assertTrue(
s"regex '${pattern}' does not match '${expected}'",
pattern.r.matches(expected)
)
}

end RegexIssuesTest