mirror of
https://github.com/gkd-kit/gkd.git
synced 2024-11-16 03:32:38 +08:00
perf(selector): wasm matches
This commit is contained in:
parent
b8598cb44e
commit
d9c56775e3
|
@ -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)
|
||||||
|
}
|
|
@ -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
|
||||||
|
@ -28,3 +32,8 @@ 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
|
||||||
|
}
|
|
@ -4,3 +4,5 @@ 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) {}
|
|
@ -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
1
wasm_matches/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/build
|
25
wasm_matches/build.gradle.kts
Normal file
25
wasm_matches/build.gradle.kts
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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) }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user