]>
git.saurik.com Git - apple/libc.git/blob - regex/engine.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1992, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * The matching engine and friends. This file is #included by regexec.c
60 * after suitable #defines of a variety of macros used herein, so that
61 * different state representations can be used without duplicating masses
66 #define matcher smatcher
69 #define dissect sdissect
70 #define backref sbackref
77 #define matcher lmatcher
80 #define dissect ldissect
81 #define backref lbackref
88 /* another structure passed up and down to avoid zillions of parameters */
92 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
93 char *offp
; /* offsets work from here */
94 char *beginp
; /* start of string -- virtual NUL precedes */
95 char *endp
; /* end of string -- virtual NUL here */
96 char *coldp
; /* can be no match starting before here */
97 char **lastpos
; /* [nplus+1] */
99 states st
; /* current states */
100 states fresh
; /* states for a fresh start */
101 states tmp
; /* temporary */
102 states empty
; /* empty set of states */
105 /* ========= begin header generated by ./mkh ========= */
110 /* === engine.c === */
111 static int matcher
__P((struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
));
112 static char *dissect
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
113 static char *backref
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
, sopno lev
));
114 static char *fast
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
115 static char *slow
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
116 static states step
__P((struct re_guts
*g
, sopno start
, sopno stop
, states bef
, int ch
, states aft
));
119 #define BOLEOL (BOL+2)
120 #define NOTHING (BOL+3)
123 #define CODEMAX (BOL+5) /* highest code used */
124 #define NONCHAR(c) ((c) > CHAR_MAX)
125 #define NNONCHAR (CODEMAX-CHAR_MAX)
127 static void print
__P((struct match
*m
, char *caption
, states st
, int ch
, FILE *d
));
130 static void at
__P((struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
));
133 static char *pchar
__P((int ch
));
139 /* ========= end header generated by ./mkh ========= */
142 #define SP(t, s, c) print(m, t, s, c, stdout)
143 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
144 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
146 #define SP(t, s, c) /* nothing */
147 #define AT(t, p1, p2, s1, s2) /* nothing */
148 #define NOTE(s) /* nothing */
152 - matcher - the actual matching engine
153 == static int matcher(register struct re_guts *g, char *string, \
154 == size_t nmatch, regmatch_t pmatch[], int eflags);
156 static int /* 0 success, REG_NOMATCH failure */
157 matcher(g
, string
, nmatch
, pmatch
, eflags
)
158 register struct re_guts
*g
;
167 register struct match
*m
= &mv
;
169 const register sopno gf
= g
->firststate
+1; /* +1 for OEND */
170 const register sopno gl
= g
->laststate
;
174 /* simplify the situation where possible */
175 if (g
->cflags
®_NOSUB
)
177 if (eflags
®_STARTEND
) {
178 start
= string
+ pmatch
[0].rm_so
;
179 stop
= string
+ pmatch
[0].rm_eo
;
182 stop
= start
+ strlen(start
);
187 /* prescreening; this does wonders for this rather slow code */
188 if (g
->must
!= NULL
) {
189 for (dp
= start
; dp
< stop
; dp
++)
190 if (*dp
== g
->must
[0] && stop
- dp
>= g
->mlen
&&
191 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
193 if (dp
== stop
) /* we didn't find g->must */
197 /* match struct setup */
212 /* this loop does only one repetition except for backrefs */
214 endp
= fast(m
, start
, stop
, gf
, gl
);
215 if (endp
== NULL
) { /* a miss */
219 if (nmatch
== 0 && !g
->backrefs
)
220 break; /* no further info needed */
223 assert(m
->coldp
!= NULL
);
225 NOTE("finding start");
226 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
229 assert(m
->coldp
< m
->endp
);
232 if (nmatch
== 1 && !g
->backrefs
)
233 break; /* no further info needed */
235 /* oh my, he wants the subexpressions... */
236 if (m
->pmatch
== NULL
)
237 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
239 if (m
->pmatch
== NULL
) {
243 for (i
= 1; i
<= m
->g
->nsub
; i
++)
244 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
245 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
247 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
249 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
250 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
252 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
257 NOTE("backref dissect");
258 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
263 /* uh-oh... we couldn't find a subexpression-level match */
264 assert(g
->backrefs
); /* must be back references doing it */
265 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
267 if (dp
!= NULL
|| endp
<= m
->coldp
)
270 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
273 /* try it on a shorter possibility */
275 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
276 assert(m
->pmatch
[i
].rm_so
== -1);
277 assert(m
->pmatch
[i
].rm_eo
== -1);
280 NOTE("backoff dissect");
281 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
283 assert(dp
== NULL
|| dp
== endp
);
284 if (dp
!= NULL
) /* found a shorter one */
287 /* despite initial appearances, there is no match here */
289 start
= m
->coldp
+ 1; /* recycle starting later */
290 assert(start
<= stop
);
293 /* fill in the details if requested */
295 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
296 pmatch
[0].rm_eo
= endp
- m
->offp
;
299 assert(m
->pmatch
!= NULL
);
300 for (i
= 1; i
< nmatch
; i
++)
302 pmatch
[i
] = m
->pmatch
[i
];
304 pmatch
[i
].rm_so
= -1;
305 pmatch
[i
].rm_eo
= -1;
309 if (m
->pmatch
!= NULL
)
310 free((char *)m
->pmatch
);
311 if (m
->lastpos
!= NULL
)
312 free((char *)m
->lastpos
);
318 - dissect - figure out what matched what, no back references
319 == static char *dissect(register struct match *m, char *start, \
320 == char *stop, sopno startst, sopno stopst);
322 static char * /* == stop (success) always */
323 dissect(m
, start
, stop
, startst
, stopst
)
324 register struct match
*m
;
331 register sopno ss
; /* start sop of current subRE */
332 register sopno es
; /* end sop of current subRE */
333 register char *sp
; /* start of string matched by it */
334 register char *stp
; /* string matched by it cannot pass here */
335 register char *rest
; /* start of rest of string */
336 register char *tail
; /* string unmatched by rest of RE */
337 register sopno ssub
; /* start sop of subsubRE */
338 register sopno esub
; /* end sop of subsubRE */
339 register char *ssp
; /* start of string matched by subsubRE */
340 register char *sep
; /* end of string matched by subsubRE */
341 register char *oldssp
; /* previous ssp */
344 AT("diss", start
, stop
, startst
, stopst
);
346 for (ss
= startst
; ss
< stopst
; ss
= es
) {
347 /* identify end of subRE */
349 switch (OP(m
->g
->strip
[es
])) {
352 es
+= OPND(m
->g
->strip
[es
]);
355 while (OP(m
->g
->strip
[es
]) != O_CH
)
356 es
+= OPND(m
->g
->strip
[es
]);
361 /* figure out what it matched */
362 switch (OP(m
->g
->strip
[ss
])) {
382 /* cases where length of match is hard to find */
386 /* how long could this one be? */
387 rest
= slow(m
, sp
, stp
, ss
, es
);
388 assert(rest
!= NULL
); /* it did match */
389 /* could the rest match the rest? */
390 tail
= slow(m
, rest
, stop
, es
, stopst
);
393 /* no -- try a shorter match for this one */
395 assert(stp
>= sp
); /* it did work */
399 /* did innards match? */
400 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
401 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
410 /* how long could this one be? */
411 rest
= slow(m
, sp
, stp
, ss
, es
);
412 assert(rest
!= NULL
); /* it did match */
413 /* could the rest match the rest? */
414 tail
= slow(m
, rest
, stop
, es
, stopst
);
417 /* no -- try a shorter match for this one */
419 assert(stp
>= sp
); /* it did work */
425 for (;;) { /* find last match of innards */
426 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
427 if (sep
== NULL
|| sep
== ssp
)
428 break; /* failed or matched null */
429 oldssp
= ssp
; /* on to next try */
433 /* last successful match */
437 assert(sep
== rest
); /* must exhaust substring */
438 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
439 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
446 /* how long could this one be? */
447 rest
= slow(m
, sp
, stp
, ss
, es
);
448 assert(rest
!= NULL
); /* it did match */
449 /* could the rest match the rest? */
450 tail
= slow(m
, rest
, stop
, es
, stopst
);
453 /* no -- try a shorter match for this one */
455 assert(stp
>= sp
); /* it did work */
458 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
459 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
460 for (;;) { /* find first matching branch */
461 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
462 break; /* it matched all of it */
463 /* that one missed, try next one */
464 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
466 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
468 esub
+= OPND(m
->g
->strip
[esub
]);
469 if (OP(m
->g
->strip
[esub
]) == OOR2
)
472 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
474 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
486 i
= OPND(m
->g
->strip
[ss
]);
487 assert(0 < i
&& i
<= m
->g
->nsub
);
488 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
491 i
= OPND(m
->g
->strip
[ss
]);
492 assert(0 < i
&& i
<= m
->g
->nsub
);
493 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
506 - backref - figure out what matched what, figuring in back references
507 == static char *backref(register struct match *m, char *start, \
508 == char *stop, sopno startst, sopno stopst, sopno lev);
510 static char * /* == stop (success) or NULL (failure) */
511 backref(m
, start
, stop
, startst
, stopst
, lev
)
512 register struct match
*m
;
517 sopno lev
; /* PLUS nesting level */
520 register sopno ss
; /* start sop of current subRE */
521 register char *sp
; /* start of string matched by it */
522 register sopno ssub
; /* start sop of subsubRE */
523 register sopno esub
; /* end sop of subsubRE */
524 register char *ssp
; /* start of string matched by subsubRE */
529 register regoff_t offsave
;
532 AT("back", start
, stop
, startst
, stopst
);
535 /* get as far as we can with easy stuff */
537 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
538 switch (OP(s
= m
->g
->strip
[ss
])) {
540 if (sp
== stop
|| *sp
++ != (char)OPND(s
))
549 cs
= &m
->g
->sets
[OPND(s
)];
550 if (sp
== stop
|| !CHIN(cs
, *sp
++))
554 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
555 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
556 (m
->g
->cflags
®_NEWLINE
)) )
562 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
563 (sp
< m
->endp
&& *sp
== '\n' &&
564 (m
->g
->cflags
®_NEWLINE
)) )
570 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
571 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
572 (m
->g
->cflags
®_NEWLINE
)) ||
574 !ISWORD(*(sp
-1))) ) &&
575 (sp
< m
->endp
&& ISWORD(*sp
)) )
581 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
582 (sp
< m
->endp
&& *sp
== '\n' &&
583 (m
->g
->cflags
®_NEWLINE
)) ||
584 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
585 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
592 case OOR1
: /* matches null but needs to skip */
596 assert(OP(s
) == OOR2
);
598 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
599 /* note that the ss++ gets us past the O_CH */
601 default: /* have to make a choice */
605 if (!hard
) { /* that was it! */
610 ss
--; /* adjust for the for's final increment */
613 AT("hard", sp
, stop
, ss
, stopst
);
616 case OBACK_
: /* the vilest depths */
618 assert(0 < i
&& i
<= m
->g
->nsub
);
619 if (m
->pmatch
[i
].rm_eo
== -1)
621 assert(m
->pmatch
[i
].rm_so
!= -1);
622 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
623 assert(stop
- m
->beginp
>= len
);
625 return(NULL
); /* not enough left to match */
626 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
627 if (memcmp(sp
, ssp
, len
) != 0)
629 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
631 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
633 case OQUEST_
: /* to null or not */
634 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
636 return(dp
); /* not */
637 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
640 assert(m
->lastpos
!= NULL
);
641 assert(lev
+1 <= m
->g
->nplus
);
642 m
->lastpos
[lev
+1] = sp
;
643 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
646 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
647 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
648 /* try another pass */
649 m
->lastpos
[lev
] = sp
;
650 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
652 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
656 case OCH_
: /* find the right one, if any */
658 esub
= ss
+ OPND(s
) - 1;
659 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
660 for (;;) { /* find first matching branch */
661 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
664 /* that one missed, try next one */
665 if (OP(m
->g
->strip
[esub
]) == O_CH
)
666 return(NULL
); /* there is none */
668 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
670 esub
+= OPND(m
->g
->strip
[esub
]);
671 if (OP(m
->g
->strip
[esub
]) == OOR2
)
674 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
677 case OLPAREN
: /* must undo assignment if rest fails */
679 assert(0 < i
&& i
<= m
->g
->nsub
);
680 offsave
= m
->pmatch
[i
].rm_so
;
681 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
682 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
685 m
->pmatch
[i
].rm_so
= offsave
;
688 case ORPAREN
: /* must undo assignment if rest fails */
690 assert(0 < i
&& i
<= m
->g
->nsub
);
691 offsave
= m
->pmatch
[i
].rm_eo
;
692 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
693 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
696 m
->pmatch
[i
].rm_eo
= offsave
;
710 - fast - step through the string at top speed
711 == static char *fast(register struct match *m, char *start, \
712 == char *stop, sopno startst, sopno stopst);
714 static char * /* where tentative match ended, or NULL */
715 fast(m
, start
, stop
, startst
, stopst
)
716 register struct match
*m
;
722 register states st
= m
->st
;
723 register states fresh
= m
->fresh
;
724 register states tmp
= m
->tmp
;
725 register char *p
= start
;
726 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
727 register int lastc
; /* previous c */
730 register char *coldp
; /* last p after which no match was underway */
734 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
741 c
= (p
== m
->endp
) ? OUT
: *p
;
745 /* is there an EOL and/or BOL between lastc and c? */
748 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
749 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
753 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
754 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
755 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
760 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
764 /* how about a word boundary? */
765 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
766 (c
!= OUT
&& ISWORD(c
)) ) {
769 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
770 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
773 if (flagch
== BOW
|| flagch
== EOW
) {
774 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
779 if (ISSET(st
, stopst
) || p
== stop
)
780 break; /* NOTE BREAK OUT */
782 /* no, we must deal with this character */
786 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
788 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
792 assert(coldp
!= NULL
);
794 if (ISSET(st
, stopst
))
801 - slow - step through the string more deliberately
802 == static char *slow(register struct match *m, char *start, \
803 == char *stop, sopno startst, sopno stopst);
805 static char * /* where it ended */
806 slow(m
, start
, stop
, startst
, stopst
)
807 register struct match
*m
;
813 register states st
= m
->st
;
814 register states empty
= m
->empty
;
815 register states tmp
= m
->tmp
;
816 register char *p
= start
;
817 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
818 register int lastc
; /* previous c */
821 register char *matchp
; /* last p at which a match ended */
823 AT("slow", start
, stop
, startst
, stopst
);
826 SP("sstart", st
, *p
);
827 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
832 c
= (p
== m
->endp
) ? OUT
: *p
;
834 /* is there an EOL and/or BOL between lastc and c? */
837 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
838 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
842 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
843 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
844 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
849 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
850 SP("sboleol", st
, c
);
853 /* how about a word boundary? */
854 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
855 (c
!= OUT
&& ISWORD(c
)) ) {
858 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
859 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
862 if (flagch
== BOW
|| flagch
== EOW
) {
863 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
864 SP("sboweow", st
, c
);
868 if (ISSET(st
, stopst
))
870 if (EQ(st
, empty
) || p
== stop
)
871 break; /* NOTE BREAK OUT */
873 /* no, we must deal with this character */
877 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
879 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
888 - step - map set of states reachable before char to set reachable after
889 == static states step(register struct re_guts *g, sopno start, sopno stop, \
890 == register states bef, int ch, register states aft);
891 == #define BOL (OUT+1)
892 == #define EOL (BOL+1)
893 == #define BOLEOL (BOL+2)
894 == #define NOTHING (BOL+3)
895 == #define BOW (BOL+4)
896 == #define EOW (BOL+5)
897 == #define CODEMAX (BOL+5) // highest code used
898 == #define NONCHAR(c) ((c) > CHAR_MAX)
899 == #define NNONCHAR (CODEMAX-CHAR_MAX)
902 step(g
, start
, stop
, bef
, ch
, aft
)
903 register struct re_guts
*g
;
904 sopno start
; /* start state within strip */
905 sopno stop
; /* state after stop state within strip */
906 register states bef
; /* states reachable before */
907 int ch
; /* character or NONCHAR code */
908 register states aft
; /* states already known reachable after */
913 register onestate here
; /* note, macros know this name */
917 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
921 assert(pc
== stop
-1);
924 /* only characters can match */
925 assert(!NONCHAR(ch
) || ch
!= (char)OPND(s
));
926 if (ch
== (char)OPND(s
))
930 if (ch
== BOL
|| ch
== BOLEOL
)
934 if (ch
== EOL
|| ch
== BOLEOL
)
950 cs
= &g
->sets
[OPND(s
)];
951 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
954 case OBACK_
: /* ignored here */
958 case OPLUS_
: /* forward, this is just an empty */
961 case O_PLUS
: /* both forward and back */
963 i
= ISSETBACK(aft
, OPND(s
));
964 BACK(aft
, aft
, OPND(s
));
965 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
966 /* oho, must reconsider loop body */
971 case OQUEST_
: /* two branches, both forward */
973 FWD(aft
, aft
, OPND(s
));
975 case O_QUEST
: /* just an empty */
978 case OLPAREN
: /* not significant here */
982 case OCH_
: /* mark the first two branches */
984 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
985 FWD(aft
, aft
, OPND(s
));
987 case OOR1
: /* done a branch, find the O_CH */
988 if (ISSTATEIN(aft
, here
)) {
990 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
992 assert(OP(s
) == OOR2
);
996 case OOR2
: /* propagate OCH_'s marking */
998 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
999 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1000 FWD(aft
, aft
, OPND(s
));
1003 case O_CH
: /* just empty */
1006 default: /* ooooops... */
1017 - print - print a set of states
1019 == static void print(struct match *m, char *caption, states st, \
1020 == int ch, FILE *d);
1024 print(m
, caption
, st
, ch
, d
)
1031 register struct re_guts
*g
= m
->g
;
1033 register int first
= 1;
1035 if (!(m
->eflags
®_TRACE
))
1038 fprintf(d
, "%s", caption
);
1040 fprintf(d
, " %s", pchar(ch
));
1041 for (i
= 0; i
< g
->nstates
; i
++)
1043 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1050 - at - print current situation
1052 == static void at(struct match *m, char *title, char *start, char *stop, \
1053 == sopno startst, sopno stopst);
1057 at(m
, title
, start
, stop
, startst
, stopst
)
1065 if (!(m
->eflags
®_TRACE
))
1068 printf("%s %s-", title
, pchar(*start
));
1069 printf("%s ", pchar(*stop
));
1070 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1074 #define PCHARDONE /* never again */
1076 - pchar - make a character printable
1078 == static char *pchar(int ch);
1081 * Is this identical to regchar() over in debug.c? Well, yes. But a
1082 * duplicate here avoids having a debugging-capable regexec.o tied to
1083 * a matching debug.o, and this is convenient. It all disappears in
1084 * the non-debug compilation anyway, so it doesn't matter much.
1086 static char * /* -> representation */
1090 static char pbuf
[10];
1092 if (isprint(ch
) || ch
== ' ')
1093 sprintf(pbuf
, "%c", ch
);
1095 sprintf(pbuf
, "\\%o", ch
);