From 2768dc0c26cdecf8d15b595322f1f28b1b16d90e Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 19 Aug 2024 11:07:34 +0200 Subject: [PATCH] Could also remove now a lot of other unused extension methods --- .../src/nativeMain/kotlin/NativeApp.kt | 20 ++++++- .../banking/fints/log/MessageLogCollector.kt | 4 +- .../datenelemente/abgeleiteteformate/Datum.kt | 3 +- .../abgeleiteteformate/Uhrzeit.kt | 4 +- .../fints/transactions/mt940/Mt940Parser.kt | 3 +- .../fints/transactions/Mt940ParserTest.kt | 6 +- .../fints/extensions/TanImageExtensions.kt | 11 ---- .../extensions/CharExtensions.kt | 13 ----- .../extensions/NumberExtensions.kt | 34 +---------- .../extensions/StringExtensions.kt | 7 --- .../extensions/ThrowableExtensions.kt | 4 -- .../NumberExtensionsTest.kt | 38 +------------ .../StringExtensionsTest.kt | 10 ---- .../dankito/utils/multiplatform/Extensions.kt | 56 ------------------- .../extensions/ByteArrayExtensions.kt | 12 ---- 15 files changed, 30 insertions(+), 195 deletions(-) delete mode 100644 fints4k/src/iosMain/kotlin/net/dankito/banking/fints/extensions/TanImageExtensions.kt delete mode 100644 multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/CharExtensions.kt delete mode 100644 multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt delete mode 100644 multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/ByteArrayExtensions.kt diff --git a/SampleApplications/NativeApp/src/nativeMain/kotlin/NativeApp.kt b/SampleApplications/NativeApp/src/nativeMain/kotlin/NativeApp.kt index 2ec91aa1..abe1ae5a 100644 --- a/SampleApplications/NativeApp/src/nativeMain/kotlin/NativeApp.kt +++ b/SampleApplications/NativeApp/src/nativeMain/kotlin/NativeApp.kt @@ -128,13 +128,13 @@ class NativeApp { val largestAmountDigits = bookedTransactions.maxByOrNull { it.amount.displayString.length }?.amount?.displayString?.length ?: 0 bookedTransactions.sortedByDescending { it.valueDate }.forEachIndexed { transactionIndex, transaction -> - println("${(transactionIndex + 1).toStringWithMinDigits(countTransactionsDigits, " ")}. ${formatDate(transaction.valueDate)} " + - "${transaction.amount.displayString.ensureMinStringLength(largestAmountDigits, " ")} ${transaction.otherPartyName ?: ""} - ${transaction.reference}") + println("${(transactionIndex + 1).toStringWithMinDigits(countTransactionsDigits, ' ')}. ${formatDate(transaction.valueDate)} " + + "${transaction.amount.displayString.padStart(largestAmountDigits, ' ')} ${transaction.otherPartyName ?: ""} - ${transaction.reference}") } } private fun formatDate(date: LocalDate): String { - return date.dayOfMonth.toStringWithTwoDigits() + "." + date.monthNumber.toStringWithTwoDigits() + "." + date.year + return date.dayOfMonth.toStringWithMinDigits(2) + "." + date.monthNumber.toStringWithMinDigits(2) + "." + date.year } @@ -158,4 +158,18 @@ class NativeApp { } } + + 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/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt index 13ae8b29..0f809da3 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/log/MessageLogCollector.kt @@ -8,7 +8,7 @@ import net.dankito.banking.fints.model.MessageLogEntry import net.dankito.banking.fints.model.MessageLogEntryType import net.dankito.utils.multiplatform.extensions.getInnerException import net.dankito.utils.multiplatform.extensions.nthIndexOf -import net.dankito.utils.multiplatform.extensions.toStringWithTwoDigits +import net.dankito.utils.multiplatform.extensions.toStringWithMinDigits import kotlin.reflect.KClass @@ -78,7 +78,7 @@ open class MessageLogCollector { } protected open fun twoDigits(number: Int): String { - return number.toStringWithTwoDigits() + return number.toStringWithMinDigits(2) } protected open fun getMessageTypeString(type: MessageLogEntryType): String { diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Datum.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Datum.kt index bd2fc98d..83a6d284 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Datum.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Datum.kt @@ -5,7 +5,6 @@ import net.codinux.log.logger import net.dankito.banking.fints.messages.Existenzstatus import net.dankito.banking.fints.messages.datenelemente.basisformate.NumerischesDatenelement import net.dankito.utils.multiplatform.extensions.toStringWithMinDigits -import net.dankito.utils.multiplatform.extensions.toStringWithTwoDigits /** @@ -23,7 +22,7 @@ open class Datum(date: Int?, existenzstatus: Existenzstatus) : NumerischesDatene fun format(date: LocalDate): String { // create HbciDateFormatString - return date.year.toStringWithMinDigits(4) + date.monthNumber.toStringWithTwoDigits() + date.dayOfMonth.toStringWithTwoDigits() // TODO: is this correct? + return date.year.toStringWithMinDigits(4) + date.monthNumber.toStringWithMinDigits(2) + date.dayOfMonth.toStringWithMinDigits(2) // TODO: is this correct? } fun parse(dateString: String): LocalDate { diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Uhrzeit.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Uhrzeit.kt index 15ce74de..14c5d1eb 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Uhrzeit.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/datenelemente/abgeleiteteformate/Uhrzeit.kt @@ -4,7 +4,7 @@ import kotlinx.datetime.LocalTime import net.codinux.log.logger import net.dankito.banking.fints.messages.Existenzstatus import net.dankito.banking.fints.messages.datenelemente.basisformate.ZiffernDatenelement -import net.dankito.utils.multiplatform.extensions.toStringWithTwoDigits +import net.dankito.utils.multiplatform.extensions.toStringWithMinDigits /** @@ -23,7 +23,7 @@ open class Uhrzeit(time: Int?, existenzstatus: Existenzstatus) : ZiffernDatenele fun format(time: LocalTime): String { // parse to HbciTimeFormatString - return time.hour.toStringWithTwoDigits() + time.minute.toStringWithTwoDigits() + time.second.toStringWithTwoDigits() // TODO: is this correct? + return time.hour.toStringWithMinDigits(2) + time.minute.toStringWithMinDigits(2) + time.second.toStringWithMinDigits(2) // TODO: is this correct? } fun parse(timeString: String): LocalTime { diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt index 0ee00f9c..83ea4820 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt @@ -8,7 +8,6 @@ import net.dankito.banking.fints.log.IMessageLogAppender import net.dankito.banking.fints.model.Amount import net.dankito.banking.fints.transactions.mt940.model.* import net.dankito.utils.multiplatform.DateFormatter -import net.dankito.utils.multiplatform.extensions.isUpperCase /* @@ -361,7 +360,7 @@ open class Mt940Parser( for (i in 1..referenceParts.size - 1) { val part = referenceParts[i] - if (part.isNotEmpty() && part.first().isUpperCase && referenceParts[i - 1].last().isUpperCase == false) { + if (part.isNotEmpty() && part.first().isUpperCase() && referenceParts[i - 1].last().isUpperCase() == false) { reference.append(" ") } diff --git a/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/transactions/Mt940ParserTest.kt b/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/transactions/Mt940ParserTest.kt index c70ac495..202d1b02 100644 --- a/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/transactions/Mt940ParserTest.kt +++ b/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/transactions/Mt940ParserTest.kt @@ -8,7 +8,7 @@ import net.dankito.banking.fints.transactions.mt940.model.StatementLine import kotlinx.datetime.LocalDate import net.dankito.banking.fints.extensions.* import net.dankito.banking.fints.model.Amount -import net.dankito.utils.multiplatform.extensions.toStringWithTwoDigits +import net.dankito.utils.multiplatform.extensions.toStringWithMinDigits import kotlin.test.Test import kotlin.test.assertContains @@ -386,12 +386,12 @@ class Mt940ParserTest : FinTsTestBase() { private fun convertMt940Date(date: LocalDate): String { // don't use DateFormatter for this as it's not implemented in Kotlin/Native - return (date.year % 100).toString() + date.monthNumber.toStringWithTwoDigits() + date.dayOfMonth.toStringWithTwoDigits() + return (date.year % 100).toString() + date.monthNumber.toStringWithMinDigits(2) + date.dayOfMonth.toStringWithMinDigits(2) } private fun convertToShortBookingDate(date: LocalDate): String { // don't use DateFormatter for this as it's not implemented in Kotlin/Native - return date.monthNumber.toStringWithTwoDigits() + date.dayOfMonth.toStringWithTwoDigits() + return date.monthNumber.toStringWithMinDigits(2) + date.dayOfMonth.toStringWithMinDigits(2) } } \ No newline at end of file diff --git a/fints4k/src/iosMain/kotlin/net/dankito/banking/fints/extensions/TanImageExtensions.kt b/fints4k/src/iosMain/kotlin/net/dankito/banking/fints/extensions/TanImageExtensions.kt deleted file mode 100644 index 614f05df..00000000 --- a/fints4k/src/iosMain/kotlin/net/dankito/banking/fints/extensions/TanImageExtensions.kt +++ /dev/null @@ -1,11 +0,0 @@ -package net.dankito.banking.fints.extensions - -import net.dankito.banking.fints.tan.TanImage -import net.dankito.utils.multiplatform.extensions.toNSData -import kotlinx.cinterop.* -import platform.Foundation.* - - -fun TanImage.imageBytesAsNSData(): NSData { - return imageBytes.toNSData() -} \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/CharExtensions.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/CharExtensions.kt deleted file mode 100644 index 7b81738b..00000000 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/CharExtensions.kt +++ /dev/null @@ -1,13 +0,0 @@ -package net.dankito.utils.multiplatform.extensions - - -val Char.isLowerCase: Boolean - get() = lowercaseChar() == this - -val Char.isUpperCase: Boolean - get() = isLowerCase == false - - -fun CharArray.asString(): String { - return this.joinToString("") -} \ 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 a7482695..a2641032 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,34 +1,6 @@ package net.dankito.utils.multiplatform.extensions -fun Int.toStringWithTwoDigits(): String { - return toStringWithMinDigits(2) -} - -fun Int.toStringWithMinDigits(minimumCountDigits: Int): String { - return toStringWithMinDigits(minimumCountDigits, "0") -} - -fun Int.toStringWithMinDigits(minimumCountDigits: Int, fillerString: String): String { - return this.toString().ensureMinStringLength(minimumCountDigits, fillerString) -} - -fun String.ensureMinStringLength(minimumStringLength: Int, fillerString: String): String { - val countDigitsToAdd = minimumStringLength - this.length - val prefix = if (countDigitsToAdd > 0) fillerString.repeat(countDigitsToAdd) else "" - - return prefix + this -} - -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 +fun Int.toStringWithMinDigits(minimumCountDigits: Int, fillerString: Char = '0'): String { + return this.toString().padStart(minimumCountDigits, fillerString) +} \ 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 index fa71efef..21451930 100644 --- 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 @@ -43,11 +43,4 @@ fun String.indicesOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = } return indices -} - -/** - * Finds all occurrences of [string] in String. - */ -fun String.countOccurrences(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int { - return this.indicesOf(string, startIndex, ignoreCase).size } \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/ThrowableExtensions.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/ThrowableExtensions.kt index 909723b1..6af358ca 100644 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/ThrowableExtensions.kt +++ b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/ThrowableExtensions.kt @@ -22,10 +22,6 @@ fun Throwable.getAllExceptionMessages(maxDepth: Int = 5): List { return exceptionMessages.toList() } -fun Throwable.getInnerExceptionMessage(maxDepth: Int = 3): String { - return this.getInnerException(maxDepth).message ?: "" -} - fun Throwable.getInnerException(maxDepth: Int = 3): Throwable { var innerException = this var depth = 0 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 index 2e8628ac..a9d3cf18 100644 --- 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 @@ -48,45 +48,9 @@ class NumberExtensionsTest { } - @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) - } - - @Test fun ensureMinStringLength() { - val result = "123,45 EUR".ensureMinStringLength(12, " ") + val result = "123,45 EUR".padStart(12, ' ') assertEquals(" 123,45 EUR", result) } diff --git a/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/StringExtensionsTest.kt b/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/StringExtensionsTest.kt index 375b2b46..acc5488c 100644 --- a/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/StringExtensionsTest.kt +++ b/multiplatform-utils/src/commonTest/kotlin/net.dankito.utils.multiplatform.extensions/StringExtensionsTest.kt @@ -14,16 +14,6 @@ internal class StringExtensionsTest { assertEquals(415, result) } - @Test - fun countOccurrences() { - val expectedOccurrences = 30 - val input = "java.lang.Exception: A severe error occurred" + IntRange(1, expectedOccurrences).map { "\r\n\tStack trace element $it" } - - val result = input.countOccurrences("\n") - - assertEquals(expectedOccurrences, result) - } - @Test fun indicesOf() { val expectedOccurrences = 30 diff --git a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt b/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt deleted file mode 100644 index 4943184b..00000000 --- a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt +++ /dev/null @@ -1,56 +0,0 @@ -package net.dankito.utils.multiplatform - -import kotlinx.cinterop.* -import platform.Foundation.* - - -fun NSArray.toList(): List { - val result = mutableListOf() - - for (i in 0L until this.count.toLong()) { - result.add(this.objectAtIndex(i.toULong()) as T) - } - - return result -} - - -fun NSDictionary.getString(key: String): String? { - return this.objectForKey(key) as? String -} - -fun NSDictionary.getString(key: String, defaultValue: String): String { - return this.getString(key) ?: defaultValue -} - -fun NSDictionary.getStringOrEmpty(key: String): String { - return this.getString(key, "") -} - - -fun NSComparisonResult.toCompareToResult(): Int { - return when (this) { - NSOrderedSame -> 0 - NSOrderedAscending -> -1 - NSOrderedDescending -> 1 - else -> 0 - } -} - - -fun String.toNSData(): NSData { - return this.encodeToByteArray().toNSData() -} - -fun ByteArray.toNSData(): NSData = NSMutableData().apply { - if (isEmpty()) return@apply - this@toNSData.usePinned { - appendBytes(it.addressOf(0), size.convert()) - } -} - -fun NSData.toByteArray(): ByteArray { - val data: CPointer = bytes!!.reinterpret() - - return ByteArray(length.toInt()) { index -> data[index] } -} \ No newline at end of file diff --git a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/ByteArrayExtensions.kt b/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/ByteArrayExtensions.kt deleted file mode 100644 index 7caa8829..00000000 --- a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/ByteArrayExtensions.kt +++ /dev/null @@ -1,12 +0,0 @@ -package net.dankito.utils.multiplatform.extensions - -import kotlinx.cinterop.* -import platform.Foundation.* - - -fun ByteArray.toNSData(): NSData = NSMutableData().apply { - if (isEmpty()) return@apply - this@toNSData.usePinned { - appendBytes(it.addressOf(0), size.convert()) - } -} \ No newline at end of file