perf(selector): wasm matches

This commit is contained in:
lisonge 2024-03-26 17:29:00 +08:00
parent b8598cb44e
commit d9c56775e3
7 changed files with 59 additions and 2 deletions

View File

@ -1,3 +1,12 @@
package li.songe.selector package li.songe.selector
import kotlin.js.JsExport
expect fun String.toMatches(): (input: CharSequence) -> Boolean expect fun String.toMatches(): (input: CharSequence) -> Boolean
expect fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean)
@JsExport
fun updateWasmToMatches(toMatches: (String) -> (String) -> Boolean) {
setWasmToMatches(toMatches)
}

View File

@ -2,8 +2,11 @@ package li.songe.selector
import kotlin.js.RegExp import kotlin.js.RegExp
// https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/js/src/kotlin/text/regex.kt
actual fun String.toMatches(): (input: CharSequence) -> Boolean { actual fun String.toMatches(): (input: CharSequence) -> Boolean {
if (wasmMatchesTemp !== null) {
val matches = wasmMatchesTemp!!(this)
return { input -> matches(input.toString()) }
}
if (length >= 4 && startsWith("(?")) { if (length >= 4 && startsWith("(?")) {
for (i in 2 until length) { for (i in 2 until length) {
when (get(i)) { when (get(i)) {
@ -15,6 +18,7 @@ actual fun String.toMatches(): (input: CharSequence) -> Boolean {
.joinToString("") .joinToString("")
val nativePattern = RegExp(substring(i + 1), flags) val nativePattern = RegExp(substring(i + 1), flags)
return { input -> return { input ->
// // https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/js/src/kotlin/text/regex.kt
nativePattern.reset() nativePattern.reset()
val match = nativePattern.exec(input.toString()) val match = nativePattern.exec(input.toString())
match != null && match.index == 0 && nativePattern.lastIndex == input.length match != null && match.index == 0 && nativePattern.lastIndex == input.length
@ -27,4 +31,9 @@ actual fun String.toMatches(): (input: CharSequence) -> Boolean {
} }
val regex = Regex(this) val regex = Regex(this)
return { input -> regex.matches(input) } return { input -> regex.matches(input) }
}
private var wasmMatchesTemp: ((String) -> (String) -> Boolean)? = null
actual fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean) {
wasmMatchesTemp = wasmToMatches
} }

View File

@ -3,4 +3,6 @@ package li.songe.selector
actual fun String.toMatches(): (input: CharSequence) -> Boolean { actual fun String.toMatches(): (input: CharSequence) -> Boolean {
val regex = Regex(this) val regex = Regex(this)
return { input -> regex.matches(input) } return { input -> regex.matches(input) }
} }
actual fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean) {}

View File

@ -2,6 +2,7 @@ rootProject.name = "gkd"
include(":app") include(":app")
include(":selector") include(":selector")
include(":hidden_api") include(":hidden_api")
include(":wasm_matches")
pluginManagement { pluginManagement {
repositories { repositories {

1
wasm_matches/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

View File

@ -0,0 +1,25 @@
plugins {
alias(libs.plugins.kotlin.multiplatform)
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = JavaVersion.VERSION_17.majorVersion
}
}
wasmJs {
binaries.executable()
useEsModules()
generateTypeScriptDefinitions()
browser {}
}
sourceSets {
all {
languageSettings.optIn("kotlin.js.ExperimentalJsExport")
}
}
sourceSets["commonMain"].dependencies {
implementation(libs.kotlin.stdlib.common)
}
}

View File

@ -0,0 +1,10 @@
package li.songe.matches
import kotlin.js.JsExport
// wasm gc
@JsExport
fun toMatches(source: String): (input: String) -> Boolean {
val regex = Regex(source)
return { input -> regex.matches(input) }
}