Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
11 kB
4
Indexable
Never
final class MRZrenewer {
    
    // Foreigner Id - firstline
    private static func getFirstLineInForeignerId(MRZ: String) -> String {
        let MRZ: String = MRZ.filter{!$0.isWhitespace}
        var firstLine: String = ""
        
        if MRZ.first != "I"
        {
            firstLine.append("I")
        } else {
            firstLine.append(MRZ.first ?? "I")
        }
        
        var counter = 0
        var temp: String = ""
        
        while counter < 36 {
            temp.append(MRZ[counter])
            counter = counter + 1
        }
        
        var wordList: [String] = [String]()
        
        for word in temp.split(separator: "<") {
            if (String(word) != "I" && String(word) != "<" && !(String(word).isNumber)  ) {
                var tempWord: String = String(word)
                if (tempWord.first == "I") { tempWord = String(tempWord.dropFirst()) }
                if (tempWord.first == "K") { tempWord = String(tempWord.dropFirst()) }
                wordList.append(String(tempWord))
            }
        }
        
        
        var counter2 = 0
        while (counter2 < wordList.count) {
            firstLine.append(String(wordList[counter2]))
            if (counter2 == 0) {
                firstLine.append("<<")
                
            }
            counter2 = counter2 + 1
        }
        
        var counter3 = 36 - firstLine.count
        while counter3 > 0 {
            firstLine.append("<")
            counter3 = counter3 - 1
        }
        
        return firstLine
    }


    // Foreigner Id - secondline
    private static func getSecondLineInForeignerId(MRZ: String) -> String {
        let MRZ: String = MRZ.filter{!$0.isWhitespace}
        var counter = 36
        var tempLine: String = ""
        var secondLine: String = ""
        
        while counter <= 80 {
            tempLine.append(MRZ[counter])
            counter = counter + 1
        }
        
        var templine1: String = String(tempLine.split(separator: "<").first ?? "")
        var line1 = ""
        
        if (templine1.first == "K") {
            line1.append(contentsOf: templine1.dropFirst())
            secondLine.append(line1)
        } else {
            secondLine.append(templine1)
        }
          
        secondLine.append("<<")
        
        if tempLine.count == 0 { return "0" }
        let tempLine2: String = String(tempLine.split(separator: "<")[1])
        var someTemp = ""
        var line2: String = ""
        
        if (tempLine2.first == "O") {
            someTemp.append(contentsOf: tempLine2.dropFirst())
            someTemp.insert("0", at: line2.startIndex)
            line2.append(someTemp)
        } else {
            tempLine2.forEach {
                if ($0.isUppercase || $0.isNumber) {
                    line2.append($0)
                }
            }
        }
            
        secondLine.append(line2)

        
        var myCounter: Int = 35 - secondLine.count - 1
        
        while (myCounter >= 0) {
            secondLine.append("<")
            myCounter = myCounter - 1
        }
        
        var line3: String = String(tempLine.split(separator: "<")[2])
        secondLine.append(line3)

        return secondLine
    }
    
    
    
    
    
    
    
    // OldId
    private static func getFirstLineInOldIdMRZ(MRZ: String) -> String {
        let MRZ: String = MRZ.filter{!$0.isWhitespace}
        var firstLine: String = ""
        if MRZ.first != "I"
        {
            firstLine.append("I")
        } else {
            firstLine.append(MRZ.first ?? "I")
        }
        firstLine.append("<")
        
        var counter = 0
        var temp: String = ""
        
        while counter <= 40 {
            temp.append(MRZ[counter])
            counter = counter + 1
        }
        
        var wordList: [String] = [String]()
        
        for word in temp.split(separator: "<") {
            if (String(word) != "I" && String(word) != "<" && !(String(word).isNumber)  ) {
                var tempWord: String = String(word)
                if (tempWord.first == "I") { tempWord = String(tempWord.dropFirst()) }
                if (tempWord.first == "K") { tempWord = String(tempWord.dropFirst()) }
                wordList.append(String(tempWord))
            }
        }
        
        
        var counter2 = 0
        while (counter2 < wordList.count) {
            firstLine.append(String(wordList[counter2]))
            if (counter2 == 0) {
                firstLine.append("<<")
                
            }
            counter2 = counter2 + 1
        }
        
        var counter3 = 36 - firstLine.count
        while counter3 > 0 {
            firstLine.append("<")
            counter3 = counter3 - 1
        }
        
        return firstLine
    }

    private static func getSecondLineInOldIdMRZ(MRZ: String) -> String {
        let MRZ: String = MRZ.filter{!$0.isWhitespace}
        var counter = 36
        var tempLine: String = ""
        var secondLine: String = ""
        
        while counter <= 80 {
            tempLine.append(MRZ[counter])
            counter = counter + 1
        }

        var line1: String = String(tempLine.split(separator: "<").first ?? "")
        if line1.count == 7 {
            line1.insert(Character(MRZ[35]), at: line1.startIndex)
        }
        secondLine.append(line1)
        secondLine.append("<")
        // test line starts here
        if tempLine.count == 0 { return "0" }
        let line2: String = String(tempLine.split(separator: "<")[1])
        // test line ends here
        var line3: String = ""
        if !(line2.last?.isNumber ?? true) {
            line3 = String(line2.dropLast())
        } else {
            line3 = String(line2)
        }
        
        secondLine.append(line3)

        return secondLine
    }

    // Re-creating the FirstLine
    private static func getFirstLineInNewIdMRZ(MRZ: String) -> String {
        var firstLine = ""
        var count: Int = 0
        
        while count <= 21  {
            firstLine.append(MRZ[count])
            count = count + 1
        }
        
        if firstLine.dropLast().last == "I" {
            if let _ = firstLine.range(of: "I") {
                    firstLine = firstLine.replacingOccurrences(of: "I", with: "1")
            }
        }
        
        if firstLine.last == "I" {
            if let _ = firstLine.range(of: "I") {
                    firstLine = firstLine.replacingOccurrences(of: "I", with: "1")
            }
        } else if firstLine.last == "O" {
            if let _ = firstLine.range(of: "O") {
                    firstLine = firstLine.replacingOccurrences(of: "O", with: "0")
            }
        }
        
        
        return firstLine + "<<<<<<<<"
    }

    // Re-creating the SecondLine
    private static func getSecondLineInNewIdMRZ(MRZ: String) -> String {
        var secondLine: String = ""
        var tempCount: Int = 28
        var cleanLine: String = ""
        
        while tempCount <= 59  {
            secondLine.append(String(Array(MRZ)[tempCount]))
            tempCount = tempCount + 1
        }
        
        if let line = secondLine.split(separator: "<").first {
            for letter in line {
                if letter == "K" {continue}
                cleanLine.append(letter)
            }
        }
        
        if cleanLine.last != "E" {
            cleanLine = String(cleanLine.dropLast())
        }
        
        secondLine = cleanLine + "<<<<<<<<<<<"
        return secondLine
    }

    // Re-creating the ThirdLine
    private static func getThirdLineInNewIdMRZ(MRZ: String) -> String {
        // This method also includes the Digit from the second line
        
        var thirdLine = ""
        var count: Int = 59
        
        while count <= 89  {
            if Array(MRZ)[safe: count] == nil { break }
            thirdLine.append(String(Array(MRZ)[count])) // out of range risk fixed
            count = count + 1
        }
        
        var fullname = ["", ""]
        var counter = 0

        for word in thirdLine.split(separator: "<") {
            if word == "K" {continue}
            if fullname[safe: counter] == nil { break }
            fullname[counter].append(contentsOf: word)
            counter = counter + 1
        }
        
        var number: String = ""
        var name: String = ""
        var surname: String = ""
        
        if let first = fullname[0].first {
            if first.isNumber {
                number = String(first)
                name = String(fullname[0].dropFirst())
                surname = fullname[1]
            } else if (first == "K" && fullname[0][1].isNumber ) {
                number = String(fullname[0][1])
                name = String(fullname[0].dropFirst().dropFirst())
                surname = fullname[1]
            } else if (first == "?") {
                number = "2"
                name = String(fullname[0].dropFirst())
                surname = fullname[1]
            }
            
        }
        

        var middleWord: String = name + "<<" + surname
        let limit = 30 - middleWord.count
        var lastCount: Int = 0
        
        while (lastCount < limit) {
            middleWord.append("<")
            lastCount = lastCount + 1
        }

        
        thirdLine = middleWord
        return number + thirdLine
    }

    private static func recreateMRZNewId(MRZ: String) -> String {
        return getFirstLineInNewIdMRZ(MRZ: MRZ)
        // + getSecondThirdLineInNewIdMRZ(MRZ: MRZ) + "<"
         + getSecondLineInNewIdMRZ(MRZ: MRZ) + getThirdLineInNewIdMRZ(MRZ: MRZ) + "<"
    }

    private static func recreateMRZOldId(MRZ: String) -> String {
        return getFirstLineInOldIdMRZ(MRZ: MRZ) + "\n" + getSecondLineInOldIdMRZ(MRZ: MRZ)
    }
    
    private static func recreateForeignerId(MRZ: String) -> String {
        return getFirstLineInForeignerId(MRZ: MRZ) + "\n" + getSecondLineInForeignerId(MRZ: MRZ)
    }

    // This method will be called
    public static func recreateMRZ(MRZ: String) -> String {
        
        if ((MRZ[1] == "A" && MRZ[2] == "A") || (MRZ.count > 90)) {
            return recreateMRZNewId(MRZ: MRZ)
        } else if (MRZ[1] == "<") || ((MRZ[1] == "A" && (MRZ[2]) == "Z")) || ((MRZ[1] == "K" && (MRZ[2]) == "A")) {
            return recreateMRZOldId(MRZ: MRZ)
        } else if ((MRZ[1] == "P") || (MRZ[1] == "T")) {
            return recreateForeignerId(MRZ: MRZ)
        } else {
            return "No match"
        }
        
    }
}
Leave a Comment