Skip to content

Commit

Permalink
perf: update JsExport
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Feb 12, 2025
1 parent 579ecdb commit 773109a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package li.songe.selector

import kotlin.js.JsExport

@JsExport
interface Stringify {
internal interface Stringify {
fun stringify(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package li.songe.selector.connect
import li.songe.selector.QueryContext
import li.songe.selector.Stringify
import li.songe.selector.Transform
import li.songe.selector.comparePrimitiveValue
import li.songe.selector.property.ValueExpression
import kotlin.js.JsExport

Expand Down Expand Up @@ -40,17 +41,6 @@ sealed class CompareOperator(val key: String) : Stringify {
).sortedBy { -it.key.length }.toTypedArray()
}

// example
// id="com.lptiyu.tanke:id/ab1"
// id="com.lptiyu.tanke:id/ab2"
private fun CharSequence.contentReversedEquals(other: CharSequence): Boolean {
if (this === other) return true
if (this.length != other.length) return false
for (i in this.length - 1 downTo 0) {
if (this[i] != other[i]) return false
}
return true
}
}

data object Equal : CompareOperator("=") {
Expand All @@ -62,18 +52,11 @@ sealed class CompareOperator(val key: String) : Stringify {
): Boolean {
val left = leftExp.getAttr(context, transform)
val right = rightExp.getAttr(context, transform)
return compare(left, right)
return comparePrimitiveValue(left, right)
}

override fun allowType(left: ValueExpression, right: ValueExpression) = true

fun compare(left: Any?, right: Any?): Boolean {
return if (left is CharSequence && right is CharSequence) {
left.contentReversedEquals(right)
} else {
left == right
}
}
}

data object NotEqual : CompareOperator("!=") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package li.songe.selector.property
import li.songe.selector.QueryContext
import li.songe.selector.Stringify
import li.songe.selector.Transform
import li.songe.selector.connect.CompareOperator
import li.songe.selector.comparePrimitiveValue
import li.songe.selector.escapeString
import li.songe.selector.optimizeMatchString
import li.songe.selector.whenNull
Expand Down Expand Up @@ -84,14 +84,14 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Stri
is Identifier -> {
when {
callee.isEqual -> {
CompareOperator.Equal.compare(
comparePrimitiveValue(
arguments[0].getAttr(context, transform),
arguments[1].getAttr(context, transform)
)
}

callee.isNotEqual -> {
!CompareOperator.Equal.compare(
!comparePrimitiveValue(
arguments[0].getAttr(context, transform),
arguments[1].getAttr(context, transform)
)
Expand Down Expand Up @@ -180,7 +180,8 @@ sealed class ValueExpression(open val value: Any?, open val type: String) : Stri

data class IntLiteral(override val value: Int) : LiteralExpression(value, "int")

data class StringLiteral @JsExport.Ignore constructor(
@ConsistentCopyVisibility
data class StringLiteral internal constructor(
override val value: String,
internal val matches: ((CharSequence) -> Boolean)? = null
) : LiteralExpression(value, "string") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import li.songe.selector.MatchOption
import li.songe.selector.QueryContext
import li.songe.selector.Transform
import li.songe.selector.TypeInfo
import kotlin.js.JsExport

@JsExport
data class LogicalSelectorExpression(
val left: SelectorExpression,
val operator: SelectorLogicalOperator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import li.songe.selector.QueryContext
import li.songe.selector.QueryResult
import li.songe.selector.Transform
import li.songe.selector.TypeInfo
import kotlin.js.JsExport


@JsExport
data class NotSelectorExpression(
val expression: SelectorExpression,
) : SelectorExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import li.songe.selector.QueryResult
import li.songe.selector.Stringify
import li.songe.selector.Transform
import li.songe.selector.TypeInfo
import kotlin.js.JsExport

internal sealed interface SelectorExpressionToken

@JsExport
sealed class SelectorExpression : Stringify, SelectorExpressionToken {

abstract fun <T> match(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import li.songe.selector.QueryContext
import li.songe.selector.QueryResult
import li.songe.selector.Stringify
import li.songe.selector.Transform
import kotlin.js.JsExport

@JsExport
sealed class SelectorLogicalOperator(val key: String) : Stringify, SelectorExpressionToken {
override fun stringify() = key

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import li.songe.selector.property.BinaryExpression
import li.songe.selector.property.PropertyWrapper
import li.songe.selector.property.ValueExpression
import kotlin.collections.addAll
import kotlin.js.JsExport

@JsExport
data class UnitSelectorExpression(
val propertyWrapper: PropertyWrapper,
) : SelectorExpression() {
Expand Down
20 changes: 20 additions & 0 deletions selector/src/commonMain/kotlin/li/songe/selector/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,23 @@ fun getCharSequenceAttr(target: CharSequence, name: String): Any? {
else -> null
}
}

// example
// id="com.lptiyu.tanke:id/ab1"
// id="com.lptiyu.tanke:id/ab2"
internal fun CharSequence.contentReversedEquals(other: CharSequence): Boolean {
if (this === other) return true
if (this.length != other.length) return false
for (i in this.length - 1 downTo 0) {
if (this[i] != other[i]) return false
}
return true
}

internal fun comparePrimitiveValue(left: Any?, right: Any?): Boolean {
return if (left is CharSequence && right is CharSequence) {
left.contentReversedEquals(right)
} else {
left == right
}
}

0 comments on commit 773109a

Please sign in to comment.