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
* functionality borrowed from def file parser and unified for interop tool
* and kotlin compiler.
* functionality borrowed from def file parser and unified for interop tool
* and kotlin compiler.
*/
fun Properties.propertyList(key: String, suffix: String? = null, escapeInQuotes: Boolean = false): List<String> {
val value = this.getProperty(key.suffix(suffix)) ?: this.getProperty(key)
if (value?.isBlank() == true) return emptyList()
return if (escapeInQuotes) value?.let { parseSpaceSeparatedArgs(it) } ?: emptyList()
else value?.split(Regex("\\s+")) ?: emptyList()
val value: String? = (getProperty(key.suffix(suffix)) ?: getProperty(key))?.trim(Char::isWhitespace)
return when {
value.isNullOrEmpty() -> emptyList()
escapeInQuotes -> parseSpaceSeparatedArgs(value)
else -> value.split(Regex("\\s+"))
}
}
fun Properties.hasProperty(key: String, suffix: String? = null): Boolean
@@ -52,8 +52,8 @@ fun parseSpaceSeparatedArgs(argsString: String): List<String> {
val parsedArgs = mutableListOf<String>()
var inQuotes = false
var currentCharSequence = StringBuilder()
fun saveArg() {
if (!currentCharSequence.isEmpty()) {
fun saveArg(wasInQuotes: Boolean) {
if (wasInQuotes || currentCharSequence.isNotBlank()) {
parsedArgs.add(currentCharSequence.toString())
currentCharSequence = StringBuilder()
}
@@ -63,11 +63,11 @@ fun parseSpaceSeparatedArgs(argsString: String): List<String> {
inQuotes = !inQuotes
// Save value which was in quotes.
if (!inQuotes) {
saveArg()
saveArg(true)
}
} else if (char == ' ' && !inQuotes) {
} else if (char.isWhitespace() && !inQuotes) {
// Space is separator.
saveArg()
saveArg(false)
} else {
currentCharSequence.append(char)
}
@@ -75,6 +75,6 @@ fun parseSpaceSeparatedArgs(argsString: String): List<String> {
if (inQuotes) {
error("No close-quote was found in $currentCharSequence.")
}
saveArg()
saveArg(false)
return parsedArgs
}
}
@@ -67,7 +67,3 @@ val KotlinLibrary.packageFqName: String?
val KotlinLibrary.exportForwardDeclarations
get() = manifestProperties.propertyList(KLIB_PROPERTY_EXPORT_FORWARD_DECLARATIONS, escapeInQuotes = true)
.asSequence()
.map { it.trim() }
.filter { it.isNotEmpty() }
.toList()