Could also remove now a lot of other unused extension methods

This commit is contained in:
dankito 2024-08-19 11:07:34 +02:00
parent 2e13a153bd
commit 2768dc0c26
15 changed files with 30 additions and 195 deletions

View File

@ -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
}
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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(" ")
}

View File

@ -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)
}
}

View File

@ -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()
}

View File

@ -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("")
}

View File

@ -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
}
fun Int.toStringWithMinDigits(minimumCountDigits: Int, fillerString: Char = '0'): String {
return this.toString().padStart(minimumCountDigits, fillerString)
}

View File

@ -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
}

View File

@ -22,10 +22,6 @@ fun Throwable.getAllExceptionMessages(maxDepth: Int = 5): List<String> {
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

View File

@ -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)
}

View File

@ -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

View File

@ -1,56 +0,0 @@
package net.dankito.utils.multiplatform
import kotlinx.cinterop.*
import platform.Foundation.*
fun <T> NSArray.toList(): List<T> {
val result = mutableListOf<T>()
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<ByteVar> = bytes!!.reinterpret()
return ByteArray(length.toInt()) { index -> data[index] }
}

View File

@ -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())
}
}