Replaced StringHelper with toStringWithMinDigits()

This commit is contained in:
dankito 2022-02-12 18:27:58 +01:00
parent da9d5c018b
commit 4acb0f8e0d
6 changed files with 103 additions and 85 deletions

View File

@ -1,14 +0,0 @@
package net.dankito.utils.multiplatform
import kotlin.String
expect class StringHelper {
companion object {
fun format(format: String, vararg args: Any?): String
}
}

View File

@ -1,16 +1,26 @@
package net.dankito.utils.multiplatform.extensions
import net.dankito.utils.multiplatform.StringHelper
fun Int.toStringWithTwoDigits(): String {
return toStringWithMinDigits(2)
}
fun Int.toStringWithMinDigits(countDigits: Int): String {
return format("%0${countDigits}d")
fun Int.toStringWithMinDigits(minimumCountDigits: Int): String {
val countDigitsToAdd = minimumCountDigits - this.numberOfDigits
val prefix = if (countDigitsToAdd > 0) "0".repeat(countDigitsToAdd) else ""
return prefix + this.toString()
}
fun Int.format(format: String): String {
return StringHelper.format(format, this)
}
val Int.numberOfDigits: Int
get() {
var number = this
var count = 0
while (number != 0) {
number /= 10
++count
}
return count
}

View File

@ -1,8 +0,0 @@
package net.dankito.utils.multiplatform.extensions
import net.dankito.utils.multiplatform.StringHelper
fun String.format(vararg args: Any?): String {
return StringHelper.format(this, args)
}

View File

@ -0,0 +1,86 @@
import net.dankito.utils.multiplatform.extensions.numberOfDigits
import net.dankito.utils.multiplatform.extensions.toStringWithMinDigits
import kotlin.test.Test
import kotlin.test.assertEquals
class NumberExtensionsTest {
@Test
fun toStringWithMinDigits_0() {
val result = 7.toStringWithMinDigits(0)
assertEquals("7", result)
}
@Test
fun toStringWithMinDigits_1() {
val result = 7.toStringWithMinDigits(1)
assertEquals("7", result)
}
@Test
fun toStringWithMinDigits_2() {
val result = 7.toStringWithMinDigits(2)
assertEquals("07", result)
}
@Test
fun toStringWithMinDigits_3() {
val result = 7.toStringWithMinDigits(3)
assertEquals("007", result)
}
@Test
fun toStringWithMinDigits_5() {
val result = 7.toStringWithMinDigits(5)
assertEquals("00007", result)
}
@Test
fun toStringWithMinDigits_10() {
val result = 7.toStringWithMinDigits(10)
assertEquals("0000000007", result)
}
@Test
fun getNumberOfDigits_0() {
val result = 0.numberOfDigits
assertEquals(0, result)
}
@Test
fun getNumberOfDigits_1Digit() {
val result = 7.numberOfDigits
assertEquals(1, result)
}
@Test
fun getNumberOfDigits_2Digits() {
val result = 42.numberOfDigits
assertEquals(2, result)
}
@Test
fun getNumberOfDigits_3Digits() {
val result = 123.numberOfDigits
assertEquals(3, result)
}
@Test
fun getNumberOfDigits_10Digits() {
val result = 1234567890.numberOfDigits
assertEquals(10, result)
}
}

View File

@ -1,40 +0,0 @@
package net.dankito.utils.multiplatform
import kotlin.String
import platform.Foundation.*
actual class StringHelper {
actual companion object {
actual fun format(format: String, vararg args: Any?): String {
val adjustedFormat = format.replace("%s", "%@") // Objective-C uses %@ for strings // TODO: also replace %$1s, ...
// return NSString.stringWithFormat("%@ %@ %d", "один" as NSString, "two" as NSString, 3)
//return NSString.stringWithFormat(adjustedFormat, args.firstOrNull()) // spread operator is not supported for varadic functions, seehttps://github.com/JetBrains/kotlin-native/issues/1834
// as spread operator is not supported for varadic functions tried to pass arguments one by one, but didn't work either
return when (args.size) {
0 -> format
1 -> format(adjustedFormat, args.first())
2 -> format(adjustedFormat, args.first(), args.get(2))
else -> format(adjustedFormat, args.first(), args.get(2), args.get(3))
}
}
fun format(format: String, arg1: Any): String {
return NSString.stringWithFormat(format, arg1 as? NSString ?: arg1)
}
fun format(format: String, arg1: Any, arg2: Any): String {
return NSString.stringWithFormat(format, arg1 as? NSString ?: arg1, arg2 as? NSString ?: arg2)
}
fun format(format: String, arg1: Any, arg2: Any, arg3: Any): String {
return NSString.stringWithFormat(format, arg1 as? NSString ?: arg1, arg2 as? NSString ?: arg2, arg3 as? NSString ?: arg3)
}
}
}

View File

@ -1,16 +0,0 @@
package net.dankito.utils.multiplatform
import kotlin.String
actual class StringHelper {
actual companion object {
actual fun format(format: String, vararg args: Any?): String {
return String.format(format, *args)
}
}
}