Standardize formatting across files using

clang-format -style=Google
This commit is contained in:
Dave MacLachlan 2017-11-18 16:11:20 -08:00
parent aa645572c7
commit ee9ead6cab
29 changed files with 10103 additions and 9373 deletions

View File

@ -8,10 +8,8 @@
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@interface AppDelegate : UIResponder<UIApplicationDelegate>
@property(strong, nonatomic) UIWindow *window;
@end

View File

@ -9,340 +9,342 @@
#import "AppDelegate.h"
@import libPhoneNumberiOS;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
[self.window setRootViewController:[[UIViewController alloc] init]];
[self.window setRootViewController:[[UIViewController alloc] init]];
[self testWithRealData];
//[self testWithGCD];
//[self testForGetSupportedRegions];
[self testWithRealData];
//[self testWithGCD];
//[self testForGetSupportedRegions];
return YES;
return YES;
}
- (void)testForGetSupportedRegions
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSLog(@"%@", [phoneUtil getSupportedRegions]);
- (void)testForGetSupportedRegions {
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSLog(@"%@", [phoneUtil getSupportedRegions]);
}
- (void)testWithRealData {
NBAsYouTypeFormatter *formatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
- (void)testWithRealData
{
NBAsYouTypeFormatter *formatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
NSLog(@"%@ (%@)", [formatter inputDigit:@"2"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"1"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"2"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y" : @"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"2"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"1"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"2"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
NSLog(@"%@", [f inputDigit:@"6"]); // "6"
NSLog(@"%@", [f inputDigit:@"5"]); // "65"
NSLog(@"%@", [f inputDigit:@"0"]); // "650"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2"
NSLog(@"%@", [f inputDigit:@"5"]); // "650 25"
NSLog(@"%@", [f inputDigit:@"3"]); // "650 253"
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
NSLog(@"%@", [f inputDigit:@"6"]); // "6"
NSLog(@"%@", [f inputDigit:@"5"]); // "65"
NSLog(@"%@", [f inputDigit:@"0"]); // "650"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2"
NSLog(@"%@", [f inputDigit:@"5"]); // "650 25"
NSLog(@"%@", [f inputDigit:@"3"]); // "650 253"
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2532"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 22"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 222"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 2222"
// Can remove last digit
NSLog(@"%@", [f removeLastDigit]); // "650 253 222"
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2532"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 22"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 222"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 2222"
// Can remove last digit
NSLog(@"%@", [f removeLastDigit]); // "650 253 222"
NSLog(@"%@", [f inputString:@"16502532222"]); // 1 650 253 2222
NSLog(@"%@", [f inputString:@"16502532222"]); // 1 650 253 2222
// Unit test for isValidNumber is failing some valid numbers. #7
// Unit test for isValidNumber is failing some valid numbers. #7
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS = [phoneUtil parse:@"(366) 522-8999" defaultRegion:@"US" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS = [phoneUtil parse:@"14155552671" defaultRegion:@"US" error:&error];
NBPhoneNumber *phoneNumberUS =
[phoneUtil parse:@"(366) 522-8999" defaultRegion:@"US" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]",
[phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
NSLog(@"- - - - -");
{
NSError *error = nil;
NBPhoneNumber *phoneNumberZZ = [phoneUtil parse:@"+84 74 883313" defaultRegion:NB_UNKNOWN_REGION error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberZZ] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberZZ error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberZZ]);
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS =
[phoneUtil parse:@"14155552671" defaultRegion:@"US" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]",
[phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
NSLog(@"- - - - - GB / +923406171134");
NSLog(@"- - - - -");
// I can't validate pakistani numbers #58
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS = [phoneUtil parse:@"+923406171134" defaultRegion:@"GB" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
{
NSError *error = nil;
NBPhoneNumber *phoneNumberZZ =
[phoneUtil parse:@"+84 74 883313" defaultRegion:NB_UNKNOWN_REGION error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberZZ] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]",
[phoneUtil isPossibleNumber:phoneNumberZZ error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberZZ]);
}
// finnish phone number don't get recognised #59
{
NSString *nationalNumber = @"";
NSNumber *countryCode = [phoneUtil extractCountryCode:@"+358401493292" nationalNumber:&nationalNumber];
NSLog(@"- %@ %@", countryCode, nationalNumber);
NSLog(@"- - - - - GB / +923406171134");
NBPhoneNumber *phonePN = [phoneUtil parse:@"+358401493292" defaultRegion:@"FIN" error:nil];
NSLog(@"- %@", [phoneUtil format:phonePN numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil]);
// I can't validate pakistani numbers #58
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS =
[phoneUtil parse:@"+923406171134" defaultRegion:@"GB" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]",
[phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
NSLog(@"-------------- customTest");
// finnish phone number don't get recognised #59
{
NSString *nationalNumber = @"";
NSNumber *countryCode =
[phoneUtil extractCountryCode:@"+358401493292" nationalNumber:&nationalNumber];
NSLog(@"- %@ %@", countryCode, nationalNumber);
NSError *anError = nil;
NBPhoneNumber *phonePN = [phoneUtil parse:@"+358401493292" defaultRegion:@"FIN" error:nil];
NSLog(@"- %@",
[phoneUtil format:phonePN numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil]);
}
NBPhoneNumber *myNumber = [phoneUtil parse:@"+8617196622520," defaultRegion:nil error:&anError];
if (anError == nil)
{
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");
NSLog(@"E164 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatE164 error:&anError]);
NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&anError]);
NSLog(@"NATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatNATIONAL error:&anError]);
NSLog(@"RFC3966 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatRFC3966 error:&anError]);
}
else
{
NSLog(@"Error : %@", [anError localizedDescription]);
}
NSLog(@"-------------- customTest");
NSLog (@"extractCountryCode [%@]", [phoneUtil extractCountryCode:@"823213123123" nationalNumber:nil]);
NSString *res = nil;
NSNumber *dRes = [phoneUtil extractCountryCode:@"823213123123" nationalNumber:&res];
NSLog (@"extractCountryCode [%@] [%@]", dRes, res);
NSError *anError = nil;
NBAsYouTypeFormatter *fr = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"FR"];
NSLog(@"%@", [fr inputDigit:@"6"]);
NSLog(@"%@", [fr inputDigit:@"7"]);
NSLog(@"%@", [fr inputDigit:@"8"]);
NSLog(@"%@", [fr inputDigit:@"9"]);
NSLog(@"%@", [fr inputDigit:@"0"]);
NSLog(@"%@", [fr inputDigit:@"1"]);
NBPhoneNumber *myNumber = [phoneUtil parse:@"+8617196622520," defaultRegion:nil error:&anError];
if (anError == nil) {
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES" : @"NO");
NSLog(@"E164 : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatE164 error:&anError]);
NSLog(
@"INTERNATIONAL : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&anError]);
NSLog(@"NATIONAL : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatNATIONAL error:&anError]);
NSLog(@"RFC3966 : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatRFC3966 error:&anError]);
} else {
NSLog(@"Error : %@", [anError localizedDescription]);
}
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [fr inputDigit:@"2"]);
NSLog(@"%@", [fr inputDigit:@"3"]);
NSLog(@"%@", [fr inputDigit:@"4"]);
NSLog(@"extractCountryCode [%@]",
[phoneUtil extractCountryCode:@"823213123123" nationalNumber:nil]);
NSString *res = nil;
NSNumber *dRes = [phoneUtil extractCountryCode:@"823213123123" nationalNumber:&res];
NSLog(@"extractCountryCode [%@] [%@]", dRes, res);
NSLog(@"%@", [fr inputString:@"0678901234"]); // 6 78 90 12 34 // it Should be like this
NBAsYouTypeFormatter *fr = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"FR"];
NSLog(@"%@", [fr inputDigit:@"6"]);
NSLog(@"%@", [fr inputDigit:@"7"]);
NSLog(@"%@", [fr inputDigit:@"8"]);
NSLog(@"%@", [fr inputDigit:@"9"]);
NSLog(@"%@", [fr inputDigit:@"0"]);
NSLog(@"%@", [fr inputDigit:@"1"]);
// JP Test
NBPhoneNumber *phoneJP = [phoneUtil parse:@"0066-33-132432" defaultRegion:@"JP" error:nil];
NSLog(@"- %@", [phoneUtil format:phoneJP numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil]);
NSLog(@"- isValidNumber %@", [phoneUtil isValidNumber:phoneJP] ? @"Y":@"N");
NSLog(@"- isPossibleNumber %@", [phoneUtil isPossibleNumber:phoneJP] ? @"Y":@"N");
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [fr inputDigit:@"2"]);
NSLog(@"%@", [fr inputDigit:@"3"]);
NSLog(@"%@", [fr inputDigit:@"4"]);
NSLog(@"%@", [fr inputString:@"0678901234"]); // 6 78 90 12 34 // it Should be like this
// JP Test
NBPhoneNumber *phoneJP = [phoneUtil parse:@"0066-33-132432" defaultRegion:@"JP" error:nil];
NSLog(@"- %@",
[phoneUtil format:phoneJP numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil]);
NSLog(@"- isValidNumber %@", [phoneUtil isValidNumber:phoneJP] ? @"Y" : @"N");
NSLog(@"- isPossibleNumber %@", [phoneUtil isPossibleNumber:phoneJP] ? @"Y" : @"N");
}
- (NSString *)stringWithRandomNumber {
NSString *numbers = @"0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity:11];
- (NSString *)stringWithRandomNumber
{
NSString *numbers = @"0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity:11];
for (int i = 0; i < 12; i++) {
[randomString appendFormat:@"%C", [numbers characterAtIndex:arc4random() % [numbers length]]];
}
for (int i=0; i<12; i++) {
[randomString appendFormat: @"%C", [numbers characterAtIndex: arc4random() % [numbers length]]];
}
return randomString;
return randomString;
}
- (NSString *)randomRegion
{
NBMetadataHelper *aHelper = [[NBMetadataHelper alloc] init];
NSDictionary *metadata = [[aHelper getAllMetadata] objectAtIndex:(arc4random() % [aHelper getAllMetadata].count)];
if (metadata) {
NSString *region = [metadata objectForKey:@"code"];
if (region) {
return region;
}
- (NSString *)randomRegion {
NBMetadataHelper *aHelper = [[NBMetadataHelper alloc] init];
NSDictionary *metadata =
[[aHelper getAllMetadata] objectAtIndex:(arc4random() % [aHelper getAllMetadata].count)];
if (metadata) {
NSString *region = [metadata objectForKey:@"code"];
if (region) {
return region;
}
}
return nil;
return nil;
}
- (void)testForMultithread {
NBPhoneNumberUtil *aUtil = [NBPhoneNumberUtil sharedInstance];
NSString *testRegion = [self randomRegion];
- (void)testForMultithread
{
NBPhoneNumberUtil *aUtil = [NBPhoneNumberUtil sharedInstance];
NSString *testRegion = [self randomRegion];
if (!testRegion) {
return;
}
if (!testRegion) {
return;
NSLog(@"- START [%@]", testRegion);
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
for (int i = 0; i < 10000; i++) {
NBPhoneNumber *phoneNumber =
[aUtil parse:[self stringWithRandomNumber] defaultRegion:testRegion error:&error];
if (error && ![error.domain isEqualToString:@"INVALID_COUNTRY_CODE"]) {
NSLog(@"error [%@]", [error localizedDescription]);
}
if (!error) {
error = nil;
[aUtil format:phoneNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&error];
}
error = nil;
}
NSLog(@"- START [%@]", testRegion);
NSLog(@"OK [%@]", testRegion);
});
}
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
for (int i=0; i<10000; i++) {
NBPhoneNumber *phoneNumber = [aUtil parse:[self stringWithRandomNumber] defaultRegion:testRegion error:&error];
if (error && ![error.domain isEqualToString:@"INVALID_COUNTRY_CODE"]) {
NSLog(@"error [%@]", [error localizedDescription]);
}
if (!error) {
error = nil;
[aUtil format:phoneNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&error];
}
error = nil;
}
NSLog(@"OK [%@]", testRegion);
- (void)testWithGCD {
for (int i = 0; i < 100; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self testForMultithread];
});
}
}
- (void)testForExtraDatas {
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
- (void)testWithGCD
{
for (int i = 0; i<100; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self testForMultithread];
});
}
NSArray *arrayData = [helper getAllMetadata];
if (arrayData && arrayData.count > 0) {
NSLog(@"Log sample metadata [%@]", [arrayData firstObject]);
} else {
NSLog(@"Fail to extract meta data");
}
}
- (void)testForExtraDatas
{
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
NSArray *arrayData = [helper getAllMetadata];
if (arrayData && arrayData.count > 0) {
NSLog(@"Log sample metadata [%@]", [arrayData firstObject]);
} else {
NSLog(@"Fail to extract meta data");
}
- (void)testCarrierRegion {
NSLog(@"testCarrierRegion %@", [self getPhoneNumberFormatted:@"1234567890"]);
}
- (void)testCarrierRegion
{
NSLog(@"testCarrierRegion %@", [self getPhoneNumberFormatted:@"1234567890"]);
- (NSString *)getPhoneNumberFormatted:(NSString *)phoneNumber {
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSString *retValue;
NBPhoneNumber *phoneNumberFormatted =
[phoneUtil parseWithPhoneCarrierRegion:phoneNumber error:nil];
retValue =
[phoneUtil format:phoneNumberFormatted numberFormat:NBEPhoneNumberFormatRFC3966 error:nil];
return retValue;
}
- (NSString *)getPhoneNumberFormatted:(NSString *)phoneNumber
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSString *retValue;
NBPhoneNumber *phoneNumberFormatted = [phoneUtil parseWithPhoneCarrierRegion:phoneNumber error:nil];
retValue = [phoneUtil format:phoneNumberFormatted numberFormat:NBEPhoneNumberFormatRFC3966 error:nil];
return retValue;
}
// FIXME: This unit test ALWAYS FAIL ... until google libPhoneNumber fix this issue
- (void)testAustriaNationalNumberParsing
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
- (void)testAustriaNationalNumberParsing {
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSError *anError = nil;
NSError *anError = nil;
NSString *internationalNumberForInput = @"436606545646";
NSString *nationalNumberForExpect = @"6606545646";
NSString *defaultRegion = @"AT";
NSString *internationalNumberForInput = @"436606545646";
NSString *nationalNumberForExpect = @"6606545646";
NSString *defaultRegion = @"AT";
NBPhoneNumber *phoneNumber = [phoneUtil parse:internationalNumberForInput defaultRegion:defaultRegion error:&anError];
NSString *nationalNumberForActual = [NSString stringWithFormat:@"%@", phoneNumber.nationalNumber];
// ALWAYS FAIL need fix "google libPhoneNumber"
NSLog(nationalNumberForExpect, nationalNumberForActual);
NBPhoneNumber *phoneNumber =
[phoneUtil parse:internationalNumberForInput defaultRegion:defaultRegion error:&anError];
NSString *nationalNumberForActual = [NSString stringWithFormat:@"%@", phoneNumber.nationalNumber];
// ALWAYS FAIL need fix "google libPhoneNumber"
NSLog(nationalNumberForExpect, nationalNumberForActual);
}
- (void)testForiOS7 {
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
- (void)testForiOS7
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSError *anError = nil;
NBPhoneNumber *myNumber = [phoneUtil parse:@"0174 2340XXX" defaultRegion:@"DE" error:&anError];
if (anError == nil) {
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");
NSLog(@"E164 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatE164 error:&anError]);
NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&anError]);
NSLog(@"NATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatNATIONAL error:&anError]);
NSLog(@"RFC3966 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatRFC3966 error:&anError]);
} else {
NSLog(@"Error : %@", [anError localizedDescription]);
}
NSError *anError = nil;
NBPhoneNumber *myNumber = [phoneUtil parse:@"0174 2340XXX" defaultRegion:@"DE" error:&anError];
if (anError == nil) {
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES" : @"NO");
NSLog(@"E164 : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatE164 error:&anError]);
NSLog(
@"INTERNATIONAL : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&anError]);
NSLog(@"NATIONAL : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatNATIONAL error:&anError]);
NSLog(@"RFC3966 : %@",
[phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatRFC3966 error:&anError]);
} else {
NSLog(@"Error : %@", [anError localizedDescription]);
}
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
// Sent when the application is about to move from active to inactive state. This can occur for
// certain types of temporary interruptions (such as an incoming phone call or SMS message) or
// when the user quits the application and it begins the transition to the background state. Use
// this method to pause ongoing tasks, disable timers, and invalidate graphics rendering
// callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// Use this method to release shared resources, save user data, invalidate timers, and store
// enough application state information to restore your application to its current state in case
// it is terminated later. If your application supports background execution, this method is
// called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
// Called as part of the transition from the background to the active state; here you can undo
// many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// Restart any tasks that were paused (or not yet started) while the application was inactive. If
// the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Called when the application is about to terminate. Save data if appropriate. See also
// applicationDidEnterBackground:.
}
@end

View File

@ -10,6 +10,4 @@
@interface ViewController : UIViewController
@end

View File

@ -15,15 +15,13 @@
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

View File

@ -9,8 +9,8 @@
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
int main(int argc, char* argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

View File

@ -14,20 +14,20 @@ FOUNDATION_EXPORT double libPhoneNumber_iOSVersionNumber;
//! Project version string for libPhoneNumber-iOS.
FOUNDATION_EXPORT const unsigned char libPhoneNumber_iOSVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <libPhoneNumber_iOS/PublicHeader.h>
// In this header, you should import all the public headers of your framework
// using statements like #import <libPhoneNumber_iOS/PublicHeader.h>
#import "NBPhoneNumberDefines.h"
// Features
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"
#import "NBPhoneNumberUtil.h"
// Metadata
#import "NBMetadataHelper.h"
// Model
#import "NBPhoneMetaData.h"
#import "NBNumberFormat.h"
#import "NBPhoneMetaData.h"
#import "NBPhoneNumber.h"
#import "NBPhoneNumberDesc.h"

View File

@ -14,20 +14,20 @@ FOUNDATION_EXPORT double libPhoneNumbermacOSVersionNumber;
//! Project version string for libPhoneNumbermacOS.
FOUNDATION_EXPORT const unsigned char libPhoneNumbermacOSVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <libPhoneNumbermacOS/PublicHeader.h>
// In this header, you should import all the public headers of your framework
// using statements like #import <libPhoneNumbermacOS/PublicHeader.h>
#import "NBPhoneNumberDefines.h"
// Features
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"
#import "NBPhoneNumberUtil.h"
// Metadata
#import "NBMetadataHelper.h"
// Model
#import "NBPhoneMetaData.h"
#import "NBNumberFormat.h"
#import "NBPhoneMetaData.h"
#import "NBPhoneNumber.h"
#import "NBPhoneNumberDesc.h"

View File

@ -14,20 +14,20 @@ FOUNDATION_EXPORT double libPhoneNumber_tvOSVersionNumber;
//! Project version string for libPhoneNumber-tvOS.
FOUNDATION_EXPORT const unsigned char libPhoneNumber_tvOSVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <libPhoneNumber_tvOS/PublicHeader.h>
// In this header, you should import all the public headers of your framework
// using statements like #import <libPhoneNumber_tvOS/PublicHeader.h>
#import "NBPhoneNumberDefines.h"
// Features
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"
#import "NBPhoneNumberUtil.h"
// Metadata
#import "NBMetadataCore.h"
// Model
#import "NBPhoneMetaData.h"
#import "NBNumberFormat.h"
#import "NBPhoneMetaData.h"
#import "NBPhoneNumber.h"
#import "NBPhoneNumberDesc.h"

View File

@ -14,20 +14,20 @@ FOUNDATION_EXPORT double libPhoneNumber_watchOSVersionNumber;
//! Project version string for libPhoneNumber-watchOS.
FOUNDATION_EXPORT const unsigned char libPhoneNumber_watchOSVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <libPhoneNumber_watchOS/PublicHeader.h>
// In this header, you should import all the public headers of your framework
// using statements like #import <libPhoneNumber_watchOS/PublicHeader.h>
#import "NBPhoneNumberDefines.h"
// Features
#import "NBPhoneNumberUtil.h"
#import "NBAsYouTypeFormatter.h"
#import "NBPhoneNumberUtil.h"
// Metadata
#import "NBMetadataCore.h"
// Model
#import "NBPhoneMetaData.h"
#import "NBNumberFormat.h"
#import "NBPhoneMetaData.h"
#import "NBPhoneNumber.h"
#import "NBPhoneNumberDesc.h"

8
libPhoneNumber/NBAsYouTypeFormatter.h Executable file → Normal file
View File

@ -7,10 +7,8 @@
#import <Foundation/Foundation.h>
@class NBAsYouTypeFormatter;
@interface NBAsYouTypeFormatter : NSObject
- (instancetype)initWithRegionCode:(NSString *)regionCode;
@ -19,8 +17,8 @@
- (NSString *)inputString:(NSString *)string;
- (NSString *)inputStringAndRememberPosition:(NSString *)string;
- (NSString *)inputDigit:(NSString*)nextChar;
- (NSString *)inputDigitAndRememberPosition:(NSString*)nextChar;
- (NSString *)inputDigit:(NSString *)nextChar;
- (NSString *)inputDigitAndRememberPosition:(NSString *)nextChar;
- (NSString *)removeLastDigit;
- (NSString *)removeLastDigitAndRememberPosition;
@ -29,6 +27,6 @@
- (void)clear;
@property (nonatomic, assign, readonly) BOOL isSuccessfulFormatting;
@property(nonatomic, assign, readonly) BOOL isSuccessfulFormatting;
@end

1843
libPhoneNumber/NBAsYouTypeFormatter.m Executable file → Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,6 @@
#import <Foundation/Foundation.h>
#import "NBPhoneNumberDefines.h"
@class NBPhoneMetaData;
@interface NBMetadataHelper : NSObject

View File

@ -7,9 +7,8 @@
//
#import "NBMetadataHelper.h"
#import "NBPhoneMetaData.h"
#import "NBGeneratedPhoneNumberMetaData.h"
#import "NBPhoneMetaData.h"
@interface NBMetadataHelper ()
@ -23,14 +22,14 @@ static NSString *StringByTrimming(NSString *aString) {
static dispatch_once_t onceToken;
static NSCharacterSet *whitespaceCharSet = nil;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *spaceCharSet = [NSMutableCharacterSet characterSetWithCharactersInString:NB_NON_BREAKING_SPACE];
NSMutableCharacterSet *spaceCharSet =
[NSMutableCharacterSet characterSetWithCharactersInString:NB_NON_BREAKING_SPACE];
[spaceCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
whitespaceCharSet = spaceCharSet;
});
return [aString stringByTrimmingCharactersInSet:whitespaceCharSet];
}
@implementation NBMetadataHelper
/*
@ -40,108 +39,106 @@ static NSString *StringByTrimming(NSString *aString) {
Ref. site (countrycode.org)
*/
+ (NSDictionary *)phoneNumberDataMap {
static NSDictionary *phoneNumberDataDictionary;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Data is a gzipped JSON file that is embedded in the binary.
// See GeneratePhoneNumberHeader.sh and PhoneNumberMetaData.h for details.
NSMutableData* gunzippedData = [NSMutableData dataWithLength:kPhoneNumberMetaDataExpandedLength];
static NSDictionary *phoneNumberDataDictionary;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Data is a gzipped JSON file that is embedded in the binary.
// See GeneratePhoneNumberHeader.sh and PhoneNumberMetaData.h for details.
NSMutableData *gunzippedData =
[NSMutableData dataWithLength:kPhoneNumberMetaDataExpandedLength];
z_stream zStream;
memset(&zStream, 0, sizeof(zStream));
__attribute((unused)) int err = inflateInit2(&zStream, 16);
NSAssert(err == Z_OK, @"Unable to init stream. err = %d", err);
z_stream zStream;
memset(&zStream, 0, sizeof(zStream));
__attribute((unused)) int err = inflateInit2(&zStream, 16);
NSAssert(err == Z_OK, @"Unable to init stream. err = %d", err);
zStream.next_in = kPhoneNumberMetaData;
zStream.avail_in = (uint)kPhoneNumberMetaDataCompressedLength;
zStream.next_out = (Bytef *)gunzippedData.bytes;
zStream.avail_out = (uint)gunzippedData.length;
zStream.next_in = kPhoneNumberMetaData;
zStream.avail_in = (uint)kPhoneNumberMetaDataCompressedLength;
zStream.next_out = (Bytef *)gunzippedData.bytes;
zStream.avail_out = (uint)gunzippedData.length;
err = inflate(&zStream, Z_FINISH);
NSAssert(err == Z_STREAM_END, @"Unable to inflate compressed data. err = %d", err);
err = inflate(&zStream, Z_FINISH);
NSAssert(err == Z_STREAM_END, @"Unable to inflate compressed data. err = %d", err);
err = inflateEnd(&zStream);
NSAssert(err == Z_OK, @"Unable to inflate compressed data. err = %d", err);
err = inflateEnd(&zStream);
NSAssert(err == Z_OK, @"Unable to inflate compressed data. err = %d", err);
NSError *error = nil;
phoneNumberDataDictionary = [NSJSONSerialization JSONObjectWithData:gunzippedData
options:0
error:&error];
NSAssert(error == nil, @"Unable to convert JSON - %@", error);
});
return phoneNumberDataDictionary;
NSError *error = nil;
phoneNumberDataDictionary =
[NSJSONSerialization JSONObjectWithData:gunzippedData options:0 error:&error];
NSAssert(error == nil, @"Unable to convert JSON - %@", error);
});
return phoneNumberDataDictionary;
}
+ (NSDictionary *)CCode2CNMap {
static NSMutableDictionary *mapCCode2CN;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *countryCodeToRegionCodeMap = [self CN2CCodeMap];
mapCCode2CN = [[NSMutableDictionary alloc] init];
for (NSString *countryCode in countryCodeToRegionCodeMap) {
NSArray *regionCodes = countryCodeToRegionCodeMap[countryCode];
for (NSString *regionCode in regionCodes) {
mapCCode2CN[regionCode] = countryCode;
}
}
});
return mapCCode2CN;
static NSMutableDictionary *mapCCode2CN;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *countryCodeToRegionCodeMap = [self CN2CCodeMap];
mapCCode2CN = [[NSMutableDictionary alloc] init];
for (NSString *countryCode in countryCodeToRegionCodeMap) {
NSArray *regionCodes = countryCodeToRegionCodeMap[countryCode];
for (NSString *regionCode in regionCodes) {
mapCCode2CN[regionCode] = countryCode;
}
}
});
return mapCCode2CN;
}
+(NSDictionary *)CN2CCodeMap {
return [self phoneNumberDataMap][@"countryCodeToRegionCodeMap"];
+ (NSDictionary *)CN2CCodeMap {
return [self phoneNumberDataMap][@"countryCodeToRegionCodeMap"];
}
- (NSArray*)getAllMetadata
{
NSArray *countryCodes = [NSLocale ISOCountryCodes];
NSMutableArray *resultMetadata = [[NSMutableArray alloc] initWithCapacity:countryCodes.count];
- (NSArray *)getAllMetadata {
NSArray *countryCodes = [NSLocale ISOCountryCodes];
NSMutableArray *resultMetadata = [[NSMutableArray alloc] initWithCapacity:countryCodes.count];
for (NSString *countryCode in countryCodes) {
id countryDictionaryInstance = [NSDictionary dictionaryWithObject:countryCode forKey:NSLocaleCountryCode];
NSString *identifier = [NSLocale localeIdentifierFromComponents:countryDictionaryInstance];
NSString *country = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
for (NSString *countryCode in countryCodes) {
id countryDictionaryInstance =
[NSDictionary dictionaryWithObject:countryCode forKey:NSLocaleCountryCode];
NSString *identifier = [NSLocale localeIdentifierFromComponents:countryDictionaryInstance];
NSString *country =
[[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
NSMutableDictionary *countryMeta = [[NSMutableDictionary alloc] init];
if (country) {
[countryMeta setObject:country forKey:@"name"];
} else {
NSString *systemCountry = [[NSLocale systemLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
if (systemCountry) {
[countryMeta setObject:systemCountry forKey:@"name"];
}
}
if (countryCode) {
[countryMeta setObject:countryCode forKey:@"code"];
}
NBPhoneMetaData *metaData = [self getMetadataForRegion:countryCode];
if (metaData) {
[countryMeta setObject:metaData forKey:@"metadata"];
}
[resultMetadata addObject:countryMeta];
NSMutableDictionary *countryMeta = [[NSMutableDictionary alloc] init];
if (country) {
[countryMeta setObject:country forKey:@"name"];
} else {
NSString *systemCountry =
[[NSLocale systemLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
if (systemCountry) {
[countryMeta setObject:systemCountry forKey:@"name"];
}
}
return resultMetadata;
}
+ (NSArray *)regionCodeFromCountryCode:(NSNumber *)countryCodeNumber
{
NSArray *res = [self CN2CCodeMap][[countryCodeNumber stringValue]];
if ([res isKindOfClass:[NSArray class]] && [res count] > 0) {
return res;
if (countryCode) {
[countryMeta setObject:countryCode forKey:@"code"];
}
return nil;
NBPhoneMetaData *metaData = [self getMetadataForRegion:countryCode];
if (metaData) {
[countryMeta setObject:metaData forKey:@"metadata"];
}
[resultMetadata addObject:countryMeta];
}
return resultMetadata;
}
+ (NSArray *)regionCodeFromCountryCode:(NSNumber *)countryCodeNumber {
NSArray *res = [self CN2CCodeMap][[countryCodeNumber stringValue]];
if ([res isKindOfClass:[NSArray class]] && [res count] > 0) {
return res;
}
+ (NSString *)countryCodeFromRegionCode:(NSString* )regionCode
{
return [self CCode2CNMap][regionCode];
return nil;
}
+ (NSString *)countryCodeFromRegionCode:(NSString *)regionCode {
return [self CCode2CNMap][regionCode];
}
/**
@ -151,44 +148,40 @@ static NSString *StringByTrimming(NSString *aString) {
* @param {?string} regionCode
* @return {i18n.phonenumbers.PhoneMetadata}
*/
- (NBPhoneMetaData *)getMetadataForRegion:(NSString *)regionCode
{
regionCode = StringByTrimming(regionCode);
if (regionCode.length == 0) {
return nil;
}
regionCode = [regionCode uppercaseString];
if ([_cachedMetaDataKey isEqualToString:regionCode]) {
return _cachedMetaData;
}
NSDictionary *dict = [[self class] phoneNumberDataMap][@"countryToMetadata"];
NSArray *entry = dict[regionCode];
if (entry) {
NBPhoneMetaData *metadata = [[NBPhoneMetaData alloc] initWithEntry:entry];
_cachedMetaData = metadata;
_cachedMetaDataKey = regionCode;
return metadata;
}
- (NBPhoneMetaData *)getMetadataForRegion:(NSString *)regionCode {
regionCode = StringByTrimming(regionCode);
if (regionCode.length == 0) {
return nil;
}
}
regionCode = [regionCode uppercaseString];
if ([_cachedMetaDataKey isEqualToString:regionCode]) {
return _cachedMetaData;
}
NSDictionary *dict = [[self class] phoneNumberDataMap][@"countryToMetadata"];
NSArray *entry = dict[regionCode];
if (entry) {
NBPhoneMetaData *metadata = [[NBPhoneMetaData alloc] initWithEntry:entry];
_cachedMetaData = metadata;
_cachedMetaDataKey = regionCode;
return metadata;
}
return nil;
}
/**
* @param countryCallingCode countryCallingCode
* @return {i18n.phonenumbers.PhoneMetadata}
*/
- (NBPhoneMetaData *)getMetadataForNonGeographicalRegion:(NSNumber *)countryCallingCode
{
NSString *countryCallingCodeStr = countryCallingCode.stringValue;
return [self getMetadataForRegion:countryCallingCodeStr];
- (NBPhoneMetaData *)getMetadataForNonGeographicalRegion:(NSNumber *)countryCallingCode {
NSString *countryCallingCodeStr = countryCallingCode.stringValue;
return [self getMetadataForRegion:countryCallingCodeStr];
}
+ (BOOL)hasValue:(NSString*)string
{
+ (BOOL)hasValue:(NSString *)string {
string = StringByTrimming(string);
return string.length != 0;
}

20
libPhoneNumber/NBNumberFormat.h Executable file → Normal file
View File

@ -6,18 +6,22 @@
#import <Foundation/Foundation.h>
@interface NBNumberFormat : NSObject
// from phonemetadata.pb.js
/* 1 */ @property (nonatomic, strong) NSString *pattern;
/* 2 */ @property (nonatomic, strong) NSString *format;
/* 3 */ @property (nonatomic, strong) NSArray *leadingDigitsPatterns;
/* 4 */ @property (nonatomic, strong) NSString *nationalPrefixFormattingRule;
/* 6 */ @property (nonatomic, assign) BOOL nationalPrefixOptionalWhenFormatting;
/* 5 */ @property (nonatomic, strong) NSString *domesticCarrierCodeFormattingRule;
/* 1 */ @property(nonatomic, strong) NSString *pattern;
/* 2 */ @property(nonatomic, strong) NSString *format;
/* 3 */ @property(nonatomic, strong) NSArray *leadingDigitsPatterns;
/* 4 */ @property(nonatomic, strong) NSString *nationalPrefixFormattingRule;
/* 6 */ @property(nonatomic, assign) BOOL nationalPrefixOptionalWhenFormatting;
/* 5 */ @property(nonatomic, strong) NSString *domesticCarrierCodeFormattingRule;
- (instancetype)initWithPattern:(NSString *)pattern withFormat:(NSString *)format withLeadingDigitsPatterns:(NSArray *)leadingDigitsPatterns withNationalPrefixFormattingRule:(NSString *)nationalPrefixFormattingRule whenFormatting:(BOOL)nationalPrefixOptionalWhenFormatting withDomesticCarrierCodeFormattingRule:(NSString *)domesticCarrierCodeFormattingRule;
- (instancetype)initWithPattern:(NSString *)pattern
withFormat:(NSString *)format
withLeadingDigitsPatterns:(NSArray *)leadingDigitsPatterns
withNationalPrefixFormattingRule:(NSString *)nationalPrefixFormattingRule
whenFormatting:(BOOL)nationalPrefixOptionalWhenFormatting
withDomesticCarrierCodeFormattingRule:(NSString *)domesticCarrierCodeFormattingRule;
- (instancetype)initWithEntry:(NSArray *)entry;
@end

124
libPhoneNumber/NBNumberFormat.m Executable file → Normal file
View File

@ -9,82 +9,86 @@
@implementation NBNumberFormat
- (instancetype)initWithPattern:(NSString *)pattern withFormat:(NSString *)format withLeadingDigitsPatterns:(NSArray *)leadingDigitsPatterns withNationalPrefixFormattingRule:(NSString *)nationalPrefixFormattingRule whenFormatting:(BOOL)nationalPrefixOptionalWhenFormatting withDomesticCarrierCodeFormattingRule:(NSString *)domesticCarrierCodeFormattingRule
{
self = [super init];
if (self) {
_pattern = pattern;
_format = format;
_leadingDigitsPatterns = leadingDigitsPatterns;
_nationalPrefixFormattingRule = nationalPrefixFormattingRule;
_nationalPrefixOptionalWhenFormatting = nationalPrefixOptionalWhenFormatting;
_domesticCarrierCodeFormattingRule = domesticCarrierCodeFormattingRule;
}
return self;
- (instancetype)initWithPattern:(NSString *)pattern
withFormat:(NSString *)format
withLeadingDigitsPatterns:(NSArray *)leadingDigitsPatterns
withNationalPrefixFormattingRule:(NSString *)nationalPrefixFormattingRule
whenFormatting:(BOOL)nationalPrefixOptionalWhenFormatting
withDomesticCarrierCodeFormattingRule:(NSString *)domesticCarrierCodeFormattingRule {
self = [super init];
if (self) {
_pattern = pattern;
_format = format;
_leadingDigitsPatterns = leadingDigitsPatterns;
_nationalPrefixFormattingRule = nationalPrefixFormattingRule;
_nationalPrefixOptionalWhenFormatting = nationalPrefixOptionalWhenFormatting;
_domesticCarrierCodeFormattingRule = domesticCarrierCodeFormattingRule;
}
return self;
}
- (instancetype)initWithEntry:(NSArray *)entry
{
self = [super init];
if (self && entry != nil) {
_pattern = [entry nb_safeStringAtIndex:1];
_format = [entry nb_safeStringAtIndex:2];
_leadingDigitsPatterns = [entry nb_safeArrayAtIndex:3];
_nationalPrefixFormattingRule = [entry nb_safeStringAtIndex:4];
_nationalPrefixOptionalWhenFormatting = [[entry nb_safeNumberAtIndex:6] boolValue];
_domesticCarrierCodeFormattingRule = [entry nb_safeStringAtIndex:5];
}
return self;
- (instancetype)initWithEntry:(NSArray *)entry {
self = [super init];
if (self && entry != nil) {
_pattern = [entry nb_safeStringAtIndex:1];
_format = [entry nb_safeStringAtIndex:2];
_leadingDigitsPatterns = [entry nb_safeArrayAtIndex:3];
_nationalPrefixFormattingRule = [entry nb_safeStringAtIndex:4];
_nationalPrefixOptionalWhenFormatting = [[entry nb_safeNumberAtIndex:6] boolValue];
_domesticCarrierCodeFormattingRule = [entry nb_safeStringAtIndex:5];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"[pattern:%@, format:%@, leadingDigitsPattern:%@, nationalPrefixFormattingRule:%@, nationalPrefixOptionalWhenFormatting:%@, domesticCarrierCodeFormattingRule:%@]",
self.pattern, self.format, self.leadingDigitsPatterns, self.nationalPrefixFormattingRule, self.nationalPrefixOptionalWhenFormatting?@"Y":@"N", self.domesticCarrierCodeFormattingRule];
- (NSString *)description {
return [NSString
stringWithFormat:
@"[pattern:%@, format:%@, leadingDigitsPattern:%@, nationalPrefixFormattingRule:%@, "
@"nationalPrefixOptionalWhenFormatting:%@, domesticCarrierCodeFormattingRule:%@]",
self.pattern, self.format, self.leadingDigitsPatterns, self.nationalPrefixFormattingRule,
self.nationalPrefixOptionalWhenFormatting ? @"Y" : @"N",
self.domesticCarrierCodeFormattingRule];
}
- (id)copyWithZone:(NSZone *)zone
{
return [[NBNumberFormat alloc] initWithPattern:self.pattern
withFormat:self.format
withLeadingDigitsPatterns:self.leadingDigitsPatterns
withNationalPrefixFormattingRule:self.nationalPrefixFormattingRule
whenFormatting:self.nationalPrefixOptionalWhenFormatting
withDomesticCarrierCodeFormattingRule:self.domesticCarrierCodeFormattingRule];
- (id)copyWithZone:(NSZone *)zone {
return [[NBNumberFormat alloc] initWithPattern:self.pattern
withFormat:self.format
withLeadingDigitsPatterns:self.leadingDigitsPatterns
withNationalPrefixFormattingRule:self.nationalPrefixFormattingRule
whenFormatting:self.nationalPrefixOptionalWhenFormatting
withDomesticCarrierCodeFormattingRule:self.domesticCarrierCodeFormattingRule];
}
#ifdef NB_USE_EXTENSIONS
// We believe these methods are unused.
// If you would like them back (not behind a flag) please file a bug with a reason for needing
// them.
- (instancetype)initWithCoder:(NSCoder*)coder
{
if (self = [super init]) {
_pattern = [coder decodeObjectForKey:@"pattern"];
_format = [coder decodeObjectForKey:@"format"];
_leadingDigitsPatterns = [coder decodeObjectForKey:@"leadingDigitsPatterns"];
_nationalPrefixFormattingRule = [coder decodeObjectForKey:@"nationalPrefixFormattingRule"];
_nationalPrefixOptionalWhenFormatting = [[coder decodeObjectForKey:@"nationalPrefixOptionalWhenFormatting"] boolValue];
_domesticCarrierCodeFormattingRule = [coder decodeObjectForKey:@"domesticCarrierCodeFormattingRule"];
}
return self;
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
_pattern = [coder decodeObjectForKey:@"pattern"];
_format = [coder decodeObjectForKey:@"format"];
_leadingDigitsPatterns = [coder decodeObjectForKey:@"leadingDigitsPatterns"];
_nationalPrefixFormattingRule = [coder decodeObjectForKey:@"nationalPrefixFormattingRule"];
_nationalPrefixOptionalWhenFormatting =
[[coder decodeObjectForKey:@"nationalPrefixOptionalWhenFormatting"] boolValue];
_domesticCarrierCodeFormattingRule =
[coder decodeObjectForKey:@"domesticCarrierCodeFormattingRule"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:self.pattern forKey:@"pattern"];
[coder encodeObject:self.format forKey:@"format"];
[coder encodeObject:self.leadingDigitsPatterns forKey:@"leadingDigitsPatterns"];
[coder encodeObject:self.nationalPrefixFormattingRule forKey:@"nationalPrefixFormattingRule"];
[coder encodeObject:[NSNumber numberWithBool:self.nationalPrefixOptionalWhenFormatting] forKey:@"nationalPrefixOptionalWhenFormatting"];
[coder encodeObject:self.domesticCarrierCodeFormattingRule forKey:@"domesticCarrierCodeFormattingRule"];
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.pattern forKey:@"pattern"];
[coder encodeObject:self.format forKey:@"format"];
[coder encodeObject:self.leadingDigitsPatterns forKey:@"leadingDigitsPatterns"];
[coder encodeObject:self.nationalPrefixFormattingRule forKey:@"nationalPrefixFormattingRule"];
[coder encodeObject:[NSNumber numberWithBool:self.nationalPrefixOptionalWhenFormatting]
forKey:@"nationalPrefixOptionalWhenFormatting"];
[coder encodeObject:self.domesticCarrierCodeFormattingRule
forKey:@"domesticCarrierCodeFormattingRule"];
}
#endif // NB_USE_EXTENSIONS
#endif // NB_USE_EXTENSIONS
@end

55
libPhoneNumber/NBPhoneMetaData.h Executable file → Normal file
View File

@ -6,39 +6,38 @@
#import <Foundation/Foundation.h>
@class NBPhoneNumberDesc, NBNumberFormat;
@interface NBPhoneMetaData : NSObject
// from phonemetadata.pb.js
/* 1 */ @property (nonatomic, strong) NBPhoneNumberDesc *generalDesc;
/* 2 */ @property (nonatomic, strong) NBPhoneNumberDesc *fixedLine;
/* 3 */ @property (nonatomic, strong) NBPhoneNumberDesc *mobile;
/* 4 */ @property (nonatomic, strong) NBPhoneNumberDesc *tollFree;
/* 5 */ @property (nonatomic, strong) NBPhoneNumberDesc *premiumRate;
/* 6 */ @property (nonatomic, strong) NBPhoneNumberDesc *sharedCost;
/* 7 */ @property (nonatomic, strong) NBPhoneNumberDesc *personalNumber;
/* 8 */ @property (nonatomic, strong) NBPhoneNumberDesc *voip;
/* 21 */ @property (nonatomic, strong) NBPhoneNumberDesc *pager;
/* 25 */ @property (nonatomic, strong) NBPhoneNumberDesc *uan;
/* 27 */ @property (nonatomic, strong) NBPhoneNumberDesc *emergency;
/* 28 */ @property (nonatomic, strong) NBPhoneNumberDesc *voicemail;
/* 24 */ @property (nonatomic, strong) NBPhoneNumberDesc *noInternationalDialling;
/* 9 */ @property (nonatomic, strong) NSString *codeID;
/* 10 */ @property (nonatomic, strong) NSNumber *countryCode;
/* 11 */ @property (nonatomic, strong) NSString *internationalPrefix;
/* 17 */ @property (nonatomic, strong) NSString *preferredInternationalPrefix;
/* 12 */ @property (nonatomic, strong) NSString *nationalPrefix;
/* 13 */ @property (nonatomic, strong) NSString *preferredExtnPrefix;
/* 15 */ @property (nonatomic, strong) NSString *nationalPrefixForParsing;
/* 16 */ @property (nonatomic, strong) NSString *nationalPrefixTransformRule;
/* 18 */ @property (nonatomic, assign) BOOL sameMobileAndFixedLinePattern;
/* 19 */ @property (nonatomic, strong) NSArray<NBNumberFormat *> *numberFormats;
/* 20 */ @property (nonatomic, strong) NSArray<NBNumberFormat *> *intlNumberFormats;
/* 22 */ @property (nonatomic, assign) BOOL mainCountryForCode;
/* 23 */ @property (nonatomic, strong) NSString *leadingDigits;
/* 26 */ @property (nonatomic, assign) BOOL leadingZeroPossible;
/* 1 */ @property(nonatomic, strong) NBPhoneNumberDesc *generalDesc;
/* 2 */ @property(nonatomic, strong) NBPhoneNumberDesc *fixedLine;
/* 3 */ @property(nonatomic, strong) NBPhoneNumberDesc *mobile;
/* 4 */ @property(nonatomic, strong) NBPhoneNumberDesc *tollFree;
/* 5 */ @property(nonatomic, strong) NBPhoneNumberDesc *premiumRate;
/* 6 */ @property(nonatomic, strong) NBPhoneNumberDesc *sharedCost;
/* 7 */ @property(nonatomic, strong) NBPhoneNumberDesc *personalNumber;
/* 8 */ @property(nonatomic, strong) NBPhoneNumberDesc *voip;
/* 21 */ @property(nonatomic, strong) NBPhoneNumberDesc *pager;
/* 25 */ @property(nonatomic, strong) NBPhoneNumberDesc *uan;
/* 27 */ @property(nonatomic, strong) NBPhoneNumberDesc *emergency;
/* 28 */ @property(nonatomic, strong) NBPhoneNumberDesc *voicemail;
/* 24 */ @property(nonatomic, strong) NBPhoneNumberDesc *noInternationalDialling;
/* 9 */ @property(nonatomic, strong) NSString *codeID;
/* 10 */ @property(nonatomic, strong) NSNumber *countryCode;
/* 11 */ @property(nonatomic, strong) NSString *internationalPrefix;
/* 17 */ @property(nonatomic, strong) NSString *preferredInternationalPrefix;
/* 12 */ @property(nonatomic, strong) NSString *nationalPrefix;
/* 13 */ @property(nonatomic, strong) NSString *preferredExtnPrefix;
/* 15 */ @property(nonatomic, strong) NSString *nationalPrefixForParsing;
/* 16 */ @property(nonatomic, strong) NSString *nationalPrefixTransformRule;
/* 18 */ @property(nonatomic, assign) BOOL sameMobileAndFixedLinePattern;
/* 19 */ @property(nonatomic, strong) NSArray<NBNumberFormat *> *numberFormats;
/* 20 */ @property(nonatomic, strong) NSArray<NBNumberFormat *> *intlNumberFormats;
/* 22 */ @property(nonatomic, assign) BOOL mainCountryForCode;
/* 23 */ @property(nonatomic, strong) NSString *leadingDigits;
/* 26 */ @property(nonatomic, assign) BOOL leadingZeroPossible;
- (instancetype)initWithEntry:(NSArray *)entry;
@end

248
libPhoneNumber/NBPhoneMetaData.m Executable file → Normal file
View File

@ -5,79 +5,90 @@
//
#import "NBPhoneMetaData.h"
#import "NBPhoneNumberDesc.h"
#import "NBNumberFormat.h"
#import "NBPhoneNumberDesc.h"
#import "NSArray+NBAdditions.h"
@implementation NBPhoneMetaData
- (instancetype)init {
self = [super init];
- (instancetype)init
{
self = [super init];
if (self) {
_numberFormats = [[NSMutableArray alloc] init];
_intlNumberFormats = [[NSMutableArray alloc] init];
if (self) {
_numberFormats = [[NSMutableArray alloc] init];
_intlNumberFormats = [[NSMutableArray alloc] init];
_leadingZeroPossible = NO;
_mainCountryForCode = NO;
_sameMobileAndFixedLinePattern = NO;
_internationalPrefix = @"NA";
}
_leadingZeroPossible = NO;
_mainCountryForCode = NO;
_sameMobileAndFixedLinePattern = NO;
_internationalPrefix = @"NA";
}
return self;
return self;
}
- (instancetype)initWithEntry:(NSArray *)entry {
self = [super init];
if (self && entry != nil) {
_generalDesc = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:1]];
_fixedLine = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:2]];
_mobile = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:3]];
_tollFree = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:4]];
_premiumRate = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:5]];
_sharedCost = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:6]];
_personalNumber = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:7]];
_voip = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:8]];
_pager = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:21]];
_uan = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:25]];
_emergency = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:27]];
_voicemail = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:28]];
_noInternationalDialling = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:24]];
_codeID = [entry nb_safeStringAtIndex:9];
_countryCode = [entry nb_safeNumberAtIndex:10];
_internationalPrefix = [entry nb_safeStringAtIndex:11];
_preferredInternationalPrefix = [entry nb_safeStringAtIndex:17];
_nationalPrefix = [entry nb_safeStringAtIndex:12];
_preferredExtnPrefix = [entry nb_safeStringAtIndex:13];
_nationalPrefixForParsing = [entry nb_safeStringAtIndex:15];
_nationalPrefixTransformRule = [entry nb_safeStringAtIndex:16];
_sameMobileAndFixedLinePattern = [[entry nb_safeNumberAtIndex:18] boolValue];
_numberFormats= [self numberFormatsFromEntry:[entry nb_safeArrayAtIndex:19]];
_intlNumberFormats = [self numberFormatsFromEntry:[entry nb_safeArrayAtIndex:20]];
_mainCountryForCode = [[entry nb_safeNumberAtIndex:22] boolValue];
_leadingDigits = [entry nb_safeStringAtIndex:23];
_leadingZeroPossible = [[entry nb_safeNumberAtIndex:26] boolValue];
}
self = [super init];
if (self && entry != nil) {
_generalDesc = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:1]];
_fixedLine = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:2]];
_mobile = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:3]];
_tollFree = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:4]];
_premiumRate = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:5]];
_sharedCost = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:6]];
_personalNumber = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:7]];
_voip = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:8]];
_pager = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:21]];
_uan = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:25]];
_emergency = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:27]];
_voicemail = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:28]];
_noInternationalDialling =
[[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:24]];
_codeID = [entry nb_safeStringAtIndex:9];
_countryCode = [entry nb_safeNumberAtIndex:10];
_internationalPrefix = [entry nb_safeStringAtIndex:11];
_preferredInternationalPrefix = [entry nb_safeStringAtIndex:17];
_nationalPrefix = [entry nb_safeStringAtIndex:12];
_preferredExtnPrefix = [entry nb_safeStringAtIndex:13];
_nationalPrefixForParsing = [entry nb_safeStringAtIndex:15];
_nationalPrefixTransformRule = [entry nb_safeStringAtIndex:16];
_sameMobileAndFixedLinePattern = [[entry nb_safeNumberAtIndex:18] boolValue];
_numberFormats = [self numberFormatsFromEntry:[entry nb_safeArrayAtIndex:19]];
_intlNumberFormats = [self numberFormatsFromEntry:[entry nb_safeArrayAtIndex:20]];
_mainCountryForCode = [[entry nb_safeNumberAtIndex:22] boolValue];
_leadingDigits = [entry nb_safeStringAtIndex:23];
_leadingZeroPossible = [[entry nb_safeNumberAtIndex:26] boolValue];
}
return self;
return self;
}
- (NSArray<NBNumberFormat *> *)numberFormatsFromEntry:(NSArray *)entry {
NSMutableArray *formats = [NSMutableArray arrayWithCapacity:entry.count];
for (NSArray *format in entry) {
NBNumberFormat *numberFormat = [[NBNumberFormat alloc] initWithEntry:format];
[formats addObject:numberFormat];
}
return formats;
NSMutableArray *formats = [NSMutableArray arrayWithCapacity:entry.count];
for (NSArray *format in entry) {
NBNumberFormat *numberFormat = [[NBNumberFormat alloc] initWithEntry:format];
[formats addObject:numberFormat];
}
return formats;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"* codeID[%@] countryCode[%@] generalDesc[%@] fixedLine[%@] mobile[%@] tollFree[%@] premiumRate[%@] sharedCost[%@] personalNumber[%@] voip[%@] pager[%@] uan[%@] emergency[%@] voicemail[%@] noInternationalDialling[%@] internationalPrefix[%@] preferredInternationalPrefix[%@] nationalPrefix[%@] preferredExtnPrefix[%@] nationalPrefixForParsing[%@] nationalPrefixTransformRule[%@] sameMobileAndFixedLinePattern[%@] numberFormats[%@] intlNumberFormats[%@] mainCountryForCode[%@] leadingDigits[%@] leadingZeroPossible[%@]",
_codeID, _countryCode, _generalDesc, _fixedLine, _mobile, _tollFree, _premiumRate, _sharedCost, _personalNumber, _voip, _pager, _uan, _emergency, _voicemail, _noInternationalDialling, _internationalPrefix, _preferredInternationalPrefix, _nationalPrefix, _preferredExtnPrefix, _nationalPrefixForParsing, _nationalPrefixTransformRule, _sameMobileAndFixedLinePattern?@"Y":@"N", _numberFormats, _intlNumberFormats, _mainCountryForCode?@"Y":@"N", _leadingDigits, _leadingZeroPossible?@"Y":@"N"];
- (NSString *)description {
return [NSString
stringWithFormat:
@"* codeID[%@] countryCode[%@] generalDesc[%@] fixedLine[%@] mobile[%@] tollFree[%@] "
@"premiumRate[%@] sharedCost[%@] personalNumber[%@] voip[%@] pager[%@] uan[%@] "
@"emergency[%@] voicemail[%@] noInternationalDialling[%@] internationalPrefix[%@] "
@"preferredInternationalPrefix[%@] nationalPrefix[%@] preferredExtnPrefix[%@] "
@"nationalPrefixForParsing[%@] nationalPrefixTransformRule[%@] "
@"sameMobileAndFixedLinePattern[%@] numberFormats[%@] intlNumberFormats[%@] "
@"mainCountryForCode[%@] leadingDigits[%@] leadingZeroPossible[%@]",
_codeID, _countryCode, _generalDesc, _fixedLine, _mobile, _tollFree, _premiumRate,
_sharedCost, _personalNumber, _voip, _pager, _uan, _emergency, _voicemail,
_noInternationalDialling, _internationalPrefix, _preferredInternationalPrefix,
_nationalPrefix, _preferredExtnPrefix, _nationalPrefixForParsing,
_nationalPrefixTransformRule, _sameMobileAndFixedLinePattern ? @"Y" : @"N",
_numberFormats, _intlNumberFormats, _mainCountryForCode ? @"Y" : @"N", _leadingDigits,
_leadingZeroPossible ? @"Y" : @"N"];
}
#ifdef NB_USE_EXTENSIONS
@ -85,72 +96,71 @@
// If you would like them back (not behind a flag) please file a bug with a reason for needing
// them.
- (instancetype)initWithCoder:(NSCoder*)coder
{
if (self = [super init]) {
_generalDesc = [coder decodeObjectForKey:@"generalDesc"];
_fixedLine = [coder decodeObjectForKey:@"fixedLine"];
_mobile = [coder decodeObjectForKey:@"mobile"];
_tollFree = [coder decodeObjectForKey:@"tollFree"];
_premiumRate = [coder decodeObjectForKey:@"premiumRate"];
_sharedCost = [coder decodeObjectForKey:@"sharedCost"];
_personalNumber = [coder decodeObjectForKey:@"personalNumber"];
_voip = [coder decodeObjectForKey:@"voip"];
_pager = [coder decodeObjectForKey:@"pager"];
_uan = [coder decodeObjectForKey:@"uan"];
_emergency = [coder decodeObjectForKey:@"emergency"];
_voicemail = [coder decodeObjectForKey:@"voicemail"];
_noInternationalDialling = [coder decodeObjectForKey:@"noInternationalDialling"];
_codeID = [coder decodeObjectForKey:@"codeID"];
_countryCode = [coder decodeObjectForKey:@"countryCode"];
_internationalPrefix = [coder decodeObjectForKey:@"internationalPrefix"];
_preferredInternationalPrefix = [coder decodeObjectForKey:@"preferredInternationalPrefix"];
_nationalPrefix = [coder decodeObjectForKey:@"nationalPrefix"];
_preferredExtnPrefix = [coder decodeObjectForKey:@"preferredExtnPrefix"];
_nationalPrefixForParsing = [coder decodeObjectForKey:@"nationalPrefixForParsing"];
_nationalPrefixTransformRule = [coder decodeObjectForKey:@"nationalPrefixTransformRule"];
_sameMobileAndFixedLinePattern = [[coder decodeObjectForKey:@"sameMobileAndFixedLinePattern"] boolValue];
_numberFormats = [coder decodeObjectForKey:@"numberFormats"];
_intlNumberFormats = [coder decodeObjectForKey:@"intlNumberFormats"];
_mainCountryForCode = [[coder decodeObjectForKey:@"mainCountryForCode"] boolValue];
_leadingDigits = [coder decodeObjectForKey:@"leadingDigits"];
_leadingZeroPossible = [[coder decodeObjectForKey:@"leadingZeroPossible"] boolValue];
}
return self;
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
_generalDesc = [coder decodeObjectForKey:@"generalDesc"];
_fixedLine = [coder decodeObjectForKey:@"fixedLine"];
_mobile = [coder decodeObjectForKey:@"mobile"];
_tollFree = [coder decodeObjectForKey:@"tollFree"];
_premiumRate = [coder decodeObjectForKey:@"premiumRate"];
_sharedCost = [coder decodeObjectForKey:@"sharedCost"];
_personalNumber = [coder decodeObjectForKey:@"personalNumber"];
_voip = [coder decodeObjectForKey:@"voip"];
_pager = [coder decodeObjectForKey:@"pager"];
_uan = [coder decodeObjectForKey:@"uan"];
_emergency = [coder decodeObjectForKey:@"emergency"];
_voicemail = [coder decodeObjectForKey:@"voicemail"];
_noInternationalDialling = [coder decodeObjectForKey:@"noInternationalDialling"];
_codeID = [coder decodeObjectForKey:@"codeID"];
_countryCode = [coder decodeObjectForKey:@"countryCode"];
_internationalPrefix = [coder decodeObjectForKey:@"internationalPrefix"];
_preferredInternationalPrefix = [coder decodeObjectForKey:@"preferredInternationalPrefix"];
_nationalPrefix = [coder decodeObjectForKey:@"nationalPrefix"];
_preferredExtnPrefix = [coder decodeObjectForKey:@"preferredExtnPrefix"];
_nationalPrefixForParsing = [coder decodeObjectForKey:@"nationalPrefixForParsing"];
_nationalPrefixTransformRule = [coder decodeObjectForKey:@"nationalPrefixTransformRule"];
_sameMobileAndFixedLinePattern =
[[coder decodeObjectForKey:@"sameMobileAndFixedLinePattern"] boolValue];
_numberFormats = [coder decodeObjectForKey:@"numberFormats"];
_intlNumberFormats = [coder decodeObjectForKey:@"intlNumberFormats"];
_mainCountryForCode = [[coder decodeObjectForKey:@"mainCountryForCode"] boolValue];
_leadingDigits = [coder decodeObjectForKey:@"leadingDigits"];
_leadingZeroPossible = [[coder decodeObjectForKey:@"leadingZeroPossible"] boolValue];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:_generalDesc forKey:@"generalDesc"];
[coder encodeObject:_fixedLine forKey:@"fixedLine"];
[coder encodeObject:_mobile forKey:@"mobile"];
[coder encodeObject:_tollFree forKey:@"tollFree"];
[coder encodeObject:_premiumRate forKey:@"premiumRate"];
[coder encodeObject:_sharedCost forKey:@"sharedCost"];
[coder encodeObject:_personalNumber forKey:@"personalNumber"];
[coder encodeObject:_voip forKey:@"voip"];
[coder encodeObject:_pager forKey:@"pager"];
[coder encodeObject:_uan forKey:@"uan"];
[coder encodeObject:_emergency forKey:@"emergency"];
[coder encodeObject:_voicemail forKey:@"voicemail"];
[coder encodeObject:_noInternationalDialling forKey:@"noInternationalDialling"];
[coder encodeObject:_codeID forKey:@"codeID"];
[coder encodeObject:_countryCode forKey:@"countryCode"];
[coder encodeObject:_internationalPrefix forKey:@"internationalPrefix"];
[coder encodeObject:_preferredInternationalPrefix forKey:@"preferredInternationalPrefix"];
[coder encodeObject:_nationalPrefix forKey:@"nationalPrefix"];
[coder encodeObject:_preferredExtnPrefix forKey:@"preferredExtnPrefix"];
[coder encodeObject:_nationalPrefixForParsing forKey:@"nationalPrefixForParsing"];
[coder encodeObject:_nationalPrefixTransformRule forKey:@"nationalPrefixTransformRule"];
[coder encodeObject:[NSNumber numberWithBool:_sameMobileAndFixedLinePattern] forKey:@"sameMobileAndFixedLinePattern"];
[coder encodeObject:_numberFormats forKey:@"numberFormats"];
[coder encodeObject:_intlNumberFormats forKey:@"intlNumberFormats"];
[coder encodeObject:[NSNumber numberWithBool:_mainCountryForCode] forKey:@"mainCountryForCode"];
[coder encodeObject:_leadingDigits forKey:@"leadingDigits"];
[coder encodeObject:[NSNumber numberWithBool:_leadingZeroPossible] forKey:@"leadingZeroPossible"];
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:_generalDesc forKey:@"generalDesc"];
[coder encodeObject:_fixedLine forKey:@"fixedLine"];
[coder encodeObject:_mobile forKey:@"mobile"];
[coder encodeObject:_tollFree forKey:@"tollFree"];
[coder encodeObject:_premiumRate forKey:@"premiumRate"];
[coder encodeObject:_sharedCost forKey:@"sharedCost"];
[coder encodeObject:_personalNumber forKey:@"personalNumber"];
[coder encodeObject:_voip forKey:@"voip"];
[coder encodeObject:_pager forKey:@"pager"];
[coder encodeObject:_uan forKey:@"uan"];
[coder encodeObject:_emergency forKey:@"emergency"];
[coder encodeObject:_voicemail forKey:@"voicemail"];
[coder encodeObject:_noInternationalDialling forKey:@"noInternationalDialling"];
[coder encodeObject:_codeID forKey:@"codeID"];
[coder encodeObject:_countryCode forKey:@"countryCode"];
[coder encodeObject:_internationalPrefix forKey:@"internationalPrefix"];
[coder encodeObject:_preferredInternationalPrefix forKey:@"preferredInternationalPrefix"];
[coder encodeObject:_nationalPrefix forKey:@"nationalPrefix"];
[coder encodeObject:_preferredExtnPrefix forKey:@"preferredExtnPrefix"];
[coder encodeObject:_nationalPrefixForParsing forKey:@"nationalPrefixForParsing"];
[coder encodeObject:_nationalPrefixTransformRule forKey:@"nationalPrefixTransformRule"];
[coder encodeObject:[NSNumber numberWithBool:_sameMobileAndFixedLinePattern]
forKey:@"sameMobileAndFixedLinePattern"];
[coder encodeObject:_numberFormats forKey:@"numberFormats"];
[coder encodeObject:_intlNumberFormats forKey:@"intlNumberFormats"];
[coder encodeObject:[NSNumber numberWithBool:_mainCountryForCode] forKey:@"mainCountryForCode"];
[coder encodeObject:_leadingDigits forKey:@"leadingDigits"];
[coder encodeObject:[NSNumber numberWithBool:_leadingZeroPossible] forKey:@"leadingZeroPossible"];
}
#endif // NB_USE_EXTENSIONS
#endif // NB_USE_EXTENSIONS
@end

21
libPhoneNumber/NBPhoneNumber.h Executable file → Normal file
View File

@ -1,24 +1,23 @@
//
// NBPhoneNumber.h
// libPhoneNumber
//
//
//
#import <Foundation/Foundation.h>
#import "NBPhoneNumberDefines.h"
@interface NBPhoneNumber : NSObject <NSCopying, NSCoding>
@interface NBPhoneNumber : NSObject<NSCopying, NSCoding>
// from phonemetadata.pb.js
/* 1 */ @property (nonatomic, strong, readwrite) NSNumber *countryCode;
/* 2 */ @property (nonatomic, strong, readwrite) NSNumber *nationalNumber;
/* 3 */ @property (nonatomic, strong, readwrite) NSString *extension;
/* 4 */ @property (nonatomic, assign, readwrite) BOOL italianLeadingZero;
/* 8 */ @property (nonatomic, strong, readwrite) NSNumber *numberOfLeadingZeros;
/* 5 */ @property (nonatomic, strong, readwrite) NSString *rawInput;
/* 6 */ @property (nonatomic, strong, readwrite) NSNumber *countryCodeSource;
/* 7 */ @property (nonatomic, strong, readwrite) NSString *preferredDomesticCarrierCode;
/* 1 */ @property(nonatomic, strong, readwrite) NSNumber *countryCode;
/* 2 */ @property(nonatomic, strong, readwrite) NSNumber *nationalNumber;
/* 3 */ @property(nonatomic, strong, readwrite) NSString *extension;
/* 4 */ @property(nonatomic, assign, readwrite) BOOL italianLeadingZero;
/* 8 */ @property(nonatomic, strong, readwrite) NSNumber *numberOfLeadingZeros;
/* 5 */ @property(nonatomic, strong, readwrite) NSString *rawInput;
/* 6 */ @property(nonatomic, strong, readwrite) NSNumber *countryCodeSource;
/* 7 */ @property(nonatomic, strong, readwrite) NSString *preferredDomesticCarrierCode;
- (void)clearCountryCodeSource;
- (NBECountryCodeSource)getCountryCodeSourceOrDefault;

162
libPhoneNumber/NBPhoneNumber.m Executable file → Normal file
View File

@ -7,112 +7,104 @@
#import "NBPhoneNumber.h"
#import "NBPhoneNumberDefines.h"
@implementation NBPhoneNumber
- (instancetype)init
{
self = [super init];
if (self) {
self.nationalNumber = @-1;
self.countryCode = @-1;
self.numberOfLeadingZeros = @(1);
}
return self;
- (instancetype)init {
self = [super init];
if (self) {
self.nationalNumber = @-1;
self.countryCode = @-1;
self.numberOfLeadingZeros = @(1);
}
return self;
}
- (void)clearCountryCodeSource
{
[self setCountryCodeSource:nil];
- (void)clearCountryCodeSource {
[self setCountryCodeSource:nil];
}
- (NBECountryCodeSource)getCountryCodeSourceOrDefault {
if (nil == self.countryCodeSource) {
return NBECountryCodeSourceFROM_NUMBER_WITH_PLUS_SIGN;
}
- (NBECountryCodeSource)getCountryCodeSourceOrDefault
{
if (nil == self.countryCodeSource) {
return NBECountryCodeSourceFROM_NUMBER_WITH_PLUS_SIGN;
}
return [self.countryCodeSource integerValue];
return [self.countryCodeSource integerValue];
}
- (NSUInteger)hash
{
// See https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
NSUInteger hash = self.countryCode.hash;
hash ^= self.nationalNumber.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= self.numberOfLeadingZeros.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= self.extension.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
- (NSUInteger)hash {
// See https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
NSUInteger hash = self.countryCode.hash;
hash ^= self.nationalNumber.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= self.numberOfLeadingZeros.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= self.extension.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[NBPhoneNumber class]]) {
return NO;
}
- (BOOL)isEqual:(id)object
{
if (![object isKindOfClass:[NBPhoneNumber class]]) {
return NO;
}
NBPhoneNumber *other = object;
return ([self.countryCode isEqualToNumber:other.countryCode]) && ([self.nationalNumber isEqualToNumber:other.nationalNumber]) &&
(self.italianLeadingZero == other.italianLeadingZero) && ([self.numberOfLeadingZeros isEqualToNumber:other.numberOfLeadingZeros]) &&
((self.extension == nil && other.extension == nil) || [self.extension isEqualToString:other.extension]);
NBPhoneNumber *other = object;
return ([self.countryCode isEqualToNumber:other.countryCode]) &&
([self.nationalNumber isEqualToNumber:other.nationalNumber]) &&
(self.italianLeadingZero == other.italianLeadingZero) &&
([self.numberOfLeadingZeros isEqualToNumber:other.numberOfLeadingZeros]) &&
((self.extension == nil && other.extension == nil) ||
[self.extension isEqualToString:other.extension]);
}
- (id)copyWithZone:(NSZone *)zone {
NBPhoneNumber *phoneNumberCopy = [[NBPhoneNumber allocWithZone:zone] init];
- (id)copyWithZone:(NSZone *)zone
{
NBPhoneNumber *phoneNumberCopy = [[NBPhoneNumber allocWithZone:zone] init];
phoneNumberCopy.countryCode = [self.countryCode copy];
phoneNumberCopy.nationalNumber = [self.nationalNumber copy];
phoneNumberCopy.extension = [self.extension copy];
phoneNumberCopy.italianLeadingZero = self.italianLeadingZero;
phoneNumberCopy.numberOfLeadingZeros = [self.numberOfLeadingZeros copy];
phoneNumberCopy.rawInput = [self.rawInput copy];
phoneNumberCopy.countryCodeSource = [self.countryCodeSource copy];
phoneNumberCopy.preferredDomesticCarrierCode = [self.preferredDomesticCarrierCode copy];
return phoneNumberCopy;
phoneNumberCopy.countryCode = [self.countryCode copy];
phoneNumberCopy.nationalNumber = [self.nationalNumber copy];
phoneNumberCopy.extension = [self.extension copy];
phoneNumberCopy.italianLeadingZero = self.italianLeadingZero;
phoneNumberCopy.numberOfLeadingZeros = [self.numberOfLeadingZeros copy];
phoneNumberCopy.rawInput = [self.rawInput copy];
phoneNumberCopy.countryCodeSource = [self.countryCodeSource copy];
phoneNumberCopy.preferredDomesticCarrierCode = [self.preferredDomesticCarrierCode copy];
return phoneNumberCopy;
}
- (instancetype)initWithCoder:(NSCoder*)coder
{
if (self = [super init]) {
self.countryCode = [coder decodeObjectForKey:@"countryCode"];
self.nationalNumber = [coder decodeObjectForKey:@"nationalNumber"];
self.extension = [coder decodeObjectForKey:@"extension"];
self.italianLeadingZero = [[coder decodeObjectForKey:@"italianLeadingZero"] boolValue];
self.numberOfLeadingZeros = [coder decodeObjectForKey:@"numberOfLeadingZeros"];
self.rawInput = [coder decodeObjectForKey:@"rawInput"];
self.countryCodeSource = [coder decodeObjectForKey:@"countryCodeSource"];
self.preferredDomesticCarrierCode = [coder decodeObjectForKey:@"preferredDomesticCarrierCode"];
}
return self;
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
self.countryCode = [coder decodeObjectForKey:@"countryCode"];
self.nationalNumber = [coder decodeObjectForKey:@"nationalNumber"];
self.extension = [coder decodeObjectForKey:@"extension"];
self.italianLeadingZero = [[coder decodeObjectForKey:@"italianLeadingZero"] boolValue];
self.numberOfLeadingZeros = [coder decodeObjectForKey:@"numberOfLeadingZeros"];
self.rawInput = [coder decodeObjectForKey:@"rawInput"];
self.countryCodeSource = [coder decodeObjectForKey:@"countryCodeSource"];
self.preferredDomesticCarrierCode = [coder decodeObjectForKey:@"preferredDomesticCarrierCode"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:self.countryCode forKey:@"countryCode"];
[coder encodeObject:self.nationalNumber forKey:@"nationalNumber"];
[coder encodeObject:self.extension forKey:@"extension"];
[coder encodeObject:[NSNumber numberWithBool:self.italianLeadingZero] forKey:@"italianLeadingZero"];
[coder encodeObject:self.numberOfLeadingZeros forKey:@"numberOfLeadingZeros"];
[coder encodeObject:self.rawInput forKey:@"rawInput"];
[coder encodeObject:self.countryCodeSource forKey:@"countryCodeSource"];
[coder encodeObject:self.preferredDomesticCarrierCode forKey:@"preferredDomesticCarrierCode"];
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.countryCode forKey:@"countryCode"];
[coder encodeObject:self.nationalNumber forKey:@"nationalNumber"];
[coder encodeObject:self.extension forKey:@"extension"];
[coder encodeObject:[NSNumber numberWithBool:self.italianLeadingZero]
forKey:@"italianLeadingZero"];
[coder encodeObject:self.numberOfLeadingZeros forKey:@"numberOfLeadingZeros"];
[coder encodeObject:self.rawInput forKey:@"rawInput"];
[coder encodeObject:self.countryCodeSource forKey:@"countryCodeSource"];
[coder encodeObject:self.preferredDomesticCarrierCode forKey:@"preferredDomesticCarrierCode"];
}
- (NSString *)description
{
return [NSString stringWithFormat:@" - countryCode[%@], nationalNumber[%@], extension[%@], italianLeadingZero[%@], numberOfLeadingZeros[%@], rawInput[%@] countryCodeSource[%@] preferredDomesticCarrierCode[%@]", self.countryCode, self.nationalNumber, self.extension, self.italianLeadingZero?@"Y":@"N", self.numberOfLeadingZeros, self.rawInput, self.countryCodeSource, self.preferredDomesticCarrierCode];
- (NSString *)description {
return [NSString stringWithFormat:
@" - countryCode[%@], nationalNumber[%@], extension[%@], "
@"italianLeadingZero[%@], numberOfLeadingZeros[%@], rawInput[%@] "
@"countryCodeSource[%@] preferredDomesticCarrierCode[%@]",
self.countryCode, self.nationalNumber, self.extension,
self.italianLeadingZero ? @"Y" : @"N", self.numberOfLeadingZeros,
self.rawInput, self.countryCodeSource, self.preferredDomesticCarrierCode];
}
@end

110
libPhoneNumber/NBPhoneNumberDefines.h Executable file → Normal file
View File

@ -15,76 +15,72 @@
#pragma mark - Enum -
typedef NS_ENUM(NSInteger, NBEPhoneNumberFormat) {
NBEPhoneNumberFormatE164 = 0,
NBEPhoneNumberFormatINTERNATIONAL = 1,
NBEPhoneNumberFormatNATIONAL = 2,
NBEPhoneNumberFormatRFC3966 = 3
NBEPhoneNumberFormatE164 = 0,
NBEPhoneNumberFormatINTERNATIONAL = 1,
NBEPhoneNumberFormatNATIONAL = 2,
NBEPhoneNumberFormatRFC3966 = 3
};
typedef NS_ENUM(NSInteger, NBEPhoneNumberType) {
NBEPhoneNumberTypeFIXED_LINE = 0,
NBEPhoneNumberTypeMOBILE = 1,
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself.
NBEPhoneNumberTypeFIXED_LINE_OR_MOBILE = 2,
// Freephone lines
NBEPhoneNumberTypeTOLL_FREE = 3,
NBEPhoneNumberTypePREMIUM_RATE = 4,
// The cost of this call is shared between the caller and the recipient, and
// is hence typically less than PREMIUM_RATE calls. See
// http://en.wikipedia.org/wiki/Shared_Cost_Service for more information.
NBEPhoneNumberTypeSHARED_COST = 5,
// Voice over IP numbers. This includes TSoIP (Telephony Service over IP).
NBEPhoneNumberTypeVOIP = 6,
// A personal number is associated with a particular person, and may be routed
// to either a MOBILE or FIXED_LINE number. Some more information can be found
// here = http://en.wikipedia.org/wiki/Personal_Numbers
NBEPhoneNumberTypePERSONAL_NUMBER = 7,
NBEPhoneNumberTypePAGER = 8,
// Used for 'Universal Access Numbers' or 'Company Numbers'. They may be
// further routed to specific offices, but allow one number to be used for a
// company.
NBEPhoneNumberTypeUAN = 9,
// Used for 'Voice Mail Access Numbers'.
NBEPhoneNumberTypeVOICEMAIL = 10,
// A phone number is of type UNKNOWN when it does not fit any of the known
// patterns for a specific region.
NBEPhoneNumberTypeUNKNOWN = -1
NBEPhoneNumberTypeFIXED_LINE = 0,
NBEPhoneNumberTypeMOBILE = 1,
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself.
NBEPhoneNumberTypeFIXED_LINE_OR_MOBILE = 2,
// Freephone lines
NBEPhoneNumberTypeTOLL_FREE = 3,
NBEPhoneNumberTypePREMIUM_RATE = 4,
// The cost of this call is shared between the caller and the recipient, and
// is hence typically less than PREMIUM_RATE calls. See
// http://en.wikipedia.org/wiki/Shared_Cost_Service for more information.
NBEPhoneNumberTypeSHARED_COST = 5,
// Voice over IP numbers. This includes TSoIP (Telephony Service over IP).
NBEPhoneNumberTypeVOIP = 6,
// A personal number is associated with a particular person, and may be routed
// to either a MOBILE or FIXED_LINE number. Some more information can be found
// here = http://en.wikipedia.org/wiki/Personal_Numbers
NBEPhoneNumberTypePERSONAL_NUMBER = 7,
NBEPhoneNumberTypePAGER = 8,
// Used for 'Universal Access Numbers' or 'Company Numbers'. They may be
// further routed to specific offices, but allow one number to be used for a
// company.
NBEPhoneNumberTypeUAN = 9,
// Used for 'Voice Mail Access Numbers'.
NBEPhoneNumberTypeVOICEMAIL = 10,
// A phone number is of type UNKNOWN when it does not fit any of the known
// patterns for a specific region.
NBEPhoneNumberTypeUNKNOWN = -1
};
typedef NS_ENUM(NSInteger, NBEMatchType) {
NBEMatchTypeNOT_A_NUMBER = 0,
NBEMatchTypeNO_MATCH = 1,
NBEMatchTypeSHORT_NSN_MATCH = 2,
NBEMatchTypeNSN_MATCH = 3,
NBEMatchTypeEXACT_MATCH = 4
NBEMatchTypeNOT_A_NUMBER = 0,
NBEMatchTypeNO_MATCH = 1,
NBEMatchTypeSHORT_NSN_MATCH = 2,
NBEMatchTypeNSN_MATCH = 3,
NBEMatchTypeEXACT_MATCH = 4
};
typedef NS_ENUM(NSInteger, NBEValidationResult) {
NBEValidationResultINVALID_LENGTH = -1,
NBEValidationResultUNKNOWN = 0,
NBEValidationResultIS_POSSIBLE = 1,
NBEValidationResultINVALID_COUNTRY_CODE = 2,
NBEValidationResultTOO_SHORT = 3,
NBEValidationResultTOO_LONG = 4,
NBEValidationResultIS_POSSIBLE_LOCAL_ONLY = 5
NBEValidationResultINVALID_LENGTH = -1,
NBEValidationResultUNKNOWN = 0,
NBEValidationResultIS_POSSIBLE = 1,
NBEValidationResultINVALID_COUNTRY_CODE = 2,
NBEValidationResultTOO_SHORT = 3,
NBEValidationResultTOO_LONG = 4,
NBEValidationResultIS_POSSIBLE_LOCAL_ONLY = 5
};
typedef NS_ENUM(NSInteger, NBECountryCodeSource) {
NBECountryCodeSourceFROM_NUMBER_WITH_PLUS_SIGN = 1,
NBECountryCodeSourceFROM_NUMBER_WITH_IDD = 5,
NBECountryCodeSourceFROM_NUMBER_WITHOUT_PLUS_SIGN = 10,
NBECountryCodeSourceFROM_DEFAULT_COUNTRY = 20
NBECountryCodeSourceFROM_NUMBER_WITH_PLUS_SIGN = 1,
NBECountryCodeSourceFROM_NUMBER_WITH_IDD = 5,
NBECountryCodeSourceFROM_NUMBER_WITHOUT_PLUS_SIGN = 10,
NBECountryCodeSourceFROM_DEFAULT_COUNTRY = 20
};
extern NSString * const NB_UNKNOWN_REGION;
extern NSString * const NB_NON_BREAKING_SPACE;
extern NSString * const NB_PLUS_CHARS;
extern NSString * const NB_VALID_DIGITS_STRING;
extern NSString * const NB_REGION_CODE_FOR_NON_GEO_ENTITY;
extern NSString* const NB_UNKNOWN_REGION;
extern NSString* const NB_NON_BREAKING_SPACE;
extern NSString* const NB_PLUS_CHARS;
extern NSString* const NB_VALID_DIGITS_STRING;
extern NSString* const NB_REGION_CODE_FOR_NON_GEO_ENTITY;
#endif

View File

@ -1,7 +1,7 @@
#import "NBPhoneNumberDefines.h"
NSString * const NB_UNKNOWN_REGION = @"ZZ";
NSString * const NB_NON_BREAKING_SPACE = @"\u00a0";
NSString * const NB_PLUS_CHARS = @"+";
NSString * const NB_VALID_DIGITS_STRING = @"0-9-٠-٩۰-۹";
NSString * const NB_REGION_CODE_FOR_NON_GEO_ENTITY = @"001";
NSString* const NB_UNKNOWN_REGION = @"ZZ";
NSString* const NB_NON_BREAKING_SPACE = @"\u00a0";
NSString* const NB_PLUS_CHARS = @"+";
NSString* const NB_VALID_DIGITS_STRING = @"0-9-٠-٩۰-۹";
NSString* const NB_REGION_CODE_FOR_NON_GEO_ENTITY = @"001";

15
libPhoneNumber/NBPhoneNumberDesc.h Executable file → Normal file
View File

@ -6,17 +6,16 @@
#import <Foundation/Foundation.h>
@interface NBPhoneNumberDesc : NSObject
// from phonemetadata.pb.js
/* 2 */ @property (nonatomic, strong, readonly) NSString *nationalNumberPattern;
/* 3 */ @property (nonatomic, strong, readonly) NSString *possibleNumberPattern;
/* 9 */ @property (nonatomic, strong, readonly) NSArray<NSNumber *> *possibleLength;
/* 10 */ @property (nonatomic, strong, readonly) NSArray<NSNumber *> *possibleLengthLocalOnly;
/* 6 */ @property (nonatomic, strong, readonly) NSString *exampleNumber;
/* 7 */ @property (nonatomic, strong, readonly) NSData *nationalNumberMatcherData;
/* 8 */ @property (nonatomic, strong, readonly) NSData *possibleNumberMatcherData;
/* 2 */ @property(nonatomic, strong, readonly) NSString *nationalNumberPattern;
/* 3 */ @property(nonatomic, strong, readonly) NSString *possibleNumberPattern;
/* 9 */ @property(nonatomic, strong, readonly) NSArray<NSNumber *> *possibleLength;
/* 10 */ @property(nonatomic, strong, readonly) NSArray<NSNumber *> *possibleLengthLocalOnly;
/* 6 */ @property(nonatomic, strong, readonly) NSString *exampleNumber;
/* 7 */ @property(nonatomic, strong, readonly) NSData *nationalNumberMatcherData;
/* 8 */ @property(nonatomic, strong, readonly) NSData *possibleNumberMatcherData;
- (instancetype)initWithEntry:(NSArray *)entry;
@end

121
libPhoneNumber/NBPhoneNumberDesc.m Executable file → Normal file
View File

@ -9,28 +9,26 @@
@implementation NBPhoneNumberDesc
- (instancetype)initWithEntry:(NSArray *)entry
{
self = [super init];
if (self && entry != nil) {
_nationalNumberPattern = [entry nb_safeStringAtIndex:2];
_possibleNumberPattern = [entry nb_safeStringAtIndex:3];
_possibleLength = [entry nb_safeArrayAtIndex:9];
_possibleLengthLocalOnly = [entry nb_safeArrayAtIndex:10];
_exampleNumber = [entry nb_safeStringAtIndex:6];
_nationalNumberMatcherData = [entry nb_safeDataAtIndex:7];
_possibleNumberMatcherData = [entry nb_safeDataAtIndex:8];
}
return self;
- (instancetype)initWithEntry:(NSArray *)entry {
self = [super init];
if (self && entry != nil) {
_nationalNumberPattern = [entry nb_safeStringAtIndex:2];
_possibleNumberPattern = [entry nb_safeStringAtIndex:3];
_possibleLength = [entry nb_safeArrayAtIndex:9];
_possibleLengthLocalOnly = [entry nb_safeArrayAtIndex:10];
_exampleNumber = [entry nb_safeStringAtIndex:6];
_nationalNumberMatcherData = [entry nb_safeDataAtIndex:7];
_possibleNumberMatcherData = [entry nb_safeDataAtIndex:8];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"nationalNumberPattern[%@] possibleNumberPattern[%@] possibleLength[%@] possibleLengthLocalOnly[%@] exampleNumber[%@]",
self.nationalNumberPattern, self.possibleNumberPattern, self.possibleLength, self.possibleLengthLocalOnly, self.exampleNumber];
- (NSString *)description {
return [NSString stringWithFormat:
@"nationalNumberPattern[%@] possibleNumberPattern[%@] possibleLength[%@] "
@"possibleLengthLocalOnly[%@] exampleNumber[%@]",
self.nationalNumberPattern, self.possibleNumberPattern, self.possibleLength,
self.possibleLengthLocalOnly, self.exampleNumber];
}
#ifdef NB_USE_EXTENSIONS
@ -38,55 +36,48 @@
// If you would like them back (not behind a flag) please file a bug with a reason for needing
// them.
- (instancetype)initWithCoder:(NSCoder*)coder
{
if (self = [super init]) {
_nationalNumberPattern = [coder decodeObjectForKey:@"nationalNumberPattern"];
_possibleNumberPattern = [coder decodeObjectForKey:@"possibleNumberPattern"];
_possibleLength = [coder decodeObjectForKey:@"possibleLength"];
_possibleLengthLocalOnly = [coder decodeObjectForKey:@"possibleLengthLocalOnly"];
_exampleNumber = [coder decodeObjectForKey:@"exampleNumber"];
_nationalNumberMatcherData = [coder decodeObjectForKey:@"nationalNumberMatcherData"];
_possibleNumberMatcherData = [coder decodeObjectForKey:@"possibleNumberMatcherData"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:self.nationalNumberPattern forKey:@"nationalNumberPattern"];
[coder encodeObject:self.possibleNumberPattern forKey:@"possibleNumberPattern"];
[coder encodeObject:self.possibleLength forKey:@"possibleLength"];
[coder encodeObject:self.possibleLengthLocalOnly forKey:@"possibleLengthLocalOnly"];
[coder encodeObject:self.exampleNumber forKey:@"exampleNumber"];
[coder encodeObject:self.nationalNumberMatcherData forKey:@"nationalNumberMatcherData"];
[coder encodeObject:self.possibleNumberMatcherData forKey:@"possibleNumberMatcherData"];
}
- (id)copyWithZone:(NSZone *)zone
{
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
_nationalNumberPattern = [coder decodeObjectForKey:@"nationalNumberPattern"];
_possibleNumberPattern = [coder decodeObjectForKey:@"possibleNumberPattern"];
_possibleLength = [coder decodeObjectForKey:@"possibleLength"];
_possibleLengthLocalOnly = [coder decodeObjectForKey:@"possibleLengthLocalOnly"];
_exampleNumber = [coder decodeObjectForKey:@"exampleNumber"];
_nationalNumberMatcherData = [coder decodeObjectForKey:@"nationalNumberMatcherData"];
_possibleNumberMatcherData = [coder decodeObjectForKey:@"possibleNumberMatcherData"];
}
return self;
}
- (BOOL)isEqual:(id)object
{
if ([object isKindOfClass:[NBPhoneNumberDesc class]] == NO) {
return NO;
}
NBPhoneNumberDesc *other = object;
return [self.nationalNumberPattern isEqual:other.nationalNumberPattern] &&
[self.possibleNumberPattern isEqual:other.possibleNumberPattern] &&
[self.possibleLength isEqual:other.possibleLength] &&
[self.possibleLengthLocalOnly isEqual:other.possibleLengthLocalOnly] &&
[self.exampleNumber isEqual:other.exampleNumber] &&
[self.nationalNumberMatcherData isEqualToData:other.nationalNumberMatcherData] &&
[self.possibleNumberMatcherData isEqualToData:other.possibleNumberMatcherData];
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.nationalNumberPattern forKey:@"nationalNumberPattern"];
[coder encodeObject:self.possibleNumberPattern forKey:@"possibleNumberPattern"];
[coder encodeObject:self.possibleLength forKey:@"possibleLength"];
[coder encodeObject:self.possibleLengthLocalOnly forKey:@"possibleLengthLocalOnly"];
[coder encodeObject:self.exampleNumber forKey:@"exampleNumber"];
[coder encodeObject:self.nationalNumberMatcherData forKey:@"nationalNumberMatcherData"];
[coder encodeObject:self.possibleNumberMatcherData forKey:@"possibleNumberMatcherData"];
}
#endif // NB_USE_EXTENSIONS
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[NBPhoneNumberDesc class]] == NO) {
return NO;
}
NBPhoneNumberDesc *other = object;
return [self.nationalNumberPattern isEqual:other.nationalNumberPattern] &&
[self.possibleNumberPattern isEqual:other.possibleNumberPattern] &&
[self.possibleLength isEqual:other.possibleLength] &&
[self.possibleLengthLocalOnly isEqual:other.possibleLengthLocalOnly] &&
[self.exampleNumber isEqual:other.exampleNumber] &&
[self.nationalNumberMatcherData isEqualToData:other.nationalNumberMatcherData] &&
[self.possibleNumberMatcherData isEqualToData:other.possibleNumberMatcherData];
}
#endif // NB_USE_EXTENSIONS
@end

146
libPhoneNumber/NBPhoneNumberUtil.h Executable file → Normal file
View File

@ -9,95 +9,129 @@
#import <Foundation/Foundation.h>
#import "NBPhoneNumberDefines.h"
@class NBPhoneMetaData, NBPhoneNumber;
@interface NBPhoneNumberUtil : NSObject
+ (NBPhoneNumberUtil*)sharedInstance;
+ (NBPhoneNumberUtil *)sharedInstance;
// regular expressions
- (NSArray*)matchesByRegex:(NSString*)sourceString regex:(NSString*)pattern;
- (NSArray*)matchedStringByRegex:(NSString*)sourceString regex:(NSString*)pattern;
- (NSString*)replaceStringByRegex:(NSString*)sourceString regex:(NSString*)pattern withTemplate:(NSString*)templateString;
- (int)stringPositionByRegex:(NSString*)sourceString regex:(NSString*)pattern;
- (NSArray *)matchesByRegex:(NSString *)sourceString regex:(NSString *)pattern;
- (NSArray *)matchedStringByRegex:(NSString *)sourceString regex:(NSString *)pattern;
- (NSString *)replaceStringByRegex:(NSString *)sourceString
regex:(NSString *)pattern
withTemplate:(NSString *)templateString;
- (int)stringPositionByRegex:(NSString *)sourceString regex:(NSString *)pattern;
// libPhoneNumber Util functions
- (NSString*)convertAlphaCharactersInNumber:(NSString*)number;
- (NSString *)convertAlphaCharactersInNumber:(NSString *)number;
- (NSString*)normalize:(NSString*)phoneNumber;
- (NSString*)normalizeDigitsOnly:(NSString*)number;
- (NSString*)normalizeDiallableCharsOnly:(NSString*)number;
- (NSString *)normalize:(NSString *)phoneNumber;
- (NSString *)normalizeDigitsOnly:(NSString *)number;
- (NSString *)normalizeDiallableCharsOnly:(NSString *)number;
- (BOOL)isNumberGeographical:(NBPhoneNumber*)phoneNumber;
- (BOOL)isNumberGeographical:(NBPhoneNumber *)phoneNumber;
- (NSString*)extractPossibleNumber:(NSString*)phoneNumber;
- (NSNumber*)extractCountryCode:(NSString*)fullNumber nationalNumber:(NSString**)nationalNumber;
- (NSString *)extractPossibleNumber:(NSString *)phoneNumber;
- (NSNumber *)extractCountryCode:(NSString *)fullNumber nationalNumber:(NSString **)nationalNumber;
#if TARGET_OS_IOS
- (NSString *)countryCodeByCarrier;
#endif
- (NSString*)getNddPrefixForRegion:(NSString*)regionCode stripNonDigits:(BOOL)stripNonDigits;
- (NSString*)getNationalSignificantNumber:(NBPhoneNumber*)phoneNumber;
- (NSString *)getNddPrefixForRegion:(NSString *)regionCode stripNonDigits:(BOOL)stripNonDigits;
- (NSString *)getNationalSignificantNumber:(NBPhoneNumber *)phoneNumber;
- (NSArray *)getSupportedRegions;
- (NBEPhoneNumberType)getNumberType:(NBPhoneNumber*)phoneNumber;
- (NBEPhoneNumberType)getNumberType:(NBPhoneNumber *)phoneNumber;
- (NSNumber*)getCountryCodeForRegion:(NSString*)regionCode;
- (NSNumber *)getCountryCodeForRegion:(NSString *)regionCode;
- (NSString*)getRegionCodeForCountryCode:(NSNumber*)countryCallingCode;
- (NSArray*)getRegionCodesForCountryCode:(NSNumber*)countryCallingCode;
- (NSString*)getRegionCodeForNumber:(NBPhoneNumber*)phoneNumber;
- (NSString *)getRegionCodeForCountryCode:(NSNumber *)countryCallingCode;
- (NSArray *)getRegionCodesForCountryCode:(NSNumber *)countryCallingCode;
- (NSString *)getRegionCodeForNumber:(NBPhoneNumber *)phoneNumber;
- (NBPhoneNumber*)getExampleNumber:(NSString*)regionCode error:(NSError**)error;
- (NBPhoneNumber*)getExampleNumberForType:(NSString*)regionCode type:(NBEPhoneNumberType)type error:(NSError**)error;
- (NBPhoneNumber*)getExampleNumberForNonGeoEntity:(NSNumber*)countryCallingCode error:(NSError**)error;
- (NBPhoneNumber *)getExampleNumber:(NSString *)regionCode error:(NSError **)error;
- (NBPhoneNumber *)getExampleNumberForType:(NSString *)regionCode
type:(NBEPhoneNumberType)type
error:(NSError **)error;
- (NBPhoneNumber *)getExampleNumberForNonGeoEntity:(NSNumber *)countryCallingCode
error:(NSError **)error;
- (BOOL)canBeInternationallyDialled:(NBPhoneNumber*)number error:(NSError**)error;
- (BOOL)canBeInternationallyDialled:(NBPhoneNumber *)number error:(NSError **)error;
- (BOOL)truncateTooLongNumber:(NBPhoneNumber*)number;
- (BOOL)truncateTooLongNumber:(NBPhoneNumber *)number;
- (BOOL)isValidNumber:(NBPhoneNumber*)number;
- (BOOL)isViablePhoneNumber:(NSString*)phoneNumber;
- (BOOL)isAlphaNumber:(NSString*)number;
- (BOOL)isValidNumberForRegion:(NBPhoneNumber*)number regionCode:(NSString*)regionCode;
- (BOOL)isNANPACountry:(NSString*)regionCode;
- (BOOL)isLeadingZeroPossible:(NSNumber*)countryCallingCode;
- (BOOL)isValidNumber:(NBPhoneNumber *)number;
- (BOOL)isViablePhoneNumber:(NSString *)phoneNumber;
- (BOOL)isAlphaNumber:(NSString *)number;
- (BOOL)isValidNumberForRegion:(NBPhoneNumber *)number regionCode:(NSString *)regionCode;
- (BOOL)isNANPACountry:(NSString *)regionCode;
- (BOOL)isLeadingZeroPossible:(NSNumber *)countryCallingCode;
- (NBEValidationResult)isPossibleNumberWithReason:(NBPhoneNumber*)number error:(NSError**)error;
- (NBEValidationResult)isPossibleNumberWithReason:(NBPhoneNumber *)number error:(NSError **)error;
- (BOOL)isPossibleNumber:(NBPhoneNumber*)number;
- (BOOL)isPossibleNumber:(NBPhoneNumber*)number error:(NSError**)error;
- (BOOL)isPossibleNumberString:(NSString*)number regionDialingFrom:(NSString*)regionDialingFrom error:(NSError**)error;
- (BOOL)isPossibleNumber:(NBPhoneNumber *)number;
- (BOOL)isPossibleNumber:(NBPhoneNumber *)number error:(NSError **)error;
- (BOOL)isPossibleNumberString:(NSString *)number
regionDialingFrom:(NSString *)regionDialingFrom
error:(NSError **)error;
- (NBEMatchType)isNumberMatch:(id)firstNumberIn second:(id)secondNumberIn error:(NSError**)error;
- (NBEMatchType)isNumberMatch:(id)firstNumberIn second:(id)secondNumberIn error:(NSError **)error;
- (int)getLengthOfGeographicalAreaCode:(NBPhoneNumber*)phoneNumber error:(NSError**)error;
- (int)getLengthOfNationalDestinationCode:(NBPhoneNumber*)phoneNumber error:(NSError**)error;
- (int)getLengthOfGeographicalAreaCode:(NBPhoneNumber *)phoneNumber error:(NSError **)error;
- (int)getLengthOfNationalDestinationCode:(NBPhoneNumber *)phoneNumber error:(NSError **)error;
- (BOOL)maybeStripNationalPrefixAndCarrierCode:(NSString**)numberStr metadata:(NBPhoneMetaData*)metadata carrierCode:(NSString**)carrierCode;
- (NBECountryCodeSource)maybeStripInternationalPrefixAndNormalize:(NSString**)numberStr possibleIddPrefix:(NSString*)possibleIddPrefix;
- (BOOL)maybeStripNationalPrefixAndCarrierCode:(NSString **)numberStr
metadata:(NBPhoneMetaData *)metadata
carrierCode:(NSString **)carrierCode;
- (NBECountryCodeSource)maybeStripInternationalPrefixAndNormalize:(NSString **)numberStr
possibleIddPrefix:(NSString *)possibleIddPrefix;
- (NSNumber*)maybeExtractCountryCode:(NSString*)number metadata:(NBPhoneMetaData*)defaultRegionMetadata
nationalNumber:(NSString**)nationalNumber keepRawInput:(BOOL)keepRawInput
phoneNumber:(NBPhoneNumber**)phoneNumber error:(NSError**)error;
- (NSNumber *)maybeExtractCountryCode:(NSString *)number
metadata:(NBPhoneMetaData *)defaultRegionMetadata
nationalNumber:(NSString **)nationalNumber
keepRawInput:(BOOL)keepRawInput
phoneNumber:(NBPhoneNumber **)phoneNumber
error:(NSError **)error;
- (NBPhoneNumber*)parse:(NSString*)numberToParse defaultRegion:(NSString*)defaultRegion error:(NSError**)error;
- (NBPhoneNumber*)parseAndKeepRawInput:(NSString*)numberToParse defaultRegion:(NSString*)defaultRegion error:(NSError**)error;
- (NBPhoneNumber*)parseWithPhoneCarrierRegion:(NSString*)numberToParse error:(NSError**)error;
- (NBPhoneNumber *)parse:(NSString *)numberToParse
defaultRegion:(NSString *)defaultRegion
error:(NSError **)error;
- (NBPhoneNumber *)parseAndKeepRawInput:(NSString *)numberToParse
defaultRegion:(NSString *)defaultRegion
error:(NSError **)error;
- (NBPhoneNumber *)parseWithPhoneCarrierRegion:(NSString *)numberToParse error:(NSError **)error;
- (NSString*)format:(NBPhoneNumber*)phoneNumber numberFormat:(NBEPhoneNumberFormat)numberFormat error:(NSError**)error;
- (NSString*)formatByPattern:(NBPhoneNumber*)number numberFormat:(NBEPhoneNumberFormat)numberFormat userDefinedFormats:(NSArray*)userDefinedFormats error:(NSError**)error;
- (NSString*)formatNumberForMobileDialing:(NBPhoneNumber*)number regionCallingFrom:(NSString*)regionCallingFrom withFormatting:(BOOL)withFormatting error:(NSError**)error;
- (NSString*)formatOutOfCountryCallingNumber:(NBPhoneNumber*)number regionCallingFrom:(NSString*)regionCallingFrom error:(NSError**)error;
- (NSString*)formatOutOfCountryKeepingAlphaChars:(NBPhoneNumber*)number regionCallingFrom:(NSString*)regionCallingFrom error:(NSError**)error;
- (NSString*)formatNationalNumberWithCarrierCode:(NBPhoneNumber*)number carrierCode:(NSString*)carrierCode error:(NSError**)error;
- (NSString*)formatInOriginalFormat:(NBPhoneNumber*)number regionCallingFrom:(NSString*)regionCallingFrom error:(NSError**)error;
- (NSString*)formatNationalNumberWithPreferredCarrierCode:(NBPhoneNumber*)number fallbackCarrierCode:(NSString*)fallbackCarrierCode error:(NSError**)error;
- (NSString *)format:(NBPhoneNumber *)phoneNumber
numberFormat:(NBEPhoneNumberFormat)numberFormat
error:(NSError **)error;
- (NSString *)formatByPattern:(NBPhoneNumber *)number
numberFormat:(NBEPhoneNumberFormat)numberFormat
userDefinedFormats:(NSArray *)userDefinedFormats
error:(NSError **)error;
- (NSString *)formatNumberForMobileDialing:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
withFormatting:(BOOL)withFormatting
error:(NSError **)error;
- (NSString *)formatOutOfCountryCallingNumber:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
error:(NSError **)error;
- (NSString *)formatOutOfCountryKeepingAlphaChars:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
error:(NSError **)error;
- (NSString *)formatNationalNumberWithCarrierCode:(NBPhoneNumber *)number
carrierCode:(NSString *)carrierCode
error:(NSError **)error;
- (NSString *)formatInOriginalFormat:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
error:(NSError **)error;
- (NSString *)formatNationalNumberWithPreferredCarrierCode:(NBPhoneNumber *)number
fallbackCarrierCode:(NSString *)fallbackCarrierCode
error:(NSError **)error;
- (BOOL)formattingRuleHasFirstGroupOnly:(NSString*)nationalPrefixFormattingRule;
- (BOOL)formattingRuleHasFirstGroupOnly:(NSString *)nationalPrefixFormattingRule;
@property (nonatomic, strong, readonly) NSDictionary *DIGIT_MAPPINGS;
@property(nonatomic, strong, readonly) NSDictionary *DIGIT_MAPPINGS;
@end

5186
libPhoneNumber/NBPhoneNumberUtil.m Executable file → Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,29 +11,29 @@
@implementation NSArray (NBAdditions)
- (id)nb_safeObjectAtIndex:(NSUInteger)index class:(Class)clazz {
if (index >= self.count) {
return nil;
}
id res = [self objectAtIndex:index];
if (![res isKindOfClass:clazz]) {
return nil;
}
return res;
if (index >= self.count) {
return nil;
}
id res = [self objectAtIndex:index];
if (![res isKindOfClass:clazz]) {
return nil;
}
return res;
}
- (NSString *)nb_safeStringAtIndex:(NSUInteger)index {
return [self nb_safeObjectAtIndex:index class:[NSString class]];
- (NSString *)nb_safeStringAtIndex : (NSUInteger)index {
return [self nb_safeObjectAtIndex:index class:[NSString class]];
}
- (NSNumber *)nb_safeNumberAtIndex:(NSUInteger)index {
return [self nb_safeObjectAtIndex:index class:[NSNumber class]];
return [self nb_safeObjectAtIndex:index class:[NSNumber class]];
}
- (NSArray *)nb_safeArrayAtIndex:(NSUInteger)index {
return [self nb_safeObjectAtIndex:index class:[NSArray class]];
return [self nb_safeObjectAtIndex:index class:[NSArray class]];
}
- (NSData *)nb_safeDataAtIndex:(NSUInteger)index {
return [self nb_safeObjectAtIndex:index class:[NSData class]];
return [self nb_safeObjectAtIndex:index class:[NSData class]];
}
@end

158
libPhoneNumberTests/NBAsYouTypeFormatterTest.m Executable file → Normal file
View File

@ -9,11 +9,9 @@
#import "NBAsYouTypeFormatter.h"
#import "NBMetadataHelper.h"
@interface NBAsYouTypeFormatterTest : XCTestCase
@end
@implementation NBAsYouTypeFormatterTest
- (void)testInvalidRegion {
@ -28,7 +26,7 @@
XCTAssertEqualObjects(@"+48 88 123", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"+48 88 123 1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"+48 88 123 12", [f inputDigit:@"2"]);
[f clear];
XCTAssertEqualObjects(@"6", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"65", [f inputDigit:@"5"]);
@ -53,7 +51,7 @@
// otherwise, no formatting is applied.
XCTAssertEqualObjects(@"+48881231+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+48881231+2", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"+48881231+", [f removeLastDigit]);
XCTAssertEqualObjects(@"+48 88 123 1", [f removeLastDigit]);
XCTAssertEqualObjects(@"+48 88 123", [f removeLastDigit]);
@ -132,7 +130,7 @@
XCTAssertEqualObjects(@"650 253 22", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"650 253 222", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"650 253 2222", [f inputDigit:@"2"]);
[f clear];
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"16", [f inputDigit:@"6"]);
@ -145,7 +143,7 @@
XCTAssertEqualObjects(@"1 650 253 22", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"1 650 253 222", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"1 650 253 2222", [f inputDigit:@"2"]);
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"01", [f inputDigit:@"1"]);
@ -162,7 +160,7 @@
XCTAssertEqualObjects(@"011 44 6 123 123 1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"011 44 6 123 123 12", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"011 44 6 123 123 123", [f inputDigit:@"3"]);
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"01", [f inputDigit:@"1"]);
@ -180,7 +178,7 @@
XCTAssertEqualObjects(@"011 54 9 11 2312 12", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"011 54 9 11 2312 123", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"011 54 9 11 2312 1234", [f inputDigit:@"4"]);
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"01", [f inputDigit:@"1"]);
@ -197,7 +195,7 @@
XCTAssertEqualObjects(@"011 244 280 000 0", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"011 244 280 000 00", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"011 244 280 000 000", [f inputDigit:@"0"]);
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+4", [f inputDigit:@"4"]);
@ -278,7 +276,7 @@
XCTAssertEqual(10, [f getRememberedPosition]);
XCTAssertEqualObjects(@"1650253222222", [f inputDigit:@"2"]);
XCTAssertEqual(10, [f getRememberedPosition]);
[f clear];
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"16", [f inputDigitAndRememberPosition:@"6"]);
@ -299,7 +297,7 @@
XCTAssertEqual(2, [f getRememberedPosition]);
XCTAssertEqualObjects(@"1650253222222", [f inputDigit:@"2"]);
XCTAssertEqual(2, [f getRememberedPosition]);
[f clear];
XCTAssertEqualObjects(@"6", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"65", [f inputDigit:@"5"]);
@ -316,7 +314,7 @@
XCTAssertEqualObjects(@"650253222;", [f inputDigit:@";"]);
XCTAssertEqual(7, [f getRememberedPosition]);
XCTAssertEqualObjects(@"650253222;2", [f inputDigit:@"2"]);
[f clear];
XCTAssertEqualObjects(@"6", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"65", [f inputDigit:@"5"]);
@ -334,7 +332,7 @@
XCTAssertEqualObjects(@"650-253-22", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"650-253-222", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"650-253-2222", [f inputDigit:@"2"]);
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"01", [f inputDigit:@"1"]);
@ -353,7 +351,7 @@
XCTAssertEqualObjects(@"011 48 88 123 12", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"011 48 88 123 12 1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"011 48 88 123 12 12", [f inputDigit:@"2"]);
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+1", [f inputDigit:@"1"]);
@ -369,7 +367,7 @@
XCTAssertEqualObjects(@"+1 650 253 22", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"+1 650 253 222", [f inputDigit:@"2"]);
XCTAssertEqual(10, [f getRememberedPosition]);
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+1", [f inputDigit:@"1"]);
@ -459,7 +457,7 @@
XCTAssertEqualObjects(@"030/12", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"030/123", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"030/1234", [f inputDigit:@"4"]);
// 04134 1234
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -471,7 +469,7 @@
XCTAssertEqualObjects(@"04134 12", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"04134 123", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"04134 1234", [f inputDigit:@"4"]);
// 08021 2345
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -483,7 +481,7 @@
XCTAssertEqualObjects(@"08021 23", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"08021 234", [f inputDigit:@"4"]);
XCTAssertEqualObjects(@"08021 2345", [f inputDigit:@"5"]);
// 00 1 650 253 2250
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -516,7 +514,7 @@
XCTAssertEqualObjects(@"011 7031-3000", [f inputDigit:@"0"]);
}
-(void)testAYTFARMobile {
- (void)testAYTFARMobile {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"AR"];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+5", [f inputDigit:@"5"]);
@ -534,7 +532,7 @@
XCTAssertEqualObjects(@"+54 9 11 2312 1234", [f inputDigit:@"4"]);
}
-(void)testAYTFKR {
- (void)testAYTFKR {
// +82 51 234 5678
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"KR"];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -549,7 +547,7 @@
XCTAssertEqualObjects(@"+82 51-234-56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+82 51-234-567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+82 51-234-5678", [f inputDigit:@"8"]);
// +82 2 531 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -563,7 +561,7 @@
XCTAssertEqualObjects(@"+82 2-531-56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+82 2-531-567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+82 2-531-5678", [f inputDigit:@"8"]);
// +82 2 3665 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -578,7 +576,7 @@
XCTAssertEqualObjects(@"+82 2-3665-56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+82 2-3665-567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+82 2-3665-5678", [f inputDigit:@"8"]);
// 02-114
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -586,7 +584,7 @@
XCTAssertEqualObjects(@"021", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"02-11", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"02-114", [f inputDigit:@"4"]);
// 02-1300
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -595,7 +593,7 @@
XCTAssertEqualObjects(@"02-13", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"02-130", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"02-1300", [f inputDigit:@"0"]);
// 011-456-7890
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -608,7 +606,7 @@
XCTAssertEqualObjects(@"011-456-78", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"011-456-789", [f inputDigit:@"9"]);
XCTAssertEqualObjects(@"011-456-7890", [f inputDigit:@"0"]);
// 011-9876-7890
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -624,9 +622,9 @@
XCTAssertEqualObjects(@"011-9876-7890", [f inputDigit:@"0"]);
}
-(void)testAYTF_MX {
- (void)testAYTF_MX {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"MX"];
// +52 800 123 4567
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+5", [f inputDigit:@"5"]);
@ -641,7 +639,7 @@
XCTAssertEqualObjects(@"+52 800 123 45", [f inputDigit:@"5"]);
XCTAssertEqualObjects(@"+52 800 123 456", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+52 800 123 4567", [f inputDigit:@"7"]);
// +52 55 1234 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -657,7 +655,7 @@
XCTAssertEqualObjects(@"+52 55 1234 56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+52 55 1234 567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+52 55 1234 5678", [f inputDigit:@"8"]);
// +52 212 345 6789
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -673,7 +671,7 @@
XCTAssertEqualObjects(@"+52 212 345 67", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+52 212 345 678", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"+52 212 345 6789", [f inputDigit:@"9"]);
// +52 1 55 1234 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -690,7 +688,7 @@
XCTAssertEqualObjects(@"+52 1 55 1234 56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+52 1 55 1234 567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+52 1 55 1234 5678", [f inputDigit:@"8"]);
// +52 1 541 234 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -709,7 +707,7 @@
XCTAssertEqualObjects(@"+52 1 541 234 5678", [f inputDigit:@"8"]);
}
-(void)testAYTF_International_Toll_Free {
- (void)testAYTF_International_Toll_Free {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
// +800 1234 5678
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -727,7 +725,7 @@
XCTAssertEqualObjects(@"+800123456789", [f inputDigit:@"9"]);
}
-(void)testAYTFMultipleLeadingDigitPatterns {
- (void)testAYTFMultipleLeadingDigitPatterns {
// +81 50 2345 6789
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"JP"];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -743,7 +741,7 @@
XCTAssertEqualObjects(@"+81 50 2345 67", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+81 50 2345 678", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"+81 50 2345 6789", [f inputDigit:@"9"]);
// +81 222 12 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -758,7 +756,7 @@
XCTAssertEqualObjects(@"+81 222 12 56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"+81 222 12 567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"+81 222 12 5678", [f inputDigit:@"8"]);
// 011113
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -767,7 +765,7 @@
XCTAssertEqualObjects(@"011 1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"011 11", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"011113", [f inputDigit:@"3"]);
// +81 3332 2 5678
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -784,7 +782,7 @@
XCTAssertEqualObjects(@"+81 3332 2 5678", [f inputDigit:@"8"]);
}
-(void)testAYTFLongIDD_AU {
- (void)testAYTFLongIDD_AU {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"AU"];
// 0011 1 650 253 2250
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -802,7 +800,7 @@
XCTAssertEqualObjects(@"0011 1 650 253 22", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"0011 1 650 253 222", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"0011 1 650 253 2222", [f inputDigit:@"2"]);
// 0011 81 3332 2 5678
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -820,7 +818,7 @@
XCTAssertEqualObjects(@"0011 81 3332 2 56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"0011 81 3332 2 567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"0011 81 3332 2 5678", [f inputDigit:@"8"]);
// 0011 244 250 253 222
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -841,7 +839,7 @@
XCTAssertEqualObjects(@"0011 244 250 253 222", [f inputDigit:@"2"]);
}
-(void)testAYTFLongIDD_KR {
- (void)testAYTFLongIDD_KR {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"KR"];
// 00300 1 650 253 2222
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -862,7 +860,7 @@
XCTAssertEqualObjects(@"00300 1 650 253 2222", [f inputDigit:@"2"]);
}
-(void)testAYTFLongNDD_KR {
- (void)testAYTFLongNDD_KR {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"KR"];
// 08811-9876-7890
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -878,7 +876,7 @@
XCTAssertEqualObjects(@"08811-9876-78", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"08811-9876-789", [f inputDigit:@"9"]);
XCTAssertEqualObjects(@"08811-9876-7890", [f inputDigit:@"0"]);
// 08500 11-9876-7890
[f clear];
XCTAssertEqualObjects(@"0", [f inputDigit:@"0"]);
@ -898,7 +896,7 @@
XCTAssertEqualObjects(@"08500 11-9876-7890", [f inputDigit:@"0"]);
}
-(void)testAYTFLongNDD_SG {
- (void)testAYTFLongNDD_SG {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"SG"];
// 777777 9876 7890
XCTAssertEqualObjects(@"7", [f inputDigit:@"7"]);
@ -917,10 +915,10 @@
XCTAssertEqualObjects(@"777777 9876 7890", [f inputDigit:@"0"]);
}
-(void)testAYTFShortNumberFormattingFix_AU {
- (void)testAYTFShortNumberFormattingFix_AU {
// For Australia, the national prefix is not optional when formatting.
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"AU"];
// 1234567890 - For leading digit 1, the national prefix formatting rule has
// first group only.
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
@ -933,7 +931,7 @@
XCTAssertEqualObjects(@"1234 567 8", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"1234 567 89", [f inputDigit:@"9"]);
XCTAssertEqualObjects(@"1234 567 890", [f inputDigit:@"0"]);
// +61 1234 567 890 - Test the same number, but with the country code.
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -949,7 +947,7 @@
XCTAssertEqualObjects(@"+61 1234 567 8", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"+61 1234 567 89", [f inputDigit:@"9"]);
XCTAssertEqualObjects(@"+61 1234 567 890", [f inputDigit:@"0"]);
// 212345678 - For leading digit 2, the national prefix formatting rule puts
// the national prefix before the first group.
[f clear];
@ -963,7 +961,7 @@
XCTAssertEqualObjects(@"02 1234 56", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"02 1234 567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"02 1234 5678", [f inputDigit:@"8"]);
// 212345678 - Test the same number, but without the leading 0.
[f clear];
XCTAssertEqualObjects(@"2", [f inputDigit:@"2"]);
@ -975,7 +973,7 @@
XCTAssertEqualObjects(@"2123456", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"21234567", [f inputDigit:@"7"]);
XCTAssertEqualObjects(@"212345678", [f inputDigit:@"8"]);
// +61 2 1234 5678 - Test the same number, but with the country code.
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -992,22 +990,22 @@
XCTAssertEqualObjects(@"+61 2 1234 5678", [f inputDigit:@"8"]);
}
-(void)testAYTFShortNumberFormattingFix_KR {
- (void)testAYTFShortNumberFormattingFix_KR {
// For Korea, the national prefix is not optional when formatting, and the
// national prefix formatting rule doesn't consist of only the first group.
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"KR"];
// 111
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"11", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"111", [f inputDigit:@"1"]);
// 114
[f clear];
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"11", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"114", [f inputDigit:@"4"]);
// 13121234 - Test a mobile number without the national prefix. Even though it
// is not an emergency number, it should be formatted as a block.
[f clear];
@ -1019,7 +1017,7 @@
XCTAssertEqualObjects(@"131212", [f inputDigit:@"2"]);
XCTAssertEqualObjects(@"1312123", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"13121234", [f inputDigit:@"4"]);
// +82 131-2-1234 - Test the same number, but with the country code.
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -1035,15 +1033,15 @@
XCTAssertEqualObjects(@"+82 131-2-1234", [f inputDigit:@"4"]);
}
-(void)testAYTFShortNumberFormattingFix_MX {
- (void)testAYTFShortNumberFormattingFix_MX {
// For Mexico, the national prefix is optional when formatting.
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"MX"];
// 911
XCTAssertEqualObjects(@"9", [f inputDigit:@"9"]);
XCTAssertEqualObjects(@"91", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"911", [f inputDigit:@"1"]);
// 800 123 4567 - Test a toll-free number, which should have a formatting rule
// applied to it even though it doesn't begin with the national prefix.
[f clear];
@ -1057,7 +1055,7 @@
XCTAssertEqualObjects(@"800 123 45", [f inputDigit:@"5"]);
XCTAssertEqualObjects(@"800 123 456", [f inputDigit:@"6"]);
XCTAssertEqualObjects(@"800 123 4567", [f inputDigit:@"7"]);
// +52 800 123 4567 - Test the same number, but with the country code.
[f clear];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
@ -1075,7 +1073,7 @@
XCTAssertEqualObjects(@"+52 800 123 4567", [f inputDigit:@"7"]);
}
-(void)testAYTFNoNationalPrefix {
- (void)testAYTFNoNationalPrefix {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"IT"];
XCTAssertEqualObjects(@"3", [f inputDigit:@"3"]);
XCTAssertEqualObjects(@"33", [f inputDigit:@"3"]);
@ -1085,21 +1083,21 @@
XCTAssertEqualObjects(@"333 333", [f inputDigit:@"3"]);
}
-(void)testAYTFShortNumberFormattingFix_US {
- (void)testAYTFShortNumberFormattingFix_US {
// For the US, an initial 1 is treated specially.
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
// 101 - Test that the initial 1 is not treated as a national prefix.
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"10", [f inputDigit:@"0"]);
XCTAssertEqualObjects(@"101", [f inputDigit:@"1"]);
// 112 - Test that the initial 1 is not treated as a national prefix.
[f clear];
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"11", [f inputDigit:@"1"]);
XCTAssertEqualObjects(@"112", [f inputDigit:@"2"]);
// 122 - Test that the initial 1 is treated as a national prefix.
[f clear];
XCTAssertEqualObjects(@"1", [f inputDigit:@"1"]);
@ -1107,9 +1105,9 @@
XCTAssertEqualObjects(@"1 22", [f inputDigit:@"2"]);
}
-(void)testAYTFDescription {
- (void)testAYTFDescription {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
[f inputDigit:@"1"];
[f inputDigit:@"6"];
[f inputDigit:@"5"];
@ -1122,47 +1120,47 @@
[f inputDigit:@"2"];
[f inputDigit:@"2"];
XCTAssertEqualObjects(@"1 650 253 2222", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650 253 222", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650 253 22", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650 253 2", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650 253", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650 25", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650 2", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 650", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1 65", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"16", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"1", [f description]);
[f removeLastDigit];
XCTAssertEqualObjects(@"", [f description]);
[f inputString:@"16502532222"];
XCTAssertEqualObjects(@"1 650 253 2222", [f description]);
}
-(void)testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss {
- (void)testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss {
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"CN"];
XCTAssertEqualObjects(@"+", [f inputDigit:@"+"]);
XCTAssertEqualObjects(@"+8", [f inputDigit:@"8"]);
XCTAssertEqualObjects(@"+86 ", [f inputDigit:@"6"]);

2355
libPhoneNumberTests/NBPhoneNumberUtilTest.m Executable file → Normal file

File diff suppressed because it is too large Load Diff