KLIB: Fix reading property lists in Properties.propertyList()

1. If property is absent, or contains empty string or a string consisting only of whitespace characters, Properties.propertyList() should always return empty List<String>.
2. If property starts or ends with whitespace characters, Properties.propertyList() should not include empty-string values for whitespace prefix or suffix.
This commit is contained in:
Dmitriy Dolovov
2020-04-06 13:18:00 +07:00
parent 9b25d1f47a
commit 4c6bddf6be
3 changed files with 15 additions and 17 deletions
@@ -39,14 +39,16 @@ fun Properties.propertyString(key: String, suffix: String? = null): String? = ge
/** /**
* TODO: this method working with suffixes should be replaced with * TODO: this method working with suffixes should be replaced with
* functionality borrowed from def file parser and unified for interop tool * functionality borrowed from def file parser and unified for interop tool
* and kotlin compiler. * and kotlin compiler.
*/ */
fun Properties.propertyList(key: String, suffix: String? = null, escapeInQuotes: Boolean = false): List<String> { fun Properties.propertyList(key: String, suffix: String? = null, escapeInQuotes: Boolean = false): List<String> {
val value = this.getProperty(key.suffix(suffix)) ?: this.getProperty(key) val value: String? = (getProperty(key.suffix(suffix)) ?: getProperty(key))?.trim(Char::isWhitespace)
if (value?.isBlank() == true) return emptyList() return when {
return if (escapeInQuotes) value?.let { parseSpaceSeparatedArgs(it) } ?: emptyList() value.isNullOrEmpty() -> emptyList()
else value?.split(Regex("\\s+")) ?: emptyList() escapeInQuotes -> parseSpaceSeparatedArgs(value)
else -> value.split(Regex("\\s+"))
}
} }
fun Properties.hasProperty(key: String, suffix: String? = null): Boolean fun Properties.hasProperty(key: String, suffix: String? = null): Boolean
@@ -52,8 +52,8 @@ fun parseSpaceSeparatedArgs(argsString: String): List<String> {
val parsedArgs = mutableListOf<String>() val parsedArgs = mutableListOf<String>()
var inQuotes = false var inQuotes = false
var currentCharSequence = StringBuilder() var currentCharSequence = StringBuilder()
fun saveArg() { fun saveArg(wasInQuotes: Boolean) {
if (!currentCharSequence.isEmpty()) { if (wasInQuotes || currentCharSequence.isNotBlank()) {
parsedArgs.add(currentCharSequence.toString()) parsedArgs.add(currentCharSequence.toString())
currentCharSequence = StringBuilder() currentCharSequence = StringBuilder()
} }
@@ -63,11 +63,11 @@ fun parseSpaceSeparatedArgs(argsString: String): List<String> {
inQuotes = !inQuotes inQuotes = !inQuotes
// Save value which was in quotes. // Save value which was in quotes.
if (!inQuotes) { if (!inQuotes) {
saveArg() saveArg(true)
} }
} else if (char == ' ' && !inQuotes) { } else if (char.isWhitespace() && !inQuotes) {
// Space is separator. // Space is separator.
saveArg() saveArg(false)
} else { } else {
currentCharSequence.append(char) currentCharSequence.append(char)
} }
@@ -75,6 +75,6 @@ fun parseSpaceSeparatedArgs(argsString: String): List<String> {
if (inQuotes) { if (inQuotes) {
error("No close-quote was found in $currentCharSequence.") error("No close-quote was found in $currentCharSequence.")
} }
saveArg() saveArg(false)
return parsedArgs return parsedArgs
} }
@@ -67,7 +67,3 @@ val KotlinLibrary.packageFqName: String?
val KotlinLibrary.exportForwardDeclarations val KotlinLibrary.exportForwardDeclarations
get() = manifestProperties.propertyList(KLIB_PROPERTY_EXPORT_FORWARD_DECLARATIONS, escapeInQuotes = true) get() = manifestProperties.propertyList(KLIB_PROPERTY_EXPORT_FORWARD_DECLARATIONS, escapeInQuotes = true)
.asSequence()
.map { it.trim() }
.filter { it.isNotEmpty() }
.toList()