Skip to content

Commit

Permalink
perf(selector): wasm matches
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Mar 26, 2024
1 parent b8598cb commit d9c5677
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
9 changes: 9 additions & 0 deletions selector/src/commonMain/kotlin/li/songe/selector/toMatches.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package li.songe.selector

import kotlin.js.JsExport

expect fun String.toMatches(): (input: CharSequence) -> Boolean

expect fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean)

@JsExport
fun updateWasmToMatches(toMatches: (String) -> (String) -> Boolean) {
setWasmToMatches(toMatches)
}
11 changes: 10 additions & 1 deletion selector/src/jsMain/kotlin/li/songe/selector/toMatches.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package li.songe.selector

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

private var wasmMatchesTemp: ((String) -> (String) -> Boolean)? = null
actual fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean) {
wasmMatchesTemp = wasmToMatches
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package li.songe.selector
actual fun String.toMatches(): (input: CharSequence) -> Boolean {
val regex = Regex(this)
return { input -> regex.matches(input) }
}
}

actual fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean) {}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ rootProject.name = "gkd"
include(":app")
include(":selector")
include(":hidden_api")
include(":wasm_matches")

pluginManagement {
repositories {
Expand Down
1 change: 1 addition & 0 deletions wasm_matches/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
25 changes: 25 additions & 0 deletions wasm_matches/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
}
10 changes: 10 additions & 0 deletions wasm_matches/src/commonMain/kotlin/li/songe/matches/toMatches.kt
Original file line number Diff line number Diff line change
@@ -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) }
}

0 comments on commit d9c5677

Please sign in to comment.