]>
git.saurik.com Git - apple/libc.git/blob - regex/engine.c
4bcabfb7a85aa2f040058c586f8dcb12692815b6
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1992, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * The matching engine and friends. This file is #included by regexec.c
63 * after suitable #defines of a variety of macros used herein, so that
64 * different state representations can be used without duplicating masses
69 #define matcher smatcher
72 #define dissect sdissect
73 #define backref sbackref
80 #define matcher lmatcher
83 #define dissect ldissect
84 #define backref lbackref
91 /* another structure passed up and down to avoid zillions of parameters */
95 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
96 char *offp
; /* offsets work from here */
97 char *beginp
; /* start of string -- virtual NUL precedes */
98 char *endp
; /* end of string -- virtual NUL here */
99 char *coldp
; /* can be no match starting before here */
100 char **lastpos
; /* [nplus+1] */
102 states st
; /* current states */
103 states fresh
; /* states for a fresh start */
104 states tmp
; /* temporary */
105 states empty
; /* empty set of states */
108 /* ========= begin header generated by ./mkh ========= */
113 /* === engine.c === */
114 static int matcher
__P((struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
));
115 static char *dissect
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
116 static char *backref
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
, sopno lev
));
117 static char *fast
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
118 static char *slow
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
119 static states step
__P((struct re_guts
*g
, sopno start
, sopno stop
, states bef
, int ch
, states aft
));
122 #define BOLEOL (BOL+2)
123 #define NOTHING (BOL+3)
126 #define CODEMAX (BOL+5) /* highest code used */
127 #define NONCHAR(c) ((c) > CHAR_MAX)
128 #define NNONCHAR (CODEMAX-CHAR_MAX)
130 static void print
__P((struct match
*m
, char *caption
, states st
, int ch
, FILE *d
));
133 static void at
__P((struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
));
136 static char *pchar
__P((int ch
));
142 /* ========= end header generated by ./mkh ========= */
145 #define SP(t, s, c) print(m, t, s, c, stdout)
146 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
147 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
149 #define SP(t, s, c) /* nothing */
150 #define AT(t, p1, p2, s1, s2) /* nothing */
151 #define NOTE(s) /* nothing */
155 - matcher - the actual matching engine
156 == static int matcher(register struct re_guts *g, char *string, \
157 == size_t nmatch, regmatch_t pmatch[], int eflags);
159 static int /* 0 success, REG_NOMATCH failure */
160 matcher(g
, string
, nmatch
, pmatch
, eflags
)
161 register struct re_guts
*g
;
170 register struct match
*m
= &mv
;
172 const register sopno gf
= g
->firststate
+1; /* +1 for OEND */
173 const register sopno gl
= g
->laststate
;
177 /* simplify the situation where possible */
178 if (g
->cflags
®_NOSUB
)
180 if (eflags
®_STARTEND
) {
181 start
= string
+ pmatch
[0].rm_so
;
182 stop
= string
+ pmatch
[0].rm_eo
;
185 stop
= start
+ strlen(start
);
190 /* prescreening; this does wonders for this rather slow code */
191 if (g
->must
!= NULL
) {
192 for (dp
= start
; dp
< stop
; dp
++)
193 if (*dp
== g
->must
[0] && stop
- dp
>= g
->mlen
&&
194 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
196 if (dp
== stop
) /* we didn't find g->must */
200 /* match struct setup */
215 /* this loop does only one repetition except for backrefs */
217 endp
= fast(m
, start
, stop
, gf
, gl
);
218 if (endp
== NULL
) { /* a miss */
222 if (nmatch
== 0 && !g
->backrefs
)
223 break; /* no further info needed */
226 assert(m
->coldp
!= NULL
);
228 NOTE("finding start");
229 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
232 assert(m
->coldp
< m
->endp
);
235 if (nmatch
== 1 && !g
->backrefs
)
236 break; /* no further info needed */
238 /* oh my, he wants the subexpressions... */
239 if (m
->pmatch
== NULL
)
240 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
242 if (m
->pmatch
== NULL
) {
246 for (i
= 1; i
<= m
->g
->nsub
; i
++)
247 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
248 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
250 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
252 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
253 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
255 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
260 NOTE("backref dissect");
261 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
266 /* uh-oh... we couldn't find a subexpression-level match */
267 assert(g
->backrefs
); /* must be back references doing it */
268 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
270 if (dp
!= NULL
|| endp
<= m
->coldp
)
273 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
276 /* try it on a shorter possibility */
278 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
279 assert(m
->pmatch
[i
].rm_so
== -1);
280 assert(m
->pmatch
[i
].rm_eo
== -1);
283 NOTE("backoff dissect");
284 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
286 assert(dp
== NULL
|| dp
== endp
);
287 if (dp
!= NULL
) /* found a shorter one */
290 /* despite initial appearances, there is no match here */
292 start
= m
->coldp
+ 1; /* recycle starting later */
293 assert(start
<= stop
);
296 /* fill in the details if requested */
298 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
299 pmatch
[0].rm_eo
= endp
- m
->offp
;
302 assert(m
->pmatch
!= NULL
);
303 for (i
= 1; i
< nmatch
; i
++)
305 pmatch
[i
] = m
->pmatch
[i
];
307 pmatch
[i
].rm_so
= -1;
308 pmatch
[i
].rm_eo
= -1;
312 if (m
->pmatch
!= NULL
)
313 free((char *)m
->pmatch
);
314 if (m
->lastpos
!= NULL
)
315 free((char *)m
->lastpos
);
321 - dissect - figure out what matched what, no back references
322 == static char *dissect(register struct match *m, char *start, \
323 == char *stop, sopno startst, sopno stopst);
325 static char * /* == stop (success) always */
326 dissect(m
, start
, stop
, startst
, stopst
)
327 register struct match
*m
;
334 register sopno ss
; /* start sop of current subRE */
335 register sopno es
; /* end sop of current subRE */
336 register char *sp
; /* start of string matched by it */
337 register char *stp
; /* string matched by it cannot pass here */
338 register char *rest
; /* start of rest of string */
339 register char *tail
; /* string unmatched by rest of RE */
340 register sopno ssub
; /* start sop of subsubRE */
341 register sopno esub
; /* end sop of subsubRE */
342 register char *ssp
; /* start of string matched by subsubRE */
343 register char *sep
; /* end of string matched by subsubRE */
344 register char *oldssp
; /* previous ssp */
347 AT("diss", start
, stop
, startst
, stopst
);
349 for (ss
= startst
; ss
< stopst
; ss
= es
) {
350 /* identify end of subRE */
352 switch (OP(m
->g
->strip
[es
])) {
355 es
+= OPND(m
->g
->strip
[es
]);
358 while (OP(m
->g
->strip
[es
]) != O_CH
)
359 es
+= OPND(m
->g
->strip
[es
]);
364 /* figure out what it matched */
365 switch (OP(m
->g
->strip
[ss
])) {
385 /* cases where length of match is hard to find */
389 /* how long could this one be? */
390 rest
= slow(m
, sp
, stp
, ss
, es
);
391 assert(rest
!= NULL
); /* it did match */
392 /* could the rest match the rest? */
393 tail
= slow(m
, rest
, stop
, es
, stopst
);
396 /* no -- try a shorter match for this one */
398 assert(stp
>= sp
); /* it did work */
402 /* did innards match? */
403 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
404 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
413 /* how long could this one be? */
414 rest
= slow(m
, sp
, stp
, ss
, es
);
415 assert(rest
!= NULL
); /* it did match */
416 /* could the rest match the rest? */
417 tail
= slow(m
, rest
, stop
, es
, stopst
);
420 /* no -- try a shorter match for this one */
422 assert(stp
>= sp
); /* it did work */
428 for (;;) { /* find last match of innards */
429 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
430 if (sep
== NULL
|| sep
== ssp
)
431 break; /* failed or matched null */
432 oldssp
= ssp
; /* on to next try */
436 /* last successful match */
440 assert(sep
== rest
); /* must exhaust substring */
441 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
442 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
449 /* how long could this one be? */
450 rest
= slow(m
, sp
, stp
, ss
, es
);
451 assert(rest
!= NULL
); /* it did match */
452 /* could the rest match the rest? */
453 tail
= slow(m
, rest
, stop
, es
, stopst
);
456 /* no -- try a shorter match for this one */
458 assert(stp
>= sp
); /* it did work */
461 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
462 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
463 for (;;) { /* find first matching branch */
464 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
465 break; /* it matched all of it */
466 /* that one missed, try next one */
467 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
469 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
471 esub
+= OPND(m
->g
->strip
[esub
]);
472 if (OP(m
->g
->strip
[esub
]) == OOR2
)
475 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
477 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
489 i
= OPND(m
->g
->strip
[ss
]);
490 assert(0 < i
&& i
<= m
->g
->nsub
);
491 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
494 i
= OPND(m
->g
->strip
[ss
]);
495 assert(0 < i
&& i
<= m
->g
->nsub
);
496 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
509 - backref - figure out what matched what, figuring in back references
510 == static char *backref(register struct match *m, char *start, \
511 == char *stop, sopno startst, sopno stopst, sopno lev);
513 static char * /* == stop (success) or NULL (failure) */
514 backref(m
, start
, stop
, startst
, stopst
, lev
)
515 register struct match
*m
;
520 sopno lev
; /* PLUS nesting level */
523 register sopno ss
; /* start sop of current subRE */
524 register char *sp
; /* start of string matched by it */
525 register sopno ssub
; /* start sop of subsubRE */
526 register sopno esub
; /* end sop of subsubRE */
527 register char *ssp
; /* start of string matched by subsubRE */
532 register regoff_t offsave
;
535 AT("back", start
, stop
, startst
, stopst
);
538 /* get as far as we can with easy stuff */
540 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
541 switch (OP(s
= m
->g
->strip
[ss
])) {
543 if (sp
== stop
|| *sp
++ != (char)OPND(s
))
552 cs
= &m
->g
->sets
[OPND(s
)];
553 if (sp
== stop
|| !CHIN(cs
, *sp
++))
557 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
558 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
559 (m
->g
->cflags
®_NEWLINE
)) )
565 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
566 (sp
< m
->endp
&& *sp
== '\n' &&
567 (m
->g
->cflags
®_NEWLINE
)) )
573 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
574 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
575 (m
->g
->cflags
®_NEWLINE
)) ||
577 !ISWORD(*(sp
-1))) ) &&
578 (sp
< m
->endp
&& ISWORD(*sp
)) )
584 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
585 (sp
< m
->endp
&& *sp
== '\n' &&
586 (m
->g
->cflags
®_NEWLINE
)) ||
587 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
588 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
595 case OOR1
: /* matches null but needs to skip */
599 assert(OP(s
) == OOR2
);
601 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
602 /* note that the ss++ gets us past the O_CH */
604 default: /* have to make a choice */
608 if (!hard
) { /* that was it! */
613 ss
--; /* adjust for the for's final increment */
616 AT("hard", sp
, stop
, ss
, stopst
);
619 case OBACK_
: /* the vilest depths */
621 assert(0 < i
&& i
<= m
->g
->nsub
);
622 if (m
->pmatch
[i
].rm_eo
== -1)
624 assert(m
->pmatch
[i
].rm_so
!= -1);
625 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
626 assert(stop
- m
->beginp
>= len
);
628 return(NULL
); /* not enough left to match */
629 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
630 if (memcmp(sp
, ssp
, len
) != 0)
632 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
634 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
636 case OQUEST_
: /* to null or not */
637 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
639 return(dp
); /* not */
640 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
643 assert(m
->lastpos
!= NULL
);
644 assert(lev
+1 <= m
->g
->nplus
);
645 m
->lastpos
[lev
+1] = sp
;
646 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
649 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
650 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
651 /* try another pass */
652 m
->lastpos
[lev
] = sp
;
653 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
655 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
659 case OCH_
: /* find the right one, if any */
661 esub
= ss
+ OPND(s
) - 1;
662 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
663 for (;;) { /* find first matching branch */
664 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
667 /* that one missed, try next one */
668 if (OP(m
->g
->strip
[esub
]) == O_CH
)
669 return(NULL
); /* there is none */
671 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
673 esub
+= OPND(m
->g
->strip
[esub
]);
674 if (OP(m
->g
->strip
[esub
]) == OOR2
)
677 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
680 case OLPAREN
: /* must undo assignment if rest fails */
682 assert(0 < i
&& i
<= m
->g
->nsub
);
683 offsave
= m
->pmatch
[i
].rm_so
;
684 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
685 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
688 m
->pmatch
[i
].rm_so
= offsave
;
691 case ORPAREN
: /* must undo assignment if rest fails */
693 assert(0 < i
&& i
<= m
->g
->nsub
);
694 offsave
= m
->pmatch
[i
].rm_eo
;
695 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
696 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
699 m
->pmatch
[i
].rm_eo
= offsave
;
713 - fast - step through the string at top speed
714 == static char *fast(register struct match *m, char *start, \
715 == char *stop, sopno startst, sopno stopst);
717 static char * /* where tentative match ended, or NULL */
718 fast(m
, start
, stop
, startst
, stopst
)
719 register struct match
*m
;
725 register states st
= m
->st
;
726 register states fresh
= m
->fresh
;
727 register states tmp
= m
->tmp
;
728 register char *p
= start
;
729 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
730 register int lastc
; /* previous c */
733 register char *coldp
; /* last p after which no match was underway */
737 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
744 c
= (p
== m
->endp
) ? OUT
: *p
;
748 /* is there an EOL and/or BOL between lastc and c? */
751 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
752 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
756 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
757 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
758 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
763 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
767 /* how about a word boundary? */
768 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
769 (c
!= OUT
&& ISWORD(c
)) ) {
772 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
773 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
776 if (flagch
== BOW
|| flagch
== EOW
) {
777 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
782 if (ISSET(st
, stopst
) || p
== stop
)
783 break; /* NOTE BREAK OUT */
785 /* no, we must deal with this character */
789 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
791 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
795 assert(coldp
!= NULL
);
797 if (ISSET(st
, stopst
))
804 - slow - step through the string more deliberately
805 == static char *slow(register struct match *m, char *start, \
806 == char *stop, sopno startst, sopno stopst);
808 static char * /* where it ended */
809 slow(m
, start
, stop
, startst
, stopst
)
810 register struct match
*m
;
816 register states st
= m
->st
;
817 register states empty
= m
->empty
;
818 register states tmp
= m
->tmp
;
819 register char *p
= start
;
820 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
821 register int lastc
; /* previous c */
824 register char *matchp
; /* last p at which a match ended */
826 AT("slow", start
, stop
, startst
, stopst
);
829 SP("sstart", st
, *p
);
830 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
835 c
= (p
== m
->endp
) ? OUT
: *p
;
837 /* is there an EOL and/or BOL between lastc and c? */
840 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
841 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
845 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
846 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
847 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
852 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
853 SP("sboleol", st
, c
);
856 /* how about a word boundary? */
857 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
858 (c
!= OUT
&& ISWORD(c
)) ) {
861 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
862 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
865 if (flagch
== BOW
|| flagch
== EOW
) {
866 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
867 SP("sboweow", st
, c
);
871 if (ISSET(st
, stopst
))
873 if (EQ(st
, empty
) || p
== stop
)
874 break; /* NOTE BREAK OUT */
876 /* no, we must deal with this character */
880 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
882 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
891 - step - map set of states reachable before char to set reachable after
892 == static states step(register struct re_guts *g, sopno start, sopno stop, \
893 == register states bef, int ch, register states aft);
894 == #define BOL (OUT+1)
895 == #define EOL (BOL+1)
896 == #define BOLEOL (BOL+2)
897 == #define NOTHING (BOL+3)
898 == #define BOW (BOL+4)
899 == #define EOW (BOL+5)
900 == #define CODEMAX (BOL+5) // highest code used
901 == #define NONCHAR(c) ((c) > CHAR_MAX)
902 == #define NNONCHAR (CODEMAX-CHAR_MAX)
905 step(g
, start
, stop
, bef
, ch
, aft
)
906 register struct re_guts
*g
;
907 sopno start
; /* start state within strip */
908 sopno stop
; /* state after stop state within strip */
909 register states bef
; /* states reachable before */
910 int ch
; /* character or NONCHAR code */
911 register states aft
; /* states already known reachable after */
916 register onestate here
; /* note, macros know this name */
920 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
924 assert(pc
== stop
-1);
927 /* only characters can match */
928 assert(!NONCHAR(ch
) || ch
!= (char)OPND(s
));
929 if (ch
== (char)OPND(s
))
933 if (ch
== BOL
|| ch
== BOLEOL
)
937 if (ch
== EOL
|| ch
== BOLEOL
)
953 cs
= &g
->sets
[OPND(s
)];
954 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
957 case OBACK_
: /* ignored here */
961 case OPLUS_
: /* forward, this is just an empty */
964 case O_PLUS
: /* both forward and back */
966 i
= ISSETBACK(aft
, OPND(s
));
967 BACK(aft
, aft
, OPND(s
));
968 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
969 /* oho, must reconsider loop body */
974 case OQUEST_
: /* two branches, both forward */
976 FWD(aft
, aft
, OPND(s
));
978 case O_QUEST
: /* just an empty */
981 case OLPAREN
: /* not significant here */
985 case OCH_
: /* mark the first two branches */
987 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
988 FWD(aft
, aft
, OPND(s
));
990 case OOR1
: /* done a branch, find the O_CH */
991 if (ISSTATEIN(aft
, here
)) {
993 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
995 assert(OP(s
) == OOR2
);
999 case OOR2
: /* propagate OCH_'s marking */
1001 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
1002 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1003 FWD(aft
, aft
, OPND(s
));
1006 case O_CH
: /* just empty */
1009 default: /* ooooops... */
1020 - print - print a set of states
1022 == static void print(struct match *m, char *caption, states st, \
1023 == int ch, FILE *d);
1027 print(m
, caption
, st
, ch
, d
)
1034 register struct re_guts
*g
= m
->g
;
1036 register int first
= 1;
1038 if (!(m
->eflags
®_TRACE
))
1041 fprintf(d
, "%s", caption
);
1043 fprintf(d
, " %s", pchar(ch
));
1044 for (i
= 0; i
< g
->nstates
; i
++)
1046 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1053 - at - print current situation
1055 == static void at(struct match *m, char *title, char *start, char *stop, \
1056 == sopno startst, sopno stopst);
1060 at(m
, title
, start
, stop
, startst
, stopst
)
1068 if (!(m
->eflags
®_TRACE
))
1071 printf("%s %s-", title
, pchar(*start
));
1072 printf("%s ", pchar(*stop
));
1073 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1077 #define PCHARDONE /* never again */
1079 - pchar - make a character printable
1081 == static char *pchar(int ch);
1084 * Is this identical to regchar() over in debug.c? Well, yes. But a
1085 * duplicate here avoids having a debugging-capable regexec.o tied to
1086 * a matching debug.o, and this is convenient. It all disappears in
1087 * the non-debug compilation anyway, so it doesn't matter much.
1089 static char * /* -> representation */
1093 static char pbuf
[10];
1095 if (isprint(ch
) || ch
== ' ')
1096 sprintf(pbuf
, "%c", ch
);
1098 sprintf(pbuf
, "\\%o", ch
);