From b71cbadf1180814048608286985875437ace78e9 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Sun, 4 Dec 2016 17:49:44 +0100 Subject: [PATCH] Solving bug with short extensions --- PhoneNumberKit/Constants.swift | 2 +- PhoneNumberKit/ParseManager.swift | 11 +++++--- PhoneNumberKit/RegexManager.swift | 27 +++---------------- .../PhoneNumberKitParsingTests.swift | 6 ++--- PhoneNumberKitTests/PhoneNumberKitTests.swift | 16 +++++++++++ 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/PhoneNumberKit/Constants.swift b/PhoneNumberKit/Constants.swift index 46142cd30..396b03618 100644 --- a/PhoneNumberKit/Constants.swift +++ b/PhoneNumberKit/Constants.swift @@ -114,7 +114,7 @@ struct PhoneNumberPatterns { static let capturingDigitPattern = "([0-90-9٠-٩۰-۹])" - static let extnPattern = "\\;(.*)" + static let extnPattern = "(?:;ext=([0-90-9٠-٩۰-۹]{1,7})|[ \\t,]*(?:e?xt(?:ensi(?:ó?|ó))?n?|e?xtn?|[,xxX##~~]|int|anexo|int)[:\\..]?[ \\t,-]*([0-90-9٠-٩۰-۹]{1,7})#?|[- ]+([0-90-9٠-٩۰-۹]{1,5})#)$" static let iddPattern = "^(?:\\+|%@)" diff --git a/PhoneNumberKit/ParseManager.swift b/PhoneNumberKit/ParseManager.swift index 3896ab1a9..9e7145e83 100644 --- a/PhoneNumberKit/ParseManager.swift +++ b/PhoneNumberKit/ParseManager.swift @@ -36,11 +36,14 @@ class ParseManager { // Extract number (2) var nationalNumber = numberString let match = try regexManager.phoneDataDetectorMatch(numberString) - if let phoneNumber = match.phoneNumber { - nationalNumber = phoneNumber - } + let matchedNumber = nationalNumber.substring(with: match.range) + nationalNumber = matchedNumber + // Strip and extract extension (3) - let numberExtension = parser.stripExtension(&nationalNumber) + var numberExtension: String? + if let rawExtension = parser.stripExtension(&nationalNumber) { + numberExtension = parser.normalizePhoneNumber(rawExtension) + } // Country code parse (4) guard var regionMetadata = metadataManager.territoriesByCountry[region] else { throw PhoneNumberError.invalidCountryCode diff --git a/PhoneNumberKit/RegexManager.swift b/PhoneNumberKit/RegexManager.swift index 30d07853a..9b311e3e8 100644 --- a/PhoneNumberKit/RegexManager.swift +++ b/PhoneNumberKit/RegexManager.swift @@ -14,16 +14,6 @@ class RegexManager { var regularExpresionPool = [String : NSRegularExpression]() - var phoneDataDetector: NSDataDetector? = { - do { - let dataDetector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue) - return dataDetector - } - catch { - return nil - } - }() - var spaceCharacterSet: CharacterSet = { let characterSet = NSMutableCharacterSet(charactersIn: "\u{00a0}") characterSet.formUnion(with: CharacterSet.whitespacesAndNewlines) @@ -32,7 +22,6 @@ class RegexManager { deinit { regularExpresionPool.removeAll() - phoneDataDetector = nil } // MARK: Regular expression @@ -67,23 +56,15 @@ class RegexManager { } func phoneDataDetectorMatch(_ string: String) throws -> NSTextCheckingResult { - guard let matches = phoneDataDetector?.matches(in: string) else { - throw PhoneNumberError.generalError - } - if let firstMatch = matches.first { + let fallBackMatches = try regexMatches(PhoneNumberPatterns.validPhoneNumberPattern, string: string) + if let firstMatch = fallBackMatches.first { return firstMatch } else { - let fallBackMatches = try regexMatches(PhoneNumberPatterns.validPhoneNumberPattern, string: string) - if let firstMatch = fallBackMatches.first { - return firstMatch - } - else { - throw PhoneNumberError.notANumber - } + throw PhoneNumberError.notANumber } } - + // MARK: Match helpers func matchesAtStart(_ pattern: String, string: String) -> Bool { diff --git a/PhoneNumberKitTests/PhoneNumberKitParsingTests.swift b/PhoneNumberKitTests/PhoneNumberKitParsingTests.swift index 6aa44745a..ca2c93297 100644 --- a/PhoneNumberKitTests/PhoneNumberKitParsingTests.swift +++ b/PhoneNumberKitTests/PhoneNumberKitParsingTests.swift @@ -25,11 +25,11 @@ class PhoneNumberKitParsingTests: XCTestCase { func testFailingNumber() { do { - let phoneNumber1 = try phoneNumberKit.parse("+5491187654321 ABC123", withRegion: "AR") - XCTAssertNotNil(phoneNumber1) + _ = try phoneNumberKit.parse("+5491187654321 ABC123", withRegion: "AR") + XCTFail() } catch { - XCTFail() + XCTAssertTrue(true) } } diff --git a/PhoneNumberKitTests/PhoneNumberKitTests.swift b/PhoneNumberKitTests/PhoneNumberKitTests.swift index fc764d219..bb315d047 100644 --- a/PhoneNumberKitTests/PhoneNumberKitTests.swift +++ b/PhoneNumberKitTests/PhoneNumberKitTests.swift @@ -175,6 +175,22 @@ class PhoneNumberKitTests: XCTestCase { } } + // American number with short extension + func testAlternativeNumberWithExtension() { + let testNumber = "2129316760 x28" + do { + let phoneNumber = try phoneNumberKit.parse(testNumber, withRegion: "US", ignoreType: false) + XCTAssertEqual(phoneNumber.countryCode, 1) + XCTAssertEqual(phoneNumber.numberExtension, "28") + XCTAssertEqual(phoneNumber.nationalNumber, 2129316760) + XCTAssertEqual(phoneNumber.leadingZero, false) + } + catch { + XCTFail() + } + } + + // French number with a plus func testValidNumberWithPlusNoWhiteSpace() { let testNumber = "+33689555555"