Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for retrieving named capture names.

  • Loading branch information...
commit 1de41091e5bb0e0adb5312243127933e8703ccff 1 parent 6317965
@shadowphoenix shadowphoenix authored
Showing with 24 additions and 0 deletions.
  1. +2 −0  framework/core/OnigRegexp.h
  2. +22 −0 framework/core/OnigRegexp.m
View
2  framework/core/OnigRegexp.h
@@ -57,6 +57,7 @@ typedef enum {
OnigRegexp* _expression;
OnigRegion* _region;
NSString* _target;
+ NSMutableArray* _captureNames;
}
- (NSString*)target;
@@ -75,6 +76,7 @@ typedef enum {
- (NSString*)postMatch;
// named capture support
+- (NSArray*) captureNames;
- (int)indexForName:(NSString*)name;
- (NSIndexSet*)indexesForName:(NSString*)name;
- (NSString*)stringForName:(NSString*)name;
View
22 framework/core/OnigRegexp.m
@@ -16,6 +16,9 @@
@interface OnigResult (Private)
- (id)initWithRegexp:(OnigRegexp*)expression region:(OnigRegion*)region target:(NSString*)target;
+
+- (NSMutableArray*) captureNameArray;
+
@end
@@ -179,6 +182,7 @@ - (id)initWithRegexp:(OnigRegexp*)expression region:(OnigRegion*)region target:(
_expression = [expression retain];
_region = region;
_target = [target copy];
+ _captureNames = [NSMutableArray array];
}
return self;
}
@@ -256,6 +260,24 @@ - (NSString*)postMatch
return [_target substringFromIndex:[self locationAt:0] + [self lengthAt:0]];
}
+- (NSMutableArray*) captureNameArray {
+ return self->_captureNames;
+}
+
+
+// Used to get list of names
+int co_name_callback(const OnigUChar *name, const OnigUChar *end, int ngroups, int *group_list, OnigRegex re, void *arg) {
+ OnigResult *result = (OnigResult *)arg;
+
+ [[result captureNameArray] addObject:[NSString stringWithUTF8String:(const char*)name]];
+ return 0;
+}
+
+- (NSArray*) captureNames {
+ onig_foreach_name([self->_expression entity],co_name_callback,self);
+ return [NSArray arrayWithArray:self->_captureNames];
+}
+
- (int)indexForName:(NSString*)name
{
NSIndexSet* indexes = [self indexesForName:name];
Please sign in to comment.
Something went wrong with that request. Please try again.