From 699ad8b5680c50e34142a2276730a3110cb5e60c Mon Sep 17 00:00:00 2001 From: lisonge Date: Mon, 16 Oct 2023 18:01:12 +0800 Subject: [PATCH] =?UTF-8?q?perf(selector):=20=E9=80=89=E6=8B=A9=E5=99=A8?= =?UTF-8?q?=E4=BC=98=E5=85=88=E6=AF=94=E8=BE=83=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../li/songe/selector/data/CompareOperator.kt | 29 ++++++++++++------- .../li/songe/selector/data/PropertySegment.kt | 7 ++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/selector/src/commonMain/kotlin/li/songe/selector/data/CompareOperator.kt b/selector/src/commonMain/kotlin/li/songe/selector/data/CompareOperator.kt index 9e51ef6..3e48a25 100644 --- a/selector/src/commonMain/kotlin/li/songe/selector/data/CompareOperator.kt +++ b/selector/src/commonMain/kotlin/li/songe/selector/data/CompareOperator.kt @@ -8,22 +8,29 @@ sealed class CompareOperator(val key: String) { val allSubClasses = listOf( Equal, NotEqual, - Start, - NotStart, - Include, - NotInclude, - End, - NotEnd, - Less, - LessEqual, - More, - MoreEqual + Start, NotStart, Include, NotInclude, End, NotEnd, Less, LessEqual, More, MoreEqual ).sortedBy { -it.key.length } + + // 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 + } } object Equal : CompareOperator("=") { override fun compare(left: Any?, right: Any?): Boolean { - return if (left is CharSequence && right is CharSequence) left.contentEquals(right) else left == right + return if (left is CharSequence && right is CharSequence) { + left.contentReversedEquals(right) + } else { + left == right + } } } diff --git a/selector/src/commonMain/kotlin/li/songe/selector/data/PropertySegment.kt b/selector/src/commonMain/kotlin/li/songe/selector/data/PropertySegment.kt index b05de9b..ecb50d9 100644 --- a/selector/src/commonMain/kotlin/li/songe/selector/data/PropertySegment.kt +++ b/selector/src/commonMain/kotlin/li/songe/selector/data/PropertySegment.kt @@ -25,7 +25,12 @@ data class PropertySegment( object : NodeMatchFc { override fun invoke(node: T, transform: Transform): Boolean { val str = transform.getName(node) ?: return false - return (str.contentEquals(name) || (str.endsWith(name) && str[str.length - name.length - 1] == '.')) + if (str.length == name.length) { + return str.contentEquals(name) + } else if (str.length > name.length) { + return str[str.length - name.length - 1] == '.' && str.endsWith(name) + } + return false } } }