Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added error reporting for OnigRegexp compile #4

Merged
merged 2 commits into from

2 participants

@griff

Added methods to OnigRegexp that take an NSError** that gets written to with the error information if the provided expression is invalid or could not be compiled.

Also reindented the tests with tabs instead of spaces since it is my impression you prefer tabs?

@psychs psychs merged commit 43a40f5 into psychs:master
@psychs
Owner

Cool. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 28, 2011
  1. @griff
Commits on Aug 11, 2011
  1. @griff
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.