Skip to content

Commit

Permalink
perf(selector): 选择器优先比较逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 16, 2023
1 parent 4d07d46 commit 699ad8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 699ad8b

Please sign in to comment.