diff --git a/src/Contacts/PhoneNumber.m b/src/Contacts/PhoneNumber.m index c9eea1f88..b29d3d30c 100644 --- a/src/Contacts/PhoneNumber.m +++ b/src/Contacts/PhoneNumber.m @@ -173,8 +173,8 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN // 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. - NSArray *possibleLocalCountryCodes = [PhoneNumberUtil countryCodesFromCallingCode:[NSString stringWithFormat:@"+%@", - callingCodeForLocalNumber]]; + NSArray *possibleLocalCountryCodes = [PhoneNumberUtil.sharedUtil + countryCodesFromCallingCode:[NSString stringWithFormat:@"+%@", callingCodeForLocalNumber]]; for (NSString *countryCode in possibleLocalCountryCodes) { tryParsingWithCountryCode([NSString stringWithFormat:@"+%@%@", callingCodeForLocalNumber, diff --git a/src/Contacts/PhoneNumberUtil.h b/src/Contacts/PhoneNumberUtil.h index ebcb0615a..0d830ec43 100644 --- a/src/Contacts/PhoneNumberUtil.h +++ b/src/Contacts/PhoneNumberUtil.h @@ -15,7 +15,7 @@ + (NSString *)callingCodeFromCountryCode:(NSString *)countryCode; + (NSString *)countryNameFromCountryCode:(NSString *)countryCode; + (NSArray *)countryCodesForSearchTerm:(NSString *)searchTerm; -+ (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode; +- (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode; + (NSUInteger)translateCursorPosition:(NSUInteger)offset from:(NSString *)source diff --git a/src/Contacts/PhoneNumberUtil.m b/src/Contacts/PhoneNumberUtil.m index a54921d64..9dc0df8e7 100644 --- a/src/Contacts/PhoneNumberUtil.m +++ b/src/Contacts/PhoneNumberUtil.m @@ -7,6 +7,14 @@ #import "FunctionalUtil.h" #import "Util.h" +@interface PhoneNumberUtil () + +@property (nonatomic, readonly) NSMutableDictionary *countryCodesFromCallingCodeCache; + +@end + +#pragma mark - + @implementation PhoneNumberUtil + (instancetype)sharedUtil { @@ -23,6 +31,7 @@ if (self) { _nbPhoneNumberUtil = [[NBPhoneNumberUtil alloc] init]; + _countryCodesFromCallingCodeCache = [NSMutableDictionary new]; OWSSingletonAssert(); } @@ -87,15 +96,26 @@ return callingCode; } -+ (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode { - NSMutableArray *countryCodes = [NSMutableArray new]; - for (NSString *countryCode in NSLocale.ISOCountryCodes) { - NSString *callingCodeForCountryCode = [self callingCodeFromCountryCode:countryCode]; - if ([callingCode isEqualToString:callingCodeForCountryCode]) { - [countryCodes addObject:countryCode]; +- (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode +{ + @synchronized(self) + { + OWSAssert(callingCode.length > 0); + + NSArray *result = self.countryCodesFromCallingCodeCache[callingCode]; + if (!result) { + NSMutableArray *countryCodes = [NSMutableArray new]; + for (NSString *countryCode in NSLocale.ISOCountryCodes) { + NSString *callingCodeForCountryCode = [PhoneNumberUtil callingCodeFromCountryCode:countryCode]; + if ([callingCode isEqualToString:callingCodeForCountryCode]) { + [countryCodes addObject:countryCode]; + } + } + result = [countryCodes copy]; + self.countryCodesFromCallingCodeCache[callingCode] = result; } + return result; } - return countryCodes; } + (BOOL)name:(NSString *)nameString matchesQuery:(NSString *)queryString {