From f6b7a8f448b5860f6c51ee62da5c365b7cbb34c8 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Fri, 30 Dec 2022 08:42:35 -0700 Subject: [PATCH] music: formalize whitespace handling Formalize how whitespace tags are handled. The checks for blank tags and removal of trailing whitespace from tags are now the same function, carefully used to prevent blank tags from setting through. More testing will need to be done in order to fully ensure this system will work as intended. --- CHANGELOG.md | 3 +++ .../auxio/music/extractor/CacheExtractor.kt | 2 +- .../auxio/music/extractor/MetadataExtractor.kt | 6 +++--- .../auxio/music/extractor/ParsingUtil.kt | 17 +++++++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cc7f6db2..a94d5e349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## dev +#### What's Improved +- Formalized whitespace handling + ## 3.0.0 #### What's New diff --git a/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheExtractor.kt b/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheExtractor.kt index e9159a0db..d5e11713a 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheExtractor.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/extractor/CacheExtractor.kt @@ -388,7 +388,7 @@ private class CacheDatabase(context: Context) : * string. Escaped delimiters are converted back into their normal forms. */ private fun String.parseSQLMultiValue() = - splitEscaped { it == ';' } + splitEscaped { it == ';' }.correctWhitespace() /** Defines the columns used in this database. */ private object Columns { diff --git a/app/src/main/java/org/oxycblt/auxio/music/extractor/MetadataExtractor.kt b/app/src/main/java/org/oxycblt/auxio/music/extractor/MetadataExtractor.kt index 6603850e4..f50b34d59 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/extractor/MetadataExtractor.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/extractor/MetadataExtractor.kt @@ -187,7 +187,7 @@ class Task(context: Context, private val raw: Song.Raw) { // Map TXXX frames differently so we can specifically index by their // descriptions. val id = tag.description?.let { "TXXX:${it.sanitize()}" } ?: tag.id.sanitize() - val values = tag.values.map { it.sanitize() }.filter { it.isNotEmpty() } + val values = tag.values.map { it.sanitize() }.correctWhitespace() if (values.isNotEmpty()) { id3v2Tags[id] = values } @@ -195,8 +195,8 @@ class Task(context: Context, private val raw: Song.Raw) { is VorbisComment -> { // Vorbis comment keys can be in any case, make them uppercase for simplicity. val id = tag.key.sanitize().uppercase() - val value = tag.value.sanitize() - if (value.isNotEmpty()) { + val value = tag.value.sanitize().correctWhitespace() + if (value != null) { vorbisTags.getOrPut(id) { mutableListOf() }.add(value) } } diff --git a/app/src/main/java/org/oxycblt/auxio/music/extractor/ParsingUtil.kt b/app/src/main/java/org/oxycblt/auxio/music/extractor/ParsingUtil.kt index 05a2deeb7..92cbeee1d 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/extractor/ParsingUtil.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/extractor/ParsingUtil.kt @@ -115,6 +115,19 @@ inline fun String.splitEscaped(selector: (Char) -> Boolean): List { return split } +/** + * Fix trailing whitespace or blank contents in a [String]. + * @return A string with trailing whitespace remove,d or null if the [String] was all whitespace + * or empty. + */ +fun String.correctWhitespace() = trim().ifBlank { null } + +/** + * Fix trailing whitespace or blank contents within a list of [String]s. + * @return A list of non-blank strings with trailing whitespace removed. + */ +fun List.correctWhitespace() = mapNotNull { it.correctWhitespace() } + /** * Parse a multi-value tag based on the user configuration. If the value is already composed of more * than one value, nothing is done. Otherwise, this function will attempt to split it based on the @@ -127,7 +140,7 @@ fun List.parseMultiValue(settings: Settings) = get(0).maybeParseSeparators(settings) } else { // Nothing to do. - this.map { it.trim() } + this } /** @@ -138,7 +151,7 @@ fun List.parseMultiValue(settings: Settings) = fun String.maybeParseSeparators(settings: Settings): List { // Get the separators the user desires. If null, there's nothing to do. val separators = settings.musicSeparators ?: return listOf(this) - return splitEscaped { separators.contains(it) }.map { it.trim() } + return splitEscaped { separators.contains(it) }.correctWhitespace() } /**