Merge branch 'master' of ssh://github.com/gnustep/libs-ec

This commit is contained in:
Richard Frith-Macdonald 2019-02-11 15:46:13 +00:00
commit e7084020d4
2 changed files with 20 additions and 35 deletions

View file

@ -1,3 +1,8 @@
2019-02-11 Wolfgang Lux <wolfgang.lux@gmail.com>
* EcAlerter.m: Define Regex class as a simple wrapper around
NSRegularExpression.
2019-02-08 Wolfgang Lux <wolfgang.lux@gmail.com> 2019-02-08 Wolfgang Lux <wolfgang.lux@gmail.com>
* EcAlerter.m: Only ignore invalid rules in setRules: and update * EcAlerter.m: Only ignore invalid rules in setRules: and update

View file

@ -36,12 +36,9 @@
#import "EcAlerter.h" #import "EcAlerter.h"
#import "NSFileHandle+Printf.h" #import "NSFileHandle+Printf.h"
#include <regex.h>
@interface Regex: NSObject @interface Regex: NSObject
{ {
regex_t regex; NSRegularExpression *regex;
BOOL built;
} }
- (id) initWithString: (NSString*)pattern; - (id) initWithString: (NSString*)pattern;
- (NSString*) match: (NSString*)string; - (NSString*) match: (NSString*)string;
@ -50,50 +47,33 @@
@implementation Regex @implementation Regex
- (void) dealloc - (void) dealloc
{ {
if (built == YES) [regex release];
{
built = NO;
regfree(&regex);
}
[super dealloc]; [super dealloc];
} }
- (id) initWithString: (NSString*)pattern - (id) initWithString: (NSString*)pattern
{ {
if (regcomp(&regex, [pattern UTF8String], REG_EXTENDED) != 0) if ((self = [super init]) != nil)
{ {
NSLog(@"Failed to compile regex - '%@'", pattern); regex = [[NSRegularExpression alloc]
DESTROY(self); initWithPattern: pattern
} options: 0
else error: NULL];
{ if (regex == nil)
built = YES; {
NSLog(@"Failed to compile regex - '%@'", pattern);
DESTROY(self);
}
} }
return self; return self;
} }
- (NSString*) match: (NSString*)string - (NSString*) match: (NSString*)string
{ {
regmatch_t matches[2]; NSRange r = NSMakeRange(0, [string length]);
const char *str = [string UTF8String];
if (0 == str) r = [regex rangeOfFirstMatchInString: string options: 0 range: r];
{ return r.length != 0 ? [string substringWithRange: r] : nil;
return nil;
}
if (regexec(&regex, str, 1, matches, 0) != 0)
{
return nil;
}
else
{
int l = matches[0].rm_eo - matches[0].rm_so;
char b[l+1];
memcpy(b, &str[matches[0].rm_so], l);
b[l] = '\0';
return [NSString stringWithUTF8String: b];
}
} }
@end @end