|
| 1 | +#import "DPLRegularExpression.h" |
| 2 | + |
| 3 | +static NSString * const DPLNamedGroupComponentPattern = @":[a-zA-Z0-9-_][^/]+"; |
| 4 | +static NSString * const DPLRouteParameterPattern = @":[a-zA-Z0-9-_]+"; |
| 5 | +static NSString * const DPLURLParameterPattern = @"([^/]+)"; |
| 6 | + |
| 7 | +@implementation DPLRegularExpression |
| 8 | + |
| 9 | ++ (instancetype)regularExpressionWithPattern:(NSString *)pattern { |
| 10 | + return [[self alloc] initWithPattern:pattern options:0 error:nil]; |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +- (instancetype)initWithPattern:(NSString *)pattern |
| 15 | + options:(NSRegularExpressionOptions)options |
| 16 | + error:(NSError *__autoreleasing *)error { |
| 17 | + |
| 18 | + NSString *cleanedPattern = [[self class] stringByRemovingNamedGroupsFromString:pattern]; |
| 19 | + |
| 20 | + self = [super initWithPattern:cleanedPattern options:options error:error]; |
| 21 | + if (self) { |
| 22 | + self.groupNames = [[self class] namedGroupsForString:pattern]; |
| 23 | + } |
| 24 | + return self; |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +- (DPLMatchResult *)matchResultForString:(NSString *)str { |
| 29 | + NSArray *matches = [self matchesInString:str options:0 range:NSMakeRange(0, str.length)]; |
| 30 | + DPLMatchResult *matchResult = [[DPLMatchResult alloc] init]; |
| 31 | + |
| 32 | + if (!matches.count) { |
| 33 | + return matchResult; |
| 34 | + } |
| 35 | + |
| 36 | + matchResult.match = YES; |
| 37 | + |
| 38 | + // Set route parameters in the routeParameters dictionary |
| 39 | + NSMutableDictionary *routeParameters = [NSMutableDictionary dictionary]; |
| 40 | + for (NSTextCheckingResult *result in matches) { |
| 41 | + // Begin at 1 as first range is the whole match |
| 42 | + for (NSInteger i = 1; i < result.numberOfRanges && i <= self.groupNames.count; i++) { |
| 43 | + NSString *parameterName = self.groupNames[i - 1]; |
| 44 | + NSString *parameterValue = [str substringWithRange:[result rangeAtIndex:i]]; |
| 45 | + routeParameters[parameterName] = parameterValue; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + matchResult.namedProperties = routeParameters; |
| 50 | + return matchResult; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +#pragma mark - Named Group Helpers |
| 55 | + |
| 56 | ++ (NSArray *)namedGroupTokensForString:(NSString *)str { |
| 57 | + NSRegularExpression *componentRegex = [NSRegularExpression regularExpressionWithPattern:DPLNamedGroupComponentPattern |
| 58 | + options:0 |
| 59 | + error:nil]; |
| 60 | + NSArray *matches = [componentRegex matchesInString:str |
| 61 | + options:0 |
| 62 | + range:NSMakeRange(0, str.length)]; |
| 63 | + |
| 64 | + NSMutableArray *namedGroupTokens = [NSMutableArray array]; |
| 65 | + for (NSTextCheckingResult *result in matches) { |
| 66 | + NSString *namedGroupToken = [str substringWithRange:result.range]; |
| 67 | + [namedGroupTokens addObject:namedGroupToken]; |
| 68 | + } |
| 69 | + return [NSArray arrayWithArray:namedGroupTokens]; |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | ++ (NSString *)stringByRemovingNamedGroupsFromString:(NSString *)str { |
| 74 | + NSString *modifiedStr = [str copy]; |
| 75 | + |
| 76 | + NSArray *namedGroupExpressions = [self namedGroupTokensForString:str]; |
| 77 | + NSRegularExpression *parameterRegex = [NSRegularExpression regularExpressionWithPattern:DPLRouteParameterPattern |
| 78 | + options:0 |
| 79 | + error:nil]; |
| 80 | + // For each of the named group expressions (including name & regex) |
| 81 | + for (NSString *namedExpression in namedGroupExpressions) { |
| 82 | + NSString *replacementExpression = [namedExpression copy]; |
| 83 | + NSTextCheckingResult *foundGroupName = [[parameterRegex matchesInString:namedExpression |
| 84 | + options:0 |
| 85 | + range:NSMakeRange(0, namedExpression.length)] firstObject]; |
| 86 | + // If it's a named group, remove the name |
| 87 | + if (foundGroupName) { |
| 88 | + NSString *stringToReplace = [namedExpression substringWithRange:foundGroupName.range]; |
| 89 | + replacementExpression = [replacementExpression stringByReplacingOccurrencesOfString:stringToReplace |
| 90 | + withString:@""]; |
| 91 | + } |
| 92 | + |
| 93 | + // If it was a named group, without regex constraining it, put in default regex |
| 94 | + if (replacementExpression.length == 0) { |
| 95 | + replacementExpression = DPLURLParameterPattern; |
| 96 | + } |
| 97 | + |
| 98 | + modifiedStr = [modifiedStr stringByReplacingOccurrencesOfString:namedExpression |
| 99 | + withString:replacementExpression]; |
| 100 | + } |
| 101 | + |
| 102 | + if (modifiedStr.length && !([modifiedStr characterAtIndex:0] == '/')) { |
| 103 | + modifiedStr = [@"^" stringByAppendingString:modifiedStr]; |
| 104 | + } |
| 105 | + modifiedStr = [modifiedStr stringByAppendingString:@"$"]; |
| 106 | + |
| 107 | + return modifiedStr; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | ++ (NSArray *)namedGroupsForString:(NSString *)str { |
| 112 | + NSMutableArray *groupNames = [NSMutableArray array]; |
| 113 | + |
| 114 | + NSArray *namedGroupExpressions = [self namedGroupTokensForString:str]; |
| 115 | + NSRegularExpression *parameterRegex = [NSRegularExpression regularExpressionWithPattern:DPLRouteParameterPattern |
| 116 | + options:0 |
| 117 | + error:nil]; |
| 118 | + |
| 119 | + for (NSString *namedExpression in namedGroupExpressions) { |
| 120 | + NSArray *componentMatches = [parameterRegex matchesInString:namedExpression |
| 121 | + options:0 |
| 122 | + range:NSMakeRange(0, namedExpression.length)]; |
| 123 | + NSTextCheckingResult *foundGroupName = [componentMatches firstObject]; |
| 124 | + if (foundGroupName) { |
| 125 | + NSString *stringToReplace = [namedExpression substringWithRange:foundGroupName.range]; |
| 126 | + NSString *variableName = [stringToReplace stringByReplacingOccurrencesOfString:@":" |
| 127 | + withString:@""]; |
| 128 | + |
| 129 | + [groupNames addObject:variableName]; |
| 130 | + } |
| 131 | + } |
| 132 | + return [NSArray arrayWithArray:groupNames]; |
| 133 | +} |
| 134 | + |
| 135 | +@end |
0 commit comments