-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegEx.cpp
More file actions
1593 lines (1289 loc) · 46.1 KB
/
RegEx.cpp
File metadata and controls
1593 lines (1289 loc) · 46.1 KB
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
/* -----------------------------------------------------------------
TEdit Regular Expression Library
This module implements regular expression and glob matching.
Michael T. Duffy
----------------------------------------------------------------- */
// RegEx.cpp
// Copyright 2004 Michael T. Duffy.
// contact: mduffor@gmail.com
// This file is part of TEdit.
//
// TEdit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// TEdit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TEdit. If not, see <http://www.gnu.org/licenses/>.
//
#include "RegEx.hpp"
#include "RStr.hpp"
#include <cctype>
#include <cstdlib>
//#include <cstdio>
#include <cstring>
RStr operator* (const RStr & strIn, RegEx & regexIn) {return (regexIn.Match (strIn));};
RStr operator* (RegEx & regexIn, const RStr & strIn) {return (regexIn.Match (strIn));};
//#define DEBUGMSG
//------------------------------------------------------------------------
// RegExMatch
//------------------------------------------------------------------------
//------------------------------------------------------------------------
RegExMatch::RegExMatch ()
{
eMatchType = kChar;
uStartMatch = 0x20;
uEndMatch = 0x20;
bInvertMatch = FALSE;
iNextOne = 0;
iNextTwo = 0;
iMinMatches = -1;
iMaxMatches = -1;
iNumMatches = 0;
bParsingError = FALSE;
};
//------------------------------------------------------------------------
RegExMatch::RegExMatch (EType eMatchTypeIn,
UINT uMatchStartIn,
UINT uMatchEndIn,
INT iNextOneIn,
INT iNextTwoIn,
BOOL bInvertMatchIn)
{
eMatchType = eMatchTypeIn;
uStartMatch = uMatchStartIn;
uEndMatch = uMatchEndIn;
iNextOne = iNextOneIn;
iNextTwo = iNextTwoIn;
bInvertMatch = bInvertMatchIn;
bParsingError = FALSE;
};
//------------------------------------------------------------------------
RegExMatch::RegExMatch (const RegExMatch & matchIn)
{
eMatchType = matchIn.eMatchType;
uStartMatch = matchIn.uStartMatch;
uEndMatch = matchIn.uEndMatch;
bInvertMatch = matchIn.bInvertMatch;
iNextOne = matchIn.iNextOne;
iNextTwo = matchIn.iNextTwo;
iMinMatches = matchIn.iMinMatches;
iMaxMatches = matchIn.iMaxMatches;
iNumMatches = matchIn.iNumMatches;
bParsingError = matchIn.bParsingError;
};
//------------------------------------------------------------------------
BOOL RegExMatch::IsMatch (const char * pchIn,
const char * pchBufferStart,
const char * pchBufferEnd)
{
#ifdef DEBUGMSG
printf ("IsMatch checking character %c\n", *pchIn);
#endif
if (bInvertMatch)
{
#ifdef DEBUGMSG
printf ("InvertedMatch\n");
#endif
return (! RawMatch (pchIn, pchBufferStart, pchBufferEnd));
}
else
{
return (RawMatch (pchIn, pchBufferStart, pchBufferEnd));
};
};
//------------------------------------------------------------------------
BOOL RegExMatch::RawMatch (const char * pchIn,
const char * pchBufferStart,
const char * pchBufferEnd)
{
if (pchIn == pchBufferEnd)
{
// end of buffer. Only line end matches
return (eMatchType == kLineEnd);
};
switch (eMatchType)
{
case kAnyChar: {return TRUE;};
case kChar: {
#ifdef DEBUGMSG
printf ("matching %c on char %c\n", UINT (*pchIn), uStartMatch);
#endif
return (UINT (*pchIn) == uStartMatch);};
case kCharRange: {return ((UINT (*pchIn) >= uStartMatch) &&
(UINT (*pchIn) <= uEndMatch));};
case kAlnum: {return (isalnum (int (*pchIn)));};
case kAlpha: {return (isalpha (int (*pchIn)));};
case kBlank: {return ((*pchIn == ' ') || (*pchIn == '\t'));};
case kCntrl: {return (iscntrl (int (*pchIn)));};
case kDigits: {return (isdigit (int (*pchIn)));};
case kGraph: {return (isgraph (int (*pchIn)));};
case kLower: {return (islower (int (*pchIn)));};
case kPrint: {return (isprint (int (*pchIn)));};
case kPunct: {return (ispunct (int (*pchIn)));};
case kSpace: {return (isspace (int (*pchIn)));};
case kUpper: {return (isupper (int (*pchIn)));};
case kXDigit: {return (isxdigit (int (*pchIn)));};
case kWordBoundary: {return ( (! IsWordChar (pchIn - 1) && IsWordChar (pchIn) ) ||
( IsWordChar (pchIn - 1) && ! IsWordChar (pchIn) ) );};
case kWordInside: {return (IsWordChar (pchIn - 1) && IsWordChar (pchIn));};
case kWordBegin: {return (! IsWordChar (pchIn - 1) && IsWordChar (pchIn) );};
case kWordEnd: {return ( IsWordChar (pchIn - 1) && ! IsWordChar (pchIn) );};
case kWordChar: {return (IsWordChar (pchIn));};
case kNotWordChar: {return (! IsWordChar (pchIn));};
case kBufferStart: {return (pchIn == pchBufferStart);};
case kBufferEnd: {return (*pchIn == '\0');}; break;
case kLineStart: {return ((pchIn == pchBufferStart) || (*(pchIn - 1) == '\n'));};
case kLineEnd: {return ((*pchIn == '\0') || (*pchIn == '\n'));};
default: break;
};
return (FALSE);
};
//------------------------------------------------------------------------
BOOL RegExMatch::IncOnMatch (VOID)
{
if (bInvertMatch)
{
// negative matches occur in a list. We want to process these as a non-incrementing
// sequence of character matches, rather than a branching tree of matches.
return (FALSE);
}
switch (eMatchType)
{
case kAnyChar:
case kChar:
case kCharRange:
case kAlnum:
case kAlpha:
case kBlank:
case kCntrl:
case kDigits:
case kGraph:
case kLower:
case kPrint:
case kPunct:
case kSpace:
case kUpper:
case kXDigit:
return (TRUE);
case kWordBoundary:
case kWordInside:
case kWordBegin:
case kWordEnd:
case kWordChar:
case kNotWordChar:
case kBufferStart:
case kBufferEnd:
case kLineStart:
case kLineEnd:
return (FALSE);
default:
break;
};
return (TRUE);
};
//------------------------------------------------------------------------
BOOL RegExMatch::IsWordChar (const char * pchIn)
{
// returns true if the character is word-constituent, false if not.
// Although some GNU RegEx libraries allow one to define a syntax table
// that defines what is part of a word and what isn't I am hard coding
// to the default value. Letters, numbers, and the underscore are
// word-constituent characters.
if (isalpha (int (*pchIn)) ||
isdigit (int (*pchIn)) ||
(*pchIn == '_'))
{
return (TRUE);
};
return (FALSE);
};
//------------------------------------------------------------------------
BOOL RegExMatch::MatchOnLineEnd (VOID)
{
return ((eMatchType == kLineEnd) ||
(eMatchType == kWordEnd) ||
(eMatchType == kWordBoundary));
};
//------------------------------------------------------------------------
BOOL RegExMatch::GetType (const char * pszPatternIn,
BOOL bInList,
INT & iSizeMatchedOut)
{
// try to match each type
eMatchType = kNull;
#ifdef DEBUGMSG
printf ("GetType %s \n", pszPatternIn);
#endif
// Make sure you are within a list bracket to match the following
if (bInList)
{
if (strncmp (pszPatternIn, "[:alnum:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kAlnum; }
else if (strncmp (pszPatternIn, "[:alpha:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kAlpha; }
else if (strncmp (pszPatternIn, "[:blank:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kBlank; }
else if (strncmp (pszPatternIn, "[:cntrl:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kCntrl; }
else if (strncmp (pszPatternIn, "[:digits:]", 10) == 0) {iSizeMatchedOut = 10; eMatchType = kDigits;}
else if (strncmp (pszPatternIn, "[:graph:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kGraph; }
else if (strncmp (pszPatternIn, "[:lower:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kLower; }
else if (strncmp (pszPatternIn, "[:print:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kPrint; }
else if (strncmp (pszPatternIn, "[:punct:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kPunct; }
else if (strncmp (pszPatternIn, "[:space:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kSpace; }
else if (strncmp (pszPatternIn, "[:upper:]", 9) == 0) {iSizeMatchedOut = 9; eMatchType = kUpper; }
else if (strncmp (pszPatternIn, "[:xdigit:]", 10) == 0) {iSizeMatchedOut = 10; eMatchType = kXDigit;}
// find chars that are valid in a list, but not outside of it.
else if ((*pszPatternIn == '|') ||
(*pszPatternIn == '{') ||
(*pszPatternIn == '}') ||
(*pszPatternIn == '?') ||
(*pszPatternIn == '+') ||
(*pszPatternIn == '*'))
{
iSizeMatchedOut = 1;
uStartMatch = *(pszPatternIn);
eMatchType = kChar;
}
else if (isalnum (*pszPatternIn) &&
(*(pszPatternIn + 1) == '-') &&
isalnum (*(pszPatternIn + 2)))
{
uStartMatch = *(pszPatternIn);
uEndMatch = *(pszPatternIn + 2);
if (uStartMatch > uEndMatch)
{
// range values are out-of-order, larger to smaller.
bParsingError = TRUE;
};
iSizeMatchedOut = 3;
eMatchType = kCharRange;
};
}
else
{
};
// check the chars that appear inside or outside of lists
if (*pszPatternIn == '.') {iSizeMatchedOut = 1; eMatchType = kAnyChar;}
else if (strncmp (pszPatternIn, "\\b", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kWordBoundary;}
else if (strncmp (pszPatternIn, "\\B", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kWordInside ;}
else if (strncmp (pszPatternIn, "\\<", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kWordBegin ;}
else if (strncmp (pszPatternIn, "\\>", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kWordEnd ;}
else if (strncmp (pszPatternIn, "\\w", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kWordChar ;}
else if (strncmp (pszPatternIn, "\\W", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kNotWordChar ;}
else if (strncmp (pszPatternIn, "\\`", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kBufferStart ;}
else if (strncmp (pszPatternIn, "\\'", 2) == 0) {iSizeMatchedOut = 2; eMatchType = kBufferEnd ;}
else if (*pszPatternIn == '\\')
{
#ifdef DEBUGMSG
printf ("Escaping character %c\n", *(pszPatternIn + 1));
#endif
// escaped char
iSizeMatchedOut = 2;
uStartMatch = *(pszPatternIn + 1);
eMatchType = kChar;
}
else if (strncmp (pszPatternIn, "^", 1) == 0) {iSizeMatchedOut = 1; eMatchType = kLineStart ;}
else if (strncmp (pszPatternIn, "$", 1) == 0) {iSizeMatchedOut = 1; eMatchType = kLineEnd ;}
else if ((*pszPatternIn != '(') &&
(*pszPatternIn != ')') &&
(*pszPatternIn != '|') &&
(*pszPatternIn != '[') &&
(*pszPatternIn != ']') &&
(*pszPatternIn != '{') &&
(*pszPatternIn != '}') &&
(*pszPatternIn != '?') &&
(*pszPatternIn != '+') &&
(*pszPatternIn != '*') &&
(eMatchType == kNull))
{
iSizeMatchedOut = 1;
uStartMatch = *(pszPatternIn);
eMatchType = kChar;
};
#ifdef DEBUGMSG
printf ("eMatchType found to be %s for %s \n", GetTypeText(), pszPatternIn);
#endif
return (eMatchType != kNull);
};
//------------------------------------------------------------------------
const char * RegExMatch::GetTypeText (VOID)
{
switch (eMatchType)
{
case kNull: return "Null";
case kOr: return "Or";
case kRepeat: return "Repeat";
case kAnyChar: return (bInvertMatch) ? "NotAnyChar" : "AnyChar";
case kChar: return (bInvertMatch) ? "NotChar" : "Char";
case kCharRange: return (bInvertMatch) ? "NotCharRange" : "CharRange";
case kAlnum: return (bInvertMatch) ? "NotAlnum" : "Alnum";
case kAlpha: return (bInvertMatch) ? "NotAlpha" : "Alpha";
case kBlank: return (bInvertMatch) ? "NotBlank" : "Blank";
case kCntrl: return (bInvertMatch) ? "NotCntrl" : "Cntrl";
case kDigits: return (bInvertMatch) ? "NotDigits" : "Digits";
case kGraph: return (bInvertMatch) ? "NotGraph" : "Graph";
case kLower: return (bInvertMatch) ? "NotLower" : "Lower";
case kPrint: return (bInvertMatch) ? "NotPrint" : "Print";
case kPunct: return (bInvertMatch) ? "NotPunct" : "Punct";
case kSpace: return (bInvertMatch) ? "NotSpace" : "Space";
case kUpper: return (bInvertMatch) ? "NotUpper" : "Upper";
case kXDigit: return (bInvertMatch) ? "NotXDigit" : "XDigit";
case kWordBoundary: return "WordBoundary";
case kWordInside: return "WordInside";
case kWordBegin: return "WordBegin";
case kWordEnd: return "WordEnd";
case kWordChar: return "WordChar";
case kNotWordChar: return "NotWordChar";
case kBufferStart: return "BufferStart";
case kBufferEnd: return "BufferEnd";
case kLineStart: return "LineStart";
case kLineEnd: return "LineEnd";
default:
break;
};
return ("");
};
//------------------------------------------------------------------------
// RegExDeque
//------------------------------------------------------------------------
// Note: This is a bit of a hack job, but should be faster to implement
// than a full deque class. Essentially instead of allocating
// elements that contain an index, a next pointer, and a prev pointer,
// I'm using three parallel arrays. Additionally I'm setting the
// first three values as sentinels for the queue head, queue tail,
// and pool of unallocated entries. Since there will be a lot of
// allocation and deallocation, a pool is useful for speed.
//------------------------------------------------------------------------
RegExDeque::RegExDeque ()
{
// clearing will initialize everything.
Clear ();
};
//------------------------------------------------------------------------
RegExDeque::~RegExDeque ()
{
};
//------------------------------------------------------------------------
VOID RegExDeque::InitSentinels (VOID)
{
// Allocate and initialize the head and tail sentinels
iHead = GetFromPool ();
iTail = GetFromPool ();
aiNext [iHead] = iTail;
aiPrev [iHead] = iTail;
aiNext [iTail] = iHead;
aiPrev [iTail] = iHead;
};
//------------------------------------------------------------------------
VOID RegExDeque::Grow (INT iGrowSizeIn)
{
INT iNewStart = aiValues.Length ();
// allocate new entries
aiValues.Insert (iNewStart, iGrowSizeIn);
aiNext. Insert (iNewStart, iGrowSizeIn);
aiPrev. Insert (iNewStart, iGrowSizeIn);
// setup the Next pointer to link all the allocations together
for (INT iIndex = 0; iIndex < iGrowSizeIn; ++iIndex)
{
aiNext [iNewStart + iIndex] = iNewStart + iIndex + 1;
};
// add the new allocations to the pool
aiNext [iNewStart + iGrowSizeIn - 1] = iPool;
iPool = iNewStart;
};
//------------------------------------------------------------------------
INT RegExDeque::GetFromPool (VOID)
{
if (iPool == -1)
{
// empty pool. Allocate more entries
Grow (50);
};
INT iReturn = iPool;
iPool = aiNext [iPool];
return (iReturn);
};
//------------------------------------------------------------------------
VOID RegExDeque::ReturnToPool (INT iIndexIn)
{
aiNext [iIndexIn] = iPool;
iPool = iIndexIn;
};
//------------------------------------------------------------------------
VOID RegExDeque::Push (INT iIn)
{
// push to head of queue
INT iNew = GetFromPool ();
aiValues [iNew] = iIn;
aiNext [iNew] = aiNext [iHead];
aiPrev [iNew] = iHead;
aiNext [iHead] = iNew;
aiPrev [aiNext [iNew]] = iNew;
};
//------------------------------------------------------------------------
INT RegExDeque::Pop (VOID)
{
// pop from head of queue
INT iReturnValue;
INT iPopIndex;
if (IsEmpty ())
{
// empty queue
return (-1);
}
iPopIndex = aiNext [iHead];
aiPrev [aiNext [iPopIndex]] = iHead;
aiNext [iHead] = aiNext [iPopIndex];
iReturnValue = aiValues [iPopIndex];
ReturnToPool (iPopIndex);
return (iReturnValue);
};
//------------------------------------------------------------------------
VOID RegExDeque::Put (INT iIn)
{
// put to tail of queue
INT iNew = GetFromPool ();
aiValues [iNew] = iIn;
aiNext [iNew] = iTail;
aiPrev [iNew] = aiPrev [iTail];
aiPrev [iTail] = iNew;
aiNext [aiPrev [iNew]] = iNew;
};
//------------------------------------------------------------------------
VOID RegExDeque::Clear (VOID)
{
// force the return of all entries to the pool
INT iNumEntries = aiValues.Length ();
// setup the Next pointer to link all the allocations together
for (INT iIndex = 0; iIndex < iNumEntries; ++iIndex)
{
aiNext [iIndex] = iIndex + 1;
};
if (iNumEntries > 0)
{
iPool = 0;
aiNext [iNumEntries - 1] = -1;
}
else
{
iPool = -1;
};
InitSentinels ();
};
//------------------------------------------------------------------------
BOOL RegExDeque::InQueueHead (INT iIn)
{
INT iCurrIndex = aiNext [iHead];
while ((iCurrIndex != iTail) && (aiValues [iCurrIndex] != -1))
{
if (aiValues [iCurrIndex] == iIn)
{
return (TRUE);
};
iCurrIndex = aiNext [iCurrIndex];
};
return (FALSE);
};
//------------------------------------------------------------------------
BOOL RegExDeque::InQueueTail (INT iIn)
{
INT iCurrIndex = aiPrev [iTail];
while ((iCurrIndex != iHead) && (aiValues [iCurrIndex] != -1))
{
if (aiValues [iCurrIndex] == iIn)
{
return (TRUE);
};
iCurrIndex = aiPrev [iCurrIndex];
};
return (FALSE);
};
//------------------------------------------------------------------------
// RegEx
//------------------------------------------------------------------------
//------------------------------------------------------------------------
RegEx::RegEx ()
{
strPattern = "";
iPatternPos = 0;
iState = 0;
};
//------------------------------------------------------------------------
RegEx::~RegEx ()
{
};
//------------------------------------------------------------------------
RegEx::RegEx (const RegEx & reIn)
{
arrayStates = reIn.arrayStates;
strPattern = reIn.strPattern;
iPatternPos = reIn.iPatternPos;
iState = reIn.iState;
};
//------------------------------------------------------------------------
RegEx::RegEx (RStr strExprIn,
EParser eParseType)
{
Set (strExprIn, eParseType);
};
//------------------------------------------------------------------------
RegEx::RegEx (const char * pszExprIn,
EParser eParseType)
{
Set (RStr (pszExprIn), eParseType);
};
//------------------------------------------------------------------------
EStatus RegEx::Set (RStr strExprIn,
EParser eParseType)
{
switch (eParseType)
{
case kRegEx: return ParseRegEx (strExprIn);
case kGlob: return ParseGlob (strExprIn);
};
return (EStatus::kFailure);
};
//------------------------------------------------------------------------
RStr RegEx::Match (const RStr & strSourceIn,
INT32 iSearchStartIn,
RStrArray * pstraSubstringsOut)
{
RStr strReturn("");
const char * pszMatch;
INT32 iMatchLength;
Match (strSourceIn.AsChar(),
strSourceIn.GetLength(),
iSearchStartIn,
&pszMatch,
iMatchLength,
pstraSubstringsOut);
if (iMatchLength > 0)
{
strReturn.AppendChars (pszMatch, iMatchLength);
};
return (strReturn);
};
//------------------------------------------------------------------------
VOID RegEx::Match (const char * szSourceIn,
INT32 iSourceLengthIn,
INT32 iSearchStartIn,
const char * * szMatchStartOut,
INT32 & iMatchSizeOut,
RStrArray * pstraSubstringsOut)
{
INT32 iStartMatch = 0;
INT32 iMaxMatch = iSourceLengthIn;
INT iMatchState;
*szMatchStartOut = NULL;
iMatchSizeOut = 0;
#ifdef DEBUGMSG
printf ("\n");
#endif
if (strPattern.GetLength () == 0) return;
for (iStartMatch = iSearchStartIn; iStartMatch <= iMaxMatch; ++iStartMatch)
{
INT iLongestMatch = -1;
#ifdef DEBUGMSG
const char * pszStartMatch = &szSourceIn[iStartMatch];
#endif
const INT iScan = -1;
INT32 iPos = iStartMatch;
iPatternPos = 0;
iMatchState = 1;
// clear the repeat flags
for (UINT32 uIndex = 0; uIndex < UINT32 (arrayStates.Length ()); ++uIndex)
{
arrayStates [uIndex].ClearNumMatches ();
};
deque.Clear ();
deque.Put (iScan);
iMatchState = arrayStates [0].GetNextOne ();
do
{
if (iMatchState == iScan)
{
#ifdef DEBUGMSG
printf ("Rex: match == scan %d\n", iMatchState);
#endif
// We read the scan value off the queue. move to next character
iPos = iPos + 1;
deque.Put (iScan);
}
else if ((arrayStates [iMatchState].IsNull ()) && (iMatchState != 0))
{
#ifdef DEBUGMSG
printf ("Rex: isNull %d\n", iMatchState);
#endif
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextOne ()))
{
deque.Push (arrayStates [iMatchState].GetNextOne ());
};
}
else if (arrayStates [iMatchState].IsOr ())
{
#ifdef DEBUGMSG
printf ("Rex: isOr %d\n", iMatchState);
#endif
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextOne ()))
{
deque.Push (arrayStates [iMatchState].GetNextOne ());
};
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextTwo ()))
{
deque.Push (arrayStates [iMatchState].GetNextTwo ());
};
}
else if (arrayStates [iMatchState].IsRepeat ())
{
#ifdef DEBUGMSG
printf ("Rex: isRepeat %d\n", iMatchState);
#endif
// The first branch is the continue state. The second is the repeat state.
// Note: Check for iterations here. To be implemented.
// Note: How do we handle zero matches?
// check curr match against min and max.
// If less than min, we must only repeat.
// If greater than max, we must fail and not add further checks.
// If between min and max inclusive, we add the continue state.
INT iNumMatches = arrayStates [iMatchState].GetNumMatches ();
INT iMin = arrayStates [iMatchState].GetMatchMin ();
INT iMax = arrayStates [iMatchState].GetMatchMax ();
if (iNumMatches < iMin)
{
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextTwo ()))
{
deque.Push (arrayStates [iMatchState].GetNextTwo ());
};
}
else if ((iNumMatches > iMax) && (iMax != -1))
{
// do nothing
}
else
{
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextOne ()))
{
deque.Push (arrayStates [iMatchState].GetNextOne ());
};
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextTwo ()))
{
deque.Push (arrayStates [iMatchState].GetNextTwo ());
};
};
arrayStates [iMatchState].IncNumMatches ();
}
else if (((iPos == iMaxMatch) && (arrayStates [iMatchState].MatchOnLineEnd ())) ||
((iPos < iMaxMatch) && (arrayStates [iMatchState].IsMatch (&szSourceIn[iPos], szSourceIn, &szSourceIn[iSourceLengthIn]))))
{
#ifdef DEBUGMSG
if (iPos < iMaxMatch)
{
printf ("Rex: isMatch %c matches %c %s\n",
(int)szSourceIn[iPos],
(int)arrayStates [iMatchState].GetStartMatch (),
arrayStates [iMatchState].GetTypeText ());
printf ("%s\n", pszStartMatch );
}
#endif
if (arrayStates [iMatchState].IncOnMatch ())
{
if (! deque.InQueueTail (arrayStates [iMatchState].GetNextOne ()))
{
deque.Put (arrayStates [iMatchState].GetNextOne ());
};
}
else
{
if (! deque.InQueueHead (arrayStates [iMatchState].GetNextOne ()))
{
deque.Push (arrayStates [iMatchState].GetNextOne ());
};
};
}
iMatchState = deque.Pop ();
#ifdef DEBUGMSG
printf ("Match State %d\n", iMatchState);
#endif
if (iMatchState == 0)
{
// found a match
#ifdef DEBUGMSG
printf ("found match at %d\n", (int)iPos);
#endif
iLongestMatch = iPos;
};
}
while ((iPos <= (iMaxMatch + 1)) && (! deque.IsEmpty ())); // (iMatchState != 0) &&
if (iLongestMatch > 0)
{
// found a successful match. This is the leftmost one we can find, so run with it.
#ifdef DEBUGMSG
printf ("Found match size %d start %d char %c\n", (int)iLongestMatch, (int)iStartMatch, szSourceIn[iStartMatch]);
#endif
*szMatchStartOut = &szSourceIn[iStartMatch];
iMatchSizeOut = iLongestMatch - iStartMatch;
//strSourceIn.GetMiddle (iStartMatch, iLongestMatch - iStartMatch, strReturn);
break;
};
}; // for each char
return;
};
//------------------------------------------------------------------------
VOID RegEx::Debug (VOID)
{
INT iMax = arrayStates.Length ();
printf("\n");
for (iState = 0; iState < iMax; ++iState)
{
int iChar = arrayStates [iState].GetStartMatch ();
if ((iChar < 32) || (iChar > 127)) {iChar = ' ';};
printf ("%4d %12s %3c (%3d) %3d %3d\n", int (iState),
arrayStates [iState].GetTypeText (),
char (iChar),
iChar,
arrayStates [iState].GetNextOne (),
arrayStates [iState].GetNextTwo ());
};
};
//------------------------------------------------------------------------
RStr RegEx::Substitute (RStr strSourceIn,
RStr strReplaceIn)
{
// to be implemented
return (RStr (""));
};
//------------------------------------------------------------------------
EStatus RegEx::ParseRegEx (RStr strExprIn)
{
arrayStates.Clear ();
iState = 1;
iPatternPos = 0;
strPattern.Reset();
strPattern = strExprIn;
bParsingError = FALSE;
iNumParenthesis = 0;
INT iStartIndex = RegExExpression ();
if ((iStartIndex == -1) || (bParsingError))
{
return (EStatus::kFailure);
}
arrayStates [0] = RegExMatch (RegExMatch::kNull, 0, 0, iStartIndex, -1);
// the last state needs to be the null which signifies the end has been reached.
arrayStates [iState] = RegExMatch (RegExMatch::kNull, 0, 0, 0, 0);
return (EStatus::kSuccess);
};
//------------------------------------------------------------------------
EStatus RegEx::ParseGlob (RStr strExprIn)
{
arrayStates.Clear ();
iState = 1;
iPatternPos = 0;
strPattern = strExprIn;
arrayStates [0] = RegExMatch (RegExMatch::kNull, 0, 0, iState, iState);
UINT32 uPatternLength = strPattern.GetLength ();
for (iPatternPos = 0; iPatternPos < uPatternLength; ++iPatternPos)
{
if (strPattern [iPatternPos] == '*')
{
RegExMatch matchNew (RegExMatch::kRepeat, 0, 0, iState + 2, iState + 1);
matchNew.SetNumMatches (0, -1);
arrayStates [iState] = matchNew;
++iState;
arrayStates [iState] = RegExMatch (RegExMatch::kAnyChar, 0, 0, iState - 1, iState - 1);
}
else if (strPattern [iPatternPos] == '?')
{
arrayStates [iState] = RegExMatch (RegExMatch::kAnyChar, 0, 0, iState + 1, iState + 1);
}
else if (strPattern [iPatternPos] == '\\')
{
++iPatternPos;
arrayStates [iState] = RegExMatch (RegExMatch::kChar, strPattern [iPatternPos], 0, iState + 1, iState + 1);
}
else
{
arrayStates [iState] = RegExMatch (RegExMatch::kChar, strPattern [iPatternPos], 0, iState + 1, iState + 1);
};
++iState;
};
arrayStates [iState] = RegExMatch (RegExMatch::kNull, 0, 0, 0, 0);
return (EStatus::kSuccess);
};
//------------------------------------------------------------------------
INT RegEx::RegExExpression (BOOL bNegativeMatchIn)
{
INT iBranchOne;
INT iBranchTwo;
INT iReturn;