]>
git.saurik.com Git - apple/libc.git/blob - regex/engine.c
2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)engine.c 8.5 (Berkeley) 3/20/94
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/regex/engine.c,v 1.23 2009/09/16 06:32:23 dds Exp $");
40 * The matching engine and friends. This file is #included by regexec.c
41 * after suitable #defines of a variety of macros used herein, so that
42 * different state representations can be used without duplicating masses
47 #define matcher smatcher
50 #define dissect sdissect
51 #define backref sbackref
58 #define matcher lmatcher
61 #define dissect ldissect
62 #define backref lbackref
69 #define matcher mmatcher
72 #define dissect mdissect
73 #define backref mbackref
80 /* another structure passed up and down to avoid zillions of parameters */
84 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
85 const char *offp
; /* offsets work from here */
86 const char *beginp
; /* start of string -- virtual NUL precedes */
87 const char *endp
; /* end of string -- virtual NUL here */
88 const char *coldp
; /* can be no match starting before here */
89 const char **lastpos
; /* [nplus+1] */
91 states st
; /* current states */
92 states fresh
; /* states for a fresh start */
93 states tmp
; /* temporary */
94 states empty
; /* empty set of states */
95 mbstate_t mbs
; /* multibyte conversion state */
98 /* ========= begin header generated by ./mkh ========= */
103 /* === engine.c === */
104 static int matcher(struct re_guts
*g
, const char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
);
105 static const char *dissect(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
106 static const char *backref(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
, sopno lev
, int);
107 static const char *fast(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
108 static const char *slow(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
109 static states
step(struct re_guts
*g
, sopno start
, sopno stop
, states bef
, wint_t ch
, states aft
);
110 #define MAX_RECURSION 100
113 #define BOLEOL (BOL-2)
114 #define NOTHING (BOL-3)
117 #define BADCHAR (BOL-6)
118 #define NONCHAR(c) ((c) <= OUT)
120 static void print(struct match
*m
, const char *caption
, states st
, int ch
, FILE *d
);
123 static void at(struct match
*m
, const char *title
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
126 static const char *pchar(int ch
);
132 /* ========= end header generated by ./mkh ========= */
135 #define SP(t, s, c) print(m, t, s, c, stdout)
136 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
137 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
139 #define SP(t, s, c) /* nothing */
140 #define AT(t, p1, p2, s1, s2) /* nothing */
141 #define NOTE(s) /* nothing */
145 - matcher - the actual matching engine
146 == static int matcher(struct re_guts *g, const char *string, \
147 == size_t nmatch, regmatch_t pmatch[], int eflags);
149 static int /* 0 success, REG_NOMATCH failure */
150 matcher(struct re_guts
*g
,
159 struct match
*m
= &mv
;
161 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
162 const sopno gl
= g
->laststate
;
165 /* Boyer-Moore algorithms variables */
168 const char *mustfirst
;
169 const char *mustlast
;
173 /* simplify the situation where possible */
174 if (g
->cflags
®_NOSUB
)
176 if (eflags
®_STARTEND
) {
177 start
= string
+ pmatch
[0].rm_so
;
178 stop
= string
+ pmatch
[0].rm_eo
;
181 stop
= start
+ strlen(start
);
186 /* prescreening; this does wonders for this rather slow code */
187 if (g
->must
!= NULL
) {
188 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
190 mustlast
= g
->must
+ g
->mlen
- 1;
191 charjump
= g
->charjump
;
192 matchjump
= g
->matchjump
;
194 for (dp
= start
+g
->mlen
-1; dp
< stop
;) {
195 /* Fast skip non-matches */
196 while (dp
< stop
&& charjump
[(int)*dp
])
197 dp
+= charjump
[(int)*dp
];
203 /* We depend on not being used for
204 * for strings of length 1
206 while (*--dp
== *--pp
&& pp
!= mustfirst
);
211 /* Jump to next possible match */
212 mj
= matchjump
[pp
- mustfirst
];
213 cj
= charjump
[(int)*dp
];
214 dp
+= (cj
< mj
? mj
: cj
);
220 for (dp
= start
; dp
< stop
; dp
++)
221 if (*dp
== g
->must
[0] &&
222 stop
- dp
>= g
->mlen
&&
223 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
225 if (dp
== stop
) /* we didn't find g->must */
230 /* match struct setup */
246 /* Adjust start according to moffset, to speed things up */
248 start
= ((dp
- g
->moffset
) < start
) ? start
: dp
- g
->moffset
;
250 SP("mloop", m
->st
, *start
);
252 /* this loop does only one repetition except for backrefs */
254 endp
= fast(m
, start
, stop
, gf
, gl
);
255 if (endp
== NULL
) { /* a miss */
256 if (m
->pmatch
!= NULL
)
257 free((char *)m
->pmatch
);
258 if (m
->lastpos
!= NULL
)
259 free((char *)m
->lastpos
);
263 if (nmatch
== 0 && !g
->backrefs
)
264 break; /* no further info needed */
267 assert(m
->coldp
!= NULL
);
269 NOTE("finding start");
270 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
273 assert(m
->coldp
< m
->endp
);
274 m
->coldp
+= XMBRTOWC(NULL
, m
->coldp
,
275 m
->endp
- m
->coldp
, &m
->mbs
, 0, g
->loc
);
277 if (nmatch
== 1 && !g
->backrefs
)
278 break; /* no further info needed */
280 /* oh my, he wants the subexpressions... */
281 if (m
->pmatch
== NULL
)
282 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
284 if (m
->pmatch
== NULL
) {
288 for (i
= 1; i
<= m
->g
->nsub
; i
++)
289 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
290 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
292 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
294 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
295 m
->lastpos
= malloc((g
->nplus
+1) *
296 sizeof(const char *));
297 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
302 NOTE("backref dissect");
303 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0, 0);
308 /* uh-oh... we couldn't find a subexpression-level match */
309 assert(g
->backrefs
); /* must be back references doing it */
310 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
312 if (dp
!= NULL
|| endp
<= m
->coldp
)
315 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
318 /* try it on a shorter possibility */
320 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
321 assert(m
->pmatch
[i
].rm_so
== -1);
322 assert(m
->pmatch
[i
].rm_eo
== -1);
325 NOTE("backoff dissect");
326 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0, 0);
328 assert(dp
== NULL
|| dp
== endp
);
329 if (dp
!= NULL
) /* found a shorter one */
332 /* despite initial appearances, there is no match here */
334 /* recycle starting later */
335 start
= m
->coldp
+ XMBRTOWC(NULL
, m
->coldp
,
336 stop
- m
->coldp
, &m
->mbs
, 0, g
->loc
);
337 assert(start
<= stop
);
340 /* fill in the details if requested */
342 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
343 pmatch
[0].rm_eo
= endp
- m
->offp
;
346 assert(m
->pmatch
!= NULL
);
347 for (i
= 1; i
< nmatch
; i
++)
349 pmatch
[i
] = m
->pmatch
[i
];
351 pmatch
[i
].rm_so
= -1;
352 pmatch
[i
].rm_eo
= -1;
356 if (m
->pmatch
!= NULL
)
357 free((char *)m
->pmatch
);
358 if (m
->lastpos
!= NULL
)
359 free((char *)m
->lastpos
);
365 - dissect - figure out what matched what, no back references
366 == static const char *dissect(struct match *m, const char *start, \
367 == const char *stop, sopno startst, sopno stopst);
369 static const char * /* == stop (success) always */
370 dissect(struct match
*m
,
377 sopno ss
; /* start sop of current subRE */
378 sopno es
; /* end sop of current subRE */
379 const char *sp
; /* start of string matched by it */
380 const char *stp
; /* string matched by it cannot pass here */
381 const char *rest
; /* start of rest of string */
382 const char *tail
; /* string unmatched by rest of RE */
383 sopno ssub
; /* start sop of subsubRE */
384 sopno esub
; /* end sop of subsubRE */
385 const char *ssp
; /* start of string matched by subsubRE */
386 const char *sep
; /* end of string matched by subsubRE */
387 const char *oldssp
; /* previous ssp */
390 AT("diss", start
, stop
, startst
, stopst
);
392 for (ss
= startst
; ss
< stopst
; ss
= es
) {
393 /* identify end of subRE */
395 switch (OP(m
->g
->strip
[es
])) {
398 es
+= OPND(m
->g
->strip
[es
]);
401 while (OP(m
->g
->strip
[es
]) != O_CH
)
402 es
+= OPND(m
->g
->strip
[es
]);
407 /* figure out what it matched */
408 switch (OP(m
->g
->strip
[ss
])) {
413 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0, m
->g
->loc
);
422 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0, m
->g
->loc
);
428 /* cases where length of match is hard to find */
432 /* how long could this one be? */
433 rest
= slow(m
, sp
, stp
, ss
, es
);
434 assert(rest
!= NULL
); /* it did match */
435 /* could the rest match the rest? */
436 tail
= slow(m
, rest
, stop
, es
, stopst
);
439 /* no -- try a shorter match for this one */
441 assert(stp
>= sp
); /* it did work */
445 /* did innards match? */
446 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
447 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
456 /* how long could this one be? */
457 rest
= slow(m
, sp
, stp
, ss
, es
);
458 assert(rest
!= NULL
); /* it did match */
459 /* could the rest match the rest? */
460 tail
= slow(m
, rest
, stop
, es
, stopst
);
463 /* no -- try a shorter match for this one */
465 assert(stp
>= sp
); /* it did work */
471 for (;;) { /* find last match of innards */
472 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
473 if (sep
== NULL
|| sep
== ssp
)
474 break; /* failed or matched null */
475 oldssp
= ssp
; /* on to next try */
479 /* last successful match */
483 else if (tail
==rest
) {
484 /* Fix for test expr 105 */
487 assert(sep
== rest
); /* must exhaust substring */
488 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
489 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
496 /* how long could this one be? */
497 rest
= slow(m
, sp
, stp
, ss
, es
);
498 assert(rest
!= NULL
); /* it did match */
499 /* could the rest match the rest? */
500 tail
= slow(m
, rest
, stop
, es
, stopst
);
503 /* no -- try a shorter match for this one */
505 assert(stp
>= sp
); /* it did work */
508 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
509 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
510 for (;;) { /* find first matching branch */
511 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
512 break; /* it matched all of it */
513 /* that one missed, try next one */
514 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
516 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
518 esub
+= OPND(m
->g
->strip
[esub
]);
519 if (OP(m
->g
->strip
[esub
]) == OOR2
)
522 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
524 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
536 i
= OPND(m
->g
->strip
[ss
]);
537 assert(0 < i
&& i
<= m
->g
->nsub
);
538 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
539 /* fix for T.regcomp 43: don't remember previous
540 subexpression matches beyond the current one (i) */
542 while (i
<= m
->g
->nsub
) {
543 m
->pmatch
[i
].rm_so
= -1;
544 m
->pmatch
[i
].rm_eo
= -1;
549 i
= OPND(m
->g
->strip
[ss
]);
550 assert(0 < i
&& i
<= m
->g
->nsub
);
551 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
564 - backref - figure out what matched what, figuring in back references
565 == static const char *backref(struct match *m, const char *start, \
566 == const char *stop, sopno startst, sopno stopst, sopno lev);
568 static const char * /* == stop (success) or NULL (failure) */
569 backref(struct match
*m
,
574 sopno lev
, /* PLUS nesting level */
578 sopno ss
; /* start sop of current subRE */
579 const char *sp
; /* start of string matched by it */
580 sopno ssub
; /* start sop of subsubRE */
581 sopno esub
; /* end sop of subsubRE */
582 const char *ssp
; /* start of string matched by subsubRE */
591 AT("back", start
, stop
, startst
, stopst
);
594 /* get as far as we can with easy stuff */
596 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
597 switch (OP(s
= m
->g
->strip
[ss
])) {
601 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
608 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
615 cs
= &m
->g
->sets
[OPND(s
)];
616 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
617 if (wc
== BADCHAR
|| !CHIN(cs
, wc
, m
->g
->loc
))
621 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
622 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
623 (m
->g
->cflags
®_NEWLINE
)) )
629 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
630 (sp
< m
->endp
&& *sp
== '\n' &&
631 (m
->g
->cflags
®_NEWLINE
)) )
637 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
638 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
639 (m
->g
->cflags
®_NEWLINE
)) ||
641 !ISWORD(*(sp
-1), m
->g
->loc
)) ) &&
642 (sp
< m
->endp
&& ISWORD(*sp
, m
->g
->loc
)) )
648 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
649 (sp
< m
->endp
&& *sp
== '\n' &&
650 (m
->g
->cflags
®_NEWLINE
)) ||
651 (sp
< m
->endp
&& !ISWORD(*sp
, m
->g
->loc
)) ) &&
652 (sp
> m
->beginp
&& ISWORD(*(sp
-1), m
->g
->loc
)) )
659 case OOR1
: /* matches null but needs to skip */
663 assert(OP(s
) == OOR2
);
665 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
666 /* note that the ss++ gets us past the O_CH */
668 default: /* have to make a choice */
672 if (!hard
) { /* that was it! */
677 ss
--; /* adjust for the for's final increment */
680 AT("hard", sp
, stop
, ss
, stopst
);
683 case OBACK_
: /* the vilest depths */
685 assert(0 < i
&& i
<= m
->g
->nsub
);
686 if (m
->pmatch
[i
].rm_eo
== -1)
688 assert(m
->pmatch
[i
].rm_so
!= -1);
689 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
690 if (len
== 0 && rec
++ > MAX_RECURSION
)
692 assert(stop
- m
->beginp
>= len
);
694 return(NULL
); /* not enough left to match */
695 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
696 if (memcmp(sp
, ssp
, len
) != 0)
698 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
700 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
, rec
));
702 case OQUEST_
: /* to null or not */
703 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
705 return(dp
); /* not */
706 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
, rec
));
709 assert(m
->lastpos
!= NULL
);
710 assert(lev
+1 <= m
->g
->nplus
);
711 m
->lastpos
[lev
+1] = sp
;
712 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1, rec
));
715 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
716 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1, rec
));
717 /* try another pass */
718 m
->lastpos
[lev
] = sp
;
719 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
, rec
);
721 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1, rec
));
725 case OCH_
: /* find the right one, if any */
727 esub
= ss
+ OPND(s
) - 1;
728 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
729 for (;;) { /* find first matching branch */
730 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
, rec
);
733 /* that one missed, try next one */
734 if (OP(m
->g
->strip
[esub
]) == O_CH
)
735 return(NULL
); /* there is none */
737 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
739 esub
+= OPND(m
->g
->strip
[esub
]);
740 if (OP(m
->g
->strip
[esub
]) == OOR2
)
743 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
746 case OLPAREN
: /* must undo assignment if rest fails */
748 assert(0 < i
&& i
<= m
->g
->nsub
);
749 offsave
= m
->pmatch
[i
].rm_so
;
750 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
751 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
754 m
->pmatch
[i
].rm_so
= offsave
;
757 case ORPAREN
: /* must undo assignment if rest fails */
759 assert(0 < i
&& i
<= m
->g
->nsub
);
760 offsave
= m
->pmatch
[i
].rm_eo
;
761 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
762 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
765 m
->pmatch
[i
].rm_eo
= offsave
;
776 return "shut up gcc";
780 - fast - step through the string at top speed
781 == static const char *fast(struct match *m, const char *start, \
782 == const char *stop, sopno startst, sopno stopst);
784 static const char * /* where tentative match ended, or NULL */
785 fast( struct match
*m
,
792 states fresh
= m
->fresh
;
794 const char *p
= start
;
796 wint_t lastc
; /* previous c */
799 const char *coldp
; /* last p after which no match was underway */
805 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
809 if (start
== m
->beginp
)
813 * XXX Wrong if the previous character was multi-byte.
814 * Newline never is (in encodings supported by FreeBSD),
815 * so this only breaks the ISWORD tests below.
817 c
= (uch
)*(start
- 1);
826 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
830 /* is there an EOL and/or BOL between lastc and c? */
833 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
834 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
838 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
839 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
840 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
845 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
849 /* how about a word boundary? */
850 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
, m
->g
->loc
))) &&
851 (c
!= OUT
&& ISWORD(c
, m
->g
->loc
)) ) {
854 if ( (lastc
!= OUT
&& ISWORD(lastc
, m
->g
->loc
)) &&
855 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
, m
->g
->loc
))) ) {
858 if (flagch
== BOW
|| flagch
== EOW
) {
859 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
864 if (ISSET(st
, stopst
) || p
== stop
|| clen
> stop
- p
)
865 break; /* NOTE BREAK OUT */
867 /* no, we must deal with this character */
871 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
873 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
877 assert(coldp
!= NULL
);
879 if (ISSET(st
, stopst
))
880 return(p
+XMBRTOWC(NULL
, p
, stop
- p
, &m
->mbs
, 0, m
->g
->loc
));
886 - slow - step through the string more deliberately
887 == static const char *slow(struct match *m, const char *start, \
888 == const char *stop, sopno startst, sopno stopst);
890 static const char * /* where it ended */
891 slow( struct match
*m
,
898 states empty
= m
->empty
;
900 const char *p
= start
;
902 wint_t lastc
; /* previous c */
905 const char *matchp
; /* last p at which a match ended */
908 AT("slow", start
, stop
, startst
, stopst
);
911 SP("sstart", st
, *p
);
912 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
914 if (start
== m
->beginp
)
918 * XXX Wrong if the previous character was multi-byte.
919 * Newline never is (in encodings supported by FreeBSD),
920 * so this only breaks the ISWORD tests below.
922 c
= (uch
)*(start
- 1);
931 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
933 /* is there an EOL and/or BOL between lastc and c? */
936 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
937 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
941 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
942 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
943 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
948 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
949 SP("sboleol", st
, c
);
952 /* how about a word boundary? */
953 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
, m
->g
->loc
))) &&
954 (c
!= OUT
&& ISWORD(c
, m
->g
->loc
)) ) {
957 if ( (lastc
!= OUT
&& ISWORD(lastc
, m
->g
->loc
)) &&
958 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
, m
->g
->loc
))) ) {
961 if (flagch
== BOW
|| flagch
== EOW
) {
962 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
963 SP("sboweow", st
, c
);
967 if (ISSET(st
, stopst
))
969 if (EQ(st
, empty
) || p
== stop
|| clen
> stop
- p
)
970 break; /* NOTE BREAK OUT */
972 /* no, we must deal with this character */
976 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
978 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
987 - step - map set of states reachable before char to set reachable after
988 == static states step(struct re_guts *g, sopno start, sopno stop, \
989 == states bef, int ch, states aft);
990 == #define BOL (OUT-1)
991 == #define EOL (BOL-1)
992 == #define BOLEOL (BOL-2)
993 == #define NOTHING (BOL-3)
994 == #define BOW (BOL-4)
995 == #define EOW (BOL-5)
996 == #define BADCHAR (BOL-6)
997 == #define NONCHAR(c) ((c) <= OUT)
1000 step(struct re_guts
*g
,
1001 sopno start
, /* start state within strip */
1002 sopno stop
, /* state after stop state within strip */
1003 states bef
, /* states reachable before */
1004 wint_t ch
, /* character or NONCHAR code */
1005 states aft
) /* states already known reachable after */
1010 onestate here
; /* note, macros know this name */
1014 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
1018 assert(pc
== stop
-1);
1021 /* only characters can match */
1022 assert(!NONCHAR(ch
) || ch
!= OPND(s
));
1027 if (ch
== BOL
|| ch
== BOLEOL
)
1031 if (ch
== EOL
|| ch
== BOLEOL
)
1047 cs
= &g
->sets
[OPND(s
)];
1048 if (!NONCHAR(ch
) && CHIN(cs
, ch
, g
->loc
))
1051 case OBACK_
: /* ignored here */
1055 case OPLUS_
: /* forward, this is just an empty */
1058 case O_PLUS
: /* both forward and back */
1060 i
= ISSETBACK(aft
, OPND(s
));
1061 BACK(aft
, aft
, OPND(s
));
1062 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
1063 /* oho, must reconsider loop body */
1068 case OQUEST_
: /* two branches, both forward */
1070 FWD(aft
, aft
, OPND(s
));
1072 case O_QUEST
: /* just an empty */
1075 case OLPAREN
: /* not significant here */
1079 case OCH_
: /* mark the first two branches */
1081 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1082 FWD(aft
, aft
, OPND(s
));
1084 case OOR1
: /* done a branch, find the O_CH */
1085 if (ISSTATEIN(aft
, here
)) {
1087 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
1089 assert(OP(s
) == OOR2
);
1090 FWD(aft
, aft
, look
+ 1);
1093 case OOR2
: /* propagate OCH_'s marking */
1095 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
1096 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1097 FWD(aft
, aft
, OPND(s
));
1100 case O_CH
: /* just empty */
1103 default: /* ooooops... */
1114 - print - print a set of states
1116 == static void print(struct match *m, const char *caption, states st, \
1117 == int ch, FILE *d);
1121 print(struct match
*m
,
1122 const char *caption
,
1127 struct re_guts
*g
= m
->g
;
1131 if (!(m
->eflags
®_TRACE
))
1134 fprintf(d
, "%s", caption
);
1136 fprintf(d
, " %s", pchar(ch
));
1137 for (i
= 0; i
< g
->nstates
; i
++)
1139 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1146 - at - print current situation
1148 == static void at(struct match *m, const char *title, const char *start, \
1149 == const char *stop, sopno startst, sopno stopst);
1153 at( struct match
*m
,
1160 if (!(m
->eflags
®_TRACE
))
1163 printf("%s %s-", title
, pchar(*start
));
1164 printf("%s ", pchar(*stop
));
1165 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1169 #define PCHARDONE /* never again */
1171 - pchar - make a character printable
1173 == static const char *pchar(int ch);
1176 * Is this identical to regchar() over in debug.c? Well, yes. But a
1177 * duplicate here avoids having a debugging-capable regexec.o tied to
1178 * a matching debug.o, and this is convenient. It all disappears in
1179 * the non-debug compilation anyway, so it doesn't matter much.
1181 static const char * /* -> representation */
1184 static char pbuf
[10];
1186 if (isprint((uch
)ch
) || ch
== ' ')
1187 sprintf(pbuf
, "%c", ch
);
1189 sprintf(pbuf
, "\\%o", ch
);