diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt deleted file mode 100644 index cc4424ea..00000000 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt +++ /dev/null @@ -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 - - } - -} \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/NumberExtensions.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/NumberExtensions.kt index b905bb03..afb5360b 100644 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/NumberExtensions.kt +++ b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/NumberExtensions.kt @@ -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) -} \ No newline at end of file +val Int.numberOfDigits: Int + get() { + var number = this + var count = 0 + + while (number != 0) { + number /= 10 + ++count + } + + return count + } \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/StringExtensions.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/StringExtensions.kt deleted file mode 100644 index a1591931..00000000 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/StringExtensions.kt +++ /dev/null @@ -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) -} \ No newline at end of file diff --git a/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/NumberExtensionsTest.kt b/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/NumberExtensionsTest.kt new file mode 100644 index 00000000..cc659368 --- /dev/null +++ b/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/NumberExtensionsTest.kt @@ -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) + } + +} \ No newline at end of file diff --git a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt b/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt deleted file mode 100644 index 619ff309..00000000 --- a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt +++ /dev/null @@ -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) - } - - } - -} \ No newline at end of file diff --git a/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt b/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt deleted file mode 100644 index a9df9b58..00000000 --- a/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/StringHelper.kt +++ /dev/null @@ -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) - } - - } - -} \ No newline at end of file