-
Notifications
You must be signed in to change notification settings - Fork 54
/
CSRegex.m
160 lines (127 loc) · 3.64 KB
/
CSRegex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// CSRegex.m
//
// http://www.cocoadev.com/index.pl?CSRegex
//
#import "CSRegex.h"
static NSString *nullstring=nil;
@implementation CSRegex
- (id) initWithPattern:(NSString *)pattern options:(int)options
{
if (self=[super init]) {
int err=regcomp(&preg,[pattern UTF8String],options|REG_EXTENDED);
if (err) {
char errbuf[256];
regerror(err,&preg,errbuf,sizeof(errbuf));
[NSException raise:@"CSRegexException"
format:@"Could not compile regex \"%@\": %s",pattern,errbuf];
}
}
return self;
}
- (void) dealloc
{
regfree(&preg);
[super dealloc];
}
- (BOOL) matchesString:(NSString *)string
{
if (regexec(&preg,[string UTF8String],0,NULL,0) == 0)
return YES;
return NO;
}
- (NSString *) matchedSubstringOfString:(NSString *)string
{
const char *cstr=[string UTF8String];
regmatch_t match;
if (regexec(&preg,cstr,1,&match,0) == 0) {
return [[[NSString alloc] initWithBytes:cstr+match.rm_so
length:match.rm_eo-match.rm_so encoding:NSUTF8StringEncoding] autorelease];
}
return nil;
}
- (NSArray *) capturedSubstringsOfString:(NSString *)string
{
const char *cstr = [string UTF8String];
int num = preg.re_nsub+1;
regmatch_t *matches = calloc(sizeof(regmatch_t),num);
if (regexec(&preg,cstr,num,matches,0) == 0) {
NSMutableArray *array=[NSMutableArray arrayWithCapacity:num];
int i;
for (i=0;i<num;i++) {
NSString *str;
if(matches[i].rm_so == -1 && matches[i].rm_eo == -1)
str=nullstring;
else
str=[[[NSString alloc] initWithBytes:cstr+matches[i].rm_so
length:matches[i].rm_eo-matches[i].rm_so encoding:NSUTF8StringEncoding] autorelease];
[array addObject:str];
}
free(matches);
return [NSArray arrayWithArray:array];
}
free(matches);
return nil;
}
+ (CSRegex *) regexWithPattern:(NSString *)pattern options:(int)options
{
return [[[CSRegex alloc] initWithPattern:pattern options:options] autorelease];
}
+ (CSRegex *) regexWithPattern:(NSString *)pattern
{
return [[[CSRegex alloc] initWithPattern:pattern options:0] autorelease];
}
+ (NSString *) null
{
return nullstring;
}
+ (void) initialize
{
if (!nullstring)
nullstring=[[NSString alloc] initWithString:@""];
}
@end
@implementation NSString (CSRegex)
- (BOOL) matchedByPattern:(NSString *)pattern options:(int)options
{
CSRegex *re=[CSRegex regexWithPattern:pattern options:options|REG_NOSUB];
return [re matchesString:self];
}
- (BOOL) matchedByPattern:(NSString *)pattern
{
return [self matchedByPattern:pattern options:0];
}
- (NSString *) substringMatchedByPattern:(NSString *)pattern options:(int)options
{
CSRegex *re=[CSRegex regexWithPattern:pattern options:options];
return [re matchedSubstringOfString:self];
}
- (NSString *) substringMatchedByPattern:(NSString *)pattern
{
return [self substringMatchedByPattern:pattern options:0];
}
- (NSArray *) substringsCapturedByPattern:(NSString *)pattern options:(int)options
{
CSRegex *re=[CSRegex regexWithPattern:pattern options:options];
return [re capturedSubstringsOfString:self];
}
- (NSArray *) substringsCapturedByPattern:(NSString *)pattern
{
return [self substringsCapturedByPattern:pattern options:0];
}
- (NSString *) escapedPattern
{
int len=[self length];
NSMutableString *escaped=[NSMutableString stringWithCapacity:len];
int i;
for (i=0;i<len;i++) {
unichar c=[self characterAtIndex:i];
if(c=='^'||c=='.'||c=='['||c=='$'||c=='('||c==')'
||c=='|'||c=='*'||c=='+'||c=='?'||c=='{'||c=='\\')
[escaped appendFormat:@"\\%C",c];
else
[escaped appendFormat:@"%C",c];
}
return [NSString stringWithString:escaped];
}
@end