Skip to content

Commit

Permalink
Make short number and geocoding support passing in NSData object as well
Browse files Browse the repository at this point in the history
  • Loading branch information
paween committed Jul 31, 2020
1 parent 7379b3d commit d9364c2
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 383 deletions.
Empty file.
30 changes: 20 additions & 10 deletions libPhoneNumber.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
0FC0D38124A2A18D0087AFCF /* NBPhoneNumberOfflineGeocoder.h in Headers */ = {isa = PBXBuildFile; fileRef = 944B9B0B2497EEDC007E17B5 /* NBPhoneNumberOfflineGeocoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
0FC0D38224A2A18F0087AFCF /* NBGeocoderMetadataHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 944B9B002497ED79007E17B5 /* NBGeocoderMetadataHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
0FC0D38324A2A1D30087AFCF /* GeocodingMetadata.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 944B9B1C2497F63B007E17B5 /* GeocodingMetadata.bundle */; };
0FC214D724D23DDE002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.c in Sources */ = {isa = PBXBuildFile; fileRef = 0FC214D524D23DDD002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.c */; };
0FC214DA24D339BD002A5E4B /* libPhoneNumberMetadataForTesting in Resources */ = {isa = PBXBuildFile; fileRef = 0FC214D924D339BD002A5E4B /* libPhoneNumberMetadataForTesting */; };
0FD8755D24D39AB300AB132F /* libPhoneNumberMetadataForTesting in Resources */ = {isa = PBXBuildFile; fileRef = 0FC214D924D339BD002A5E4B /* libPhoneNumberMetadataForTesting */; };
0FD8755E24D39DD900AB132F /* libPhoneNumberMetadataForTesting in Resources */ = {isa = PBXBuildFile; fileRef = 0FC214D924D339BD002A5E4B /* libPhoneNumberMetadataForTesting */; };
1485C5271E06F4890092F541 /* NBAsYouTypeFormatterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1485C5231E06F4890092F541 /* NBAsYouTypeFormatterTest.m */; };
1485C5291E06F4890092F541 /* NBPhoneNumberUtilTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1485C5251E06F4890092F541 /* NBPhoneNumberUtilTest.m */; };
14B7A2AB1DE9BF160051AED7 /* NBMetadataHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = FD12C2691A87401B00B53856 /* NBMetadataHelper.m */; };
Expand Down Expand Up @@ -244,8 +246,7 @@
0FC0D36724A29F510087AFCF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0FC0D37624A2A05E0087AFCF /* libPhoneNumberGeocodingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = libPhoneNumberGeocodingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
0FC0D37A24A2A05E0087AFCF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0FC214D524D23DDD002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = NBGeneratedPhoneNumberMetadataForTesting.c; sourceTree = "<group>"; };
0FC214D624D23DDD002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBGeneratedPhoneNumberMetadataForTesting.h; sourceTree = "<group>"; };
0FC214D924D339BD002A5E4B /* libPhoneNumberMetadataForTesting */ = {isa = PBXFileReference; lastKnownFileType = text; path = libPhoneNumberMetadataForTesting; sourceTree = "<group>"; };
1485C5231E06F4890092F541 /* NBAsYouTypeFormatterTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NBAsYouTypeFormatterTest.m; path = libPhoneNumberTests/NBAsYouTypeFormatterTest.m; sourceTree = SOURCE_ROOT; };
1485C5251E06F4890092F541 /* NBPhoneNumberUtilTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NBPhoneNumberUtilTest.m; path = libPhoneNumberTests/NBPhoneNumberUtilTest.m; sourceTree = SOURCE_ROOT; };
1485C52B1E06F4930092F541 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = libPhoneNumberTests/Info.plist; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -457,6 +458,14 @@
path = libPhoneNumberGeocodingTests;
sourceTree = "<group>";
};
0FC214D824D339A2002A5E4B /* Resources */ = {
isa = PBXGroup;
children = (
0FC214D924D339BD002A5E4B /* libPhoneNumberMetadataForTesting */,
);
path = Resources;
sourceTree = "<group>";
};
14B7A2681DE9AC4B0051AED7 /* libPhoneNumber.generator */ = {
isa = PBXGroup;
children = (
Expand All @@ -480,8 +489,6 @@
14B7A2941DE9B65D0051AED7 /* libPhoneNumber.tests */ = {
isa = PBXGroup;
children = (
0FC214D524D23DDD002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.c */,
0FC214D624D23DDD002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.h */,
0FAE11902037959800193503 /* NBPhoneNumberParsingPerfTest.m */,
1485C52B1E06F4930092F541 /* Info.plist */,
1485C5231E06F4890092F541 /* NBAsYouTypeFormatterTest.m */,
Expand Down Expand Up @@ -615,6 +622,7 @@
FD7A0611167715A0004BBEB6 = {
isa = PBXGroup;
children = (
0FC214D824D339A2002A5E4B /* Resources */,
14C114F01DE9A6D60029FE1C /* libPhoneNumber.frameworks */,
FD7A0626167715A0004BBEB6 /* libPhoneNumber.core */,
14B7A2681DE9AC4B0051AED7 /* libPhoneNumber.generator */,
Expand Down Expand Up @@ -1082,6 +1090,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0FD8755E24D39DD900AB132F /* libPhoneNumberMetadataForTesting in Resources */,
94C9AF6024B3CE1000469F54 /* TestingSource.bundle in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -1090,6 +1099,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0FC214DA24D339BD002A5E4B /* libPhoneNumberMetadataForTesting in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1142,6 +1152,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0FD8755D24D39AB300AB132F /* libPhoneNumberMetadataForTesting in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1174,7 +1185,6 @@
1485C5291E06F4890092F541 /* NBPhoneNumberUtilTest.m in Sources */,
0F4314DC203CA833005FE065 /* NBPhoneNumberParsingPerfTest.m in Sources */,
14B7A2AB1DE9BF160051AED7 /* NBMetadataHelper.m in Sources */,
0FC214D724D23DDE002A5E4B /* NBGeneratedPhoneNumberMetadataForTesting.c in Sources */,
14B7A2B01DE9BF160051AED7 /* NBPhoneNumber.m in Sources */,
14B7A2B11DE9BF160051AED7 /* NBNumberFormat.m in Sources */,
8B0FD3031E4A88AC0049DF81 /* NSArray+NBAdditions.m in Sources */,
Expand Down Expand Up @@ -1455,7 +1465,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
INFOPLIST_FILE = libPhoneNumberGeocodingTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -1491,7 +1501,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
INFOPLIST_FILE = libPhoneNumberGeocodingTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -2105,7 +2115,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
INFOPLIST_FILE = libPhoneNumberShortNumberTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.5;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -2140,7 +2150,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
INFOPLIST_FILE = libPhoneNumberShortNumberTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.5;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "34ACBB841B7122AC0064B3BD"
BuildableName = "libPhoneNumberiOS.framework"
BlueprintName = "libPhoneNumberiOS"
ReferencedContainer = "container:libPhoneNumber.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
26 changes: 26 additions & 0 deletions libPhoneNumber/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>libPhoneNumberwatchOS</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
5 changes: 3 additions & 2 deletions libPhoneNumberGeocoding/Geocoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ FOUNDATION_EXPORT double GeocodingVersionNumber;
//! Project version string for Geocoding.
FOUNDATION_EXPORT const unsigned char GeocodingVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <Geocoding/PublicHeader.h>
// In this header, you should import all the public headers of your framework
// using statements like #import <Geocoding/PublicHeader.h>

#import <libPhoneNumberGeocoding/NBPhoneNumberOfflineGeocoder.h>
#import <libPhoneNumberGeocoding/NBGeocoderMetadataHelper.h>
#import <libPhoneNumberGeocoding/NBPhoneNumberOfflineGeocoder.h>
10 changes: 6 additions & 4 deletions libPhoneNumberGeocoding/NBPhoneNumberOfflineGeocoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#import <libPhoneNumberGeocoding/NBGeocoderMetadataHelper.h>

@class NBPhoneNumber;
@class NBPhoneNumberUtil;

NS_ASSUME_NONNULL_BEGIN

typedef NBGeocoderMetadataHelper *_Nonnull (^NBGeocoderMetadataHelperFactory)(
NSNumber *countryCode, NSString *language);
typedef NBGeocoderMetadataHelper *_Nonnull (^NBGeocoderMetadataHelperFactory)(NSNumber *countryCode,
NSString *language);

@interface NBPhoneNumberOfflineGeocoder : NSObject

Expand All @@ -25,11 +26,12 @@ typedef NBGeocoderMetadataHelper *_Nonnull (^NBGeocoderMetadataHelperFactory)(
*
* @param factory a factory method, with countryCode and language parameters,
* that returns an instance of NBGeocoderMetadataHelper
* @param phoneNumberUtil an instance of NBPhoneNumberUtil class.
* @return an instance of NBPhoneNumberOfflineGeocoder that creates
* NBGeocoderMetadataHelper instances using the factory method parameter
*/
- (instancetype)initWithMetadataHelperFactory:
(NBGeocoderMetadataHelperFactory)factory;
- (instancetype)initWithMetadataHelperFactory:(NBGeocoderMetadataHelperFactory)factory
phoneNumberUtil:(NBPhoneNumberUtil *)phoneNumberUtil;

/**
* Returns an instance of NBPhoneNumberOfflineGeocoder that uses a default
Expand Down
16 changes: 10 additions & 6 deletions libPhoneNumberGeocoding/NBPhoneNumberOfflineGeocoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ @implementation NBPhoneNumberOfflineGeocoder {
static NSString *const INVALID_REGION_CODE = @"ZZ";

- (instancetype)init {
return [self initWithMetadataHelperFactory:^NBGeocoderMetadataHelper *(
NSNumber *_Nonnull countryCode, NSString *_Nonnull language) {
return [[NBGeocoderMetadataHelper alloc] initWithCountryCode:countryCode withLanguage:language];
}];
return [self
initWithMetadataHelperFactory:^NBGeocoderMetadataHelper *(NSNumber *_Nonnull countryCode,
NSString *_Nonnull language) {
return [[NBGeocoderMetadataHelper alloc] initWithCountryCode:countryCode
withLanguage:language];
}
phoneNumberUtil:NBPhoneNumberUtil.sharedInstance];
}

- (instancetype)initWithMetadataHelperFactory:(NBGeocoderMetadataHelperFactory)factory {
- (instancetype)initWithMetadataHelperFactory:(NBGeocoderMetadataHelperFactory)factory
phoneNumberUtil:(NBPhoneNumberUtil *)phoneNumberUtil {
self = [super init];
if (self != nil) {
_phoneNumberUtil = NBPhoneNumberUtil.sharedInstance;
_phoneNumberUtil = phoneNumberUtil;
_metadataHelpers = [[NSCache alloc] init];
_metadataHelperFactory = [factory copy];
}
Expand Down
14 changes: 13 additions & 1 deletion libPhoneNumberGeocodingTests/NBPhoneNumberOfflineGeocoderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

#import <XCTest/XCTest.h>

#import "NBMetadataHelper.h"
#import "NBPhoneNumber.h"
#import "NBPhoneNumberOfflineGeocoder.h"
#import "NBPhoneNumberUtil.h"

static size_t kPhoneNumberMetaDataForTestingExpandedLength = 33021;

@interface NBPhoneNumberOfflineGeocoderTest : XCTestCase

@property(nonatomic, strong) NBPhoneNumberOfflineGeocoder *geocoder;
Expand Down Expand Up @@ -44,13 +47,22 @@ - (void)setUp {
NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder];
NSURL *resourceURL = [[bundle resourceURL] URLByAppendingPathComponent:@"TestingSource.bundle"];
NSBundle *testDatabaseBundle = [NSBundle bundleWithURL:resourceURL];

NSString *metadataPath = [bundle pathForResource:@"libPhoneNumberMetadataForTesting" ofType:nil];
NSData *metadataData = [NSData dataWithContentsOfFile:metadataPath];
NBMetadataHelper *helper =
[[NBMetadataHelper alloc] initWithZippedData:metadataData
expandedLength:kPhoneNumberMetaDataForTestingExpandedLength];
NBPhoneNumberUtil *phoneNumberUtil = [[NBPhoneNumberUtil alloc] initWithMetadataHelper:helper];

self.geocoder = [[NBPhoneNumberOfflineGeocoder alloc]
initWithMetadataHelperFactory:^NBGeocoderMetadataHelper *(NSNumber *_Nonnull countryCode,
NSString *_Nonnull language) {
return [[NBGeocoderMetadataHelper alloc] initWithCountryCode:countryCode
withLanguage:language
withBundle:testDatabaseBundle];
}];
}
phoneNumberUtil:phoneNumberUtil];
}

- (NBPhoneNumber *)koreanPhoneNumber1 {
Expand Down
5 changes: 5 additions & 0 deletions libPhoneNumberShortNumber/NBShortNumberMetadataHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
#import <Foundation/Foundation.h>

@class NBPhoneMetaData;
@class NBMetadataHelper;

NS_ASSUME_NONNULL_BEGIN

@interface NBShortNumberMetadataHelper : NSObject

- (instancetype)initWithZippedData:(NSData *)data
expandedLength:(NSUInteger)expandedLength
metadataHelper:(NBMetadataHelper *)helper;

/**
* Returns the short number metadata for the given region code or {@code nil} if the region
* code is invalid or unknown.
Expand Down
40 changes: 27 additions & 13 deletions libPhoneNumberShortNumber/NBShortNumberMetadataHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,41 @@
@implementation NBShortNumberMetadataHelper {
NSCache<NSString *, NBPhoneMetaData *> *_shortNumberMetadataCache;
NBMetadataHelper *_helper;
NSDictionary *_shortNumberDataMap;
}

- (instancetype)init {
return [self initWithZippedDataBytes:kShortNumberMetaData
compressedLength:kShortNumberMetaDataCompressedLength
expandedLength:kShortNumberMetaDataExpandedLength
metadataHelper:[[NBMetadataHelper alloc] init]];
}

- (instancetype)initWithZippedData:(NSData *)data
expandedLength:(NSUInteger)expandedLength
metadataHelper:(NBMetadataHelper *)helper {
return [self initWithZippedDataBytes:(z_const Bytef *)data.bytes
compressedLength:data.length
expandedLength:expandedLength
metadataHelper:helper];
}

- (instancetype)initWithZippedDataBytes:(z_const Bytef *)data
compressedLength:(NSUInteger)compressedLength
expandedLength:(NSUInteger)expandedLength
metadataHelper:(NBMetadataHelper *)helper {
self = [super init];

if (self != nil) {
_helper = [[NBMetadataHelper alloc] init];
_shortNumberMetadataCache = [[NSCache alloc] init];
_shortNumberDataMap =
[NBShortNumberMetadataHelper jsonObjectFromZippedDataWithBytes:data
compressedLength:compressedLength
expandedLength:expandedLength];
}
return self;
}

+ (NSDictionary *)shortNumberDataMap {
static NSDictionary *shortNumberDataDictionary;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shortNumberDataDictionary =
[self jsonObjectFromZippedDataWithBytes:kShortNumberMetaData
compressedLength:kShortNumberMetaDataCompressedLength
expandedLength:kShortNumberMetaDataExpandedLength];
});
return shortNumberDataDictionary;
return self;
}

- (NBPhoneMetaData *)shortNumberMetadataForRegion:(NSString *)regionCode {
Expand All @@ -61,7 +75,7 @@ - (NBPhoneMetaData *)shortNumberMetadataForRegion:(NSString *)regionCode {
return cachedMetadata;
}

NSDictionary *dict = [[self class] shortNumberDataMap][@"countryToMetadata"];
NSDictionary *dict = _shortNumberDataMap[@"countryToMetadata"];
NSArray *entry = dict[regionCode];
if (entry) {
NBPhoneMetaData *metadata = [[NBPhoneMetaData alloc] initWithEntry:entry];
Expand Down
11 changes: 10 additions & 1 deletion libPhoneNumberShortNumber/NBShortNumberUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <Foundation/Foundation.h>

@class NBPhoneNumber;
@class NBShortNumberMetadataHelper;
@class NBPhoneNumberUtil;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -23,7 +25,14 @@ typedef NS_ENUM(NSUInteger, NBEShortNumberCost) {

@property(nonatomic) NSDictionary<NSNumber *, NSArray<NSString *> *> *countryToRegionCodeMap;

// Short number related methods
/// Initialize short number util with a default metadata helper.
- (instancetype)init;

/// Initialize short number util with a metadata helper.
/// @param helper A metadata helper.
- (instancetype)initWithMetadataHelper:(NBShortNumberMetadataHelper *)helper
phoneNumberUtil:(NBPhoneNumberUtil *)phoneNumberUtil;

/**
* Check whether a short number is a possible number when dialed from the given region. This
* provides a more lenient check than {@link #isValidShortNumberForRegion}.
Expand Down
Loading

0 comments on commit d9364c2

Please sign in to comment.