<commit_before>//
//  PhoneNumber.swift
//  PhoneNumberKit
//
//  Created by <NAME> on 26/09/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public struct PhoneNumber {
    private(set) public var countryCode: UInt64
    private(set) public var nationalNumber: UInt64
    private(set) public var numberExtension: String?
    private(set) public var rawNumber: String
    private(set) public var leadingZero: Bool
    private(set) public var type: PNPhoneNumberType
}

public extension PhoneNumber {
    
    // Parse on init
    public init(rawNumber: String) throws {
        let parser = PhoneNumberParser()
        let phoneNumberKit = PhoneNumberKit.sharedInstance
        let defaultRegion = phoneNumberKit.defaultRegionCode()
        self.rawNumber = rawNumber
        
        // Validations
        if (rawNumber.isEmpty) {
            throw PNParsingError.NotANumber
        } else if (rawNumber.characters.count > PNMaxInputStringLength) {
            throw PNParsingError.TooLong
        }
        var nationalNumber = parser.extractPossibleNumber(rawNumber)
        
        if (!parser.isViablePhoneNumber(nationalNumber)) {
            throw PNParsingError.NotANumber
        }
        if (!parser.checkRegionForParsing(nationalNumber, defaultRegion: defaultRegion)) {
            throw PNParsingError.InvalidCountryCode
        }
        
        // Extension parsing
        let extn = parser.stripExtension(&nationalNumber)
        if (extn != nil && extn?.characters.count > 0) {
            self.numberExtension = extn
        }
        
        // Country code parsing
        var regionMetaData =  phoneNumberKit.metadata.filter { $0.codeID == defaultRegion}.first
        var countryCode : UInt64 = 0
        do {
            countryCode = try parser.extractCountryCode(nationalNumber, nationalNumber: &nationalNumber, metadata: regionMetaData!)
            self.countryCode = countryCode
        } catch {
            do {
                let plusRemovedNumebrString = replaceStringByRegex(PNLeadingPlusCharsPattern, string: nationalNumber as String)
                countryCode = try parser.extractCountryCode(plusRemovedNumebrString, nationalNumber: &nationalNumber, metadata: regionMetaData!)
                self.countryCode = countryCode
            } catch {
                throw PNParsingError.InvalidCountryCode
            }
        }
        if (countryCode == 0) {
            self.countryCode = regionMetaData!.countryCode
        }
        
        // Length Validations
        var normalizedNationalNumber = parser.normalizePhoneNumber(nationalNumber as String)
        if (normalizedNationalNumber.characters.count <=
            PNMinLengthForNSN) {
            throw PNParsingError.TooShort
        }
        if (normalizedNationalNumber.characters.count >= PNMaxLengthForNSN) {
            throw PNParsingError.TooLong
        }
        
        // Regex validations
        if (self.countryCode != regionMetaData!.countryCode) {
            let country = phoneNumberKit.countriesForCode(countryCode).first
            if  (country == nil) {
                throw PNParsingError.NotANumber
            }
            regionMetaData =  phoneNumberKit.metadata.filter { $0.codeID == country}.first
        }
        
        // Carrier Code
        var carrierCode : NSString = NSString()
        parser.stripNationalPrefixAndCarrierCode(&normalizedNationalNumber, metadata: regionMetaData!, carrierCode: &carrierCode)
        
        self.type = parser.extractNumberType(normalizedNationalNumber, metadata: regionMetaData!)
        if (self.type == PNPhoneNumberType.Unknown) {
            throw PNParsingError.NotANumber
        }
        self.leadingZero = normalizedNationalNumber.hasPrefix("0")
        self.nationalNumber = UInt64(normalizedNationalNumber)!
    }
    
    private func adjustedNationalNumber() -> String {
        if (self.leadingZero) {
            return "0" + String(nationalNumber)
        }
        else {
            return String(nationalNumber)
        }
    }
    
    // Format to E164 format (e.g. +33689123456)<commit_msg>Use main country for code<commit_after>//
//  PhoneNumber.swift
//  PhoneNumberKit
//
//  Created by <NAME> on 26/09/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public struct PhoneNumber {
    private(set) public var countryCode: UInt64
    private(set) public var nationalNumber: UInt64
    private(set) public var numberExtension: String?
    private(set) public var rawNumber: String
    private(set) public var leadingZero: Bool
    private(set) public var type: PNPhoneNumberType
}

public extension PhoneNumber {
    
    // Parse on init
    public init(rawNumber: String) throws {
        let parser = PhoneNumberParser()
        let phoneNumberKit = PhoneNumberKit.sharedInstance
        let defaultRegion = phoneNumberKit.defaultRegionCode()
        self.rawNumber = rawNumber
        
        // Validations
        if (rawNumber.isEmpty) {
            throw PNParsingError.NotANumber
        } else if (rawNumber.characters.count > PNMaxInputStringLength) {
            throw PNParsingError.TooLong
        }
        var nationalNumber = parser.extractPossibleNumber(rawNumber)
        
        if (!parser.isViablePhoneNumber(nationalNumber)) {
            throw PNParsingError.NotANumber
        }
        if (!parser.checkRegionForParsing(nationalNumber, defaultRegion: defaultRegion)) {
            throw PNParsingError.InvalidCountryCode
        }
        
        // Extension parsing
        let extn = parser.stripExtension(&nationalNumber)
        if (extn != nil && extn?.characters.count > 0) {
            self.numberExtension = extn
        }
        
        // Country code parsing
        var regionMetaData =  phoneNumberKit.metadata.filter { $0.codeID == defaultRegion}.first
        var countryCode : UInt64 = 0
        do {
            countryCode = try parser.extractCountryCode(nationalNumber, nationalNumber: &nationalNumber, metadata: regionMetaData!)
            self.countryCode = countryCode
        } catch {
            do {
                let plusRemovedNumebrString = replaceStringByRegex(PNLeadingPlusCharsPattern, string: nationalNumber as String)
                countryCode = try parser.extractCountryCode(plusRemovedNumebrString, nationalNumber: &nationalNumber, metadata: regionMetaData!)
                self.countryCode = countryCode
            } catch {
                throw PNParsingError.InvalidCountryCode
            }
        }
        if (countryCode == 0) {
            self.countryCode = regionMetaData!.countryCode
        }
        
        // Length Validations
        var normalizedNationalNumber = parser.normalizePhoneNumber(nationalNumber as String)
        if (normalizedNationalNumber.characters.count <=
            PNMinLengthForNSN) {
            throw PNParsingError.TooShort
        }
        if (normalizedNationalNumber.characters.count >= PNMaxLengthForNSN) {
            throw PNParsingError.TooLong
        }
        
        // Regex validations
        if (self.countryCode != regionMetaData!.countryCode) {
            let country = phoneNumberKit.mainCountryForCode(countryCode)
            if  (country == nil) {
                throw PNParsingError.NotANumber
            }
            regionMetaData =  phoneNumberKit.metadata.filter { $0.codeID == country}.first
        }
        
        // Carrier Code
        var carrierCode : NSString = NSString()
        parser.stripNationalPrefixAndCarrierCode(&normalizedNationalNumber, metadata: regionMetaData!, carrierCode: &carrierCode)
        
        self.type = parser.extractNumberType(normalizedNationalNumber, metadata: regionMetaData!)
        if (self.type == PNPhoneNumberType.Unknown) {
            throw PNParsingError.NotANumber
        }
        self.leadingZero = normalizedNationalNumber.hasPrefix("0")
        self.nationalNumber = UInt64(normalizedNationalNumber)!
    }
    
    private func adjustedNationalNumber() -> String {
        if (self.leadingZero) {
            return "0" + String(nationalNumber)
        }
        else {
            return String(nationalNumber)
        }
    }
    
    // Format to E164 format (e.g. +33689123456)
{'fraction_non_alphanumeric': 0.07730462134043067, 'fraction_numerical': 0.009920154851197677, 'mean_word_length': 2.267588932806324, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7368680', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2102, 'n_words': 465}