From 5846131976aaabafbf8d3df88b317c7cdb54d78f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 21 Mar 2017 15:52:49 -0400 Subject: [PATCH] Further refinements to phone number parsing. // FREEBIE --- src/Contacts/PhoneNumber.m | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/Contacts/PhoneNumber.m b/src/Contacts/PhoneNumber.m index 40034d4ff..bd264bde9 100644 --- a/src/Contacts/PhoneNumber.m +++ b/src/Contacts/PhoneNumber.m @@ -144,17 +144,25 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN } }; - if ([sanitizedString hasPrefix:@"+"]) { - // If the text starts with "+", just parse it. - tryParsingWithCountryCode(sanitizedString, [self defaultRegionCode]); + tryParsingWithCountryCode(sanitizedString, [self defaultRegionCode]); + if ([sanitizedString hasPrefix:@"+"]) { + // If the text starts with "+", don't try prepending + // anything else. return result; } + // Try just adding "+" and parsing it. + tryParsingWithCountryCode([NSString stringWithFormat:@"+%@", sanitizedString], [self defaultRegionCode]); + // Order matters; better results should appear first so prefer // matches with the same country code as this client's phone number. OWSAssert(clientPhoneNumber.length > 0); if (clientPhoneNumber.length > 0) { + // Note that NBPhoneNumber uses "country code" to refer to what we call a + // "calling code" (i.e. 44 in +44123123). Within SSK we use "country code" + // (and sometimes "region code") to refer to a country's ISO 2-letter code + // (ISO 3166-1 alpha-2). NSNumber *callingCodeForLocalNumber = [[PhoneNumber phoneNumberFromE164:clientPhoneNumber] getCountryCode]; if (callingCodeForLocalNumber != nil) { tryParsingWithCountryCode([NSString stringWithFormat:@"+%@%@", @@ -162,13 +170,6 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN sanitizedString], [self defaultRegionCode]); - if ([sanitizedString hasPrefix:[NSString stringWithFormat:@"%d", callingCodeForLocalNumber.intValue]]) { - // If the text starts with the calling code for the local number, try - // just adding "+" and parsing it. - tryParsingWithCountryCode( - [NSString stringWithFormat:@"+%@", sanitizedString], [self defaultRegionCode]); - } - // It's gratuitous to try all country codes associated with a given // calling code, but it can't hurt and this isn't a performance // hotspot. @@ -183,9 +184,6 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN } } - // Also try matches using the phone's default region. - tryParsingWithCountryCode(sanitizedString, [self defaultRegionCode]); - return result; }