diff --git a/CaptureSDK/GiniCaptureSDK/Sources/GiniCaptureSDK/Core/Screens/Camera/IBAN/IBANRecogniser.swift b/CaptureSDK/GiniCaptureSDK/Sources/GiniCaptureSDK/Core/Screens/Camera/IBAN/IBANRecogniser.swift index b4b4d4c56..2fad0146f 100644 --- a/CaptureSDK/GiniCaptureSDK/Sources/GiniCaptureSDK/Core/Screens/Camera/IBAN/IBANRecogniser.swift +++ b/CaptureSDK/GiniCaptureSDK/Sources/GiniCaptureSDK/Core/Screens/Camera/IBAN/IBANRecogniser.swift @@ -6,13 +6,36 @@ import Foundation -func extractIBANS(string: String) -> [String] { - var results = [String]() - var prefferedIBANs = [String]() +private func extractIBANMatches(from string: String) -> [NSTextCheckingResult] { let stringRange = NSRange(location: 0, length: string.count) let allIBANs = IBANKnowledge().universalIBANRegex.matches(in: string, options: [], range: stringRange) let iBANsInBlocks = IBANKnowledge().ibanInBlocksRegex.matches(in: string, options: [], range: stringRange) - let matches = allIBANs + iBANsInBlocks + return allIBANs + iBANsInBlocks +} + +func extractIBANS(string: String) -> [String] { + var results = [String]() + var prefferedIBANs = [String]() + var matches = extractIBANMatches(from: string) + + // try to fix invalid strings + if matches.isEmpty, string.count > 2 { + var number = String(string.dropFirst(2)) + + number = number.replacingOccurrences(of: "s", with: "5") + number = number.replacingOccurrences(of: "S", with: "5") + number = number.replacingOccurrences(of: "I", with: "1") + number = number.replacingOccurrences(of: "i", with: "1") + number = number.replacingOccurrences(of: "l", with: "1") + number = number.replacingOccurrences(of: "T", with: "1") + number = number.replacingOccurrences(of: "o", with: "0") + number = number.replacingOccurrences(of: "O", with: "0") + number = number.replacingOccurrences(of: "Q", with: "0") + number = number.trimmingCharacters(in: .whitespaces) + number = number.replacingOccurrences(of: ",", with: "") + number = number.replacingOccurrences(of: ".", with: "") + matches = extractIBANMatches(from: String(string.prefix(2)) + number) + } for match in matches { if let range = Range(match.range, in: string) {