diff --git a/selector/src/commonMain/kotlin/li/songe/selector/toMatches.kt b/selector/src/commonMain/kotlin/li/songe/selector/toMatches.kt index c02b67928..5bb0fd9b1 100644 --- a/selector/src/commonMain/kotlin/li/songe/selector/toMatches.kt +++ b/selector/src/commonMain/kotlin/li/songe/selector/toMatches.kt @@ -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) +} \ No newline at end of file diff --git a/selector/src/jsMain/kotlin/li/songe/selector/toMatches.js.kt b/selector/src/jsMain/kotlin/li/songe/selector/toMatches.js.kt index 41f588027..098fcc02d 100644 --- a/selector/src/jsMain/kotlin/li/songe/selector/toMatches.js.kt +++ b/selector/src/jsMain/kotlin/li/songe/selector/toMatches.js.kt @@ -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)) { @@ -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 @@ -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 } \ No newline at end of file diff --git a/selector/src/jvmMain/kotlin/li/songe/selector/toMatches.jvm.kt b/selector/src/jvmMain/kotlin/li/songe/selector/toMatches.jvm.kt index 1495d9b13..626bfde00 100644 --- a/selector/src/jvmMain/kotlin/li/songe/selector/toMatches.jvm.kt +++ b/selector/src/jvmMain/kotlin/li/songe/selector/toMatches.jvm.kt @@ -3,4 +3,6 @@ package li.songe.selector actual fun String.toMatches(): (input: CharSequence) -> Boolean { val regex = Regex(this) return { input -> regex.matches(input) } -} \ No newline at end of file +} + +actual fun setWasmToMatches(wasmToMatches: (String) -> (String) -> Boolean) {} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 684a881b4..986b215f7 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,6 +2,7 @@ rootProject.name = "gkd" include(":app") include(":selector") include(":hidden_api") +include(":wasm_matches") pluginManagement { repositories { diff --git a/wasm_matches/.gitignore b/wasm_matches/.gitignore new file mode 100644 index 000000000..42afabfd2 --- /dev/null +++ b/wasm_matches/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/wasm_matches/build.gradle.kts b/wasm_matches/build.gradle.kts new file mode 100644 index 000000000..f0cf42db3 --- /dev/null +++ b/wasm_matches/build.gradle.kts @@ -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) + } +} diff --git a/wasm_matches/src/commonMain/kotlin/li/songe/matches/toMatches.kt b/wasm_matches/src/commonMain/kotlin/li/songe/matches/toMatches.kt new file mode 100644 index 000000000..8c0c5c77f --- /dev/null +++ b/wasm_matches/src/commonMain/kotlin/li/songe/matches/toMatches.kt @@ -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) } +} \ No newline at end of file