Merge branch 'charlesmchen/phoneNumberParsingPerf'

pull/1/head
Matthew Chen 8 years ago
commit 955c4d8a02

@ -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,

@ -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

@ -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 {

Loading…
Cancel
Save