]>
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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)engine.c 8.5 (Berkeley) 3/20/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/regex/engine.c,v 1.14 2004/07/12 07:35:59 tjr Exp $");
44 * The matching engine and friends. This file is #included by regexec.c
45 * after suitable #defines of a variety of macros used herein, so that
46 * different state representations can be used without duplicating masses
51 #define matcher smatcher
54 #define dissect sdissect
55 #define backref sbackref
62 #define matcher lmatcher
65 #define dissect ldissect
66 #define backref lbackref
73 #define matcher mmatcher
76 #define dissect mdissect
77 #define backref mbackref
84 /* another structure passed up and down to avoid zillions of parameters */
88 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
89 char *offp
; /* offsets work from here */
90 char *beginp
; /* start of string -- virtual NUL precedes */
91 char *endp
; /* end of string -- virtual NUL here */
92 char *coldp
; /* can be no match starting before here */
93 char **lastpos
; /* [nplus+1] */
95 states st
; /* current states */
96 states fresh
; /* states for a fresh start */
97 states tmp
; /* temporary */
98 states empty
; /* empty set of states */
99 mbstate_t mbs
; /* multibyte conversion state */
102 /* ========= begin header generated by ./mkh ========= */
107 /* === engine.c === */
108 static int matcher(struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
);
109 static char *dissect(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
);
110 static char *backref(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
, sopno lev
);
111 static char *fast(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
);
112 static char *slow(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
);
113 static states
step(struct re_guts
*g
, sopno start
, sopno stop
, states bef
, wint_t ch
, states aft
);
116 #define BOLEOL (BOL-2)
117 #define NOTHING (BOL-3)
120 #define BADCHAR (BOL-6)
121 #define NONCHAR(c) ((c) <= OUT)
123 static void print(struct match
*m
, char *caption
, states st
, int ch
, FILE *d
);
126 static void at(struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
);
129 static char *pchar(int ch
);
135 /* ========= end header generated by ./mkh ========= */
138 #define SP(t, s, c) print(m, t, s, c, stdout)
139 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
140 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
142 #define SP(t, s, c) /* nothing */
143 #define AT(t, p1, p2, s1, s2) /* nothing */
144 #define NOTE(s) /* nothing */
148 - matcher - the actual matching engine
149 == static int matcher(struct re_guts *g, char *string, \
150 == size_t nmatch, regmatch_t pmatch[], int eflags);
152 static int /* 0 success, REG_NOMATCH failure */
153 matcher(g
, string
, nmatch
, pmatch
, eflags
)
163 struct match
*m
= &mv
;
165 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
166 const sopno gl
= g
->laststate
;
169 /* Boyer-Moore algorithms variables */
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 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
194 mustlast
= g
->must
+ g
->mlen
- 1;
195 charjump
= g
->charjump
;
196 matchjump
= g
->matchjump
;
198 for (dp
= start
+g
->mlen
-1; dp
< stop
;) {
199 /* Fast skip non-matches */
200 while (dp
< stop
&& charjump
[(int)*dp
])
201 dp
+= charjump
[(int)*dp
];
207 /* We depend on not being used for
208 * for strings of length 1
210 while (*--dp
== *--pp
&& pp
!= mustfirst
);
215 /* Jump to next possible match */
216 mj
= matchjump
[pp
- mustfirst
];
217 cj
= charjump
[(int)*dp
];
218 dp
+= (cj
< mj
? mj
: cj
);
224 for (dp
= start
; dp
< stop
; dp
++)
225 if (*dp
== g
->must
[0] &&
226 stop
- dp
>= g
->mlen
&&
227 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
229 if (dp
== stop
) /* we didn't find g->must */
234 /* match struct setup */
250 /* Adjust start according to moffset, to speed things up */
252 start
= ((dp
- g
->moffset
) < start
) ? start
: dp
- g
->moffset
;
254 /* this loop does only one repetition except for backrefs */
256 endp
= fast(m
, start
, stop
, gf
, gl
);
257 if (endp
== NULL
) { /* a miss */
261 if (nmatch
== 0 && !g
->backrefs
)
262 break; /* no further info needed */
265 assert(m
->coldp
!= NULL
);
267 NOTE("finding start");
268 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
271 assert(m
->coldp
< m
->endp
);
272 m
->coldp
+= XMBRTOWC(NULL
, m
->coldp
,
273 m
->endp
- m
->coldp
, &m
->mbs
, 0, g
->loc
);
275 if (nmatch
== 1 && !g
->backrefs
)
276 break; /* no further info needed */
278 /* oh my, he wants the subexpressions... */
279 if (m
->pmatch
== NULL
)
280 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
282 if (m
->pmatch
== NULL
) {
286 for (i
= 1; i
<= m
->g
->nsub
; i
++)
287 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
288 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
290 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
292 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
293 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
295 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
300 NOTE("backref dissect");
301 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
306 /* uh-oh... we couldn't find a subexpression-level match */
307 assert(g
->backrefs
); /* must be back references doing it */
308 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
310 if (dp
!= NULL
|| endp
<= m
->coldp
)
313 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
316 /* try it on a shorter possibility */
318 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
319 assert(m
->pmatch
[i
].rm_so
== -1);
320 assert(m
->pmatch
[i
].rm_eo
== -1);
323 NOTE("backoff dissect");
324 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
326 assert(dp
== NULL
|| dp
== endp
);
327 if (dp
!= NULL
) /* found a shorter one */
330 /* despite initial appearances, there is no match here */
332 /* recycle starting later */
333 start
= m
->coldp
+ XMBRTOWC(NULL
, m
->coldp
,
334 m
->endp
- m
->coldp
, &m
->mbs
, 0, g
->loc
);
335 assert(start
<= stop
);
338 /* fill in the details if requested */
340 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
341 pmatch
[0].rm_eo
= endp
- m
->offp
;
344 assert(m
->pmatch
!= NULL
);
345 for (i
= 1; i
< nmatch
; i
++)
347 pmatch
[i
] = m
->pmatch
[i
];
349 pmatch
[i
].rm_so
= -1;
350 pmatch
[i
].rm_eo
= -1;
354 if (m
->pmatch
!= NULL
)
355 free((char *)m
->pmatch
);
356 if (m
->lastpos
!= NULL
)
357 free((char *)m
->lastpos
);
363 - dissect - figure out what matched what, no back references
364 == static char *dissect(struct match *m, char *start, \
365 == char *stop, sopno startst, sopno stopst);
367 static char * /* == stop (success) always */
368 dissect(m
, start
, stop
, startst
, stopst
)
376 sopno ss
; /* start sop of current subRE */
377 sopno es
; /* end sop of current subRE */
378 char *sp
; /* start of string matched by it */
379 char *stp
; /* string matched by it cannot pass here */
380 char *rest
; /* start of rest of string */
381 char *tail
; /* string unmatched by rest of RE */
382 sopno ssub
; /* start sop of subsubRE */
383 sopno esub
; /* end sop of subsubRE */
384 char *ssp
; /* start of string matched by subsubRE */
385 char *sep
; /* end of string matched by subsubRE */
386 char *oldssp
; /* previous ssp */
389 AT("diss", start
, stop
, startst
, stopst
);
391 for (ss
= startst
; ss
< stopst
; ss
= es
) {
392 /* identify end of subRE */
394 switch (OP(m
->g
->strip
[es
])) {
397 es
+= OPND(m
->g
->strip
[es
]);
400 while (OP(m
->g
->strip
[es
]) != O_CH
)
401 es
+= OPND(m
->g
->strip
[es
]);
406 /* figure out what it matched */
407 switch (OP(m
->g
->strip
[ss
])) {
412 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0, m
->g
->loc
);
421 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0, m
->g
->loc
);
427 /* cases where length of match is hard to find */
431 /* how long could this one be? */
432 rest
= slow(m
, sp
, stp
, ss
, es
);
433 assert(rest
!= NULL
); /* it did match */
434 /* could the rest match the rest? */
435 tail
= slow(m
, rest
, stop
, es
, stopst
);
438 /* no -- try a shorter match for this one */
440 assert(stp
>= sp
); /* it did work */
444 /* did innards match? */
445 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
446 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
455 /* how long could this one be? */
456 rest
= slow(m
, sp
, stp
, ss
, es
);
457 assert(rest
!= NULL
); /* it did match */
458 /* could the rest match the rest? */
459 tail
= slow(m
, rest
, stop
, es
, stopst
);
462 /* no -- try a shorter match for this one */
464 assert(stp
>= sp
); /* it did work */
470 for (;;) { /* find last match of innards */
471 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
472 if (sep
== NULL
|| sep
== ssp
)
473 break; /* failed or matched null */
474 oldssp
= ssp
; /* on to next try */
478 /* last successful match */
482 else if (tail
==rest
) {
483 /* Fix for test expr 105 */
486 assert(sep
== rest
); /* must exhaust substring */
487 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
488 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
495 /* how long could this one be? */
496 rest
= slow(m
, sp
, stp
, ss
, es
);
497 assert(rest
!= NULL
); /* it did match */
498 /* could the rest match the rest? */
499 tail
= slow(m
, rest
, stop
, es
, stopst
);
502 /* no -- try a shorter match for this one */
504 assert(stp
>= sp
); /* it did work */
507 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
508 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
509 for (;;) { /* find first matching branch */
510 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
511 break; /* it matched all of it */
512 /* that one missed, try next one */
513 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
515 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
517 esub
+= OPND(m
->g
->strip
[esub
]);
518 if (OP(m
->g
->strip
[esub
]) == OOR2
)
521 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
523 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
535 i
= OPND(m
->g
->strip
[ss
]);
536 assert(0 < i
&& i
<= m
->g
->nsub
);
537 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
538 /* fix for T.regcomp 43: don't remember previous
539 subexpression matches beyond the current one (i) */
541 while (i
<= m
->g
->nsub
) {
542 m
->pmatch
[i
].rm_so
= -1;
543 m
->pmatch
[i
].rm_eo
= -1;
548 i
= OPND(m
->g
->strip
[ss
]);
549 assert(0 < i
&& i
<= m
->g
->nsub
);
550 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
563 - backref - figure out what matched what, figuring in back references
564 == static char *backref(struct match *m, char *start, \
565 == char *stop, sopno startst, sopno stopst, sopno lev);
567 static char * /* == stop (success) or NULL (failure) */
568 backref(m
, start
, stop
, startst
, stopst
, lev
)
574 sopno lev
; /* PLUS nesting level */
577 sopno ss
; /* start sop of current subRE */
578 char *sp
; /* start of string matched by it */
579 sopno ssub
; /* start sop of subsubRE */
580 sopno esub
; /* end sop of subsubRE */
581 char *ssp
; /* start of string matched by subsubRE */
590 AT("back", start
, stop
, startst
, stopst
);
593 /* get as far as we can with easy stuff */
595 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
596 switch (OP(s
= m
->g
->strip
[ss
])) {
600 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
607 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
614 cs
= &m
->g
->sets
[OPND(s
)];
615 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
616 if (wc
== BADCHAR
|| !CHIN(cs
, wc
, m
->g
->loc
))
620 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
621 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
622 (m
->g
->cflags
®_NEWLINE
)) )
628 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
629 (sp
< m
->endp
&& *sp
== '\n' &&
630 (m
->g
->cflags
®_NEWLINE
)) )
636 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
637 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
638 (m
->g
->cflags
®_NEWLINE
)) ||
640 !ISWORD(*(sp
-1), m
->g
->loc
)) ) &&
641 (sp
< m
->endp
&& ISWORD(*sp
, m
->g
->loc
)) )
647 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
648 (sp
< m
->endp
&& *sp
== '\n' &&
649 (m
->g
->cflags
®_NEWLINE
)) ||
650 (sp
< m
->endp
&& !ISWORD(*sp
, m
->g
->loc
)) ) &&
651 (sp
> m
->beginp
&& ISWORD(*(sp
-1), m
->g
->loc
)) )
658 case OOR1
: /* matches null but needs to skip */
662 assert(OP(s
) == OOR2
);
664 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
665 /* note that the ss++ gets us past the O_CH */
667 default: /* have to make a choice */
671 if (!hard
) { /* that was it! */
676 ss
--; /* adjust for the for's final increment */
679 AT("hard", sp
, stop
, ss
, stopst
);
682 case OBACK_
: /* the vilest depths */
684 assert(0 < i
&& i
<= m
->g
->nsub
);
685 if (m
->pmatch
[i
].rm_eo
== -1)
687 assert(m
->pmatch
[i
].rm_so
!= -1);
688 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
689 assert(stop
- m
->beginp
>= len
);
691 return(NULL
); /* not enough left to match */
692 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
693 if (memcmp(sp
, ssp
, len
) != 0)
695 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
697 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
699 case OQUEST_
: /* to null or not */
700 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
702 return(dp
); /* not */
703 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
706 assert(m
->lastpos
!= NULL
);
707 assert(lev
+1 <= m
->g
->nplus
);
708 m
->lastpos
[lev
+1] = sp
;
709 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
712 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
713 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
714 /* try another pass */
715 m
->lastpos
[lev
] = sp
;
716 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
718 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
722 case OCH_
: /* find the right one, if any */
724 esub
= ss
+ OPND(s
) - 1;
725 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
726 for (;;) { /* find first matching branch */
727 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
730 /* that one missed, try next one */
731 if (OP(m
->g
->strip
[esub
]) == O_CH
)
732 return(NULL
); /* there is none */
734 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
736 esub
+= OPND(m
->g
->strip
[esub
]);
737 if (OP(m
->g
->strip
[esub
]) == OOR2
)
740 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
743 case OLPAREN
: /* must undo assignment if rest fails */
745 assert(0 < i
&& i
<= m
->g
->nsub
);
746 offsave
= m
->pmatch
[i
].rm_so
;
747 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
748 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
751 m
->pmatch
[i
].rm_so
= offsave
;
754 case ORPAREN
: /* must undo assignment if rest fails */
756 assert(0 < i
&& i
<= m
->g
->nsub
);
757 offsave
= m
->pmatch
[i
].rm_eo
;
758 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
759 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
762 m
->pmatch
[i
].rm_eo
= offsave
;
773 return "shut up gcc";
777 - fast - step through the string at top speed
778 == static char *fast(struct match *m, char *start, \
779 == char *stop, sopno startst, sopno stopst);
781 static char * /* where tentative match ended, or NULL */
782 fast(m
, start
, stop
, startst
, stopst
)
790 states fresh
= m
->fresh
;
794 wint_t lastc
; /* previous c */
797 char *coldp
; /* last p after which no match was underway */
802 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
806 if (start
== m
->beginp
)
810 * XXX Wrong if the previous character was multi-byte.
811 * Newline never is (in encodings supported by FreeBSD),
812 * so this only breaks the ISWORD tests below.
814 c
= (uch
)*(start
- 1);
822 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
826 /* is there an EOL and/or BOL between lastc and c? */
829 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
830 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
834 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
835 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
836 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
841 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
845 /* how about a word boundary? */
846 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
, m
->g
->loc
))) &&
847 (c
!= OUT
&& ISWORD(c
, m
->g
->loc
)) ) {
850 if ( (lastc
!= OUT
&& ISWORD(lastc
, m
->g
->loc
)) &&
851 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
, m
->g
->loc
))) ) {
854 if (flagch
== BOW
|| flagch
== EOW
) {
855 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
860 if (ISSET(st
, stopst
) || p
== stop
)
861 break; /* NOTE BREAK OUT */
863 /* no, we must deal with this character */
867 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
869 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
873 assert(coldp
!= NULL
);
875 if (ISSET(st
, stopst
))
876 return(p
+XMBRTOWC(NULL
, p
, m
->endp
- p
, &m
->mbs
, 0, m
->g
->loc
));
882 - slow - step through the string more deliberately
883 == static char *slow(struct match *m, char *start, \
884 == char *stop, sopno startst, sopno stopst);
886 static char * /* where it ended */
887 slow(m
, start
, stop
, startst
, stopst
)
895 states empty
= m
->empty
;
899 wint_t lastc
; /* previous c */
902 char *matchp
; /* last p at which a match ended */
905 AT("slow", start
, stop
, startst
, stopst
);
908 SP("sstart", st
, *p
);
909 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
911 if (start
== m
->beginp
)
915 * XXX Wrong if the previous character was multi-byte.
916 * Newline never is (in encodings supported by FreeBSD),
917 * so this only breaks the ISWORD tests below.
919 c
= (uch
)*(start
- 1);
928 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
, m
->g
->loc
);
930 /* is there an EOL and/or BOL between lastc and c? */
933 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
934 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
938 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
939 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
940 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
945 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
946 SP("sboleol", st
, c
);
949 /* how about a word boundary? */
950 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
, m
->g
->loc
))) &&
951 (c
!= OUT
&& ISWORD(c
, m
->g
->loc
)) ) {
954 if ( (lastc
!= OUT
&& ISWORD(lastc
, m
->g
->loc
)) &&
955 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
, m
->g
->loc
))) ) {
958 if (flagch
== BOW
|| flagch
== EOW
) {
959 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
960 SP("sboweow", st
, c
);
964 if (ISSET(st
, stopst
))
966 if (EQ(st
, empty
) || p
== stop
)
967 break; /* NOTE BREAK OUT */
969 /* no, we must deal with this character */
973 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
975 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
984 - step - map set of states reachable before char to set reachable after
985 == static states step(struct re_guts *g, sopno start, sopno stop, \
986 == states bef, int ch, states aft);
987 == #define BOL (OUT-1)
988 == #define EOL (BOL-1)
989 == #define BOLEOL (BOL-2)
990 == #define NOTHING (BOL-3)
991 == #define BOW (BOL-4)
992 == #define EOW (BOL-5)
993 == #define BADCHAR (BOL-6)
994 == #define NONCHAR(c) ((c) <= OUT)
997 step(g
, start
, stop
, bef
, ch
, aft
)
999 sopno start
; /* start state within strip */
1000 sopno stop
; /* state after stop state within strip */
1001 states bef
; /* states reachable before */
1002 wint_t ch
; /* character or NONCHAR code */
1003 states aft
; /* states already known reachable after */
1008 onestate here
; /* note, macros know this name */
1012 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
1016 assert(pc
== stop
-1);
1019 /* only characters can match */
1020 assert(!NONCHAR(ch
) || ch
!= OPND(s
));
1025 if (ch
== BOL
|| ch
== BOLEOL
)
1029 if (ch
== EOL
|| ch
== BOLEOL
)
1045 cs
= &g
->sets
[OPND(s
)];
1046 if (!NONCHAR(ch
) && CHIN(cs
, ch
, g
->loc
))
1049 case OBACK_
: /* ignored here */
1053 case OPLUS_
: /* forward, this is just an empty */
1056 case O_PLUS
: /* both forward and back */
1058 i
= ISSETBACK(aft
, OPND(s
));
1059 BACK(aft
, aft
, OPND(s
));
1060 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
1061 /* oho, must reconsider loop body */
1066 case OQUEST_
: /* two branches, both forward */
1068 FWD(aft
, aft
, OPND(s
));
1070 case O_QUEST
: /* just an empty */
1073 case OLPAREN
: /* not significant here */
1077 case OCH_
: /* mark the first two branches */
1079 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1080 FWD(aft
, aft
, OPND(s
));
1082 case OOR1
: /* done a branch, find the O_CH */
1083 if (ISSTATEIN(aft
, here
)) {
1085 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
1087 assert(OP(s
) == OOR2
);
1088 FWD(aft
, aft
, look
);
1091 case OOR2
: /* propagate OCH_'s marking */
1093 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
1094 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1095 FWD(aft
, aft
, OPND(s
));
1098 case O_CH
: /* just empty */
1101 default: /* ooooops... */
1112 - print - print a set of states
1114 == static void print(struct match *m, char *caption, states st, \
1115 == int ch, FILE *d);
1119 print(m
, caption
, st
, ch
, d
)
1126 struct re_guts
*g
= m
->g
;
1130 if (!(m
->eflags
®_TRACE
))
1133 fprintf(d
, "%s", caption
);
1135 fprintf(d
, " %s", pchar(ch
));
1136 for (i
= 0; i
< g
->nstates
; i
++)
1138 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1145 - at - print current situation
1147 == static void at(struct match *m, char *title, char *start, char *stop, \
1148 == sopno startst, sopno stopst);
1152 at(m
, title
, start
, stop
, startst
, stopst
)
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 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 char * /* -> representation */
1185 static char pbuf
[10];
1187 if (isprint((uch
)ch
) || ch
== ' ')
1188 sprintf(pbuf
, "%c", ch
);
1190 sprintf(pbuf
, "\\%o", ch
);