-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegExMatch.m
195 lines (148 loc) · 4.98 KB
/
RegExMatch.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
//
// RegExMatch.m
// RegExhibit
//
// Copyright 2007 Roger Jolly. All rights reserved.
//
// A RegExMatch object holds all the information needed to describe a match made by a regular expression. It had instance variables that deal with
// the text matched, the text that is used to replace the match, (if any), the captures inside the match and the number of the match. Most of these
// variables are other objects that hold more specfic information. Both the text of the match and the replacement text are RegExLabel-objects. The
// captures are held inside an array consisting of RegExCapture objects.
//
#import "RegExMatch.h"
int const showLengthMatch = 20;
@implementation RegExMatch
#pragma mark-
#pragma mark Initialisation
- (id) init {
self = [super init];
if (self != nil) {
replacementText = [[RegExLabel alloc]init];
matchedText = [[RegExLabel alloc]init];
captures = [[NSMutableArray alloc]init];
[self setMatchDrawn: NO];
[self setMatchNumber: -1];
}
return self;
}
- (id) initMatchNumber: (int) aNumber beginPosition: (int) beginPosition endPosition: (int) endPosition {
self = [super init];
if (self != nil) {
replacementText = [[RegExLabel alloc]init];
matchedText = [[RegExLabel alloc]init];
captures = [[NSMutableArray alloc]init];
[self setMatchNumber: aNumber];
[self setMatchDrawn: NO];
[self setBeginPosition: beginPosition endPosition: endPosition];
}
return self;
}
#pragma mark-
#pragma mark Accessors
- (void) setBeginPosition: (int) beginPosition {
[matchedText setBeginPosition: beginPosition];
}
- (int) beginPosition {
return [matchedText beginPosition];
}
- (void) setEndPosition: (int) endPosition {
[matchedText setEndPosition: endPosition];
}
- (int) endPosition {
return [matchedText endPosition];
}
- (void) setBeginPosition: (int) beginPosition endPosition: (int) endPosition {
[self setBeginPosition: beginPosition];
[self setEndPosition: endPosition];
}
- (void) setMatchNumber: (int) aNumber {
matchNumber = aNumber;
}
- (int) matchNumber {
return matchNumber;
}
- (void) setMatchDrawn: (BOOL) aBool {
matchDrawn = aBool;
}
- (BOOL) matchDrawn {
return matchDrawn;
}
- (NSString *) matchedTextWithSource: (NSString *) sourceText {
NSString *returnString;
returnString = [[NSString alloc]initWithString: [sourceText substringWithRange: [self range]]];
[returnString autorelease];
return returnString;
}
- (void) addCaptureWithBeginPosition: (int) beginPosition endPosition: (int) endPosition {
[captures addObject: [[RegExCapture alloc]initCaptureNumber: [captures count] + 1
beginPosition: beginPosition
endPosition: endPosition]];
}
- (RegExCapture *) captureNumber: (int) captureNumber {
return [captures objectAtIndex: captureNumber - 1];
}
- (void) setReplacementText: (NSString *) aString {
[replacementText setTheText: aString];
}
- (NSString *) replacementText {
return [replacementText theText];
}
#pragma mark-
#pragma mark Other methods
- (NSString *) displayInOutlineWithSource: (NSString *) sourceText {
// Use when object is to be called from a NSOutlineView-datasource.
NSString *returnString;
NSMutableString *tempString = [NSMutableString string];
[tempString appendFormat: NSLocalizedString(@"Matchnumber&positions", nil), [self matchNumber], [self beginPosition], [self endPosition]];
if (([self endPosition] - [self beginPosition]) <= showLengthMatch) {
[tempString appendFormat: @"%@)", [sourceText substringWithRange: [self range]]];
} else {
[tempString appendFormat: @"%@ ...)", [sourceText substringWithRange:NSMakeRange([self beginPosition], showLengthMatch)]];
}
// If there is a newline in the string, get rid of it and the text following.
NSRange tempRange = [tempString rangeOfString:@"\n"];
if (tempRange.location != NSNotFound) {
[tempString replaceCharactersInRange:NSMakeRange(tempRange.location,[tempString length] - tempRange.location) withString:@" ...)"];
}
returnString = [[NSString alloc] initWithString: tempString];
[returnString autorelease];
return returnString;
}
- (int) numberOfCaptures {
return [captures count];
}
- (int) numberOfChildrenShowingReplacements: (BOOL) showReplacements {
// Use when object is to be called from a NSOutlineView-datasource.
if (showReplacements) {
return [captures count] + 2;
} else {
return [captures count] + 1;
}
}
- (id) child: (int) index {
if (index == 0) { // item 0 is always matchedText
return matchedText;
} else if (index == [captures count] + 1) { // if there is an item beyond the number of captures, it is the replacementText
return replacementText;
} else {
return [self captureNumber: index];
}
}
- (NSRange) range {
return [matchedText range];
}
#pragma mark-
#pragma mark Cleaning up
- (void) dealloc {
NSEnumerator *enumerator;
id capturedItem;
enumerator = [captures objectEnumerator];
while (capturedItem = [enumerator nextObject]) {
[capturedItem release];
}
[captures release];
[matchedText release];
[replacementText release];
[super dealloc];
}
@end