-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregex_e.c
1624 lines (1396 loc) · 43.2 KB
/
regex_e.c
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
/* $Id: regex_e.c,v 1.2 2003/02/22 08:09:47 amura Exp $ */
/* Imagine Generic gnuemacs copyright notice here */
/* To test, compile with -Dtest.
This Dtestable feature turns this into a self-contained program
which reads a pattern, describes how it compiles,
then reads a string and searches for it. */
#ifdef MSDOS /* 90.03.28 by A.Shirahashi */
#include <stdlib.h>
#include <string.h>
#endif /* MSDOS */
#ifdef emacs
/* The `emacs' switch turns on certain special matching commands
that make sense only in emacs. */
#include "config.h"
#include "lisp.h"
#include "buffer.h"
#include "syntax.h"
#else /* not emacs */
/*
* Define the syntax stuff, so we can do the \<...\> things.
*/
#ifndef Sword /* must be non-zero in some of the tests below... */
#define Sword 1
#endif
#define SYNTAX(c) re_syntax_table[c]
#ifdef SYNTAX_TABLE
char *re_syntax_table;
#else
static char re_syntax_table[256];
static VOID
init_syntax_once ()
{
register int c;
static int done = 0;
if (done)
return;
bzero (re_syntax_table, sizeof re_syntax_table);
for (c = 'a'; c <= 'z'; c++)
re_syntax_table[c] = Sword;
for (c = 'A'; c <= 'Z'; c++)
re_syntax_table[c] = Sword;
for (c = '0'; c <= '9'; c++)
re_syntax_table[c] = Sword;
done = 1;
}
#endif /* SYNTAX_TABLE */
#endif /* not emacs */
#include "regex_e.h"
#ifdef MSDOS /* 90.03.28 by A.Shirahashi */
/* 90.12.26 Moved to here for TC++ 1.0 by Junn Ohta */
void *alloca(size_t);
void init_syntax_once(void);
char *re_compile_pattern(char *,int,struct re_pattern_buffer *);
int store_jump(char *,char,char *);
int insert_jump(char,char *,char *,char *);
void re_compile_fastmap(struct re_pattern_buffer *);
int re_search(struct re_pattern_buffer *,char *,int,int,int,
struct re_registers *);
int re_search_2(struct re_pattern_buffer *,char *,int,char *,int,int,
int,struct re_registers *,int);
int re_match(struct re_pattern_buffer *,char *,int,int,
struct re_registers *);
int re_match_2(struct re_pattern_buffer *,char *,int,
char *,int,int,struct re_registers *,int);
int bcmp_translate(char *,char *,int,char *);
char *re_comp(char *);
int re_exec(char *);
#endif /* MSDOS */
/* Number of failure points to allocate space for initially,
when matching. If this number is exceeded, more space is allocated,
so it is not a hard limit. */
#ifndef NFAILURES
#define NFAILURES 80
#endif NFAILURES
/* width of a byte in bits */
#define BYTEWIDTH 8
/* These are the command codes that appear in compiled regular expressions, one per byte.
Some command codes are followed by argument bytes.
A command code can specify any interpretation whatever for its arguments.
Zero-bytes may appear in the compiled regular expression. */
typedef char regexpcode;
#define unused 0
#define exactn 1 /* followed by one byte giving n, and then by n literal bytes */
#define begline 2 /* fails unless at beginning of line */
#define endline 3 /* fails unless at end of line */
#define jump 4 /* followed by two bytes giving relative address to jump to */
#define on_failure_jump 5 /* followed by two bytes giving relative address of place */
/* to resume at in case of failure. */
#define finalize_jump 6 /* Throw away latest failure point and then jump to address. */
#define maybe_finalize_jump 7 /* Like jump but finalize if safe to do so. */
/* This is used to jump back to the beginning
of a repeat. If the command that follows
this jump is clearly incompatible with the
one at the beginning of the repeat, such that
we can be sure that there is no use backtracking
out of repetitions already completed,
then we finalize. */
#define dummy_failure_jump 8 /* jump, and push a dummy failure point. */
/* This failure point will be thrown away
if an attempt is made to use it for a failure.
A + construct makes this before the first repeat. */
#define anychar 9 /* matches any one character */
#define charset 10 /* matches any one char belonging to specified set. */
/* First following byte is # bitmap bytes.
Then come bytes for a bit-map saying which chars are in.
Bits in each byte are ordered low-bit-first.
A character is in the set if its bit is 1.
A character too large to have a bit in the map
is automatically not in the set */
#define charset_not 11 /* similar but match any character that is NOT one of those specified */
#define start_memory 12 /* starts remembering the text that is matched */
/* and stores it in a memory register.
followed by one byte containing the register number.
Register numbers must be in the range 0 through NREGS. */
#define stop_memory 13 /* stops remembering the text that is matched */
/* and stores it in a memory register.
followed by one byte containing the register number.
Register numbers must be in the range 0 through NREGS. */
#define duplicate 14 /* match a duplicate of something remembered. */
/* Followed by one byte containing the index of the memory register. */
#define before_dot 15 /* Succeeds if before dot */
#define at_dot 16 /* Succeeds if at dot */
#define after_dot 17 /* Succeeds if after dot */
#define begbuf 18 /* Succeeds if at beginning of buffer */
#define endbuf 19 /* Succeeds if at end of buffer */
#define wordchar 20 /* Matches any word-constituent character */
#define notwordchar 21 /* Matches any char that is not a word-constituent */
#define wordbeg 22 /* Succeeds if at word beginning */
#define wordend 23 /* Succeeds if at word end */
#define wordbound 24 /* Succeeds if at a word boundary */
#define notwordbound 25 /* Succeeds if not at a word boundary */
#define syntaxspec 26 /* Matches any character whose syntax is specified. */
/* followed by a byte which contains a syntax code, Sword or such like */
#define notsyntaxspec 27 /* Matches any character whose syntax differs from the specified. */
#ifndef SIGN_EXTEND_CHAR
#define SIGN_EXTEND_CHAR(x) (x)
#endif
/* compile_pattern takes a regular-expression descriptor string in the user's format
and converts it into a buffer full of byte commands for matching.
pattern is the address of the pattern string
size is the length of it.
bufp is a struct re_pattern_buffer * which points to the info
on where to store the byte commands.
This structure contains a char * which points to the
actual space, which should have been obtained with malloc.
compile_pattern may use realloc to grow the buffer space.
The number of bytes of commands can be found out by looking in
the struct re_pattern_buffer that bufp pointed to,
after compile_pattern returns.
*/
#define PATPUSH(ch) (*b++ = (char) (ch))
#define PATFETCH(c) \
{if (p == pend) goto end_of_pattern; \
c = * (unsigned char *) p++; \
if (translate) c = translate[c]; }
#define PATFETCH_RAW(c) \
{if (p == pend) goto end_of_pattern; \
c = * (unsigned char *) p++; }
#define PATUNFETCH p--
#ifdef MSDOS /* 90.03.28 by A.Shirahashi */
#define SHIFT_EXTEND 13
#else /* NOT MSDOS */
#define SHIFT_EXTEND 16
#endif /* MSDOS */
#define EXTEND_BUFFER \
{ char *old_buffer = bufp->buffer; \
if (bufp->allocated == (1<<SHIFT_EXTEND)) goto too_big; \
bufp->allocated *= 2; \
if (bufp->allocated > (1<<SHIFT_EXTEND)) bufp->allocated = (1<<SHIFT_EXTEND); \
if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
goto memory_exhausted; \
c = bufp->buffer - old_buffer; \
b += c; \
if (fixup_jump) \
fixup_jump += c; \
if (laststart) \
laststart += c; \
begalt += c; \
if (pending_exact) \
pending_exact += c; \
}
static int store_jump (), insert_jump ();
static int bcmp_translate(); /* 91.02.06 Add by S.Yoshida */
char *
re_compile_pattern (pattern, size, bufp)
char *pattern;
int size;
struct re_pattern_buffer *bufp;
{
register char *b = bufp->buffer;
register char *p = pattern;
char *pend = pattern + size;
register unsigned c, c1;
char *p1;
unsigned char *translate = (unsigned char *) bufp->translate;
/* address of the count-byte of the most recently inserted "exactn" command.
This makes it possible to tell whether a new exact-match character
can be added to that command or requires a new "exactn" command. */
char *pending_exact = 0;
/* address of the place where a forward-jump should go
to the end of the containing expression.
Each alternative of an "or", except the last, ends with a forward-jump
of this sort. */
char *fixup_jump = 0;
/* address of start of the most recently finished expression.
This tells postfix * where to find the start of its operand. */
char *laststart = 0;
/* In processing a repeat, 1 means zero matches is allowed */
char zero_times_ok;
/* In processing a repeat, 1 means many matches is allowed */
char many_times_ok;
/* address of beginning of regexp, or inside of last \( */
char *begalt = b;
/* Stack of information saved by \( and restored by \).
Four stack elements are pushed by each \(:
First, the value of b.
Second, the value of fixup_jump.
Third, the value of regnum.
Fourth, the value of begalt. */
int stackb[40];
int *stackp = stackb;
int *stacke = stackb + 40;
int *stackt;
/* Counts \('s as they are encountered. Remembered for the matching \),
where it becomes the "register number" to put in the stop_memory command */
int regnum = 1;
bufp->fastmap_accurate = 0;
#ifndef SYNTAX_TABLE
#ifndef emacs
/*
* Initialize the syntax table.
*/
init_syntax_once();
#endif
#endif
if (bufp->allocated == 0)
{
bufp->allocated = 28;
if (bufp->buffer)
/* EXTEND_BUFFER loses when bufp->allocated is 0 */
bufp->buffer = (char *) realloc (bufp->buffer, 28);
else
/* Caller did not allocate a buffer. Do it for him */
bufp->buffer = (char *) malloc (28);
if (!bufp->buffer) goto memory_exhausted;
begalt = b = bufp->buffer;
}
while (p != pend)
{
if (b - bufp->buffer > bufp->allocated - 10)
/* Note that EXTEND_BUFFER clobbers c */
EXTEND_BUFFER;
PATFETCH (c);
switch (c)
{
case '$':
/* $ means succeed if at end of line, but only in special contexts.
If randonly in the middle of a pattern, it is a normal character. */
if (p == pend || (*p == '\\' && (p[1] == ')' || p[1] == '|')))
{
PATPUSH (endline);
break;
}
goto normal_char;
case '^':
/* ^ means succeed if at beg of line, but only if no preceding pattern. */
if (laststart) goto normal_char;
PATPUSH (begline);
break;
case '*':
case '+':
case '?':
/* If there is no previous pattern, char not special. */
if (!laststart)
goto normal_char;
/* If there is a sequence of repetition chars,
collapse it down to equivalent to just one. */
zero_times_ok = 0;
many_times_ok = 0;
while (1)
{
zero_times_ok |= c != '+';
many_times_ok |= c != '?';
if (p == pend)
break;
PATFETCH (c);
if (!(c == '*' || c == '+' || c == '?'))
{
PATUNFETCH;
break;
}
}
/* Now we know whether 0 matches is allowed,
and whether 2 or more matches is allowed. */
if (many_times_ok)
{
/* If more than one repetition is allowed,
put in a backward jump at the end. */
store_jump (b, maybe_finalize_jump, laststart - 3);
b += 3;
}
insert_jump (on_failure_jump, laststart, b + 3, b);
pending_exact = 0;
b += 3;
if (!zero_times_ok)
{
/* At least one repetition required: insert before the loop
a skip over the initial on-failure-jump instruction */
insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
b += 3;
}
break;
case '.':
laststart = b;
PATPUSH (anychar);
break;
case '[':
if (b - bufp->buffer
> bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
/* Note that EXTEND_BUFFER clobbers c */
EXTEND_BUFFER;
laststart = b;
if (*p == '^')
PATPUSH (charset_not), p++;
else
PATPUSH (charset);
p1 = p;
PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
/* Clear the whole map */
bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
/* Read in characters and ranges, setting map bits */
while (1)
{
PATFETCH (c);
if (c == ']' && p != p1 + 1) break;
if (*p == '-')
{
PATFETCH (c1);
PATFETCH (c1);
while (c <= c1)
b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
}
else
{
b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
}
}
/* Discard any bitmap bytes that are all 0 at the end of the map.
Decrement the map-length byte too. */
while (b[-1] > 0 && b[b[-1] - 1] == 0)
b[-1]--;
b += b[-1];
break;
case '\\':
if (p == pend) goto invalid_pattern;
PATFETCH_RAW (c);
switch (c)
{
case '(':
if (stackp == stacke) goto nesting_too_deep;
if (regnum < RE_NREGS)
{
PATPUSH (start_memory);
PATPUSH (regnum);
}
*stackp++ = b - bufp->buffer;
*stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
*stackp++ = regnum++;
*stackp++ = begalt - bufp->buffer;
fixup_jump = 0;
laststart = 0;
begalt = b;
break;
case ')':
if (stackp == stackb) goto unmatched_close;
begalt = *--stackp + bufp->buffer;
if (fixup_jump)
store_jump (fixup_jump, jump, b);
if (stackp[-1] < RE_NREGS)
{
PATPUSH (stop_memory);
PATPUSH (stackp[-1]);
}
stackp -= 2;
fixup_jump = 0;
if (*stackp)
fixup_jump = *stackp + bufp->buffer - 1;
laststart = *--stackp + bufp->buffer;
break;
case '|':
insert_jump (on_failure_jump, begalt, b + 6, b);
pending_exact = 0;
b += 3;
if (fixup_jump)
store_jump (fixup_jump, jump, b);
fixup_jump = b;
b += 3;
laststart = 0;
begalt = b;
break;
#ifdef emacs
case '=':
PATPUSH (at_dot);
break;
case 's':
laststart = b;
PATPUSH (syntaxspec);
PATFETCH (c);
PATPUSH (syntax_spec_code[c]);
break;
case 'S':
laststart = b;
PATPUSH (notsyntaxspec);
PATFETCH (c);
PATPUSH (syntax_spec_code[c]);
break;
#endif emacs
case 'w':
laststart = b;
PATPUSH (wordchar);
break;
case 'W':
laststart = b;
PATPUSH (notwordchar);
break;
case '<':
PATPUSH (wordbeg);
break;
case '>':
PATPUSH (wordend);
break;
case 'b':
PATPUSH (wordbound);
break;
case 'B':
PATPUSH (notwordbound);
break;
case '`':
PATPUSH (begbuf);
break;
case '\'':
PATPUSH (endbuf);
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
c1 = c - '0';
if (c1 >= regnum)
goto normal_char;
for (stackt = stackp - 2; stackt > stackb; stackt -= 4)
if (*stackt == c1)
goto normal_char;
laststart = b;
PATPUSH (duplicate);
PATPUSH (c1);
break;
default:
goto normal_char;
}
break;
default:
normal_char:
if (!pending_exact || pending_exact + *pending_exact + 1 != b
|| *pending_exact == 0177 || *p == '*' || *p == '^'
|| *p == '+' || *p == '?')
{
laststart = b;
PATPUSH (exactn);
pending_exact = b;
PATPUSH (0);
}
PATPUSH (c);
(*pending_exact)++;
}
}
if (fixup_jump)
store_jump (fixup_jump, jump, b);
if (stackp != stackb) goto unmatched_open;
bufp->used = b - bufp->buffer;
return 0;
invalid_pattern:
return "Invalid regular expression";
unmatched_open:
return "Unmatched \\(";
unmatched_close:
return "Unmatched \\)";
end_of_pattern:
return "Premature end of regular expression";
nesting_too_deep:
return "Nesting too deep";
too_big:
return "Regular expression too big";
memory_exhausted:
return "Memory exhausted";
}
/* Store where `from' points a jump operation to jump to where `to' points.
`opcode' is the opcode to store. */
static int
store_jump (from, opcode, to)
char *from, *to;
char opcode;
{
from[0] = opcode;
from[1] = (to - (from + 3)) & 0377;
from[2] = (to - (from + 3)) >> 8;
}
/* Open up space at char FROM, and insert there a jump to TO.
CURRENT_END gives te end of the storage no in use,
so we know how much data to copy up.
OP is the opcode of the jump to insert.
If you call this function, you must zero out pending_exact. */
static int
insert_jump (op, from, to, current_end)
char op;
char *from, *to, *current_end;
{
register char *pto = current_end + 3;
register char *pfrom = current_end;
while (pfrom != from)
*--pto = *--pfrom;
store_jump (from, op, to);
}
/* Given a pattern, compute a fastmap from it.
The fastmap records which of the (1 << BYTEWIDTH) possible characters
can start a string that matches the pattern.
This fastmap is used by re_search to skip quickly over totally implausible text.
The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
as bufp->fastmap.
The other components of bufp describe the pattern to be used. */
VOID
re_compile_fastmap (bufp)
struct re_pattern_buffer *bufp;
{
unsigned char *pattern = (unsigned char *) bufp->buffer;
int size = bufp->used;
register char *fastmap = bufp->fastmap;
register unsigned char *p = pattern;
register unsigned char *pend = pattern + size;
register int j, k;
unsigned char *translate = (unsigned char *) bufp->translate;
unsigned char *stackb[NFAILURES];
unsigned char **stackp = stackb;
bzero (fastmap, (1 << BYTEWIDTH));
bufp->fastmap_accurate = 1;
bufp->can_be_null = 0;
while (p)
{
if (p == pend)
{
bufp->can_be_null = 1;
break;
}
switch ((regexpcode) *p++)
{
case exactn:
if (translate)
fastmap[translate[p[1]]] = 1;
else
fastmap[p[1]] = 1;
break;
case begline:
case before_dot:
case at_dot:
case after_dot:
case begbuf:
case endbuf:
case wordbound:
case notwordbound:
case wordbeg:
case wordend:
continue;
case endline:
if (translate)
fastmap[translate['\n']] = 1;
else
fastmap['\n'] = 1;
bufp->can_be_null = 1;
break;
case finalize_jump:
case maybe_finalize_jump:
case jump:
case dummy_failure_jump:
bufp->can_be_null = 1;
j = *p++ & 0377;
j += SIGN_EXTEND_CHAR (*(char *)p++) << 8;
p += j;
if (j > 0)
continue;
/* Jump backward reached implies we just went through
the body of a loop and matched nothing.
Opcode jumped to should be an on_failure_jump.
Just treat it like an ordinary jump.
For a * loop, it has pushed its failure point already;
if so, discard that as redundant. */
if ((regexpcode) *p != on_failure_jump)
continue;
p++;
j = *p++ & 0377;
j += SIGN_EXTEND_CHAR (*(char *)p++) << 8;
p += j;
if (stackp != stackb && *stackp == p)
stackp--;
continue;
case on_failure_jump:
j = *p++ & 0377;
j += SIGN_EXTEND_CHAR (*(char *)p++) << 8;
*++stackp = p + j;
continue;
case start_memory:
case stop_memory:
p++;
continue;
case duplicate:
bufp->can_be_null = 1;
fastmap['\n'] = 1;
case anychar:
for (j = 0; j < (1 << BYTEWIDTH); j++)
if (j != '\n')
fastmap[j] = 1;
if (bufp->can_be_null)
return;
/* Don't return; check the alternative paths
so we can set can_be_null if appropriate. */
break;
case wordchar:
for (j = 0; j < (1 << BYTEWIDTH); j++)
if (SYNTAX (j) == Sword)
fastmap[j] = 1;
break;
case notwordchar:
for (j = 0; j < (1 << BYTEWIDTH); j++)
if (SYNTAX (j) != Sword)
fastmap[j] = 1;
break;
#ifdef emacs
case syntaxspec:
k = *p++;
for (j = 0; j < (1 << BYTEWIDTH); j++)
if (SYNTAX (j) == (enum syntaxcode) k)
fastmap[j] = 1;
break;
case notsyntaxspec:
for (j = 0; j < (1 << BYTEWIDTH); j++)
if (SYNTAX (j) != (enum syntaxcode) k)
fastmap[j] = 1;
break;
#endif emacs
case charset:
for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
{
if (translate)
fastmap[translate[j]] = 1;
else
fastmap[j] = 1;
}
break;
case charset_not:
/* Chars beyond end of map must be allowed */
for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
if (translate)
fastmap[translate[j]] = 1;
else
fastmap[j] = 1;
for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
{
if (translate)
fastmap[translate[j]] = 1;
else
fastmap[j] = 1;
}
break;
}
/* Get here means we have successfully found the possible starting characters
of one path of the pattern. We need not follow this path any farther.
Instead, look at the next alternative remembered in the stack. */
if (stackp != stackb)
p = *stackp--;
else
break;
}
}
/* Like re_search_2, below, but only one string is specified. */
int
re_search (pbufp, string, size, startpos, range, regs)
struct re_pattern_buffer *pbufp;
char *string;
int size, startpos, range;
struct re_registers *regs;
{
return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
}
/* Like re_match_2 but tries first a match starting at index `startpos',
then at startpos + 1, and so on.
`range' is the number of places to try before giving up.
If `range' is negative, the starting positions tried are
startpos, startpos - 1, etc.
It is up to the caller to make sure that range is not so large
as to take the starting position outside of the input strings.
The value returned is the position at which the match was found,
or -1 if no match was found. */
int
re_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs, mstop)
struct re_pattern_buffer *pbufp;
char *string1, *string2;
int size1, size2;
int startpos;
register int range;
struct re_registers *regs;
int mstop;
{
register char *fastmap = pbufp->fastmap;
register char *translate = pbufp->translate;
int total = size1 + size2;
/* Update the fastmap now if not correct already */
if (fastmap && !pbufp->fastmap_accurate)
re_compile_fastmap (pbufp);
while (1)
{
/* If a fastmap is supplied, skip quickly over characters
that cannot possibly be the start of a match.
Note, however, that if the pattern can possibly match
the null string, we must test it at each starting point
so that we take the first null string we get. */
if (fastmap && startpos < total && !pbufp->can_be_null)
{
if (range > 0)
{
register int lim = 0;
register char *p;
int irange = range;
if (startpos < size1 && startpos + range >= size1)
lim = range - (size1 - startpos);
p = &(startpos >= size1 ? string2 - size1 : string1)[startpos];
if (translate)
{
while (range > lim && !fastmap[translate[*p++]])
range--;
}
else
{
while (range > lim && !fastmap[*p++])
range--;
}
startpos += irange - range;
}
else
{
register char c;
if (startpos >= size1) c = string2[startpos - size1];
else c = string1[startpos];
if (translate ? !fastmap[translate[c]] : !fastmap[c])
goto advance;
}
}
if (range >= 0 && startpos == total
&& fastmap && !pbufp->can_be_null)
return -1;
if (0 <= re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs, mstop))
return startpos;
#ifdef C_ALLOCA
alloca (0);
#endif /* C_ALLOCA */
advance:
if (!range) break;
if (range > 0) range--, startpos++; else range++, startpos--;
}
return -1;
}
#ifndef ng /* Ng never uses this */
#ifndef emacs /* emacs never uses this */
int
re_match (pbufp, string, size, pos, regs)
struct re_pattern_buffer *pbufp;
char *string;
int size, pos;
struct re_registers *regs;
{
return re_match_2 (pbufp, 0, 0, string, size, pos, regs, size);
}
#endif /* emacs */
#endif /* ng */
/* Match the pattern described by `pbufp'
against data which is the virtual concatenation of `string1' and `string2'.
`size1' and `size2' are the sizes of the two data strings.
Start the match at position `pos'.
Do not consider matching past the position `mstop'.
If pbufp->fastmap is nonzero, then it had better be up to date.
The reason that the data to match is specified as two components
which are to be regarded as concatenated
is so that this function can be used directly on the contents of an Emacs buffer.
-1 is returned if there is no match. Otherwise the value is the length
of the substring which was matched.
*/
int
re_match_2 (pbufp, string1, size1, string2, size2, pos, regs, mstop)
struct re_pattern_buffer *pbufp;
char *string1, *string2;
int size1, size2;
int pos;
struct re_registers *regs;
int mstop;
{
register char *p = pbufp->buffer;
register char *pend = p + pbufp->used;
/* End of first string */
char *end1;
/* End of second string */
char *end2;
/* Pointer just past last char to consider matching */
char *end_match_1, *end_match_2;
register char *d, *dend;
register int mcnt;
char *translate = pbufp->translate;
/* Failure point stack. Each place that can handle a failure further down the line
pushes a failure point on this stack. It consists of two char *'s.
The first one pushed is where to resume scanning the pattern;
the second pushed is where to resume scanning the strings.
If the latter is zero, the failure point is a "dummy".
If a failure happens and the innermost failure point is dormant,
it discards that failure point and tries the next one. */
char **stackb = (char **) alloca (2 * NFAILURES * sizeof (char *));
char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
/* Information on the "contents" of registers.
These are pointers into the input strings; they record
just what was matched (on this attempt) by some part of the pattern.
The start_memory command stores the start of a register's contents
and the stop_memory command stores the end.
At that point, regstart[regnum] points to the first character in the register,
regend[regnum] points to the first character beyond the end of the register,
and regstart_segend[regnum] is either the same as regend[regnum]
or else points to the end of the input string into which regstart[regnum] points.
The latter case happens when regstart[regnum] is in string1 and
regend[regnum] is in string2. */
char *regstart[RE_NREGS];
char *regstart_segend[RE_NREGS];
char *regend[RE_NREGS];
/* Set up pointers to ends of strings.
Don't allow the second string to be empty unless both are empty. */
if (!size2)
{
string2 = string1;
size2 = size1;
string1 = 0;
size1 = 0;