libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 6a1d7bcd2ac187d58c7e2d2c2ea8e966047479bc
parent 411fbc4e1e12f3a6528feb54b57db72d540b4363
Author: Antoine A <>
Date:   Sat, 11 Jan 2025 17:00:43 +0100

common: clean subject parsing code

Diffstat:
Mcommon/src/main/kotlin/Subject.kt | 25++++++-------------------
1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/common/src/main/kotlin/Subject.kt b/common/src/main/kotlin/Subject.kt @@ -59,6 +59,7 @@ private data class Candidate(val subject: IncomingSubject, val quality: Base32Qu private const val KEY_SIZE = 52; private const val KYC_SIZE = KEY_SIZE + 3; +private val ALPHA_NUMBERIC_PATTERN = Regex("[0-9a-zA-Z]*") /** * Extract the public key from an unstructured incoming transfer subject. @@ -99,25 +100,11 @@ fun parseIncomingSubject(subject: String): IncomingSubject { } // Find and concatenate valid parts of a keys - val parts = mutableListOf<IntRange>() + val parts = mutableListOf(0) val concatenated = StringBuilder() - var part: Int? = null - for ((i, c) in subject.withIndex()) { - if (c.isLetterOrDigit()) { - part = part ?: i - } else if (part != null) { - val start = concatenated.length - concatenated.append(subject.substring(part until i)); - val end = concatenated.length - parts.add(start until end); - part = null - } - } - if (part != null) { - val start = concatenated.length - concatenated.append(subject.substring(part until subject.length)); - val end = concatenated.length - parts.add(start until end); + for (match in ALPHA_NUMBERIC_PATTERN.findAll(subject)) { + concatenated.append(match.value); + parts.add(concatenated.length); } // Find best candidates @@ -126,7 +113,7 @@ fun parseIncomingSubject(subject: String): IncomingSubject { for ((i, start) in parts.withIndex()) { // Use progressively longer concatenation for (end in parts.subList(i, parts.size)) { - val range = start.start..end.endInclusive + val range = start until end // Until they are to long to be a key if (range.count() > KYC_SIZE) { break;