Tweak saved filename byte size limiting logic

Based on comment from https://github.com/inorichi/tachiyomi/commit/6940ad3fd9d37d81988c9f42c26e5ada0c178d8a
This commit is contained in:
arkon
2020-03-07 13:15:21 -05:00
parent 5e9496ef36
commit 1c978f64b1
2 changed files with 26 additions and 3 deletions
@@ -32,3 +32,24 @@ fun String.truncateCenter(count: Int, replacement: String = "..."): String {
fun String.compareToCaseInsensitiveNaturalOrder(other: String): Int {
return String.CASE_INSENSITIVE_ORDER.then(naturalOrder()).compare(this, other)
}
/**
* Returns the size of the string as the number of bytes.
*/
fun String.byteSize(): Int {
return toByteArray(Charsets.UTF_8).size
}
/**
* Returns a string containing the first [n] bytes from this string, or the entire string if this
* string is shorter.
*/
@UseExperimental(ExperimentalStdlibApi::class)
fun String.takeBytes(n: Int): String {
val bytes = toByteArray(Charsets.UTF_8)
return if (bytes.size <= n) {
this
} else {
bytes.decodeToString(endIndex = n).replace("\uFFFD", "")
}
}