This repository has been archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbregcomp.cpp
1953 lines (1762 loc) · 42.4 KB
/
bregcomp.cpp
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
//
// bregcomp.cc
//
////////////////////////////////////////////////////////////////////////////////
// 1999.11.24 update by Tatsuo Baba
//
// You may distribute under the terms of either the GNU General Public
// License or the Artistic License, as specified in the perl_license.txt file.
////////////////////////////////////////////////////////////////////////////////
/*
* "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
*/
/* NOTE: this is derived from Henry Spencer's regexp code, and should not
* confused with the original package (see point 3 below). Thanks, Henry!
*/
/* Additional note: this code is very heavily munged from Henry's version
* in places. In some spots I've traded clarity for efficiency, so don't
* blame Henry for some of the lack of readability.
*/
/* The names of the functions have been changed from regcomp and
* regexec to pregcomp and pregexec in order to avoid conflicts
* with the POSIX routines of the same names.
*/
/*SUPPRESS 112*/
/*
* pregcomp and pregexec -- regsub and regerror are not used in perl
*
* Copyright (c) 1986 by University of Toronto.
* Written by Henry Spencer. Not derived from licensed software.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
*
**** Alterations to Henry's code are...
****
**** Copyright (c) 1991-1994, Larry Wall
****
**** You may distribute under the terms of either the GNU General Public
**** License or the Artistic License, as specified in the README file.
*
* Beware that some of this code is subtly aware of the way operator
* precedence is structured in regular expressions. Serious changes in
* regular-expression syntax might require a total rethink.
*/
////////////////////////////////////////////////////////////////////////////////
// Alterations to Larry Wall's code are...
// Copyright 1999 Tatsuo Baba
//
// 1999.11.24 Tatsuo Baba
//
// You may distribute under the terms of either the GNU General Public
// License or the Artistic License, as specified in the perl_license.txt file.
////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define KANJI
//#define hints 1
//#define HINT_KANJI_STRING 1
#include "global.h"
#include "sv.h"
#include "intreg.h"
typedef struct bregexp {
char *outp; /* matched or substitute string start ptr */
char *outendp; /* matched or substitute string end ptr */
char **splitp; /* split result pointer ptr */
} BREGEXP;
extern "C"
void BRegfree(BREGEXP* exp);
static char regarglen[] = {0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0};
/* The following have no fixed length. */
static char varies[] = {BRANCH,BACK,STAR,PLUS,CURLY,CURLYX,REF,WHILEM,0};
/* The following always have a length of 1. */
static char simple[] = {ANY,SANY,ANYOF,ALNUM,NALNUM,SPACE,NSPACE,DIGIT,NDIGIT,0};
static char regdummy;
#ifdef MSDOS
# if defined(BUGGY_MSC6)
/* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
# pragma optimize("a",off)
/* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
# pragma optimize("w",on )
# endif /* BUGGY_MSC6 */
#endif /* MSDOS */
#ifndef STATIC
#define STATIC static
#endif
#define ISKANJI(c) (KANJI_MODE && iskanji(c))
#define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
#define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
((*s) == '{' && regcurly(s)))
#ifdef atarist
#define PERL_META "^$.[()|?+*\\"
#else
#define META "^$.[()|?+*\\"
#endif
#ifdef SPSTART
#undef SPSTART /* dratted cpp namespace... */
#endif
/*
* Flags to be passed up and down.
*/
#define WORST 0 /* Worst case. */
#define HASWIDTH 0x1 /* Known never to match null string. */
#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
#define SPSTART 0x4 /* Starts with * or +. */
#define TRYAGAIN 0x8 /* Weeded out a declaration. */
/*
* Forward declarations for pregcomp()'s friends.
*/
/*
- pregcomp - compile a regular expression into internal code
*
* We can't allocate space until we know how big the compiled form will be,
* but we can't compile it (and thus know how big it is) until we've got a
* place to put the code. So we cheat: we compile it twice, once with code
* generation turned off and size counting turned on, and once "for real".
* This also means that we don't allocate space until we are sure that the
* thing really will compile successfully, and we never have to move the
* code and thus invalidate pointers into it. (Note that it has to be in
* one piece because free() must be able to free it all.) [NB: not true in perl]
*
* Beware that the optimization-preparation code in here knows about some
* of the structure of the compiled regexp. [I'll say.]
*/
#define croak(c) throw(c)
char* savepvn (char* sv, int len);
void regc(char b,GLOBAL*);
static char* nextchar(GLOBAL*);
static char* Reg(int,int*,GLOBAL*);
static int regcurly(register char*);
char * regnext(register char *p);
void pmflag(int*, int);
static char * regbranch(int*,GLOBAL*);
static void regtail(char*,char*);
static void regoptail(char*,char*);
static void reginsert(char, char*,GLOBAL*);
static char *regpiece(int*,GLOBAL*);
static char *regnode(char,GLOBAL*);
static char *regclass(GLOBAL*);
static char *reganode(char,unsigned short,GLOBAL*);
#define DEFSTATIC
regexp *bregcomp(
char* exp,
char* xend,
int* flag,
char *msg) // error message
{
msg[0] = '\0'; //ensure no message at this time
// int fold = pm->op_pmflags & PMf_FOLD;
int fold = *flag & PMf_FOLD;
register regexp *r;
register char *scan;
register SV *longish;
SV *longest;
register int len;
register char *first;
int flags;
int backish;
int backest;
int curback;
int minlen = 0;
int sawplus = 0;
int sawopen = 0;
//baba
GLOBAL global;
GLOBAL *globalp = &global;
memset((void*)globalp,0,sizeof(GLOBAL));
kanji_mode_flag = *flag & PMf_KANJI; // using KANJI_MODE macro
///// try start all croak issue throw
try {
if (exp == NULL)
croak("NULL argument");
/* First pass: determine size, legality. */
// regflags = pm->op_pmflags;
multiline = *flag & PMf_MULTILINE; // set multi line mode
regflags = *flag;
regparse = exp;
regxend = xend;
if (xend > exp)
regprecomp = savepvn(exp,xend-exp);
regnaughty = 0;
regsawback = 0;
regnpar = 1;
regsize = 0L;
regcode = ®dummy;
regc((char)MAGIC,globalp);
if (Reg(0, &flags,globalp) == NULL) {
// Safefree(regprecomp);
delete [] regprecomp;
regprecomp = NULL;
return(NULL);
}
/* Small enough for pointer-storage convention? */
if (regsize >= 32767L) /* Probably could be 65535L. */
croak("too big compile");
/* Allocate space. */
// Newc(1001, r, sizeof(regexp) + (unsigned)regsize, char, regexp);
r = (regexp*)new char[sizeof(regexp) + (unsigned)regsize];
if (r == NULL)
croak("out of space regexp");
memset(r,0,sizeof(regexp) + (unsigned)regsize);
regexp_current = (char*)r; // save for croak
/* Second pass: emit code. */
r->prelen = xend-exp;
if (r->prelen == 0) { // no string
r->pmflags = *flag;
return (r);
}
r->precomp = regprecomp;
regprecomp = NULL;
r->subbeg = r->subbase = NULL;
regnaughty = 0;
regparse = exp;
regnpar = 1;
regcode = r->program;
regc((char)MAGIC,globalp);
if (Reg(0, &flags,globalp) == NULL)
return(NULL);
/* Dig out information for optimizations. */
// pm->op_pmflags = regflags;
// fold = pm->op_pmflags & PMf_FOLD;
r->pmflags = *flag;
*flag = regflags;
fold = *flag & PMf_FOLD;
r->regstart = NULL; /* Worst-case defaults. */
// r->reganch = ((hints & HINT_KANJI_REGEXP) ? ROPT_KANJI : 0);
r->reganch = ROPT_KANJI;
r->regmust = NULL;
r->regback = -1;
r->regstclass = NULL;
r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */
scan = r->program+1; /* First BRANCH. */
if (OP(regnext(scan)) == END) {/* Only one top-level choice. */
scan = NEXTOPER(scan);
first = scan;
while ((OP(first) == OPEN && (sawopen = 1)) ||
(OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
(OP(first) == PLUS) ||
(OP(first) == MINMOD) ||
(regkind[(unsigned char)OP(first)] == CURLY && ARG1(first) > 0) ) {
if (OP(first) == PLUS)
sawplus = 1;
else
first += regarglen[(unsigned char)OP(first)];
first = NEXTOPER(first);
}
/* Starting-point info. */
again:
if (OP(first) == EXACTLY) {
r->regstart = newSVpv(OPERAND(first)+1,*OPERAND(first));
// if (SvCUR(r->regstart) > (unsigned int)!(sawstudy|fold))
if (SvCUR(r->regstart) > (int)!(fold))
fbm_compile(r->regstart,fold);
else
sv_upgrade(r->regstart, SVt_PVBM);
}
else if (strchr(simple+2,OP(first)))
r->regstclass = first;
else if (OP(first) == BOUND || OP(first) == NBOUND)
r->regstclass = first;
else if (regkind[(unsigned char)OP(first)] == BOL) {
r->reganch |= ROPT_ANCH;
first = NEXTOPER(first);
goto again;
}
else if ((OP(first) == STAR &&
regkind[(unsigned char)OP(NEXTOPER(first))] == ANY) &&
!(r->reganch & ROPT_ANCH) )
{
/* turn .* into ^.* with an implied $*=1 */
r->reganch |= ROPT_ANCH | ROPT_IMPLICIT ;
first = NEXTOPER(first);
goto again;
}
if (sawplus && (!sawopen || !regsawback))
r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
/*
* If there's something expensive in the r.e., find the
* longest literal string that must appear and make it the
* regmust. Resolve ties in favor of later strings, since
* the regstart check works with the beginning of the r.e.
* and avoiding duplication strengthens checking. Not a
* strong reason, but sufficient in the absence of others.
* [Now we resolve ties in favor of the earlier string if
* it happens that curback has been invalidated, since the
* earlier string may buy us something the later one won't.]
*/
longish = newSVpv("",0);
longest = newSVpv("",0);
len = 0;
minlen = 0;
curback = 0;
backish = 0;
backest = 0;
while (OP(scan) != END) {
if (OP(scan) == BRANCH) {
if (OP(regnext(scan)) == BRANCH) {
curback = -30000;
while (OP(scan) == BRANCH)
scan = regnext(scan);
}
else /* single branch is ok */
scan = NEXTOPER(scan);
continue;
}
if (OP(scan) == UNLESSM) {
curback = -30000;
scan = regnext(scan);
continue;
}
if (OP(scan) == EXACTLY) {
char *t;
first = scan;
while (OP(t = regnext(scan)) == CLOSE)
scan = t;
minlen += *OPERAND(first);
if (curback - backish == len) {
sv_catpvn(longish, OPERAND(first)+1,
*OPERAND(first));
len += *OPERAND(first);
curback += *OPERAND(first);
first = regnext(scan);
}
else if (*OPERAND(first) >= len + (curback >= 0)) {
len = *OPERAND(first);
sv_setpvn(longish, OPERAND(first)+1,len);
backish = curback;
curback += len;
first = regnext(scan);
}
else
curback += *OPERAND(first);
}
else if (strchr(varies,OP(scan))) {
curback = -30000;
len = 0;
if (SvCUR(longish) > SvCUR(longest)) {
sv_setsv(longest,longish);
backest = backish;
}
sv_setpvn(longish,"",0);
if (OP(scan) == PLUS && strchr(simple,OP(NEXTOPER(scan))))
minlen++;
else if (regkind[(unsigned char)OP(scan)] == CURLY &&
strchr(simple,OP(NEXTOPER(scan)+4)))
minlen += ARG1(scan);
}
else if (strchr(simple,OP(scan))) {
curback++;
minlen++;
len = 0;
if (SvCUR(longish) > SvCUR(longest)) {
sv_setsv(longest,longish);
backest = backish;
}
sv_setpvn(longish,"",0);
}
scan = regnext(scan);
}
/* Prefer earlier on tie, unless we can tail match latter */
if (SvCUR(longish) + (regkind[(unsigned char)OP(first)] == EOL) >
SvCUR(longest))
{
sv_setsv(longest,longish);
backest = backish;
}
else
sv_setpvn(longish,"",0);
if (SvCUR(longest)
&&
(!r->regstart
||
!fbm_instr((unsigned char*) SvPVX(r->regstart),
(unsigned char *) SvPVX(r->regstart)
+ SvCUR(r->regstart),
longest,multiline,KANJI_MODE)
)
)
{
r->regmust = longest;
if (backest < 0)
backest = -1;
r->regback = backest;
// if (SvCUR(longest) > (unsigned int)!(sawstudy || fold ||
if (SvCUR(longest) > (int)!(fold ||
regkind[(unsigned char)OP(first)]==EOL))
fbm_compile(r->regmust,fold);
(void)SvUPGRADE(r->regmust, SVt_PVBM);
BmUSEFUL(r->regmust) = 100;
if (regkind[(unsigned char)OP(first)] == EOL && SvCUR(longish))
SvTAIL_on(r->regmust);
}
else {
SvREFCNT_dec(longest);
longest = NULL;
}
SvREFCNT_dec(longish);
}
r->do_folding = (char)fold;
r->nparens = regnpar - 1;
r->minlen = minlen;
// Newz(1002, r->startp, regnpar, char*);
r->startp = (char**)new char[regnpar * sizeof(char*)];
if (r->startp == NULL)
croak("out of space startp");
memset(r->startp,0,regnpar * sizeof(char*));
// Newz(1002, r->endp, regnpar, char*);
r->endp = (char**)new char[regnpar * sizeof(char*)];
if (r->endp == NULL)
croak("out of space endp");
memset(r->endp,0,regnpar * sizeof(char*));
return(r);
/////// catch start
}
catch (const char* err) {
if (regexp_current)
BRegfree((BREGEXP*)regexp_current);
if (regprecomp)
delete [] regprecomp;
strcpy(msg,err);
return NULL;
}
catch (...) {
if (regexp_current)
BRegfree((BREGEXP*)regexp_current);
if (regprecomp)
delete [] regprecomp;
strcpy(msg,"fatal error");
return NULL;
}
}
/*
- regatom - the lowest level
*
* Optimization: gobbles an entire sequence of ordinary characters so that
* it can turn them into a single node, which is smaller to store and
* faster to run. Backslashed characters are exceptions, each becoming a
* separate node; the code is simpler that way and it's not worth fixing.
*
* [Yes, it is worth fixing, some scripts can run twice the speed.]
*/
static char *regatom(int *flagp,GLOBAL* globalp)
{
register char *ret = 0;
int flags;
*flagp = WORST; /* Tentatively. */
tryagain:
switch (*regparse) {
case '^':
nextchar(globalp);
if (regflags & PMf_MULTILINE)
ret = regnode(MBOL,globalp);
else if (regflags & PMf_SINGLELINE)
ret = regnode(SBOL,globalp);
else
ret = regnode(BOL,globalp);
break;
case '$':
nextchar(globalp);
if (regflags & PMf_MULTILINE)
ret = regnode(MEOL,globalp);
else if (regflags & PMf_SINGLELINE)
ret = regnode(SEOL,globalp);
else
ret = regnode(EOL,globalp);
break;
case '.':
nextchar(globalp);
if (regflags & PMf_SINGLELINE)
ret = regnode(SANY,globalp);
else
ret = regnode(ANY,globalp);
regnaughty++;
*flagp |= HASWIDTH;
break;
case '[':
regparse++;
ret = regclass(globalp);
*flagp |= HASWIDTH;
break;
case '(':
nextchar(globalp);
ret = Reg(1, &flags,globalp);
if (ret == NULL) {
if (flags & TRYAGAIN)
goto tryagain;
return(NULL);
}
*flagp |= flags&(HASWIDTH|SPSTART);
break;
case '|':
case ')':
if (flags & TRYAGAIN) {
*flagp |= TRYAGAIN;
return NULL;
}
croak("internal urp at /%s/");
/* Supposed to be caught earlier. */
break;
case '?':
case '+':
case '*':
croak("?+* follows nothing");
break;
case '\\':
switch (*++regparse) {
case 'A':
ret = regnode(SBOL,globalp);
*flagp |= SIMPLE;
nextchar(globalp);
break;
case 'G':
ret = regnode(GBOL,globalp);
*flagp |= SIMPLE;
nextchar(globalp);
break;
case 'Z':
ret = regnode(SEOL,globalp);
*flagp |= SIMPLE;
nextchar(globalp);
break;
case 'w':
ret = regnode(ALNUM,globalp);
*flagp |= HASWIDTH|SIMPLE;
nextchar(globalp);
break;
case 'W':
ret = regnode(NALNUM,globalp);
*flagp |= HASWIDTH|SIMPLE;
nextchar(globalp);
break;
case 'b':
ret = regnode(BOUND,globalp);
*flagp |= SIMPLE;
nextchar(globalp);
break;
case 'B':
ret = regnode(NBOUND,globalp);
*flagp |= SIMPLE;
nextchar(globalp);
break;
case 's':
ret = regnode(SPACE,globalp);
*flagp |= HASWIDTH|SIMPLE;
nextchar(globalp);
break;
case 'S':
ret = regnode(NSPACE,globalp);
*flagp |= HASWIDTH|SIMPLE;
nextchar(globalp);
break;
case 'd':
ret = regnode(DIGIT,globalp);
*flagp |= HASWIDTH|SIMPLE;
nextchar(globalp);
break;
case 'D':
ret = regnode(NDIGIT,globalp);
*flagp |= HASWIDTH|SIMPLE;
nextchar(globalp);
break;
case 'n':
case 'r':
case 't':
case 'f':
case 'e':
case 'a':
case 'x':
case 'c':
case '0':
goto defchar;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
int num = atoi(regparse);
if (num > 9 && num >= regnpar)
goto defchar;
else {
regsawback = 1;
ret = reganode(REF, (unsigned short)num,globalp);
*flagp |= HASWIDTH;
while (isDIGIT(*regparse))
regparse++;
regparse--;
nextchar(globalp);
}
}
break;
case '\0':
if (regparse >= regxend)
FAIL("trailing \\ in regexp");
/* FALL THROUGH */
default:
goto defchar;
}
break;
case '#':
if (regflags & PMf_EXTENDED) {
while (regparse < regxend && *regparse != '\n') regparse++;
if (regparse < regxend)
goto tryagain;
}
/* FALL THROUGH */
default: {
register int len;
register char ender;
register char *p;
char *oldp;
int numlen;
regparse++;
defchar:
ret = regnode(EXACTLY,globalp);
regc(0,globalp); /* save spot for len */
for (len = 0, p = regparse - 1;
len < 126 && p < regxend;
len++)
{
oldp = p;
switch (*p) {
case '^':
case '$':
case '.':
case '[':
case '(':
case ')':
case '|':
goto loopdone;
case '\\':
switch (*++p) {
case 'A':
case 'G':
case 'Z':
case 'w':
case 'W':
case 'b':
case 'B':
case 's':
case 'S':
case 'd':
case 'D':
--p;
goto loopdone;
case 'n':
ender = '\n';
p++;
break;
case 'r':
ender = '\r';
p++;
break;
case 't':
ender = '\t';
p++;
break;
case 'f':
ender = '\f';
p++;
break;
case 'e':
ender = '\033';
p++;
break;
case 'a':
ender = '\007';
p++;
break;
case 'x':
ender = (char)scan_hex(++p, 2, &numlen);
p += numlen;
break;
case 'c':
p++;
ender = *p++;
if (isLOWER(ender))
ender = toUPPER(ender);
ender ^= 64;
break;
case '0': case '1': case '2': case '3':case '4':
case '5': case '6': case '7': case '8':case '9':
if (*p == '0' ||
(isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
ender = (char)scan_oct(p, 3, &numlen);
p += numlen;
}
else {
--p;
goto loopdone;
}
break;
case '\0':
if (p >= regxend)
FAIL("trailing \\ in regexp");
/* FALL THROUGH */
default:
ender = *p++;
break;
}
break;
case '#':
if (regflags & PMf_EXTENDED) {
while (p < regxend && *p != '\n') p++;
}
/* FALL THROUGH */
case ' ': case '\t': case '\n': case '\r': case '\f': case '\v':
if (regflags & PMf_EXTENDED) {
p++;
len--;
continue;
}
/* FALL THROUGH */
default:
ender = *p++;
break;
}
if (regflags & PMf_FOLD && isUPPER(ender))
ender = toLOWER(ender);
if (ISKANJI(ender) && *p) {
if (ISMULT2((p+1))) {
/* */
if (len) {
p = oldp;
} else {
len += 2;
regc(ender,globalp);
ender = *p++;
regc(ender,globalp);
}
break;
}
len++;
regc(ender,globalp);
ender = *p++;
} else if (ISMULT2(p)) { /* Back off on ?+*. */
if (len)
p = oldp;
else {
len++;
regc(ender,globalp);
}
break;
}
regc(ender,globalp);
}
loopdone:
regparse = p - 1;
nextchar(globalp);
if (len < 0)
FAIL("internal disaster in regexp");
if (len > 0)
*flagp |= HASWIDTH;
if (len == 1)
*flagp |= SIMPLE;
if (regcode != ®dummy)
*OPERAND(ret) = (char)len;
regc('\0',globalp);
}
break;
}
return(ret);
}
static void reg_class_exact(int ex,register GLOBAL* globalp)
{
char c;
regc((char)ANYOF_EXACT,globalp);
c = ex >> 8; regc(c,globalp);
c = ex; regc(c,globalp);
}
static void reg_class_fromto(int from,int to,GLOBAL* globalp)
{
char c;
regc((char)ANYOF_FROMTO,globalp);
c = from >> 8; regc(c,globalp);
c = from; regc(c,globalp);
c = to >> 8; regc(c,globalp);
c = to; regc(c,globalp);
}
static void char_bitmap_set(int c,GLOBAL* globalp)
{
c &= 255;
char_bitmap[c >> 3] |= (1 << (c & 7));
}
static void char_bitmap_unset(int c,GLOBAL* globalp)
{
c &= 255;
char_bitmap[c >> 3] &= ~(1 << (c & 7));
}
static unsigned char char_bitmap_set_p(int c,GLOBAL* globalp)
{
return (char_bitmap[c >> 3] & (1 << (c & 7)));
}
static void char_bitmap_compile(GLOBAL* globalp)
{
int i = 0;
int from;
int to;
// char c;
/* look for the first nonzero */
for (;;) {
for (; i < 256; i++) {
if (char_bitmap_set_p(i,globalp)) {
break;
}
}
if (i >= 256) break;
from = i;
for (; i < 256; i++) {
if (!char_bitmap_set_p(i,globalp)) {
break;
}
}
to = i-1;
#ifndef originalkanji_reg
if (from == to)
reg_class_exact(to,globalp) ;
else
#endif
reg_class_fromto(from, to,globalp);
if (i >= 256) break;
}
}
static char *regclass(GLOBAL* globalp)
{
/* Since we possibly have 16 bit characters, we can't use
simple bitmap method deployed by the original source.
So, we introduce 4 new operators.
ANYOF_EXACT 'c' -- exact char match in [..]
ANYOF_FROMTO 'f' 't' -- 'f' to 't'.
ANYOF_ENDMARK -- end of [..]
ANYOF_COMPL -- indicates [^.. ].
'c', 'f', 't' are 2 bytes long.
*/
// register char *bits;
register int localClass;
register int lastclass;
register int range = 0;
register char *ret;
int numlen;
ret = regnode(ANYOF,globalp);
if (*regparse == '^') { /* Complement of range. */
regparse++;
regnaughty++;
regc((char)ANYOF_COMPL,globalp);
} else {
regc(0,globalp);
}
if (*regparse == ']' || *regparse == '-')
goto skipcond; /* allow 1st char to be ] or - */
while (regparse < regxend && *regparse != ']') {
skipcond:
localClass = UCHARAT(regparse++);
if (ISKANJI((unsigned char)localClass) && regparse < regxend) {
localClass = (localClass << 8) + UCHARAT(regparse++);
}
if (localClass == '\\') {
localClass = UCHARAT(regparse++);
switch (localClass) {
case 'w':
memset(char_bitmap, 0, sizeof char_bitmap);
for (localClass = 0; localClass < 256; localClass++) {
if (isALNUM(localClass)) char_bitmap_set(localClass,globalp);
}
char_bitmap_compile(globalp);
lastclass = 0xFFFF;
continue;
case 'W':
memset(char_bitmap, ~0, sizeof char_bitmap);
for (localClass = 0; localClass < 256; localClass++) {
if (isALNUM(localClass)) char_bitmap_unset(localClass,globalp);
//if (!isALNUM(localClass)) char_bitmap_unset(localClass);
// This is defect condition.
}
char_bitmap_compile(globalp);
reg_class_fromto(0x100, 0xFFFF,globalp);
lastclass = 0xFFFF;
continue;
case 's':
memset(char_bitmap, 0, sizeof char_bitmap);
for (localClass = 0; localClass < 256; localClass++)
if (isSPACE(localClass)) char_bitmap_set(localClass,globalp);