8000 Regex: Turn CharSequence into Strings upon entry in our implementation. · renowncoder/scala-js@72d5ed9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72d5ed9

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 0896ff4 commit 72d5ed9

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
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`
@@ -155,12 +155,13 @@ final class Matcher private[regex] (
155155
def reset(): Matcher = {
156156
regionStart0 = 0
157157
regionEnd0 = input0.length()
158-
inputstr = input0.toString()
158+
inputstr = input0
159159
resetMatch()
160160
}
161161

162+
@inline // `input` is almost certainly a String at call site
162163
def reset(input: CharSequence): Matcher = {
163-
input0 = input
164+
input0 = input.toString()
164165
reset()
165166
}
166167

@@ -242,7 +243,7 @@ final class Matcher private[regex] (
242243
def region(start: Int, end: Int): Matcher = {
243244
regionStart0 = start
244245
regionEnd0 = end
245-
inputstr = input0.subSequence(regionStart0, regionEnd0).toString
246+
inputstr = input0.substring(start, end)
246247
resetMatch()
247248
}
248249

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

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

140140
override def toString(): String = pattern()
141141

142+
@inline // `input` is almost certainly a String at call site
142143
def matcher(input: CharSequence): Matcher =
143-
new Matcher(this, input)
144+
new Matcher(this, input.toString())
144145

146+
@inline // `input` is almost certainly a String at call site
145147
def split(input: CharSequence): Array[String] =
146148
split(input, 0)
147149

148-
def split(input: CharSequence, limit: Int): Array[String] = {
149-
val inputStr = input.toString
150+
@inline // `input` is almost certainly a String at call site
151+
def split(input: CharSequence, limit: Int): Array[String] =
152+
split(input.toString(), limit)
150153

154+
private def split(inputStr: String, limit: Int): Array[String] = {
151155
// If the input string is empty, always return Array("") - #987, #2592
152156
if (inputStr == "") {
153157
Array("")
@@ -210,7 +214,11 @@ object Pattern {
210214
def compile(regex: String): Pattern =
211215
compile(regex, 0)
212216

217+
@inline // `input` is almost certainly a String at call site
213218
def matches(regex: String, input: CharSequence): Boolean =
219+
matches(regex, input.toString())
220+
221+
private def matches(regex: String, input: String): Boolean =
214222
compile(regex).matcher(input).matches()
215223

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

linker/shared/src/test/scala/org/scalajs/linker/LibrarySizeTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class LibrarySizeTest {
7070
)
7171

7272
testLinkedSizes(
73-
expectedFastLinkSize = 186816,
74-
expectedFullLinkSizeWithoutClosure = 173972,
75-
expectedFullLinkSizeWithClosure = 31629,
73+
expectedFastLinkSize = 186693,
74+
expectedFullLinkSizeWithoutClosure = 173849,
75+
expectedFullLinkSizeWithClosure = 31572,
7676
classDefs,
7777
moduleInitializers = MainTestModuleInitializers
7878
)

0 commit comments

Comments
 (0)
0