Skip to content

Modernization and improvements. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions RegExCategories.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@
* Returns the index of the first match of the passed string.
*
* ie.
* int i = [RX(@"\d+") indexOf:@"Buy 1 dog or buy 2?"]; // => 4
* NSUInteger i = [RX(@"\d+") indexOf:@"Buy 1 dog or buy 2?"]; // => 4
*/

- (int) indexOf:(NSString*)str;
- (NSUInteger) indexOf:(NSString*)str;


/**
Expand Down Expand Up @@ -248,7 +248,7 @@
* => @[ @"[email protected]", @"[email protected]" ]
*/

- (NSArray*) matches:(NSString*)str;
- (NSArray<NSTextCheckingResult *> *) matches:(NSString*)str;


/**
Expand All @@ -272,7 +272,7 @@
* NSArray* matches = [str matchesWithDetails:RX(@"\\w+[@]\\w+[.](\\w+)")];
*/

- (NSArray*) matchesWithDetails:(NSString*)str;
- (NSArray<RxMatch *> *) matchesWithDetails:(NSString*)str;


/**
Expand Down Expand Up @@ -346,10 +346,10 @@
* the regex passed in.
*
* ie.
* int i = [@"Buy 1 dog or buy 2?" indexOf:RX(@"\d+")]; // => 4
* NSUInteger i = [@"Buy 1 dog or buy 2?" indexOf:RX(@"\d+")]; // => 4
*/

- (int) indexOf:(NSRegularExpression*)rx;
- (NSUInteger) indexOf:(NSRegularExpression*)rx;


/**
Expand Down
72 changes: 45 additions & 27 deletions RegExCategories.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ - (BOOL) isMatch:(NSString*)matchee
return [self numberOfMatchesInString:matchee options:0 range:NSMakeRange(0, matchee.length)] > 0;
}

- (int) indexOf:(NSString*)matchee
- (NSUInteger) indexOf:(NSString*)matchee
{
NSRange range = [self rangeOfFirstMatchInString:matchee options:0 range:NSMakeRange(0, matchee.length)];
return range.location == NSNotFound ? -1 : (int)range.location;
return range.location; // Can return NSNotFound!
}

- (NSArray*) split:(NSString *)str
Expand All @@ -81,7 +81,7 @@ - (NSArray*) split:(NSString *)str
(matchingRanges.count == 0 ? str.length : [matchingRanges[0] rangeValue].location))]];

//add between splits ranges and last range
for(int i=0; i<matchingRanges.count; i++){
for(NSUInteger i=0; i<matchingRanges.count; i++){
BOOL isLast = i+1 == matchingRanges.count;
unsigned long startLoc = [matchingRanges[i] rangeValue].location + [matchingRanges[i] rangeValue].length;
unsigned long endLoc = isLast ? str.length : [matchingRanges[i+1] rangeValue].location;
Expand Down Expand Up @@ -112,16 +112,25 @@ - (NSString*) replace:(NSString*)string withBlock:(NSString*(^)(NSString* match)
//copy the string so we can replace subsections
NSMutableString* result = [string mutableCopy];

//get matches
NSArray* matches = [self matchesInString:string options:0 range:NSMakeRange(0, string.length)];

//replace each match (right to left so indexing doesn't get messed up)
for (int i=(int)matches.count-1; i>=0; i--) {
NSTextCheckingResult* match = matches[i];
NSString* matchStr = [string substringWithRange:match.range];
NSString* replacement = replacer(matchStr);
[result replaceCharactersInRange:match.range withString:replacement];
}
//process matches
__block NSInteger offset = 0;
[self enumerateMatchesInString:string
options:0
range:NSMakeRange(0, string.length)
usingBlock:
^(NSTextCheckingResult * _Nullable match, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSRange range = match.range;

NSString* matchStr = [string substringWithRange:range];
NSString* replacement = replacer(matchStr);
if (replacement == nil) return;

range.location += offset;

[result replaceCharactersInRange:range withString:replacement];

offset += replacement.length - match.range.length;
}];

return result;
}
Expand All @@ -134,21 +143,30 @@ - (NSString*) replace:(NSString *)string withDetailsBlock:(NSString*(^)(RxMatch*
//copy the string so we can replace subsections
NSMutableString* replaced = [string mutableCopy];

//get matches
NSArray* matches = [self matchesInString:string options:0 range:NSMakeRange(0, string.length)];

//replace each match (right to left so indexing doesn't get messed up)
for (int i=(int)matches.count-1; i>=0; i--) {
NSTextCheckingResult* result = matches[i];
RxMatch* match = [self resultToMatch:result original:string];
NSString* replacement = replacer(match);
[replaced replaceCharactersInRange:result.range withString:replacement];
}
//process matches
__block NSInteger offset = 0;
[self enumerateMatchesInString:string
options:0
range:NSMakeRange(0, string.length)
usingBlock:
^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSRange range = result.range;

RxMatch* match = [self resultToMatch:result original:string];
NSString* replacement = replacer(match);
if (replacement == nil) return;

range.location += offset;

[replaced replaceCharactersInRange:range withString:replacement];

offset += replacement.length - result.range.length;
}];

return replaced;
}

- (NSArray*) matches:(NSString*)str
- (NSArray<NSTextCheckingResult *> *) matches:(NSString*)str
{
NSMutableArray* matches = [NSMutableArray array];

Expand Down Expand Up @@ -179,7 +197,7 @@ - (RxMatch*) resultToMatch:(NSTextCheckingResult*)result original:(NSString*)ori

//groups
NSMutableArray* groups = [NSMutableArray array];
for(int i=0; i<result.numberOfRanges; i++){
for(NSUInteger i=0; i<result.numberOfRanges; i++){
RxMatchGroup* group = [[RxMatchGroup alloc] init];
group.range = [result rangeAtIndex:i];
group.value = group.range.length ? [original substringWithRange:group.range] : nil;
Expand All @@ -190,7 +208,7 @@ - (RxMatch*) resultToMatch:(NSTextCheckingResult*)result original:(NSString*)ori
return match;
}

- (NSArray*) matchesWithDetails:(NSString*)str
- (NSArray<RxMatch *> *) matchesWithDetails:(NSString*)str
{
NSMutableArray* matches = [NSMutableArray array];

Expand Down Expand Up @@ -238,7 +256,7 @@ - (BOOL) isMatch:(NSRegularExpression*)rx
return [rx isMatch:self];
}

- (int) indexOf:(NSRegularExpression*)rx
- (NSUInteger) indexOf:(NSRegularExpression*)rx
{
return [rx indexOf:self];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
BC7CC817184AF846007FCB6B /* NSString+IndexOf.m in Sources */ = {isa = PBXBuildFile; fileRef = BC7CC816184AF846007FCB6B /* NSString+IndexOf.m */; };
BC7CC819184AF963007FCB6B /* NSString+IsMatch.m in Sources */ = {isa = PBXBuildFile; fileRef = BC7CC818184AF963007FCB6B /* NSString+IsMatch.m */; };
BC7CC81B184AF9ED007FCB6B /* NSString+Initialization.m in Sources */ = {isa = PBXBuildFile; fileRef = BC7CC81A184AF9ED007FCB6B /* NSString+Initialization.m */; };
BCCD4868185009E80038ADCB /* docs.md in Resources */ = {isa = PBXBuildFile; fileRef = BCCD4863185009E80038ADCB /* docs.md */; };
BCCD4869185009E80038ADCB /* LICENSE.txt in Resources */ = {isa = PBXBuildFile; fileRef = BCCD4864185009E80038ADCB /* LICENSE.txt */; };
BCCD486A185009E80038ADCB /* RegExCategories.m in Sources */ = {isa = PBXBuildFile; fileRef = BCCD4866185009E80038ADCB /* RegExCategories.m */; };
BCCD486B185009E80038ADCB /* readme.md in Resources */ = {isa = PBXBuildFile; fileRef = BCCD4867185009E80038ADCB /* readme.md */; };
Expand Down Expand Up @@ -74,7 +73,7 @@
BC7CC816184AF846007FCB6B /* NSString+IndexOf.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+IndexOf.m"; sourceTree = "<group>"; };
BC7CC818184AF963007FCB6B /* NSString+IsMatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+IsMatch.m"; sourceTree = "<group>"; };
BC7CC81A184AF9ED007FCB6B /* NSString+Initialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+Initialization.m"; sourceTree = "<group>"; };
BCCD4863185009E80038ADCB /* docs.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = docs.md; path = ../docs.md; sourceTree = "<group>"; };
BCCD4863185009E80038ADCB /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = readme.md; path = ../readme.md; sourceTree = "<group>"; };
BCCD4864185009E80038ADCB /* LICENSE.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = LICENSE.txt; path = ../LICENSE.txt; sourceTree = "<group>"; };
BCCD4865185009E80038ADCB /* RegExCategories.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RegExCategories.h; path = ../RegExCategories.h; sourceTree = "<group>"; };
BCCD4866185009E80038ADCB /* RegExCategories.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RegExCategories.m; path = ../RegExCategories.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -108,7 +107,7 @@
BC7CC7C61848F63D007FCB6B = {
isa = PBXGroup;
children = (
BCCD4863185009E80038ADCB /* docs.md */,
BCCD4863185009E80038ADCB /* readme.md */,
BCCD4864185009E80038ADCB /* LICENSE.txt */,
BCCD4865185009E80038ADCB /* RegExCategories.h */,
BCCD4866185009E80038ADCB /* RegExCategories.m */,
Expand All @@ -119,6 +118,7 @@
BC7CC7D01848F63D007FCB6B /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
BC7CC7D01848F63D007FCB6B /* Products */ = {
isa = PBXGroup;
Expand Down Expand Up @@ -236,7 +236,7 @@
BC7CC7C71848F63D007FCB6B /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0500;
LastUpgradeCheck = 0720;
ORGANIZATIONNAME = "Bendy Tree";
TargetAttributes = {
BC7CC7E91848F63E007FCB6B = {
Expand Down Expand Up @@ -270,7 +270,6 @@
BCCD4869185009E80038ADCB /* LICENSE.txt in Resources */,
BC7CC7DD1848F63E007FCB6B /* InfoPlist.strings in Resources */,
BCCD486B185009E80038ADCB /* readme.md in Resources */,
BCCD4868185009E80038ADCB /* docs.md in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -349,7 +348,6 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand All @@ -364,6 +362,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand All @@ -389,7 +388,6 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down Expand Up @@ -427,6 +425,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Objective-C-Regex-Categories/Objective-C-Regex-Categories-Prefix.pch";
INFOPLIST_FILE = "Objective-C-Regex-Categories/Objective-C-Regex-Categories-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.bendytree.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -440,6 +439,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Objective-C-Regex-Categories/Objective-C-Regex-Categories-Prefix.pch";
INFOPLIST_FILE = "Objective-C-Regex-Categories/Objective-C-Regex-Categories-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.bendytree.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -448,7 +448,6 @@
BC7CC7FF1848F63E007FCB6B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/Objective-C-Regex-Categories.app/Objective-C-Regex-Categories";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
Expand All @@ -462,6 +461,7 @@
"$(inherited)",
);
INFOPLIST_FILE = "Objective-C-Regex-CategoriesTests/Objective-C-Regex-CategoriesTests-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.bendytree.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand All @@ -471,7 +471,6 @@
BC7CC8001848F63E007FCB6B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_INCLUDING_64_BIT)";
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/Objective-C-Regex-Categories.app/Objective-C-Regex-Categories";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
Expand All @@ -481,6 +480,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Objective-C-Regex-Categories/Objective-C-Regex-Categories-Prefix.pch";
INFOPLIST_FILE = "Objective-C-Regex-CategoriesTests/Objective-C-Regex-CategoriesTests-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.bendytree.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0500"
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -37,10 +37,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -62,17 +62,21 @@
ReferencedContainer = "container:Objective-C-Regex-Categories.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BC7CC7CE1848F63D007FCB6B"
Expand All @@ -85,12 +89,13 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BC7CC7CE1848F63D007FCB6B"
Expand Down
4 changes: 0 additions & 4 deletions TestProject/Objective-C-Regex-Categories/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ @implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.bendytree.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ @implementation NSRegularExpression_IndexOf

- (void) test_indexOf_on_matching_string_returns_index_of_first_match
{
int i = [RX(@"\\d") indexOf:@"You 2 can have 3 cows."];
NSUInteger i = [RX(@"\\d") indexOf:@"You 2 can have 3 cows."];
XCTAssertEqual(i, 4, @"Expected to match index 4.");
}

- (void) test_indexOf_on_non_matching_string_returns_negative_one
{
int i = [RX(@"\\d") indexOf:@"You two can have three cows."];
XCTAssertEqual(i, -1, @"Expected to match index -1.");
NSUInteger i = [RX(@"\\d") indexOf:@"You two can have three cows."];
XCTAssertEqual(i, NSNotFound);
}

- (void) test_indexOf_on_matching_rx_returns_index_of_first_match
{
int i = [RX(@"\\d") indexOf:@"You 2 can have 3 cows."];
NSUInteger i = [RX(@"\\d") indexOf:@"You 2 can have 3 cows."];
XCTAssertEqual(i, 4, @"Expected to match index 4.");
}

Expand Down
Loading