perf(selector): 选择器优先比较逻辑

This commit is contained in:
lisonge 2023-10-16 18:01:12 +08:00
parent 4d07d46e09
commit 699ad8b568
2 changed files with 24 additions and 12 deletions

View File

@ -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
}
}
}

View File

@ -25,7 +25,12 @@ data class PropertySegment(
object : NodeMatchFc {
override fun <T> invoke(node: T, transform: Transform<T>): 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
}
}
}