Skip to content

Commit 85b9d6f

Browse files
committed
Regex: Turn CharSequence into Strings upon entry in our implementation.
Since we have to convert the input `CharSequence`s to `String`s anyway to be able to give them to `js.RegExp`, it is better to do that upon entry into our implementation. We can force the `toString()`s to be at the call site, which can then be inlined and removed when the arguments are already Strings, which should be the vast majority of use cases.
1 parent c62d3c7 commit 85b9d6f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

javalib/src/main/scala/java/util/regex/Matcher.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import scala.annotation.switch
1919
import scala.scalajs.js
2020

2121
final class Matcher private[regex] (
22-
private var pattern0: Pattern, private var input0: CharSequence)
22+
private var pattern0: Pattern, private var input0: String)
2323
extends AnyRef with MatchResult {
2424

2525
import Matcher._
@@ -29,7 +29,7 @@ final class Matcher private[regex] (
2929
// Region configuration (updated by reset() and region())
3030
private var regionStart0 = 0
3131
private var regionEnd0 = input0.length()
32-
private var inputstr = input0.toString()
32+
private var inputstr = input0
3333

3434
// Match result (updated by successful matches)
3535
private var position: Int = 0 // within `inputstr`, not `input0`
@@ -158,12 +158,13 @@ final class Matcher private[regex] (
158158
def reset(): Matcher = {
159159
regionStart0 = 0
160160
regionEnd0 = input0.length()
161-
inputstr = input0.toString()
161+
inputstr = input0
162162
resetMatch()
163163
}
164164

165+
@inline // `input` is almost certainly a String at call site
165166
def reset(input: CharSequence): Matcher = {
166-
input0 = input
167+
input0 = input.toString()
167168
reset()
168169
}
169170

@@ -245,7 +246,7 @@ final class Matcher private[regex] (
245246
def region(start: Int, end: Int): Matcher = {
246247
regionStart0 = start
247248
regionEnd0 = end
248-
inputstr = input0.subSequence(regionStart0, regionEnd0).toString
249+
inputstr = input0.substring(start, end)
249250
resetMatch()
250251
}
251252

javalib/src/main/scala/java/util/regex/Pattern.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,19 @@ final class Pattern private[regex] (
136136

137137
override def toString(): String = pattern()
138138

139+
@inline // `input` is almost certainly a String at call site
139140
def matcher(input: CharSequence): Matcher =
140-
new Matcher(this, input)
141+
new Matcher(this, input.toString())
141142

143+
@inline // `input` is almost certainly a String at call site
142144
def split(input: CharSequence): Array[String] =
143145
split(input, 0)
144146

145-
def split(input: CharSequence, limit: Int): Array[String] = {
146-
val inputStr = input.toString
147+
@inline // `input` is almost certainly a String at call site
148+
def split(input: CharSequence, limit: Int): Array[String] =
149+
splitString(input.toString(), limit)
147150

151+
private def splitString(inputStr: String, limit: Int): Array[String] = {
148152
// If the input string is empty, always return Array("") - #987, #2592
149153
if (inputStr == "") {
150154
Array("")
@@ -207,7 +211,11 @@ object Pattern {
207211
def compile(regex: String): Pattern =
208212
compile(regex, 0)
209213

214+
@inline // `input` is almost certainly a String at call site
210215
def matches(regex: String, input: CharSequence): Boolean =
216+
matchesString(regex, input.toString())
217+
218+
private def matchesString(regex: String, input: String): Boolean =
211219
compile(regex).matcher(input).matches()
212220

213221
def quote(s: String): String = {

0 commit comments

Comments
 (0)