Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

NSSTring_RegEx: Add support for regular expressions to NSString

This is an extension found via Cocoadev, created by John R Chang. Thanks!
  • Loading branch information...
commit e79c11b9a1fc682ba694de878133768b9e149f2f 1 parent aa43e18
@pieter pieter authored
View
6 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 = "<group>"; };
F56244070E9684B0002B6C44 /* PBUnsortableTableHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBUnsortableTableHeader.h; sourceTree = "<group>"; };
F56244080E9684B0002B6C44 /* PBUnsortableTableHeader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBUnsortableTableHeader.m; sourceTree = "<group>"; };
+ F562C8850FE1766C000EC528 /* NSString_RegEx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSString_RegEx.h; sourceTree = "<group>"; };
+ F562C8860FE1766C000EC528 /* NSString_RegEx.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSString_RegEx.m; sourceTree = "<group>"; };
F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSFileHandleExt.m; sourceTree = "<group>"; };
F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSFileHandleExt.h; sourceTree = "<group>"; };
F56524EE0E02D45200F03B52 /* PBGitCommit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitCommit.h; sourceTree = "<group>"; };
@@ -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;
};
View
34 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 <Foundation/Foundation.h>
+
+/*
+ For regular expression help, see re_format(7) man page.
+*/
+
+@interface NSString (RegEx)
+
+/*
+ Common <options> are REG_ICASE and REG_NEWLINE. For other possible option flags,
+ see regex(3) man page. You don't need to specify REG_EXTENDED.
+
+ <nmatch> 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
View
95 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 <regex.h>
+
+
+@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<nmatch+1; i++)
+ {
+ if (pmatch[i].rm_so == -1 || pmatch[i].rm_eo == -1)
+ break;
+
+ NSRange range = NSMakeRange(pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
+ NSString * substring = [self substringWithRange:range];
+ [outMatches addObject:substring];
+
+ if (ranges)
+ {
+ NSValue * value = [NSValue valueWithRange:range];
+ [(NSMutableArray *)*ranges addObject:value];
+ }
+ }
+
+catch_error:
+ if (errcode != 0 && error)
+ {
+ // Construct error object
+ NSMutableDictionary * userInfo = [NSMutableDictionary dictionary];
+ char errbuf[256];
+ int len = regerror(errcode, &preg, errbuf, sizeof(errbuf));
+ if (len > 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
Please sign in to comment.
Something went wrong with that request. Please try again.