Skip to content

Commit

Permalink
fix issue #226
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee authored and neighbWang committed May 8, 2021
1 parent 1dc9b4b commit c2afbe5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.didiglobal.booster.kotlinx
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
import java.util.regex.Pattern

private val PATTERN_JAVA_IDENTIFIER = Pattern.compile("[a-zA-Z_\$][a-zA-Z\\d_\$]*")

fun String.separatorsToUnix(): String {
return if ('/' == File.separatorChar) this else this.replace(File.separatorChar, '/')
Expand All @@ -19,3 +22,5 @@ fun String.md5(): String {
fun String.matches(wildcard: Wildcard): Boolean {
return wildcard.matches(this)
}

fun String.isValidJavaIdentifier(): Boolean = PATTERN_JAVA_IDENTIFIER.matcher(this).matches()
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.didiglobal.booster.kotlinx

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Unit test for String extension
*
* @author johnsonlee
*/
class TextTest {

@Test
fun `test java identifier validation`() {
assertFalse("".isValidJavaIdentifier())
assertFalse("1".isValidJavaIdentifier())
assertFalse(".".isValidJavaIdentifier())
assertTrue("_".isValidJavaIdentifier())
assertTrue("$".isValidJavaIdentifier())
assertFalse("1a".isValidJavaIdentifier())
assertFalse(".a".isValidJavaIdentifier())
assertTrue("_a".isValidJavaIdentifier())
assertTrue("\$a".isValidJavaIdentifier())
assertTrue("ab".isValidJavaIdentifier())
assertTrue("ab1".isValidJavaIdentifier())
assertTrue("ab$".isValidJavaIdentifier())
assertTrue("ab_".isValidJavaIdentifier())
assertFalse("ab.".isValidJavaIdentifier())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.didiglobal.booster.aapt2.MAGIC
import com.didiglobal.booster.aapt2.RES_FILE
import com.didiglobal.booster.aapt2.Resources
import com.didiglobal.booster.aapt2.ResourcesInternal
import com.didiglobal.booster.kotlinx.isValidJavaIdentifier
import com.didiglobal.booster.kotlinx.stackTraceAsString
import org.gradle.api.logging.Logging
import java.io.File
Expand Down Expand Up @@ -112,10 +113,21 @@ internal fun Resources.XmlNode.findAllRetainedSymbols(): Collection<String> {
}.attributeList?.forEach { attr ->
when (attr.name) {
"constraint_referenced_ids" -> addAll(attr.value.split(PATTERN_COMMA))
"layout_constraintHorizontal_bias",
"layout_constraintVertical_bias",
"layout_constraintCircleRadius",
"layout_constraintCircleAngle",
"layout_constraintDimensionRatio",
"layout_constraintWidth_default",
"layout_constraintHeight_default",
"layout_constraintWidth_percent",
"layout_constraintHeight_percent",
"layout_constraintHorizontal_chainStyle",
"layout_constraintVertical_chainStyle",
"layout_constraintHorizontal_weight",
"layout_constraintVertical_weight" -> Unit // just ignore
else -> if (attr.name.startsWith("layout_constraint")) {
addAll(attr.value.split(PATTERN_COMMA).filter {
it != "parent"
}.map {
addAll(attr.value.split(PATTERN_COMMA).filter(::isValidSymbol).map {
it.substringAfter('/')
})
}
Expand All @@ -125,6 +137,8 @@ internal fun Resources.XmlNode.findAllRetainedSymbols(): Collection<String> {
}
}

internal fun isValidSymbol(token: String) = token.isNotEmpty() && "parent" != token && token.isValidJavaIdentifier()

private val logger = Logging.getLogger(RetainedSymbolCollector::class.java)

private val PATTERN_COMMA = Pattern.compile("\\s*,\\s*")
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.didiglobal.booster.transform.r.inline

import java.io.File
import java.util.regex.Pattern
import kotlin.test.Test
import kotlin.test.assertFalse
Expand All @@ -23,4 +22,12 @@ class RegexTest {
}
}

@Test
fun `valid symbol`() {
assertTrue(isValidSymbol("H"))
assertTrue(isValidSymbol("icon"))
assertTrue(isValidSymbol("ic_app"))
assertFalse(isValidSymbol("4:1"))
}

}

0 comments on commit c2afbe5

Please sign in to comment.