mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-24 05:22:33 +01:00
Just use a Java formatter
This commit is contained in:
@@ -50,74 +50,17 @@ class XmlResourceBundle internal constructor(internal val lookup: ConcurrentHash
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getStringA(key: String): String {
|
fun getStringA(key: String): String {
|
||||||
return getString(key)
|
return Formatter().format(getString(key))
|
||||||
.replace("\\n", "\n")
|
.let { formatter ->
|
||||||
.replace("\\t", "\t")
|
formatter.toString().also { formatter.close() }
|
||||||
.replace("\\?", "?")
|
}
|
||||||
.replace("\\@", "@")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getString(key: String, vararg replacements: Any): String {
|
fun getString(key: String, vararg replacements: Any?): String {
|
||||||
val stringBuilder = StringBuilder()
|
return Formatter().format(getString(key), *replacements)
|
||||||
var string = getStringA(key)
|
.let { formatter ->
|
||||||
while (true) {
|
formatter.toString().also { formatter.close() }
|
||||||
val index = string.indexOf('%')
|
|
||||||
if (index < 0) {
|
|
||||||
stringBuilder.append(string)
|
|
||||||
break
|
|
||||||
} else if (string[(index - 1).coerceAtLeast(0)] == '\\') {
|
|
||||||
stringBuilder.append(string.substring(0, (index - 1).coerceAtLeast(0)))
|
|
||||||
stringBuilder.append('%')
|
|
||||||
string = string.substring((index + 1).coerceAtMost(string.length))
|
|
||||||
} else {
|
|
||||||
stringBuilder.append(string.substring(0, index))
|
|
||||||
val stringConfig = string.substring(index, index + 4)
|
|
||||||
val item = replacements[stringConfig[1].digitToInt() - 1]
|
|
||||||
when (stringConfig[3]) {
|
|
||||||
's', 'S' -> {
|
|
||||||
stringBuilder.append(
|
|
||||||
if (item is Formattable) {
|
|
||||||
val formatter = Formatter()
|
|
||||||
item.formatTo(formatter, 0, -1, -1)
|
|
||||||
formatter.toString().also { formatter.close() }
|
|
||||||
} else item.toString()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
'b', 'B' -> when (item) {
|
|
||||||
null -> stringBuilder.append("false")
|
|
||||||
is Boolean -> stringBuilder.append(item.toString())
|
|
||||||
else -> stringBuilder.append("true")
|
|
||||||
}
|
|
||||||
'h', 'H' -> stringBuilder.append(item?.hashCode()?.let { Integer.toHexString(it) }.toString())
|
|
||||||
'd' -> when (item) {
|
|
||||||
is Int -> stringBuilder.append(item)
|
|
||||||
is Long -> stringBuilder.append(item)
|
|
||||||
else -> throw IllegalArgumentException("Expected Int or Long, got ${item?.let { it::class.java.simpleName }}")
|
|
||||||
}
|
|
||||||
'c', 'C' -> stringBuilder.append(
|
|
||||||
(item as? Char) ?: throw IllegalArgumentException("Expected Char, got ${item?.let { it::class.java.simpleName }}")
|
|
||||||
)
|
|
||||||
'o' -> when (item) {
|
|
||||||
is Int -> stringBuilder.append(Integer.toOctalString(item))
|
|
||||||
is Long -> stringBuilder.append(JLong.toOctalString(item))
|
|
||||||
else -> throw IllegalArgumentException("Expected Int or Long, got ${item?.let { it::class.java.simpleName }}")
|
|
||||||
}
|
|
||||||
'x', 'X' -> when (item) {
|
|
||||||
is Int -> stringBuilder.append(Integer.toHexString(item))
|
|
||||||
is Long -> stringBuilder.append(JLong.toHexString(item))
|
|
||||||
else -> throw IllegalArgumentException("Expected Int or Long, got ${item?.let { it::class.java.simpleName }}")
|
|
||||||
}
|
|
||||||
'f' -> when (item) {
|
|
||||||
is Float -> stringBuilder.append(item)
|
|
||||||
is Double -> stringBuilder.append(item)
|
|
||||||
else -> throw IllegalArgumentException("Expected Float or Double, got ${item?.let { it::class.java.simpleName }}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stringBuilder.append(item)
|
|
||||||
string = string.substring((index + 4).coerceAtMost(string.length))
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return stringBuilder.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
Reference in New Issue
Block a user