| 1 |
/* |
| 2 |
** CWRegEx.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2004 Ludovic Marcotte, Francis Lachapelle |
| 5 |
** |
| 6 |
** Author: Francis Lachapelle <[email protected]> |
| 7 |
** Ludovic Marcotte <[email protected]> |
| 8 |
** |
| 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. |
| 13 |
** |
| 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. |
| 18 |
** |
| 19 |
** You should have received a copy of the GNU General Public License |
| 20 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <Pantomime/CWRegEx.h> |
| 24 |
|
| 25 |
#include <Pantomime/CWConstants.h> |
| 26 |
|
| 27 |
#import <Foundation/NSArray.h> |
| 28 |
#import <Foundation/NSValue.h> |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
|
| 33 |
// |
| 34 |
// |
| 35 |
// |
| 36 |
@implementation CWRegEx |
| 37 |
|
| 38 |
- (id) init |
| 39 |
{ |
| 40 |
return [self initWithPattern: @""]; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
// |
| 45 |
// |
| 46 |
// |
| 47 |
- (id) initWithPattern: (NSString *) thePattern |
| 48 |
{ |
| 49 |
return [self initWithPattern: thePattern flags: REG_EXTENDED]; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
// |
| 54 |
// |
| 55 |
// |
| 56 |
- (id) initWithPattern: (NSString *) thePattern |
| 57 |
flags: (int) theFlags |
| 58 |
{ |
| 59 |
int status; |
| 60 |
char *error; |
| 61 |
|
| 62 |
if ((self = [super init])) |
| 63 |
{ |
| 64 |
status = regcomp(&_re, [thePattern cString], theFlags); |
| 65 |
if (status != 0) |
| 66 |
{ |
| 67 |
error = malloc(255*sizeof(char)); |
| 68 |
regerror(status, &_re, error, 255); |
| 69 |
free(error); |
| 70 |
|
| 71 |
AUTORELEASE(self); |
| 72 |
self = nil; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return self; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
// |
| 81 |
// |
| 82 |
// |
| 83 |
+ (id) regexWithPattern: (NSString *) thePattern |
| 84 |
{ |
| 85 |
return AUTORELEASE([[self alloc] initWithPattern: thePattern]); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
// |
| 90 |
// |
| 91 |
// |
| 92 |
+ (id) regexWithPattern: (NSString *) thePattern |
| 93 |
flags: (int) theFlags |
| 94 |
{ |
| 95 |
return AUTORELEASE([[self alloc] initWithPattern: thePattern flags: theFlags]); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
// |
| 100 |
// |
| 101 |
// |
| 102 |
- (void)dealloc |
| 103 |
{ |
| 104 |
regfree(&_re); |
| 105 |
[super dealloc]; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// |
| 110 |
// |
| 111 |
// |
| 112 |
- (NSArray *) matchString: (NSString *) theString |
| 113 |
{ |
| 114 |
NSMutableArray *aMutableArray; |
| 115 |
|
| 116 |
int offset, status; |
| 117 |
char *s, *error; |
| 118 |
regmatch_t rm[1]; |
| 119 |
|
| 120 |
s = (char*)[theString lossyCString]; |
| 121 |
aMutableArray = [[NSMutableArray alloc] init]; |
| 122 |
|
| 123 |
status = regexec(&_re, s, 1, rm, 0); |
| 124 |
offset = 0; |
| 125 |
|
| 126 |
while (status == 0) |
| 127 |
{ |
| 128 |
NSValue *aValue; |
| 129 |
|
| 130 |
aValue = [NSValue valueWithRange: NSMakeRange(offset + rm[0].rm_so, rm[0].rm_eo - rm[0].rm_so)]; |
| 131 |
|
| 132 |
[aMutableArray addObject: aValue]; |
| 133 |
|
| 134 |
offset += rm[0].rm_eo; |
| 135 |
|
| 136 |
if (rm[0].rm_eo - rm[0].rm_so == 0) |
| 137 |
{ |
| 138 |
status = 1; |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
status = regexec(&_re, s + offset, 1, rm, REG_NOTBOL); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (status != REG_NOMATCH) |
| 147 |
{ |
| 148 |
error = malloc(255*sizeof(char)); |
| 149 |
regerror(status, &_re, error, 255); |
| 150 |
free(error); |
| 151 |
} |
| 152 |
|
| 153 |
return AUTORELEASE(aMutableArray); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
// |
| 158 |
// |
| 159 |
// |
| 160 |
+ (NSArray *) matchString: (NSString *) theString |
| 161 |
withPattern: (NSString *) thePattern |
| 162 |
isCaseSensitive: (BOOL) theBOOL |
| 163 |
{ |
| 164 |
int flags; |
| 165 |
CWRegEx *regex; |
| 166 |
NSArray *result; |
| 167 |
|
| 168 |
flags = REG_EXTENDED; |
| 169 |
|
| 170 |
if (!theBOOL) |
| 171 |
{ |
| 172 |
flags |= REG_ICASE; |
| 173 |
} |
| 174 |
|
| 175 |
if ((regex = [CWRegEx regexWithPattern: thePattern flags: flags]) == nil) |
| 176 |
{ |
| 177 |
result = [NSArray array]; |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
result = [regex matchString: theString]; |
| 182 |
} |
| 183 |
|
| 184 |
return result; |
| 185 |
} |
| 186 |
|
| 187 |
@end |