Fix a hotspot in the phone number parsing logic.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 71a304f84f
commit 6c2de6ed56

@ -173,8 +173,8 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
// It's gratuitous to try all country codes associated with a given // 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 // calling code, but it can't hurt and this isn't a performance
// hotspot. // hotspot.
NSArray *possibleLocalCountryCodes = [PhoneNumberUtil countryCodesFromCallingCode:[NSString stringWithFormat:@"+%@", NSArray *possibleLocalCountryCodes = [PhoneNumberUtil.sharedUtil
callingCodeForLocalNumber]]; countryCodesFromCallingCode:[NSString stringWithFormat:@"+%@", callingCodeForLocalNumber]];
for (NSString *countryCode in possibleLocalCountryCodes) { for (NSString *countryCode in possibleLocalCountryCodes) {
tryParsingWithCountryCode([NSString stringWithFormat:@"+%@%@", tryParsingWithCountryCode([NSString stringWithFormat:@"+%@%@",
callingCodeForLocalNumber, callingCodeForLocalNumber,

@ -15,7 +15,7 @@
+ (NSString *)callingCodeFromCountryCode:(NSString *)countryCode; + (NSString *)callingCodeFromCountryCode:(NSString *)countryCode;
+ (NSString *)countryNameFromCountryCode:(NSString *)countryCode; + (NSString *)countryNameFromCountryCode:(NSString *)countryCode;
+ (NSArray *)countryCodesForSearchTerm:(NSString *)searchTerm; + (NSArray *)countryCodesForSearchTerm:(NSString *)searchTerm;
+ (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode; - (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode;
+ (NSUInteger)translateCursorPosition:(NSUInteger)offset + (NSUInteger)translateCursorPosition:(NSUInteger)offset
from:(NSString *)source from:(NSString *)source

@ -7,6 +7,14 @@
#import "FunctionalUtil.h" #import "FunctionalUtil.h"
#import "Util.h" #import "Util.h"
@interface PhoneNumberUtil ()
@property (nonatomic, readonly) NSMutableDictionary *countryCodesFromCallingCodeCache;
@end
#pragma mark -
@implementation PhoneNumberUtil @implementation PhoneNumberUtil
+ (instancetype)sharedUtil { + (instancetype)sharedUtil {
@ -23,6 +31,7 @@
if (self) { if (self) {
_nbPhoneNumberUtil = [[NBPhoneNumberUtil alloc] init]; _nbPhoneNumberUtil = [[NBPhoneNumberUtil alloc] init];
_countryCodesFromCallingCodeCache = [NSMutableDictionary new];
OWSSingletonAssert(); OWSSingletonAssert();
} }
@ -87,15 +96,26 @@
return callingCode; return callingCode;
} }
+ (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode { - (NSArray *)countryCodesFromCallingCode:(NSString *)callingCode
NSMutableArray *countryCodes = [NSMutableArray new]; {
for (NSString *countryCode in NSLocale.ISOCountryCodes) { @synchronized(self)
NSString *callingCodeForCountryCode = [self callingCodeFromCountryCode:countryCode]; {
if ([callingCode isEqualToString:callingCodeForCountryCode]) { OWSAssert(callingCode.length > 0);
[countryCodes addObject:countryCode];
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 { + (BOOL)name:(NSString *)nameString matchesQuery:(NSString *)queryString {

Loading…
Cancel
Save