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.
This commit is contained in:
Alexander Capehart 2022-12-30 08:42:35 -07:00
parent b7bc0a6206
commit f6b7a8f448
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 22 additions and 6 deletions

View file

@ -2,6 +2,9 @@
## dev
#### What's Improved
- Formalized whitespace handling
## 3.0.0
#### What's New

View file

@ -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 {

View file

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

View file

@ -115,6 +115,19 @@ inline fun String.splitEscaped(selector: (Char) -> Boolean): List<String> {
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<String>.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<String>.parseMultiValue(settings: Settings) =
get(0).maybeParseSeparators(settings)
} else {
// Nothing to do.
this.map { it.trim() }
this
}
/**
@ -138,7 +151,7 @@ fun List<String>.parseMultiValue(settings: Settings) =
fun String.maybeParseSeparators(settings: Settings): List<String> {
// 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()
}
/**