Skip to content

Commit

Permalink
Create the regex object when the route is added
Browse files Browse the repository at this point in the history
This is faster than compiling the regex for every request.
  • Loading branch information
mattstevens committed Nov 22, 2011
1 parent 0609fd0 commit 9a46b50
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Source/Route.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@


@interface Route : NSObject {
NSString *path;
NSRegularExpression *regex;
RequestHandler handler;
id target;
SEL selector;
NSArray *keys;
}

@property (nonatomic, retain) NSString *path;
@property (nonatomic, retain) NSRegularExpression *regex;
@property (nonatomic, copy) RequestHandler handler;
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL selector;
Expand Down
4 changes: 2 additions & 2 deletions Source/Route.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

@implementation Route

@synthesize path;
@synthesize regex;
@synthesize handler;
@synthesize target;
@synthesize selector;
@synthesize keys;

- (void)dealloc {
self.path = nil;
self.regex = nil;
self.keys = nil;
self.handler = nil;
[super dealloc];
Expand Down
7 changes: 3 additions & 4 deletions Source/RoutingHTTPServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ - (Route *)routeWithPath:(NSString *)path {
path = [NSString stringWithFormat:@"^%@$", path_];
}

route.path = path;
route.regex = [NSRegularExpression regularExpressionWithPattern:path options:NSRegularExpressionCaseInsensitive error:nil];
if ([keys count] > 0) {
route.keys = keys;
}
Expand Down Expand Up @@ -222,12 +222,11 @@ - (RouteResponse *)routeMethod:(NSString *)method withPath:(NSString *)path para
for (Route *route in methodRoutes) {
// The first element in the captures array is all of the text matched by the regex.
// If there is nothing in the array the regex did not match.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:route.path options:NSRegularExpressionCaseInsensitive error:nil];
NSMutableArray *captures = [NSMutableArray array];
[regex enumerateMatchesInString:path options:NSMatchingReportCompletion range:NSMakeRange(0, path.length)
[route.regex enumerateMatchesInString:path options:NSMatchingReportCompletion range:NSMakeRange(0, path.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
if (result != nil && *stop != YES) {
for (NSUInteger i = 0; i <= regex.numberOfCaptureGroups; i++) {
for (NSUInteger i = 0; i <= route.regex.numberOfCaptureGroups; i++) {
[captures addObject:[path substringWithRange:[result rangeAtIndex:i]]];
}
}
Expand Down

0 comments on commit 9a46b50

Please sign in to comment.