-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegExRoot.m
1072 lines (881 loc) · 43 KB
/
RegExRoot.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// RegExRoot.m
// RegExhibit
//
// Copyright 2007 Roger Jolly. All rights reserved.
//
// This is the model-class that does the basic work matching regular expressions and arranging the storage of their results.
// When requested to do a match, replacement or split, it will build a Perl-program and run it. The results will put into its
// instance variables.
// RegExRoot contains an array "matches" and an array "splits". The matches array contains for every match a RegExMatch object.
// A RegExMatch object consist of other objects with the number of the match, its starting and ending position, containing captures and
// the text used to replace the match, if appropriate.
// The splits array holds a NSString for each split.
// With the demo match this means:
// RegExMatch object --> Match 1 (from 306 to 330: the proper point of ...)
// RegExLabel object --> Matched text
// RegExText object --> the proper point of view
// RegExCapture object --> Capture 1 (from 310 to 316: proper)
// RegExText object --> proper
// The RegExLabel object for replacement text is empty
// The splits array is empty
//
#import "RegExRoot.h"
int const showLength = 20;
@implementation RegExRoot
- (id) init
{
self = [super init];
if (self != nil) {
matchSucceeded = FALSE;
encodingToUse = NSMacOSRomanStringEncoding;
allowCode = FALSE;
matchAll = FALSE;
doSplit = FALSE;
matches = [[NSMutableArray alloc]init];
splits = [[NSMutableArray alloc]init];
dummyText = FALSE;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// Register with notificationcenter to hear when match if finished,
[nc addObserver: self
selector: @selector(regExFinished:)
name: NSFileHandleReadToEndOfFileCompletionNotification
object: nil];
// or ended through an error or by being aborted.
[nc addObserver: self
selector: @selector(regExError:)
name: NSFileHandleReadCompletionNotification
object: nil];
}
return self;
}
#pragma mark-
#pragma mark Accessors
- (void) setAllowCode: (BOOL) aBool
// Only use if you want to allow runtime evaluation. Not recommended!
{
allowCode = aBool;
}
- (int) allowCode
{
return allowCode;
}
- (void) setEncodingToUse: (int) anEncoding
{
encodingToUse = anEncoding;
}
- (int) encodingToUse
{
return encodingToUse;
}
- (void) setMatchAll: (BOOL) aBOOL
{
matchAll = aBOOL;
}
- (BOOL) matchAll
{
return matchAll;
}
- (void) setTextToMatch: (NSString *) aString
{
// If there is no user provided text. set textToMatch to a dummy text to be able to check the validity of the regular expressions.
if ((aString == nil) || [aString isEqualToString:@""]) {
dummyText = TRUE;
[textToMatch release];
textToMatch = [[NSString alloc] initWithString: @" "];
} else {
dummyText = FALSE;
[aString retain];
[textToMatch release];
textToMatch = aString;
}
}
- (NSString *) textToMatch
{
return textToMatch;
}
- (void) setRegExModifiers: (NSSet *) modifiers
{
[modifiers retain];
[regExModifiers release];
regExModifiers = modifiers;
}
- (NSSet *) regExModifiers
{
return regExModifiers;
}
- (void) setMatchRegEx: (NSString *) aString
{
if (matchRegEx == aString) {
return;
}
[matchRegEx release];
// Process the regex, because for one reason or other, I cannot get Perl to recognize $ in embedded lines, (i.e. when using multiline mode).
// Therefore: walk through the regex and replace all $ with (?:$|(?=\\n^)), which means the same and gets processed correctly.
// This also could be used in future to do more pre-processing.
BOOL escapeFound = FALSE;
int i = 0;
int stringLength = [aString length] - 1; // strings are 0-based.
NSMutableString *currentChar = [[NSMutableString alloc] init];
NSMutableString *charBuffer = [[NSMutableString alloc] init];
NSMutableString *tempResult = [[NSMutableString alloc] init];
for (i = 0; i <= stringLength; i++) {
[charBuffer setString:@""];
[currentChar setString: [aString substringWithRange: NSMakeRange(i,1)]];
if (escapeFound) {
if ([currentChar isEqualToString:@"Q"]) { // Start quotemeta
[charBuffer appendString:@"\\"]; // Add skipped escape, currentChar will be added in the for loop.
for (i; (i < stringLength) && ![[aString substringWithRange: NSMakeRange(i,2)] isEqualToString: @"\\E"]; i++) {
[charBuffer appendString: [aString substringWithRange: NSMakeRange(i,1)]];
}
if (i < stringLength) { // Loop has been exited before length condition was true, therefore it must have ended on \E.
[charBuffer appendString: @"\\E"];
i++;
} else { // Add last character. i has been upped in the if-condition!
[charBuffer appendString: [aString substringWithRange: NSMakeRange(i,1)]];
}
} else { // Normal escape
[charBuffer appendString:@"\\"]; // Add skipped escape
[charBuffer appendString: [aString substringWithRange: NSMakeRange(i,1)]];
}
escapeFound = FALSE;
[tempResult appendString: charBuffer];
} else if ([currentChar isEqualToString:@"$"]) { // Found an unescaped $
[tempResult appendString: @"(?:$|(?=\\n^))"];
} else if ([currentChar isEqualToString:@"\\"]) { // Found unescaped escape character
escapeFound = TRUE; // Don't add escape. No reason not to now, but handy if we ever extend this routine in future.
} else {
[tempResult appendString: currentChar];
}
}
matchRegEx = [[NSString alloc] initWithString: tempResult];
[tempResult release];
[charBuffer release];
[currentChar release];
}
- (NSString *) matchRegEx
{
return matchRegEx;
}
- (void) setReplacementText: (NSString *) aString
{
[aString retain];
[replacementText release];
replacementText = aString;
}
- (NSString *) replacementText
{
return replacementText;
}
- (void) setRegExTask: (NSTask *) aTask
{
[aTask retain];
[regExTask release];
regExTask = aTask;
}
- (NSTask *) regExTask
{
return regExTask;
}
- (void) addMatchWithBeginPosition: (int) beginPosition endPosition: (int) endPosition
{
[matches addObject: [[RegExMatch alloc] initMatchNumber: [matches count] + 1
beginPosition: beginPosition
endPosition: endPosition]];
}
- (RegExMatch *) matchNumber: (int) matchNumber
{
return [matches objectAtIndex: matchNumber - 1];
}
- (NSString *) splitNumber: (int) splitNumber
{
return [splits objectAtIndex: splitNumber - 1];
}
- (void) setMatchSucceeded: (BOOL) anError
{
matchSucceeded = anError;
}
- (BOOL) matchSucceeded
{
return matchSucceeded;
}
- (BOOL) matchError
{
return ![self matchSucceeded];
}
- (void) setMatchFinished: (BOOL) aBOOL
{
matchFinished = aBOOL;
}
- (BOOL) matchFinished
{
return matchFinished;
}
- (void) setDoSplit: (BOOL) aBOOL
{
doSplit = aBOOL;
}
- (BOOL) doSplit
{
return doSplit;
}
#pragma mark-
#pragma mark Regular expression methods
- (void) matchText: (NSString *) matchText
toRegEx: (NSString *) regEx
modifiers: (NSSet *) modifiers
replacement: (NSString *) replaceString
allowCode: (BOOL) codeAllowed
{
[self setMatchFinished: FALSE]; // After this, we still might need to replace text.
// Save the variables, so the can be used later when possibly replacing.
[self setAllowCode: codeAllowed];
[self setTextToMatch: matchText];
[self setMatchRegEx: regEx];
[self setRegExModifiers: modifiers];
[self setReplacementText: replaceString];
// Make some assumptions about modifiers.
[self setEncodingToUse: NSMacOSRomanStringEncoding];
[self setMatchAll: FALSE];
// Change the modifiersset into a string and some instance variables.
NSMutableString *modifiersString = [[NSMutableString alloc] init];
[self modifiersToString: modifiersString];
// Build Perl program.
NSMutableString *matchProgram = [[NSMutableString alloc] init];
[self buildPerlProgram: matchProgram version: regExMatch modifiers: modifiersString];
// Assemble the input.
NSMutableString *programInput = [[NSMutableString alloc] initWithString: [self matchRegEx]]; // First get the regex to use.
[programInput appendString: @"\n\0\n"]; // The null-string is used as seperator;
// ignore the compiler warning.
[programInput appendString: [self textToMatch]]; // Add the text against which to match the regex.
// Match the text by running the program.
[self runPerlProgram: matchProgram withInput: programInput];
// Do some cleaning up.
[matchProgram release];
[programInput release];
[modifiersString release];
}
- (void) replaceInText: (NSString *) textToReplace
regEx: (NSString *) regEx
modifiers: (NSSet *) modifiers
replacement: (NSString *) replaceString
allowCode: (BOOL) codeAllowed
{
if ((replaceString == nil) || ([replaceString length] == 0)) { // If there is nothing to replace, set the replacement text
[self setMatchFinished: TRUE];
int matchNumber; // of all matches to an empty string.
for (matchNumber = 1; matchNumber <= [self numberOfMatches]; matchNumber++) {
[[self matchNumber: matchNumber] setReplacementText: @""];
[[self matchNumber: matchNumber] setMatchDrawn: NO];
}
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"RDJRegExFinished" object:@"matching"];
} else {
if ([self matchFinished]) { // matchFinished was set by previous match.
[self setEncodingToUse: NSMacOSRomanStringEncoding]; // Ignore, we came here direct from Controller.
[self setAllowCode: codeAllowed]; // This is the first time for this match (only replace has changed).
[self setMatchAll: FALSE];
[self setTextToMatch: textToReplace];
[self setMatchRegEx: regEx];
[self setRegExModifiers: modifiers];
[self setReplacementText: replaceString];
int matchNumber;
for (matchNumber = 1; matchNumber <= [self numberOfMatches]; matchNumber++) {
[[self matchNumber: matchNumber] setMatchDrawn: NO];
}
} else { // We came here from "replaceInText"
[self setMatchFinished: TRUE];
}
// Change the modifiersset into a string and some instance variables.
NSMutableString *modifiersString = [[NSMutableString alloc] init];
[self modifiersToString: modifiersString];
NSMutableString *programInput = [[NSMutableString alloc] initWithString: [self matchRegEx]]; // get the regex to use
[programInput appendString: @"\n\0\n"]; // null-string on purpose, ignore compiler warning
[programInput appendString: [self replacementText]];
[programInput appendString: @"\n\0\n"]; // null-string on purpose, ignore compiler warning
[programInput appendString: [self textToMatch]]; // add text to search
NSMutableString *matchProgram = [[NSMutableString alloc] init];
[self buildPerlProgram: matchProgram version: regExReplace modifiers: modifiersString];
[self runPerlProgram: matchProgram withInput: programInput];
[matchProgram release];
[programInput release];
[modifiersString release];
}
}
- (void) splitText: (NSString *) textToSplit
onRegEx: (NSString *) regEx
modifiers: (NSSet *) modifiers
allowCode: (BOOL) codeAllowed
{
if ([textToSplit length] == 0) { // With no text, no results,
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"RDJRegExFinished" object:@"splitting"];
return; // and no further processing needed.
}
[self setEncodingToUse: NSMacOSRomanStringEncoding];
[self setAllowCode: codeAllowed];
[self setMatchAll: FALSE];
[self setTextToMatch: textToSplit];
[self setMatchRegEx: regEx];
[self setRegExModifiers: modifiers];
[self setMatchFinished: TRUE];
[self setDoSplit: TRUE];
[self setReplacementText: nil];
// Change the modifiersset into a string and some instance variables.
NSMutableString *modifiersString = [[NSMutableString alloc] init];
[self modifiersToString: modifiersString];
// Build Perl program
NSMutableString *programInput = [[NSMutableString alloc] initWithString: [self matchRegEx]]; // get the regex to use
[programInput appendString: @"\n\0\n"]; // null-string on purpose, ignore compiler warning
[programInput appendString: [self textToMatch]]; // add text to search
NSMutableString *matchProgram = [[NSMutableString alloc] init];
[self buildPerlProgram: matchProgram version: regExSplit modifiers: modifiersString];
// Split text
[self runPerlProgram: matchProgram withInput: programInput];
[matchProgram release];
[programInput release];
[modifiersString release];
}
- (void) modifiersToString: (NSMutableString *) modifiersString
{
// Determine modifiers
NSEnumerator *enumerator;
NSString *modifierItem;
enumerator = [[self regExModifiers] objectEnumerator];
while (modifierItem = [enumerator nextObject]) {
switch ([modifierItem intValue]) {
case regExFindAll:
[self setMatchAll: TRUE];
break;
case regExCaseInsensitive:
[modifiersString appendString: @"i"];
break;
case regExWhiteSpace:
[modifiersString appendString: @"x"];
break;
case regExDotMatchNEwline:
[modifiersString appendString: @"s"];
break;
case regExMultiline:
[modifiersString appendString: @"m"];
break;
case regExUnicode:
[self setEncodingToUse: NSUTF8StringEncoding];
break;
default:
NSLog(@"Found unknown modifier %d\n",[modifierItem intValue]);
}
}
}
- (void) buildPerlProgram: (NSMutableString *) programString
version: (int) goal
modifiers: (NSMutableString *) modifiersString
{
// Create Perl program to match text. This program expects the regex to use in the match, followed by a null-string, followed by the text to match.
[programString appendString:@"use strict;"]; // Use strict processing.
[programString appendString:@"my $_m_;"]; // $_m_ for match.
if ([self encodingToUse] == NSUTF8StringEncoding) {
[programString appendString:@"binmode STDIN, \":utf8\";"]; // stdin will be in UTF-8
[programString appendString:@"binmode STDOUT, \":utf8\";"]; // stdout will be in UTF-8
}
if ([self allowCode]) {
[programString appendString:@"use re \'eval\';"];
// To prevent errors caused by the user using "print" redirect STDOUT
[programString appendString:@"open(SAVED_OUT,\">&STDOUT\");"]; // Save STDOUT
[programString appendString:@"open(STDOUT,\"> /dev/null\") or die;"]; // Redirect to /dev/null
}
[programString appendString:@"while (<>) {"]; // read each line of input
[programString appendString: @"if (/\\0/) {"]; // if you find a null-string
[programString appendString: @"last;"]; // exit the loop
[programString appendString: @"} else {"]; // otherwise
[programString appendString: @"$_m_ .= $_;"]; // add the line to the regex
[programString appendString: @"}"];
[programString appendString:@"}"]; // (end while)
[programString appendString:@"chomp ($_m_);"];
// trouble passing things like \u by pipe, therefore do it manually. (Escaped \'s are needed for compiler)
// First process \u or \l if followed by \E. Perl allows this, even though it doesn't make sense/
// Find all \u's possibly followed by \E and replace it with the appropriate text and remove the \E,
// unless that belongs to a previous \l, \L ,\u, \U or \Q.
[programString appendString:@"$_m_ =~ s/"]; // Match
[programString appendString: @"\\\\u"]; // \u (obj-c escaped)
[programString appendString: @"(.?)"]; // capture the following character ($1)
[programString appendString: @"("]; // capture ($2) the next
[programString appendString: @"(?:"]; // non matching group
[programString appendString: @"."]; // beginning with a character
[programString appendString: @"(?!\\\\"]; // if not followed by an escaped backslash
[programString appendString: @"(?:"]; // and one of a non matching group
[programString appendString: @"l|L|u|U|Q"]; // containing either l L u U or Q
[programString appendString: @")"]; // end non matching group
[programString appendString: @")"]; // end negative lookahead
[programString appendString: @")*?"]; // end non matching group
[programString appendString: @")"]; // end capture ($2)
[programString appendString: @"\\\\E"]; // until \E (obj-c escaped)
[programString appendString: @"/"]; // and replace it with
[programString appendString: @"\\u$1$2"]; // $1 in uppercase followed by $2
[programString appendString: @"/g;"];
// Find all \l's possibly followed by \E and replace it with the appropriate text and remove the \E,
// unless that belongs to a previous \l, \L ,\u, \U or \Q.
[programString appendString:@"$_m_ =~ s/"]; // Match
[programString appendString: @"\\\\l"]; // \u (obj-c escaped)
[programString appendString: @"(.?)"]; // capture the following character ($1)
[programString appendString: @"("]; // capture ($2) the next
[programString appendString: @"(?:"]; // non matching group
[programString appendString: @"."]; // beginning with a character
[programString appendString: @"(?!\\\\"]; // if not followed by an escaped backslash
[programString appendString: @"(?:"]; // and one of a non matching group
[programString appendString: @"l|L|u|U|Q"]; // containing either l L u U or Q
[programString appendString: @")"]; // end non matching group
[programString appendString: @")"]; // end negative lookahead
[programString appendString: @")*?"]; // end non matching group
[programString appendString: @")"]; // end capture ($2)
[programString appendString: @"\\\\E"]; // until \E (obj-c escaped)
[programString appendString: @"/"]; // and replace it with
[programString appendString: @"\\l$1$2"]; // $1 in uppercase followed by $2
[programString appendString: @"/g;"];
[programString appendString:@"$_m_ =~ s/\\\\u(.?)/\\u$1/g;"]; // Find all \u's without an associated \E and replace.
[programString appendString:@"$_m_ =~ s/\\\\l(.?)/\\l$1/g;"]; // Find all \l's without an associated \E and replace.
// Find all \Q's possibly followed by \E and replace it with the appropriate text,
[programString appendString:@"$_m_ =~ s/"]; // Match
[programString appendString: @"\\\\Q"]; // \Q (obj-c escaped)
[programString appendString: @"("]; // capture ($1)
[programString appendString: @".*?"]; // the following characters
[programString appendString: @")"]; // end capture
[programString appendString: @"(?:"]; // non matching group
[programString appendString: @"\\\\E|$"]; // until you find either \E (obj-c escaped) or the end of the string
[programString appendString: @")"]; // end non matching group
[programString appendString: @"/"]; // and replace it with
[programString appendString: @"\\Q$1\\E"]; // the appropriate text
[programString appendString: @"/g;"];
// Find all \U's possibly followed by \E and replace it with the appropriate text,
[programString appendString:@"$_m_ =~ s/"]; // Match
[programString appendString: @"\\\\U"]; // \U (obj-c escaped)
[programString appendString: @"("]; // capture ($1)
[programString appendString: @".*?"]; // the following characters
[programString appendString: @")"]; // end capture
[programString appendString: @"(?:"]; // non matching group
[programString appendString: @"\\\\E|$"]; // until you find either \E (obj-c escaped) or the end of the string
[programString appendString: @")"]; // end non matching group
[programString appendString: @"/"]; // and replace it with
[programString appendString: @"\\U$1\\E"]; // the appropriate text
[programString appendString: @"/g;"];
// Find all \L's possibly followed by \E and replace it with the appropriate text,
[programString appendString:@"$_m_ =~ s/"]; // Match
[programString appendString: @"\\\\L"]; // \L (obj-c escaped)
[programString appendString: @"("]; // capture ($1)
[programString appendString: @".*?"]; // the following characters
[programString appendString: @")"]; // end capture
[programString appendString: @"(?:"]; // non matching group
[programString appendString: @"\\\\E|$"]; // until you find either \E (obj-c escaped) or the end of the string
[programString appendString: @")"]; // end non matching group
[programString appendString: @"/"]; // and replace it with
[programString appendString: @"\\L$1\\E"]; // the appropriate text
[programString appendString: @"/g;"];
if (goal == regExReplace) {
[programString appendString:@"my $_s_;"]; // $_s_ for substitute.
[programString appendString:@"while (<>) {"]; // read each line of input
[programString appendString: @"if (/\\0/) {"]; // if you find a null-string
[programString appendString: @"last;"]; // exit the loop
[programString appendString: @"} else {"]; // otherwise
[programString appendString: @"$_s_ .= $_;"]; // add the line to the regex
[programString appendString: @"}"];
[programString appendString:@"}"]; // (end while)
[programString appendString:@"chomp ($_s_);"];
// trouble passing things like \u by pipe, therefore do it manually. (Escaped \'s are needed for compiler)
// For the substitution \u, \U, \l and \L shouldn't be changed, because we are feeding Perl a string it has to interpret. If we change them as we
// did for the match, the won't stay around long enough te be correctly used. Only processing \Q is needed.
// Find all \Q's possibly followed by \E and replace it with the appropriate text,
[programString appendString:@"$_s_ =~ s/"]; // Match
[programString appendString: @"\\\\Q"]; // \Q (obj-c escaped)
[programString appendString: @"("]; // capture ($1)
[programString appendString: @".*?"]; // the following characters
[programString appendString: @")"]; // end capture
[programString appendString: @"(?:"]; // non matching group
[programString appendString: @"\\\\E|$"]; // until you find either \E (obj-c escaped) or the end of the string
[programString appendString: @")"]; // end non matching group
[programString appendString: @"/"]; // and replace it with
[programString appendString: @"\\Q$1\\E"]; // the appropriate text
[programString appendString: @"/g;"];
}
[programString appendString:@"eval {\"\" =~ /$_m_/};"]; // is this a valid regex?
[programString appendString:@"if ($@) {"]; // if not, there will be an error-message ($@)
[programString appendString: @"warn \"no valid match\";"]; // mention there is an error
[programString appendString: @"while (<>) {};"]; // don't exit immediately, because there is still input in the pipe
[programString appendString: @"exit;"]; // ignore it and then exit
[programString appendString:@"}"];
[programString appendString:@"undef $/;"]; // valid regex, so ignore record seperator
[programString appendString:@"$_ = <>;"]; // so we can get the remaining input in one slurp
if ([self matchAll] && goal == regExMatch) { // For some reason, Perl -e & cocoa give an "out of memory" error
[programString appendString:@"my $_i_ = 1;"]; // when finding all and matching for example 18 ordinary letters,
// e.g. try "ordinary gentleman" as regex and text.
if ([modifiersString rangeOfString:@"x"].location == NSNotFound) {
[programString appendString:@"while ($_m_ =~ /(?<!\\\\)"]; // Match if there are is no \ (this is an escaped Obj-c Perl escaped \),
[programString appendString: @"(?:\\\\\\\\)*"]; // followed by an even number of \'s (again double escaping)
[programString appendString: @"("]; // capture
[programString appendString: @"\\("]; // a (
[programString appendString: @")"]; // end capture
[programString appendString: @"(?!\\?)/g"]; // unless followed by a ? (Perl escaped ?)
} else { // Freeflow mode, watch out for extra spaces.
[programString appendString:@"while ($_m_ =~ /(?<!\\\\)"]; // Match if there are is no \ (this is an escaped Obj-c Perl escaped \),
[programString appendString: @"(?:\\\\\\\\)*"]; // followed by an even number of \'s (again double escaping)
[programString appendString: @"\\s*"]; // perhaps followed by spaces
[programString appendString: @"("]; // capture
[programString appendString: @"\\s*"]; // perhaps followed by spaces
[programString appendString: @"\\("]; // a (
[programString appendString: @")"]; // end capture
[programString appendString: @"\\s*"]; // perhaps followed by spaces
[programString appendString: @"(?!\\?)/g"]; // unless followed by a ? (Perl escaped ?)
}
[programString appendString: modifiersString]; // By first finding how many captures there are and setting $_i_ to it,
[programString appendString:@") {"]; // this is prevented. Of course, this causes overhead,
[programString appendString:@"$_i_++;"]; // but that is better than an error.
[programString appendString:@"}"];
}
[programString appendString:@"$_m_ = qr/$_m_/"]; // quote regex
[programString appendString: modifiersString]; // add modifiers
[programString appendString: @";"];
switch (goal) {
case regExMatch:
if ([self matchAll]) {
[programString appendString:@"while (m/$_m_/g"]; // loop if all matches need to be found
} else {
[programString appendString:@"if (m/$_m_/"]; // or just once if there is a match
}
[programString appendString: @") {"]; // close
if ([self allowCode]) {
// Re-allow printing to STDOUT. Looping through this is probably costly, but necessary for safety.
[programString appendString:@"close(STDOUT) or die;"]; // Close redirected STDOUT
[programString appendString:@"open(STDOUT,\">&SAVED_OUT\") or die;"]; // Restore normal STDOUT
[programString appendString:@"close(SAVED_OUT) or die;"]; // Close to prevent memory leaks
}
[programString appendString: @"my $_t_=\"\";"]; // Perl doesn't capture undefined captures after the last defined capture
// this variable saves undefined captures until it is clear whether they
// are followed by a defined one.
[programString appendString: @"my $_j_;"]; // $_j_ counter.
if ([self matchAll] && goal == regExMatch) { // More prevention of per -e & cocoa problem from above
[programString appendString: @"for ($_j_= 0; $_j_ < $_i_; $_j_++) {"];
} else {
[programString appendString: @"for ($_j_= 0; $_j_ < @-; $_j_++) {"]; // for each (captured) match
}
[programString appendString: @"if (defined $-[$_j_]) {"]; // needed in case of undef
[programString appendString: @"print $_t_;"]; // print any undefined captures
[programString appendString: @"$_t_ = \"\";"]; // clear the temporary string for undefined captures
[programString appendString: @"print \"$-[$_j_]\\0$+[$_j_]\\0\";"]; // print them to STDOUT
[programString appendString: @"} elsif ($_j_ > 0) {"];
[programString appendString: @"$_t_ .= \"-1\\0-1\\0\";"]; // add placeholder to the temporary string for undefined captures
[programString appendString: @"}"];
[programString appendString: @"}"];
[programString appendString: @"print \"|\\0\";"]; // after each match, print a seperator
if ([self allowCode]) {
[programString appendString:@"open(SAVED_OUT,\">&STDOUT\");"]; // Save STDOUT again for match at the top of the loop
[programString appendString:@"open(STDOUT,\"> /dev/null\") or die;"]; // Redirect to /dev/null
}
[programString appendString:@"};"];
break;
case regExReplace:
// We need to check whether the replacement string is valid with a dummy text and match, because the eval on the set from the user
// will not give an error on the replacement string if the regex does not match.
// First set $_i_ to one more than the number of captures.
[programString appendString:@"my $_i_ = 0;"];
if ([modifiersString rangeOfString:@"x"].location == NSNotFound) {
[programString appendString:@"while ($_m_ =~ /(?<!\\\\)"]; // Match if there are is no \ (this is an escaped Obj-c Perl escaped \),
[programString appendString: @"(?:\\\\\\\\)*"]; // followed by an even number of \'s (again double escaping)
[programString appendString: @"("]; // capture
[programString appendString: @"\\("]; // a (
[programString appendString: @")"]; // end capture
[programString appendString: @"(?!\\?)/g"]; // unless followed by a ? (Perl escaped ?)
} else { // Freeflow mode, watch out for extra spaces.
[programString appendString:@"while ($_m_ =~ /(?<!\\\\)"]; // Match if there are is no \ (this is an escaped Obj-c Perl escaped \),
[programString appendString: @"(?:\\\\\\\\)*"]; // followed by an even number of \'s (again double escaping)
[programString appendString: @"\\s*"]; // perhaps followed by spaces
[programString appendString: @"("]; // capture
[programString appendString: @"\\s*"]; // perhaps followed by spaces
[programString appendString: @"\\("]; // a (
[programString appendString: @")"]; // end capture
[programString appendString: @"\\s*"]; // perhaps followed by spaces
[programString appendString: @"(?!\\?)/g"]; // unless followed by a ? (Perl escaped ?)
}
[programString appendString: modifiersString];
[programString appendString:@") {"];
[programString appendString:@"$_i_++;"];
[programString appendString:@"}"];
// Set $_i_ to a dummy match regex consisting of just empty captures. (Needed in case the replacementstring has backreferences.)
[programString appendString: @"$_i_ = \"()\" x $_i_;"];
// Set $_j_ to a dummy text.
[programString appendString: @"my $_j_ = \"test\";"];
[programString appendString:@"$_s_ = qq/\"\\0$_s_\\0\"/;"]; // Pad the replacementstring with null-strings for split
[programString appendString:@"eval {"]; // See if the regex is valid, with the dummy.
[programString appendString: @"$_j_ =~ s/$_i_/$_s_/ee;"];
[programString appendString:@"};"];
[programString appendString: @"if ($@) {"]; // if not, there will be an error-message ($@)
[programString appendString: @"die \"no valid match\";"]; // mention there is an error and exit.
[programString appendString: @"}"];
// If we get here, the replacement string is valid. No use it with the real text and regex.
[programString appendString:@"eval {"]; // See if the regex is valid, while doing the substitution.
[programString appendString: @"s/$_m_/$_s_/ee"];
if ([self matchAll]) {
[programString appendString: @"g"];
}
[programString appendString: @";"];
[programString appendString:@"};"];
[programString appendString: @"if ($@) {"]; // if not, there will be an error-message ($@)
[programString appendString: @"die \"no valid match\";"]; // mention there is an error and exit.
[programString appendString: @"}"];
[programString appendString:@"my @_sp_;"];
[programString appendString:@"@_sp_ = split(/\\0/,$_);"]; // split $_ to get the parts
if ([self allowCode]) {
// Re-allow printing to STDOUT. Looping through this is probably costly, but necessary for safety.
[programString appendString:@"close(STDOUT) or die;"]; // Close redirected STDOUT
[programString appendString:@"open(STDOUT,\">&SAVED_OUT\") or die;"]; // Restore normal STDOUT
[programString appendString:@"close(SAVED_OUT) or die;"]; // Close to prevent memory leaks
}
[programString appendString:@"for ($_i_ = 0; $_i_ <= $#_sp_; $_i_++) {"];
[programString appendString: @"if ($_i_%2 != 0) {"];
[programString appendString: @"print \"$_sp_[$_i_]\\0\";"]; // print the items seperated by a null-string to STDOUT
[programString appendString: @"}"];
[programString appendString:@"}"];
break;
case regExSplit:
[programString appendString:@"my @_sp_;"];
[programString appendString:@"@_sp_ = split(/$_m_/,$_);"]; // split $_
if ([self allowCode]) {
// Re-allow printing to STDOUT. Looping through this is probably costly, but necessary for safety.
[programString appendString:@"close(STDOUT) or die;"]; // Close redirected STDOUT
[programString appendString:@"open(STDOUT,\">&SAVED_OUT\") or die;"]; // Restore normal STDOUT
[programString appendString:@"close(SAVED_OUT) or die;"]; // Close to prevent memory leaks
}
[programString appendString:@"foreach (@_sp_) {"];
[programString appendString: @"if (defined $_) {"];
[programString appendString: @"s/\\\\1/\\\\1\\\\1/g;"];
[programString appendString: @"print \"$_\\0\";"]; // print the items seperated by a null-string to STDOUT
[programString appendString: @"} else {"];
[programString appendString: @"print \"\\1\\0\";"]; // print a placeholder followed by a null-string to STDOUT
[programString appendString: @"}"];
[programString appendString:@"}"];
break;
}
}
- (void) runPerlProgram: (NSString *) programString withInput: (NSString *) programInput
// The matching by Perl is done in a seperate thread to allow the user to break of the matching.
{
[self setMatchSucceeded: TRUE];
[self setRegExTask: [[NSTask alloc] init]];
[[self regExTask] setLaunchPath: @"/usr/bin/perl"];
NSPipe *readPipe = [NSPipe pipe];
NSFileHandle *readHandle = [readPipe fileHandleForReading];
NSPipe *writePipe = [NSPipe pipe];
NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
NSPipe *errorPipe = [NSPipe pipe];
NSFileHandle *errorHandle = [errorPipe fileHandleForReading];
[[self regExTask] setStandardInput: writePipe];
[[self regExTask] setStandardOutput: readPipe];
[[self regExTask] setStandardError: errorPipe];
[readHandle readToEndOfFileInBackgroundAndNotify];
[errorHandle readInBackgroundAndNotify];
NSArray *arguments = [NSArray arrayWithObjects: @"-w", @"-e", programString, nil];
[[self regExTask] setArguments: arguments];
[[self regExTask] launch];
[writeHandle writeData: [programInput dataUsingEncoding: [self encodingToUse]]];
[writeHandle closeFile];
}
- (void) abortMatching
{
if ([[self regExTask] isRunning]) {
[[self regExTask] terminate];
}
[self setMatchSucceeded:-1];
}
- (void) regExError: (NSNotification *) note
{
NSMutableData *data = [[note userInfo] objectForKey: NSFileHandleNotificationDataItem];
NSString *programErrorOutput = [[NSMutableString alloc] initWithData: data encoding: [self encodingToUse]];
if (![programErrorOutput isEqualToString:@""]) {
[self setMatchSucceeded:-1];
if ([[self regExTask] isRunning]) {
[[self regExTask] terminate];
}
}
// Unquote this to see if there is any ErrorOutput.
// NSMutableString *tempString = [NSMutableString stringWithString:programErrorOutput];
// [tempString replaceOccurrencesOfString:@"\0" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[tempString length])];
// NSLog(@"Error %@\n",tempString);
[programErrorOutput release];
}
- (void) regExFinished: (NSNotification *) note
{
NSMutableData *data = [[note userInfo] objectForKey: NSFileHandleNotificationDataItem];
NSString *programOutput;
if ([self matchSucceeded] == -1) {
[self setMatchSucceeded: FALSE];
programOutput = [[NSString alloc] initWithString:@""];
} else {
[self setMatchSucceeded:TRUE];
if (dummyText) {
programOutput = [[NSString alloc] initWithString: @""];
} else {
programOutput = [[NSString alloc] initWithData: data encoding: [self encodingToUse]];
}
}
// Unquote this to see the output of the Perl program.
// NSMutableString *tempString = [NSMutableString stringWithString:programOutput];
// [tempString replaceOccurrencesOfString:@"\0" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[tempString length])];
// NSLog(@"output %@\n",tempString);
if ([[self regExTask] isRunning]) {
[[self regExTask] terminate];
}
[[self regExTask] release];
if ([self matchFinished]) {
if ([self matchSucceeded]) {
if ([self doSplit]) {
[splits removeAllObjects];
[splits release];
splits = [[NSMutableArray alloc]init];
int i;
NSArray *tempArray = [[NSArray alloc]initWithArray:[programOutput componentsSeparatedByString:@"\0"]];
int numberOfItems = [tempArray count];
for (i = 0; i < numberOfItems; i++) {
NSMutableString *tempString = [[NSMutableString alloc]initWithString:[tempArray objectAtIndex:i]];
if ([tempString isEqualToString:@"\1"]) {
[splits addObject:@"\0"];
} else {
[tempString replaceOccurrencesOfString:@"\1\1" withString:@"\1" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[tempString length])];
[splits addObject:[[NSString alloc]initWithString:tempString]];
}
[tempString release];
}
[tempArray release];
[self setDoSplit: FALSE];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"RDJRegExFinished" object:@"splitting"];
} else { // We're not splitting, so we are replacing.
NSArray *replaceArray = [programOutput componentsSeparatedByString:@"\0"];
if (([replaceArray count] - 1) != [self numberOfMatches]) { // Every match should have a replacement text
[self setMatchSucceeded: FALSE];
programOutput = @"";
[self buildResultsWith: programOutput];
} else {
int matchNumber;
for (matchNumber = 1; matchNumber <= [self numberOfMatches]; matchNumber++) {
[[self matchNumber: matchNumber] setReplacementText: [replaceArray objectAtIndex: (matchNumber - 1)]];
}
}
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"RDJRegExFinished" object:@"replacing"];
}
} else { // Match didn't succeed.
programOutput = @"";
[self setMatchSucceeded: FALSE];
[self buildResultsWith: programOutput];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"RDJRegExFinished" object:@"with replace error"];
}
[programOutput release];
} else { // Still replacing to do.
[self buildResultsWith: programOutput];
[programOutput release];
if ([self matchError]) {
[self setReplacementText:nil]; // If there is an error, no need to go through replacing.
}
// Replace text if necessary, otherwise set the replacement text of the matches to nothing.
[self replaceInText: [self textToMatch] // Always go through replaceInText even if not replacing
regEx: [self matchRegEx] // to set the replacement text of matches to nothing.
modifiers: [self regExModifiers]
replacement: [self replacementText]
allowCode: [self allowCode]];
}
}
- (void) buildResultsWith: (NSString *) matchResults
{
if (![self matchSucceeded]) { // Match failed, just leave.
return;
}
[self clearSelf];
[self setMatchSucceeded:TRUE];
matches = [[NSMutableArray alloc]init];
splits = [[NSMutableArray alloc]init];
if ([[self textToMatch] length] == 0) { // With no text, no results,
return; // and no further processing needed.
}
if ([matchResults isEqualToString:@"|\0"] || ([matchResults length] == 0)) { // The result is an (effectively) empty string,
return; // no further processing needed.
}
NSArray *matchArray = [matchResults componentsSeparatedByString:@"\0"];
int i, beginPos, endPos;
int numberOfItems = [matchArray count] - 2;
for (i = -1 ; i < numberOfItems; i++){ // First item has no separator.
if (i == -1 || [[matchArray objectAtIndex: i] isEqualToString:@"|"]) {
// found a match, next item will be position at which the match starts, item after that position at which it ends
// Thanks to Brian Bergstrand (http://www.bergstrand.org/brian/) for solving why the Intel version would crash when using
// [self addMatchWithBeginPosition: [[matchArray objectAtIndex: ++i] intValue] endPosition: [[matchArray objectAtIndex: ++i] intValue]];
//
// WARNING: one of the problems with the use of pre/post operators: depending on the order of expression evaluation
// on Intel, the args are passed on the stack, not in registers so expression evaluation is not in the order written. Example:
// when i = -1, the endPostion index will be 0 and the beginPosition index will be 1 - the reverse of what the actual results are in the array.
// to get around this set the indexes to temp vars to force to them to be evaulted in the correct order no matter how args are passed
// in the current ABI
beginPos = [[matchArray objectAtIndex: ++i] intValue];
endPos = [[matchArray objectAtIndex: ++i] intValue];
[self addMatchWithBeginPosition: beginPos endPosition: endPos];
} else {
// item must be the starting position a captured match, the next item will be its end position. Add these to current match.
// Same problem, again thanks Brian for the solution.
// [[matches lastObject] addCaptureWithBeginPosition: [[matchArray objectAtIndex: i] intValue] endPosition: [[matchArray objectAtIndex: ++i] intValue]];
beginPos = [[matchArray objectAtIndex: i] intValue];
endPos = [[matchArray objectAtIndex: ++i] intValue];
[[matches lastObject] addCaptureWithBeginPosition: beginPos endPosition: endPos];
}
}
}