Permalink
Browse files

Implemented error reporting using NSError

  • Loading branch information...
1 parent 94a217b commit cd1f24f5eb7778ff5a94f294c75ee967b978651c @griff griff committed Aug 11, 2011
Showing with 68 additions and 5 deletions.
  1. +5 −0 framework/core/OnigRegexp.h
  2. +48 −5 framework/core/OnigRegexp.m
  3. +15 −0 framework/tests/OnigRegexpTest.m
@@ -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;
@@ -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;
}
@@ -184,4 +184,19 @@ - (void)testReplaceAll
#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 cd1f24f

Please sign in to comment.