4
** Copyright (c) 2001, 2002
6
** Author: Francis Lachapelle <francis@Sophos.ca>
7
** Ludovic Marcotte <ludovic@Sophos.ca>
9
** This library is free software; you can redistribute it and/or
10
** modify it under the terms of the GNU Lesser General Public
11
** License as published by the Free Software Foundation; either
12
** version 2.1 of the License, or (at your option) any later version.
14
** This library is distributed in the hope that it will be useful,
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
** Lesser General Public License for more details.
19
** You should have received a copy of the GNU Lesser General Public
20
** License along with this library; if not, write to the Free Software
21
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include <Pantomime/NSRegEx.h>
26
#include <Pantomime/Constants.h>
28
#import <Foundation/NSArray.h>
29
#import <Foundation/NSDebug.h>
30
#import <Foundation/NSValue.h>
34
@implementation NSRegEx
38
return [self initWithPattern:@""];
41
- (id) initWithPattern: (NSString *) thePattern
43
return [self initWithPattern:thePattern flags:REG_EXTENDED];
46
- (id) initWithPattern: (NSString *) thePattern
52
if ((self = [super init]))
54
status = regcomp(&re, [thePattern cString], theFlags);
57
error = malloc(255*sizeof(char));
58
regerror(status, &re, error, 255);
59
NSDebugLog(@"error: %s\n", error);
70
+ (id) regexWithPattern: (NSString *) thePattern
72
return AUTORELEASE([[self alloc] initWithPattern:thePattern]);
75
+ (id) regexWithPattern: (NSString *) thePattern
78
return AUTORELEASE([[self alloc] initWithPattern:thePattern flags:theFlags]);
87
- (NSArray *) matchString: (NSString *) theString
89
NSMutableArray *aMutableArray;
95
s = (char*)[theString lossyCString];
96
aMutableArray = [[NSMutableArray alloc] init];
98
status = regexec(&re, s, 1, rm, 0);
105
aValue = [NSValue valueWithRange: NSMakeRange(offset + rm[0].rm_so, rm[0].rm_eo - rm[0].rm_so)];
107
[aMutableArray addObject: aValue];
109
offset += rm[0].rm_eo;
111
if (rm[0].rm_eo - rm[0].rm_so == 0)
117
status = regexec(&re, s + offset, 1, rm, REG_NOTBOL);
121
if (status != REG_NOMATCH)
123
error = malloc(255*sizeof(char));
124
regerror(status, &re, error, 255);
125
NSDebugLog(@"error: %s\n", error);
129
return AUTORELEASE(aMutableArray);
132
+ (NSArray *) matchString: (NSString *) theString
133
withPattern: (NSString *) thePattern
134
isCaseSensitive: (BOOL) caseSensitive
140
flags = REG_EXTENDED;
142
if ( !caseSensitive )
147
if ( (regex = [NSRegEx regexWithPattern:thePattern flags:flags]) == nil )
149
result = [NSArray array];
153
result = [regex matchString:theString];