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