From e79c11b9a1fc682ba694de878133768b9e149f2f Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Thu, 11 Jun 2009 18:50:06 +0100 Subject: [PATCH] NSSTring_RegEx: Add support for regular expressions to NSString This is an extension found via Cocoadev, created by John R Chang. Thanks! --- GitX.xcodeproj/project.pbxproj | 6 +++ NSString_RegEx.h | 34 +++++++++++++++ NSString_RegEx.m | 95 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 NSString_RegEx.h create mode 100644 NSString_RegEx.m diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index 40eb92d..b874424 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -59,6 +59,7 @@ F53FF2050E7ABB5300389171 /* PBGitRevSpecifier.m in Sources */ = {isa = PBXBuildFile; fileRef = F53FF2040E7ABB5300389171 /* PBGitRevSpecifier.m */; }; F56174570E058893001DCD79 /* PBGitTree.m in Sources */ = {isa = PBXBuildFile; fileRef = F56174560E058893001DCD79 /* PBGitTree.m */; }; F56244090E9684B0002B6C44 /* PBUnsortableTableHeader.m in Sources */ = {isa = PBXBuildFile; fileRef = F56244080E9684B0002B6C44 /* PBUnsortableTableHeader.m */; }; + F562C8870FE1766C000EC528 /* NSString_RegEx.m in Sources */ = {isa = PBXBuildFile; fileRef = F562C8860FE1766C000EC528 /* NSString_RegEx.m */; }; F56524BB0E02D22D00F03B52 /* NSFileHandleExt.m in Sources */ = {isa = PBXBuildFile; fileRef = F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */; }; F56524F00E02D45200F03B52 /* PBGitCommit.m in Sources */ = {isa = PBXBuildFile; fileRef = F56524EF0E02D45200F03B52 /* PBGitCommit.m */; }; F56526240E03D85900F03B52 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F56526230E03D85900F03B52 /* WebKit.framework */; }; @@ -213,6 +214,8 @@ F56174560E058893001DCD79 /* PBGitTree.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitTree.m; sourceTree = ""; }; F56244070E9684B0002B6C44 /* PBUnsortableTableHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBUnsortableTableHeader.h; sourceTree = ""; }; F56244080E9684B0002B6C44 /* PBUnsortableTableHeader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBUnsortableTableHeader.m; sourceTree = ""; }; + F562C8850FE1766C000EC528 /* NSString_RegEx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSString_RegEx.h; sourceTree = ""; }; + F562C8860FE1766C000EC528 /* NSString_RegEx.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSString_RegEx.m; sourceTree = ""; }; F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSFileHandleExt.m; sourceTree = ""; }; F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSFileHandleExt.h; sourceTree = ""; }; F56524EE0E02D45200F03B52 /* PBGitCommit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitCommit.h; sourceTree = ""; }; @@ -510,6 +513,8 @@ F5AD56780E79B78100EDAAFE /* PBCommitList.m */, F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */, F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */, + F562C8850FE1766C000EC528 /* NSString_RegEx.h */, + F562C8860FE1766C000EC528 /* NSString_RegEx.m */, F57CC38F0E05DDF2000472E2 /* PBEasyPipe.h */, F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */, F5DFFA6A0E075D8800617813 /* PBEasyFS.h */, @@ -842,6 +847,7 @@ 47DBDB580E94EDE700671A1E /* DBPrefsWindowController.m in Sources */, 47DBDB670E94EE8B00671A1E /* PBPrefsWindowController.m in Sources */, 47DBDBCA0E95016F00671A1E /* PBNSURLPathUserDefaultsTransfomer.m in Sources */, + F562C8870FE1766C000EC528 /* NSString_RegEx.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/NSString_RegEx.h b/NSString_RegEx.h new file mode 100644 index 0000000..784e9ff --- /dev/null +++ b/NSString_RegEx.h @@ -0,0 +1,34 @@ +// +// NSString_RegEx.h +// +// Created by John R Chang on 2005-11-08. +// This code is Creative Commons Public Domain. You may use it for any purpose whatsoever. +// http://creativecommons.org/licenses/publicdomain/ +// + +#import + +/* + For regular expression help, see re_format(7) man page. +*/ + +@interface NSString (RegEx) + +/* + Common are REG_ICASE and REG_NEWLINE. For other possible option flags, + see regex(3) man page. You don't need to specify REG_EXTENDED. + + is the number of subexpressions to match. + Returns an array of strings. The first string is the matching substring, + the remaining are the matching subexpressions, up to nmatch+1 number. + + If nmatch is -1, works like grep. Returns an array containing self if matching. + + Returns nil if regular expression does not match or if an error has occurred. +*/ +- (NSArray *) substringsMatchingRegularExpression:(NSString *)pattern count:(int)nmatch + options:(int)options ranges:(NSArray **)ranges error:(NSError **)error; + +- (BOOL) grep:(NSString *)pattern options:(int)options; + +@end diff --git a/NSString_RegEx.m b/NSString_RegEx.m new file mode 100644 index 0000000..ee0e9bd --- /dev/null +++ b/NSString_RegEx.m @@ -0,0 +1,95 @@ +// +// NSString_RegEx.m +// +// Created by John R Chang on 2005-11-08. +// This code is Creative Commons Public Domain. You may use it for any purpose whatsoever. +// http://creativecommons.org/licenses/publicdomain/ +// + +#import "NSString_RegEx.h" +#include + + +@implementation NSString (RegEx) + +- (NSArray *) substringsMatchingRegularExpression:(NSString *)pattern count:(int)nmatch options:(int)options ranges:(NSArray **)ranges error:(NSError **)error +{ + options |= REG_EXTENDED; + if (error) + *error = nil; + + int errcode = 0; + regex_t preg; + regmatch_t * pmatch = NULL; + NSMutableArray * outMatches = nil; + + // Compile the regular expression + errcode = regcomp(&preg, [pattern UTF8String], options); + if (errcode != 0) + goto catch_error; // regcomp error + + // Match the regular expression against substring self + pmatch = calloc(sizeof(regmatch_t), nmatch+1); + errcode = regexec(&preg, [self UTF8String], (nmatch<0 ? 0 : nmatch+1), pmatch, 0); + + /*if (errcode == REG_NOMATCH) + { + outMatches = [NSMutableArray array]; + goto catch_exit; // no match + }*/ + if (errcode != 0) + goto catch_error; // regexec error + + if (nmatch == -1) + { + outMatches = [NSArray arrayWithObject:self]; + goto catch_exit; // simple match + } + + // Iterate through pmatch + outMatches = [NSMutableArray array]; + if (ranges) + *ranges = [NSMutableArray array]; + int i; + for (i=0; i 0) + [userInfo setObject:[NSString stringWithUTF8String:errbuf] forKey:NSLocalizedDescriptionKey]; + *error = [NSError errorWithDomain:@"regerror" code:errcode userInfo:userInfo]; + } + +catch_exit: + if (pmatch) + free(pmatch); + regfree(&preg); + return outMatches; +} + +- (BOOL) grep:(NSString *)pattern options:(int)options +{ + NSArray * substrings = [self substringsMatchingRegularExpression:pattern count:-1 options:options ranges:NULL error:NULL]; + return (substrings && [substrings count] > 0); +} + +@end -- 2.11.4.GIT