Skip to content
Browse files

Merge pull request #4 from griff/master

Added error reporting for OnigRegexp compile
  • Loading branch information...
2 parents cc0ed03 + cd1f24f commit 43a40f5ff09c4607538151f833ba09078381e706 @psychs committed Aug 11, 2011
Showing with 101 additions and 38 deletions.
  1. +5 −0 framework/core/OnigRegexp.h
  2. +48 −5 framework/core/OnigRegexp.m
  3. +48 −33 framework/tests/OnigRegexpTest.m
View
5 framework/core/OnigRegexp.h
@@ -34,10 +34,15 @@ typedef enum {
}
+ (OnigRegexp*)compile:(NSString*)expression;
++ (OnigRegexp*)compile:(NSString*)expression error:(NSError **)error;
+ (OnigRegexp*)compileIgnorecase:(NSString*)expression;
++ (OnigRegexp*)compileIgnorecase:(NSString*)expression error:(NSError **)error;
+ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline;
++ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline error:(NSError **)error;
+ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline extended:(BOOL)extended;
++ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline extended:(BOOL)extended error:(NSError **)error;
+ (OnigRegexp*)compile:(NSString*)expression options:(OnigOption)options;
++ (OnigRegexp*)compile:(NSString*)expression options:(OnigOption)options error:(NSError **)error;
- (OnigResult*)search:(NSString*)target;
- (OnigResult*)search:(NSString*)target start:(int)start;
View
53 framework/core/OnigRegexp.m
@@ -49,31 +49,64 @@ - (void)finalize
+ (OnigRegexp*)compile:(NSString*)expression
{
- return [self compile:expression ignorecase:NO multiline:NO extended:NO];
+ return [self compile:expression ignorecase:NO multiline:NO extended:NO error:NULL];
+}
+
++ (OnigRegexp*)compile:(NSString*)expression error:(NSError **)error
+{
+ return [self compile:expression ignorecase:NO multiline:NO extended:NO error:error];
}
+ (OnigRegexp*)compileIgnorecase:(NSString*)expression
{
- return [self compile:expression ignorecase:YES multiline:NO extended:NO];
+ return [self compile:expression ignorecase:YES multiline:NO extended:NO error:NULL];
+}
+
++ (OnigRegexp*)compileIgnorecase:(NSString*)expression error:(NSError **)error
+{
+ return [self compile:expression ignorecase:YES multiline:NO extended:NO error:error];
}
+ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline
{
- return [self compile:expression ignorecase:ignorecase multiline:multiline extended:NO];
+ return [self compile:expression ignorecase:ignorecase multiline:multiline extended:NO error:NULL];
+}
+
++ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline error:(NSError **)error
+{
+ return [self compile:expression ignorecase:ignorecase multiline:multiline extended:NO error:NULL];
}
+ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline extended:(BOOL)extended
{
+ return [self compile:expression ignorecase:ignorecase multiline:multiline extended:extended error:NULL];
+}
+
++ (OnigRegexp*)compile:(NSString*)expression ignorecase:(BOOL)ignorecase multiline:(BOOL)multiline extended:(BOOL)extended error:(NSError **)error
+{
OnigOption options = OnigOptionNone;
options |= multiline ? OnigOptionMultiline : OnigOptionSingleline;
if(ignorecase) options |= OnigOptionIgnorecase;
if(extended) options |= OnigOptionExtend;
- return [self compile:expression options:options];
+ return [self compile:expression options:options error:error];
}
+ (OnigRegexp*)compile:(NSString*)expression options:(OnigOption)theOptions
{
- if (!expression) return nil;
+ return [self compile:expression options:theOptions error:NULL];
+}
+
++ (OnigRegexp*)compile:(NSString*)expression options:(OnigOption)theOptions error:(NSError **)error
+{
+ if (!expression) {
+ if(error != NULL) {
+ //Make NSError;
+ NSDictionary* dict = [NSDictionary dictionaryWithObject:@"Invalid expression argument"
+ forKey:NSLocalizedDescriptionKey];
+ *error = [NSError errorWithDomain:@"CocoaOniguruma" code:ONIG_NORMAL userInfo:dict];
+ }
+ return nil;
+ }
OnigOptionType option = theOptions;
@@ -93,6 +126,16 @@ + (OnigRegexp*)compile:(NSString*)expression options:(OnigOption)theOptions
return [[[self alloc] initWithEntity:entity expression:expression] autorelease];
}
else {
+ if(error != NULL) {
+ //Make NSError;
+ UChar str[ONIG_MAX_ERROR_MESSAGE_LEN];
+ onig_error_code_to_str(str, status, &err);
+ NSString* errorStr = [NSString stringWithCString:(char*)str
+ encoding:NSASCIIStringEncoding];
+ NSDictionary* dict = [NSDictionary dictionaryWithObject:errorStr
+ forKey:NSLocalizedDescriptionKey];
+ *error = [NSError errorWithDomain:@"CocoaOniguruma" code:status userInfo:dict];
+ }
if (entity) onig_free(entity);
return nil;
}
View
81 framework/tests/OnigRegexpTest.m
@@ -102,21 +102,21 @@ - (void)testSplit
- (NSString *)succReplace:(OnigResult *)res
{
- unichar ch[2];
- ch[0] = [[res body] characterAtIndex:0] + 1;
- ch[1] = ' ';
- return [NSString stringWithCharacters:ch length:2];
+ unichar ch[2];
+ ch[0] = [[res body] characterAtIndex:0] + 1;
+ ch[1] = ' ';
+ return [NSString stringWithCharacters:ch length:2];
}
- (NSString *)describeReplace:(OnigResult *)res
{
- NSString* body = [res body];
- return [NSString stringWithFormat:@"%@[%@]", [body class], body];
+ NSString* body = [res body];
+ return [NSString stringWithFormat:@"%@[%@]", [body class], body];
}
- (NSString *)xReplace:(OnigResult *)res
{
- return @"x";
+ return @"x";
}
- (void)testReplace
@@ -134,21 +134,21 @@ - (void)testReplace
#if defined(NS_BLOCKS_AVAILABLE)
STAssertEqualObjects([@"hello" replaceByRegexp:@"." withBlock:^(OnigResult* res) {
- unichar ch[2];
- ch[0] = [[res body] characterAtIndex:0] + 1;
- ch[1] = ' ';
- return (NSString *)[NSString stringWithCharacters:ch length:2];
- }], @"i ello", nil);
-
- NSString* actual = [@"hello!" replaceByRegexp:@"(.)(.)" withBlock:^(OnigResult* res) {
- NSString* body = [res body];
- return (NSString *)[NSString stringWithFormat:@"%@[%@]", [body class], body];
- }];
+ unichar ch[2];
+ ch[0] = [[res body] characterAtIndex:0] + 1;
+ ch[1] = ' ';
+ return (NSString *)[NSString stringWithCharacters:ch length:2];
+ }], @"i ello", nil);
+
+ NSString* actual = [@"hello!" replaceByRegexp:@"(.)(.)" withBlock:^(OnigResult* res) {
+ NSString* body = [res body];
+ return (NSString *)[NSString stringWithFormat:@"%@[%@]", [body class], body];
+ }];
STAssertEqualObjects(actual, @"NSCFString[he]llo!", nil);
-
+
STAssertEqualObjects([@"hello" replaceByRegexp:@"l" withBlock:^(OnigResult* res) {
- return @"x";
- }], @"hexlo", nil);
+ return @"x";
+ }], @"hexlo", nil);
#endif
}
@@ -166,22 +166,37 @@ - (void)testReplaceAll
#if defined(NS_BLOCKS_AVAILABLE)
STAssertEqualObjects([@"hello" replaceAllByRegexp:@"." withBlock:^(OnigResult* res) {
- unichar ch[2];
- ch[0] = [[res body] characterAtIndex:0] + 1;
- ch[1] = ' ';
- return (NSString *)[NSString stringWithCharacters:ch length:2];
- }], @"i f m m p ", nil);
-
- NSString* actual = [@"hello!" replaceAllByRegexp:@"(.)(.)" withBlock:^(OnigResult* res) {
- NSString* body = [res body];
- return (NSString *)[NSString stringWithFormat:@"%@[%@]", [body class], body];
- }];
+ unichar ch[2];
+ ch[0] = [[res body] characterAtIndex:0] + 1;
+ ch[1] = ' ';
+ return (NSString *)[NSString stringWithCharacters:ch length:2];
+ }], @"i f m m p ", nil);
+
+ NSString* actual = [@"hello!" replaceAllByRegexp:@"(.)(.)" withBlock:^(OnigResult* res) {
+ NSString* body = [res body];
+ return (NSString *)[NSString stringWithFormat:@"%@[%@]", [body class], body];
+ }];
STAssertEqualObjects(actual, @"NSCFString[he]NSCFString[ll]NSCFString[o!]", nil);
-
+
STAssertEqualObjects([@"hello" replaceAllByRegexp:@"l" withBlock:^(OnigResult* res) {
- return @"x";
- }], @"hexxo", nil);
+ return @"x";
+ }], @"hexxo", nil);
#endif
}
+- (void)testError
+{
+ NSError *error = NULL;
+ id ret = [OnigRegexp compileIgnorecase:nil error:&error];
+ STAssertNil(ret, @"Parsed expression");
+ STAssertEquals([error code], (NSInteger)ONIG_NORMAL, @"Wrong error code");
+ STAssertEqualObjects([error localizedDescription], @"Invalid expression argument", nil);
+
+ error = NULL;
+ ret = [OnigRegexp compileIgnorecase:@"(?<openb>\\[)?year(?(<openb>)\\])" error:&error];
+ STAssertNil(ret, @"Parsed expression");
+ STAssertEquals([error code], (NSInteger)ONIGERR_UNDEFINED_GROUP_OPTION, @"Wrong error code");
+ STAssertEqualObjects([error localizedDescription], @"undefined group option", nil);
+}
+
@end

0 comments on commit 43a40f5

Please sign in to comment.
Something went wrong with that request. Please try again.