PHondogo
03/31/2023, 6:03 AMephemient
03/31/2023, 6:12 AMCLOVIS
03/31/2023, 7:48 AMPHondogo
03/31/2023, 7:50 AMCLOVIS
03/31/2023, 7:50 AMPHondogo
03/31/2023, 7:56 AMephemient
03/31/2023, 10:20 PMfun String.format(vararg args: Any?): String = buildString {
var index = 0
var position = 0
while (position < this@format.length) {
val next = this@format.indexOf('%', startIndex = position)
if (next < 0) break
append(this@format.substring(position, next))
when (val c = this@format.getOrNull(next + 1)) {
'%' -> append('%')
's' -> append(args[index++])
'd' -> append((args[index++] as Number).toInt())
'l' -> append((args[index++] as Number).toLong())
'e', 'f', 'g' -> append((args[index++] as Number).toDouble())
else -> throw IllegalArgumentException(if (c == null) "%" else "%$c")
}
position = next + 2
}
append(this@format.substring(position))
}
but supporting the full set of features that the Java string formatter has (which Kotlin/JVM simply delegates to) is challengingPHondogo
04/01/2023, 3:59 AM