Improved performance for splitting into statements by factor 10

This commit is contained in:
dankito 2020-05-20 18:21:52 +02:00
parent bee07743d9
commit 419e97d138
1 changed files with 5 additions and 18 deletions

View File

@ -24,8 +24,6 @@ None of lines include only Space.
open class Mt940Parser : IMt940Parser { open class Mt940Parser : IMt940Parser {
companion object { companion object {
val AccountStatementsSeparatorPattern = Pattern.compile("^\\s*-\\s*\$") // a line only with '-' and may other white space characters
val AccountStatementFieldSeparatorPattern = Pattern.compile(":\\d\\d\\w?:") val AccountStatementFieldSeparatorPattern = Pattern.compile(":\\d\\d\\w?:")
@ -75,26 +73,15 @@ open class Mt940Parser : IMt940Parser {
protected open fun splitIntoSingleAccountStatements(mt940String: String): List<String> { protected open fun splitIntoSingleAccountStatements(mt940String: String): List<String> {
val accountStatements = mutableListOf<String>() return mt940String.split("\n-")
// MT940 line separator is "\r\n", but i added also "\n" and "\r" cause if parsed from file on Linux or MacOS these are the line separators there
val lines = mt940String.split("\r\n", "\n", "\r")
var lastMatchedLine = 0
lines.forEachIndexed { index, line ->
if (line == "-") {
val test = AccountStatementsSeparatorPattern.matcher(line).matches() // TODO
accountStatements.add(lines.subList(lastMatchedLine, index).joinToString("\n"))
lastMatchedLine = index + 1
}
}
return accountStatements
} }
protected open fun parseAccountStatement(accountStatementString: String): AccountStatement? { protected open fun parseAccountStatement(accountStatementString: String): AccountStatement? {
if (accountStatementString.isBlank()) {
return null
}
try { try {
val fieldsByCode = splitIntoFields(accountStatementString) val fieldsByCode = splitIntoFields(accountStatementString)