This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
forked from bendytree/Objective-C-RegEx-Categories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegExCategories.m
297 lines (233 loc) · 8.93 KB
/
RegExCategories.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// RegExCategories.m
//
// https://github.com/bendytree/Objective-C-RegEx-Categories
//
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Josh Wright <@BendyTree>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import "RegExCategories.h"
@implementation NSRegularExpression (ObjectiveCRegexCategories)
- (id) initWithPattern:(NSString*)pattern
{
return [self initWithPattern:pattern options:0 error:nil];
}
+ (NSRegularExpression*) rx:(NSString*)pattern
{
return [[self alloc] initWithPattern:pattern];
}
+ (NSRegularExpression*) rx:(NSString*)pattern ignoreCase:(BOOL)ignoreCase
{
return [[self alloc] initWithPattern:pattern options:ignoreCase?NSRegularExpressionCaseInsensitive:0 error:nil];
}
+ (NSRegularExpression*) rx:(NSString*)pattern options:(NSRegularExpressionOptions)options
{
return [[self alloc] initWithPattern:pattern options:options error:nil];
}
- (BOOL) isMatch:(NSString*)matchee
{
return [self numberOfMatchesInString:matchee options:0 range:NSMakeRange(0, matchee.length)] > 0;
}
- (int) indexOf:(NSString*)matchee
{
NSRange range = [self rangeOfFirstMatchInString:matchee options:0 range:NSMakeRange(0, matchee.length)];
return range.location == NSNotFound ? -1 : (int)range.location;
}
- (NSArray*) split:(NSString *)str
{
NSRange range = NSMakeRange(0, str.length);
//get locations of matches
NSMutableArray* matchingRanges = [NSMutableArray array];
NSArray* matches = [self matchesInString:str options:0 range:range];
for(NSTextCheckingResult* match in matches) {
[matchingRanges addObject:[NSValue valueWithRange:match.range]];
}
//invert ranges - get ranges of non-matched pieces
NSMutableArray* pieceRanges = [NSMutableArray array];
//add first range
[pieceRanges addObject:[NSValue valueWithRange:NSMakeRange(0,
(matchingRanges.count == 0 ? str.length : [matchingRanges[0] rangeValue].location))]];
//add between splits ranges and last range
for(int i=0; i<matchingRanges.count; i++){
BOOL isLast = i+1 == matchingRanges.count;
unsigned long startLoc = [matchingRanges[i] rangeValue].location + [matchingRanges[i] rangeValue].length;
unsigned long endLoc = isLast ? str.length : [matchingRanges[i+1] rangeValue].location;
[pieceRanges addObject:[NSValue valueWithRange:NSMakeRange(startLoc, endLoc-startLoc)]];
}
//use split ranges to select pieces
NSMutableArray* pieces = [NSMutableArray array];
for(NSValue* val in pieceRanges) {
NSRange range = [val rangeValue];
NSString* piece = [str substringWithRange:range];
[pieces addObject:piece];
}
return pieces;
}
- (NSString*) replace:(NSString*)string with:(NSString*)replacement
{
return [self stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, string.length) withTemplate:replacement];
}
- (NSString*) replace:(NSString*)string withBlock:(NSString*(^)(NSString* match))replacer
{
//no replacer? just return
if (!replacer) return string;
//copy the string so we can replace subsections
NSMutableString* result = [string mutableCopy];
//get matches
NSArray* matches = [self matchesInString:string options:0 range:NSMakeRange(0, string.length)];
//replace each match (right to left so indexing doesn't get messed up)
for (int i=(int)matches.count-1; i>=0; i--) {
NSTextCheckingResult* match = matches[i];
NSString* matchStr = [string substringWithRange:match.range];
NSString* replacement = replacer(matchStr);
[result replaceCharactersInRange:match.range withString:replacement];
}
return result;
}
- (NSString*) replace:(NSString *)string withDetailsBlock:(NSString*(^)(RxMatch* match))replacer
{
//no replacer? just return
if (!replacer) return string;
//copy the string so we can replace subsections
NSMutableString* replaced = [string mutableCopy];
//get matches
NSArray* matches = [self matchesInString:string options:0 range:NSMakeRange(0, string.length)];
//replace each match (right to left so indexing doesn't get messed up)
for (int i=(int)matches.count-1; i>=0; i--) {
NSTextCheckingResult* result = matches[i];
RxMatch* match = [self resultToMatch:result original:string];
NSString* replacement = replacer(match);
[replaced replaceCharactersInRange:result.range withString:replacement];
}
return replaced;
}
- (NSArray*) matches:(NSString*)str
{
NSMutableArray* matches = [NSMutableArray array];
NSArray* results = [self matchesInString:str options:0 range:NSMakeRange(0, str.length)];
for (NSTextCheckingResult* result in results) {
NSString* match = [str substringWithRange:result.range];
[matches addObject:match];
}
return matches;
}
- (NSString*) firstMatch:(NSString*)str
{
NSTextCheckingResult* match = [self firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (!match) return nil;
return [str substringWithRange:match.range];
}
- (RxMatch*) resultToMatch:(NSTextCheckingResult*)result original:(NSString*)original
{
RxMatch* match = [[RxMatch alloc] init];
match.original = original;
match.range = result.range;
match.value = result.range.length ? [original substringWithRange:result.range] : nil;
//groups
NSMutableArray* groups = [NSMutableArray array];
match.groups = groups;
for(int i=0; i<result.numberOfRanges; i++){
RxMatchGroup* group = [[RxMatchGroup alloc] init];
group.range = [result rangeAtIndex:i];
group.value = group.range.length ? [original substringWithRange:group.range] : nil;
[groups addObject:group];
}
return match;
}
- (NSArray*) matchesWithDetails:(NSString*)str
{
NSMutableArray* matches = [NSMutableArray array];
NSArray* results = [self matchesInString:str options:0 range:NSMakeRange(0, str.length)];
for (NSTextCheckingResult* result in results) {
[matches addObject:[self resultToMatch:result original:str]];
}
return matches;
}
- (RxMatch*) firstMatchWithDetails:(NSString*)str
{
NSArray* results = [self matchesInString:str options:0 range:NSMakeRange(0, str.length)];
if (results.count == 0)
return nil;
return [self resultToMatch:results[0] original:str];
}
@end
@implementation NSString (ObjectiveCRegexCategories)
- (NSRegularExpression*) toRx
{
return [[NSRegularExpression alloc] initWithPattern:self];
}
- (NSRegularExpression*) toRxIgnoreCase:(BOOL)ignoreCase
{
return [NSRegularExpression rx:self ignoreCase:ignoreCase];
}
- (NSRegularExpression*) toRxWithOptions:(NSRegularExpressionOptions)options
{
return [NSRegularExpression rx:self options:options];
}
- (BOOL) isMatch:(NSRegularExpression*)rx
{
return [rx isMatch:self];
}
- (int) indexOf:(NSRegularExpression*)rx
{
return [rx indexOf:self];
}
- (NSArray*) split:(NSRegularExpression*)rx
{
return [rx split:self];
}
- (NSString*) replace:(NSRegularExpression*)rx with:(NSString*)replacement
{
return [rx replace:self with:replacement];
}
- (NSString*) replace:(NSRegularExpression *)rx withBlock:(NSString*(^)(NSString* match))replacer
{
return [rx replace:self withBlock:replacer];
}
- (NSString*) replace:(NSRegularExpression *)rx withDetailsBlock:(NSString*(^)(RxMatch* match))replacer
{
return [rx replace:self withDetailsBlock:replacer];
}
- (NSArray*) matches:(NSRegularExpression*)rx
{
return [rx matches:self];
}
- (NSString*) firstMatch:(NSRegularExpression*)rx
{
return [rx firstMatch:self];
}
- (NSArray*) matchesWithDetails:(NSRegularExpression*)rx
{
return [rx matchesWithDetails:self];
}
- (RxMatch*) firstMatchWithDetails:(NSRegularExpression*)rx
{
return [rx firstMatchWithDetails:self];
}
@end
@implementation RxMatch
@synthesize value, range, groups, original;
@end
@implementation RxMatchGroup
@synthesize value, range;
@end