]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/RESearch.cxx
allow customizing wxStandardPaths logic for the program directory determination
[wxWidgets.git] / src / stc / scintilla / src / RESearch.cxx
1 // Scintilla source code edit control
2 /** @file RESearch.cxx
3 ** Regular expression search library.
4 **/
5
6 /*
7 * regex - Regular expression pattern matching and replacement
8 *
9 * By: Ozan S. Yigit (oz)
10 * Dept. of Computer Science
11 * York University
12 *
13 * Original code available from http://www.cs.yorku.ca/~oz/
14 * Translation to C++ by Neil Hodgson neilh@scintilla.org
15 * Removed all use of register.
16 * Converted to modern function prototypes.
17 * Put all global/static variables into an object so this code can be
18 * used from multiple threads, etc.
19 * Some extensions by Philippe Lhoste PhiLho(a)GMX.net
20 *
21 * These routines are the PUBLIC DOMAIN equivalents of regex
22 * routines as found in 4.nBSD UN*X, with minor extensions.
23 *
24 * These routines are derived from various implementations found
25 * in software tools books, and Conroy's grep. They are NOT derived
26 * from licensed/restricted software.
27 * For more interesting/academic/complicated implementations,
28 * see Henry Spencer's regexp routines, or GNU Emacs pattern
29 * matching module.
30 *
31 * Modification history removed.
32 *
33 * Interfaces:
34 * RESearch::Compile: compile a regular expression into a NFA.
35 *
36 * const char *RESearch::Compile(const char *pat, int length,
37 * bool caseSensitive, bool posix)
38 *
39 * Returns a short error string if they fail.
40 *
41 * RESearch::Execute: execute the NFA to match a pattern.
42 *
43 * int RESearch::Execute(characterIndexer &ci, int lp, int endp)
44 *
45 * RESearch::Substitute: substitute the matched portions in a new string.
46 *
47 * int RESearch::Substitute(CharacterIndexer &ci, char *src, char *dst)
48 *
49 * re_fail: failure routine for RESearch::Execute. (no longer used)
50 *
51 * void re_fail(char *msg, char op)
52 *
53 * Regular Expressions:
54 *
55 * [1] char matches itself, unless it is a special
56 * character (metachar): . \ [ ] * + ^ $
57 * and ( ) if posix option.
58 *
59 * [2] . matches any character.
60 *
61 * [3] \ matches the character following it, except:
62 * - \a, \b, \f, \n, \r, \t, \v match the corresponding C
63 * escape char, respectively BEL, BS, FF, LF, CR, TAB and VT;
64 * Note that \r and \n are never matched because Scintilla
65 * regex searches are made line per line
66 * (stripped of end-of-line chars).
67 * - if not in posix mode, when followed by a
68 * left or right round bracket (see [7]);
69 * - when followed by a digit 1 to 9 (see [8]);
70 * - when followed by a left or right angle bracket
71 * (see [9]);
72 * - when followed by d, D, s, S, w or W (see [10]);
73 * - when followed by x and two hexa digits (see [11].
74 * Backslash is used as an escape character for all
75 * other meta-characters, and itself.
76 *
77 * [4] [set] matches one of the characters in the set.
78 * If the first character in the set is "^",
79 * it matches the characters NOT in the set, i.e.
80 * complements the set. A shorthand S-E (start dash end)
81 * is used to specify a set of characters S up to
82 * E, inclusive. S and E must be characters, otherwise
83 * the dash is taken literally (eg. in expression [\d-a]).
84 * The special characters "]" and "-" have no special
85 * meaning if they appear as the first chars in the set.
86 * To include both, put - first: [-]A-Z]
87 * (or just backslash them).
88 * examples: match:
89 *
90 * [-]|] matches these 3 chars,
91 *
92 * []-|] matches from ] to | chars
93 *
94 * [a-z] any lowercase alpha
95 *
96 * [^-]] any char except - and ]
97 *
98 * [^A-Z] any char except uppercase
99 * alpha
100 *
101 * [a-zA-Z] any alpha
102 *
103 * [5] * any regular expression form [1] to [4]
104 * (except [7], [8] and [9] forms of [3]),
105 * followed by closure char (*)
106 * matches zero or more matches of that form.
107 *
108 * [6] + same as [5], except it matches one or more.
109 * Both [5] and [6] are greedy (they match as much as possible).
110 *
111 * [7] a regular expression in the form [1] to [12], enclosed
112 * as \(form\) (or (form) with posix flag) matches what
113 * form matches. The enclosure creates a set of tags,
114 * used for [8] and for pattern substitution.
115 * The tagged forms are numbered starting from 1.
116 *
117 * [8] a \ followed by a digit 1 to 9 matches whatever a
118 * previously tagged regular expression ([7]) matched.
119 *
120 * [9] \< a regular expression starting with a \< construct
121 * \> and/or ending with a \> construct, restricts the
122 * pattern matching to the beginning of a word, and/or
123 * the end of a word. A word is defined to be a character
124 * string beginning and/or ending with the characters
125 * A-Z a-z 0-9 and _. Scintilla extends this definition
126 * by user setting. The word must also be preceded and/or
127 * followed by any character outside those mentioned.
128 *
129 * [10] \l a backslash followed by d, D, s, S, w or W,
130 * becomes a character class (both inside and
131 * outside sets []).
132 * d: decimal digits
133 * D: any char except decimal digits
134 * s: whitespace (space, \t \n \r \f \v)
135 * S: any char except whitespace (see above)
136 * w: alphanumeric & underscore (changed by user setting)
137 * W: any char except alphanumeric & underscore (see above)
138 *
139 * [11] \xHH a backslash followed by x and two hexa digits,
140 * becomes the character whose Ascii code is equal
141 * to these digits. If not followed by two digits,
142 * it is 'x' char itself.
143 *
144 * [12] a composite regular expression xy where x and y
145 * are in the form [1] to [11] matches the longest
146 * match of x followed by a match for y.
147 *
148 * [13] ^ a regular expression starting with a ^ character
149 * $ and/or ending with a $ character, restricts the
150 * pattern matching to the beginning of the line,
151 * or the end of line. [anchors] Elsewhere in the
152 * pattern, ^ and $ are treated as ordinary characters.
153 *
154 *
155 * Acknowledgements:
156 *
157 * HCR's Hugh Redelmeier has been most helpful in various
158 * stages of development. He convinced me to include BOW
159 * and EOW constructs, originally invented by Rob Pike at
160 * the University of Toronto.
161 *
162 * References:
163 * Software tools Kernighan & Plauger
164 * Software tools in Pascal Kernighan & Plauger
165 * Grep [rsx-11 C dist] David Conroy
166 * ed - text editor Un*x Programmer's Manual
167 * Advanced editing on Un*x B. W. Kernighan
168 * RegExp routines Henry Spencer
169 *
170 * Notes:
171 *
172 * This implementation uses a bit-set representation for character
173 * classes for speed and compactness. Each character is represented
174 * by one bit in a 256-bit block. Thus, CCL always takes a
175 * constant 32 bytes in the internal nfa, and RESearch::Execute does a single
176 * bit comparison to locate the character in the set.
177 *
178 * Examples:
179 *
180 * pattern: foo*.*
181 * compile: CHR f CHR o CLO CHR o END CLO ANY END END
182 * matches: fo foo fooo foobar fobar foxx ...
183 *
184 * pattern: fo[ob]a[rz]
185 * compile: CHR f CHR o CCL bitset CHR a CCL bitset END
186 * matches: fobar fooar fobaz fooaz
187 *
188 * pattern: foo\\+
189 * compile: CHR f CHR o CHR o CHR \ CLO CHR \ END END
190 * matches: foo\ foo\\ foo\\\ ...
191 *
192 * pattern: \(foo\)[1-3]\1 (same as foo[1-3]foo)
193 * compile: BOT 1 CHR f CHR o CHR o EOT 1 CCL bitset REF 1 END
194 * matches: foo1foo foo2foo foo3foo
195 *
196 * pattern: \(fo.*\)-\1
197 * compile: BOT 1 CHR f CHR o CLO ANY END EOT 1 CHR - REF 1 END
198 * matches: foo-foo fo-fo fob-fob foobar-foobar ...
199 */
200
201 #include "CharClassify.h"
202 #include "RESearch.h"
203
204 // Shut up annoying Visual C++ warnings:
205 #ifdef _MSC_VER
206 #pragma warning(disable: 4514)
207 #endif
208
209 #ifdef SCI_NAMESPACE
210 using namespace Scintilla;
211 #endif
212
213 #define OKP 1
214 #define NOP 0
215
216 #define CHR 1
217 #define ANY 2
218 #define CCL 3
219 #define BOL 4
220 #define EOL 5
221 #define BOT 6
222 #define EOT 7
223 #define BOW 8
224 #define EOW 9
225 #define REF 10
226 #define CLO 11
227
228 #define END 0
229
230 /*
231 * The following defines are not meant to be changeable.
232 * They are for readability only.
233 */
234 #define BLKIND 0370
235 #define BITIND 07
236
237 const char bitarr[] = { 1, 2, 4, 8, 16, 32, 64, '\200' };
238
239 #define badpat(x) (*nfa = END, x)
240
241 /*
242 * Character classification table for word boundary operators BOW
243 * and EOW is passed in by the creator of this object (Scintilla
244 * Document). The Document default state is that word chars are:
245 * 0-9, a-z, A-Z and _
246 */
247
248 RESearch::RESearch(CharClassify *charClassTable) {
249 charClass = charClassTable;
250 Init();
251 }
252
253 RESearch::~RESearch() {
254 Clear();
255 }
256
257 void RESearch::Init() {
258 sta = NOP; /* status of lastpat */
259 bol = 0;
260 for (int i = 0; i < MAXTAG; i++)
261 pat[i] = 0;
262 for (int j = 0; j < BITBLK; j++)
263 bittab[j] = 0;
264 }
265
266 void RESearch::Clear() {
267 for (int i = 0; i < MAXTAG; i++) {
268 delete []pat[i];
269 pat[i] = 0;
270 bopat[i] = NOTFOUND;
271 eopat[i] = NOTFOUND;
272 }
273 }
274
275 bool RESearch::GrabMatches(CharacterIndexer &ci) {
276 bool success = true;
277 for (unsigned int i = 0; i < MAXTAG; i++) {
278 if ((bopat[i] != NOTFOUND) && (eopat[i] != NOTFOUND)) {
279 unsigned int len = eopat[i] - bopat[i];
280 pat[i] = new char[len + 1];
281 if (pat[i]) {
282 for (unsigned int j = 0; j < len; j++)
283 pat[i][j] = ci.CharAt(bopat[i] + j);
284 pat[i][len] = '\0';
285 } else {
286 success = false;
287 }
288 }
289 }
290 return success;
291 }
292
293 void RESearch::ChSet(unsigned char c) {
294 bittab[((c) & BLKIND) >> 3] |= bitarr[(c) & BITIND];
295 }
296
297 void RESearch::ChSetWithCase(unsigned char c, bool caseSensitive) {
298 if (caseSensitive) {
299 ChSet(c);
300 } else {
301 if ((c >= 'a') && (c <= 'z')) {
302 ChSet(c);
303 ChSet(static_cast<unsigned char>(c - 'a' + 'A'));
304 } else if ((c >= 'A') && (c <= 'Z')) {
305 ChSet(c);
306 ChSet(static_cast<unsigned char>(c - 'A' + 'a'));
307 } else {
308 ChSet(c);
309 }
310 }
311 }
312
313 const unsigned char escapeValue(unsigned char ch) {
314 switch (ch) {
315 case 'a': return '\a';
316 case 'b': return '\b';
317 case 'f': return '\f';
318 case 'n': return '\n';
319 case 'r': return '\r';
320 case 't': return '\t';
321 case 'v': return '\v';
322 }
323 return 0;
324 }
325
326 static int GetHexaChar(unsigned char hd1, unsigned char hd2) {
327 int hexValue = 0;
328 if (hd1 >= '0' && hd1 <= '9') {
329 hexValue += 16 * (hd1 - '0');
330 } else if (hd1 >= 'A' && hd1 <= 'F') {
331 hexValue += 16 * (hd1 - 'A' + 10);
332 } else if (hd1 >= 'a' && hd1 <= 'f') {
333 hexValue += 16 * (hd1 - 'a' + 10);
334 } else
335 return -1;
336 if (hd2 >= '0' && hd2 <= '9') {
337 hexValue += hd2 - '0';
338 } else if (hd2 >= 'A' && hd2 <= 'F') {
339 hexValue += hd2 - 'A' + 10;
340 } else if (hd2 >= 'a' && hd2 <= 'f') {
341 hexValue += hd2 - 'a' + 10;
342 } else
343 return -1;
344 return hexValue;
345 }
346
347 /**
348 * Called when the parser finds a backslash not followed
349 * by a valid expression (like \( in non-Posix mode).
350 * @param pat: pointer on the char after the backslash.
351 * @param incr: (out) number of chars to skip after expression evaluation.
352 * @return the char if it resolves to a simple char,
353 * or -1 for a char class. In this case, bittab is changed.
354 */
355 int RESearch::GetBackslashExpression(
356 const char *pat,
357 int &incr) {
358 // Since error reporting is primitive and messages are not used anyway,
359 // I choose to interpret unexpected syntax in a logical way instead
360 // of reporting errors. Otherwise, we can stick on, eg., PCRE behavior.
361 incr = 0; // Most of the time, will skip the char "naturally".
362 int c;
363 int result = -1;
364 unsigned char bsc = *pat;
365 if (!bsc) {
366 // Avoid overrun
367 result = '\\'; // \ at end of pattern, take it literally
368 return result;
369 }
370
371 switch (bsc) {
372 case 'a':
373 case 'b':
374 case 'n':
375 case 'f':
376 case 'r':
377 case 't':
378 case 'v':
379 result = escapeValue(bsc);
380 break;
381 case 'x': {
382 unsigned char hd1 = *(pat + 1);
383 unsigned char hd2 = *(pat + 2);
384 int hexValue = GetHexaChar(hd1, hd2);
385 if (hexValue >= 0) {
386 result = hexValue;
387 incr = 2; // Must skip the digits
388 } else {
389 result = 'x'; // \x without 2 digits: see it as 'x'
390 }
391 }
392 break;
393 case 'd':
394 for (c = '0'; c <= '9'; c++) {
395 ChSet(static_cast<unsigned char>(c));
396 }
397 break;
398 case 'D':
399 for (c = 0; c < MAXCHR; c++) {
400 if (c < '0' || c > '9') {
401 ChSet(static_cast<unsigned char>(c));
402 }
403 }
404 break;
405 case 's':
406 ChSet(' ');
407 ChSet('\t');
408 ChSet('\n');
409 ChSet('\r');
410 ChSet('\f');
411 ChSet('\v');
412 break;
413 case 'S':
414 for (c = 0; c < MAXCHR; c++) {
415 if (c != ' ' && !(c >= 0x09 && c <= 0x0D)) {
416 ChSet(static_cast<unsigned char>(c));
417 }
418 }
419 case 'w':
420 for (c = 0; c < MAXCHR; c++) {
421 if (iswordc(static_cast<unsigned char>(c))) {
422 ChSet(static_cast<unsigned char>(c));
423 }
424 }
425 break;
426 case 'W':
427 for (c = 0; c < MAXCHR; c++) {
428 if (!iswordc(static_cast<unsigned char>(c))) {
429 ChSet(static_cast<unsigned char>(c));
430 }
431 }
432 break;
433 default:
434 result = bsc;
435 }
436 return result;
437 }
438
439 const char *RESearch::Compile(const char *pat, int length, bool caseSensitive, bool posix) {
440 char *mp=nfa; /* nfa pointer */
441 char *lp; /* saved pointer */
442 char *sp=nfa; /* another one */
443 char *mpMax = mp + MAXNFA - BITBLK - 10;
444
445 int tagi = 0; /* tag stack index */
446 int tagc = 1; /* actual tag count */
447
448 int n;
449 char mask; /* xor mask -CCL/NCL */
450 int c1, c2, prevChar;
451
452 if (!pat || !length)
453 if (sta)
454 return 0;
455 else
456 return badpat("No previous regular expression");
457 sta = NOP;
458
459 const char *p=pat; /* pattern pointer */
460 for (int i=0; i<length; i++, p++) {
461 if (mp > mpMax)
462 return badpat("Pattern too long");
463 lp = mp;
464 switch (*p) {
465
466 case '.': /* match any char */
467 *mp++ = ANY;
468 break;
469
470 case '^': /* match beginning */
471 if (p == pat)
472 *mp++ = BOL;
473 else {
474 *mp++ = CHR;
475 *mp++ = *p;
476 }
477 break;
478
479 case '$': /* match endofline */
480 if (!*(p+1))
481 *mp++ = EOL;
482 else {
483 *mp++ = CHR;
484 *mp++ = *p;
485 }
486 break;
487
488 case '[': /* match char class */
489 *mp++ = CCL;
490 prevChar = 0;
491
492 i++;
493 if (*++p == '^') {
494 mask = '\377';
495 i++;
496 p++;
497 } else
498 mask = 0;
499
500 if (*p == '-') { /* real dash */
501 i++;
502 prevChar = *p;
503 ChSet(*p++);
504 }
505 if (*p == ']') { /* real brace */
506 i++;
507 prevChar = *p;
508 ChSet(*p++);
509 }
510 while (*p && *p != ']') {
511 if (*p == '-') {
512 if (prevChar < 0) {
513 // Previous def. was a char class like \d, take dash literally
514 prevChar = *p;
515 ChSet(*p);
516 } else if (*(p+1)) {
517 if (*(p+1) != ']') {
518 c1 = prevChar + 1;
519 i++;
520 c2 = *++p;
521 if (c2 == '\\') {
522 if (!*(p+1)) // End of RE
523 return badpat("Missing ]");
524 else {
525 i++;
526 p++;
527 int incr;
528 c2 = GetBackslashExpression(p, incr);
529 i += incr;
530 p += incr;
531 if (c2 >= 0) {
532 // Convention: \c (c is any char) is case sensitive, whatever the option
533 ChSet(static_cast<unsigned char>(c2));
534 prevChar = c2;
535 } else {
536 // bittab is already changed
537 prevChar = -1;
538 }
539 }
540 }
541 if (prevChar < 0) {
542 // Char after dash is char class like \d, take dash literally
543 prevChar = '-';
544 ChSet('-');
545 } else {
546 // Put all chars between c1 and c2 included in the char set
547 while (c1 <= c2) {
548 ChSetWithCase(static_cast<unsigned char>(c1++), caseSensitive);
549 }
550 }
551 } else {
552 // Dash before the ], take it literally
553 prevChar = *p;
554 ChSet(*p);
555 }
556 } else {
557 return badpat("Missing ]");
558 }
559 } else if (*p == '\\' && *(p+1)) {
560 i++;
561 p++;
562 int incr;
563 int c = GetBackslashExpression(p, incr);
564 i += incr;
565 p += incr;
566 if (c >= 0) {
567 // Convention: \c (c is any char) is case sensitive, whatever the option
568 ChSet(static_cast<unsigned char>(c));
569 prevChar = c;
570 } else {
571 // bittab is already changed
572 prevChar = -1;
573 }
574 } else {
575 prevChar = *p;
576 ChSetWithCase(*p, caseSensitive);
577 }
578 i++;
579 p++;
580 }
581 if (!*p)
582 return badpat("Missing ]");
583
584 for (n = 0; n < BITBLK; bittab[n++] = 0)
585 *mp++ = static_cast<char>(mask ^ bittab[n]);
586
587 break;
588
589 case '*': /* match 0 or more... */
590 case '+': /* match 1 or more... */
591 if (p == pat)
592 return badpat("Empty closure");
593 lp = sp; /* previous opcode */
594 if (*lp == CLO) /* equivalence... */
595 break;
596 switch (*lp) {
597
598 case BOL:
599 case BOT:
600 case EOT:
601 case BOW:
602 case EOW:
603 case REF:
604 return badpat("Illegal closure");
605 default:
606 break;
607 }
608
609 if (*p == '+')
610 for (sp = mp; lp < sp; lp++)
611 *mp++ = *lp;
612
613 *mp++ = END;
614 *mp++ = END;
615 sp = mp;
616 while (--mp > lp)
617 *mp = mp[-1];
618 *mp = CLO;
619 mp = sp;
620 break;
621
622 case '\\': /* tags, backrefs... */
623 i++;
624 switch (*++p) {
625 case '<':
626 *mp++ = BOW;
627 break;
628 case '>':
629 if (*sp == BOW)
630 return badpat("Null pattern inside \\<\\>");
631 *mp++ = EOW;
632 break;
633 case '1':
634 case '2':
635 case '3':
636 case '4':
637 case '5':
638 case '6':
639 case '7':
640 case '8':
641 case '9':
642 n = *p-'0';
643 if (tagi > 0 && tagstk[tagi] == n)
644 return badpat("Cyclical reference");
645 if (tagc > n) {
646 *mp++ = static_cast<char>(REF);
647 *mp++ = static_cast<char>(n);
648 } else
649 return badpat("Undetermined reference");
650 break;
651 default:
652 if (!posix && *p == '(') {
653 if (tagc < MAXTAG) {
654 tagstk[++tagi] = tagc;
655 *mp++ = BOT;
656 *mp++ = static_cast<char>(tagc++);
657 } else
658 return badpat("Too many \\(\\) pairs");
659 } else if (!posix && *p == ')') {
660 if (*sp == BOT)
661 return badpat("Null pattern inside \\(\\)");
662 if (tagi > 0) {
663 *mp++ = static_cast<char>(EOT);
664 *mp++ = static_cast<char>(tagstk[tagi--]);
665 } else
666 return badpat("Unmatched \\)");
667 } else {
668 int incr;
669 int c = GetBackslashExpression(p, incr);
670 i += incr;
671 p += incr;
672 if (c >= 0) {
673 *mp++ = CHR;
674 *mp++ = static_cast<unsigned char>(c);
675 } else {
676 *mp++ = CCL;
677 mask = 0;
678 for (n = 0; n < BITBLK; bittab[n++] = 0)
679 *mp++ = static_cast<char>(mask ^ bittab[n]);
680 }
681 }
682 }
683 break;
684
685 default : /* an ordinary char */
686 if (posix && *p == '(') {
687 if (tagc < MAXTAG) {
688 tagstk[++tagi] = tagc;
689 *mp++ = BOT;
690 *mp++ = static_cast<char>(tagc++);
691 } else
692 return badpat("Too many () pairs");
693 } else if (posix && *p == ')') {
694 if (*sp == BOT)
695 return badpat("Null pattern inside ()");
696 if (tagi > 0) {
697 *mp++ = static_cast<char>(EOT);
698 *mp++ = static_cast<char>(tagstk[tagi--]);
699 } else
700 return badpat("Unmatched )");
701 } else {
702 unsigned char c = *p;
703 if (!c) // End of RE
704 c = '\\'; // We take it as raw backslash
705 if (caseSensitive || !iswordc(c)) {
706 *mp++ = CHR;
707 *mp++ = c;
708 } else {
709 *mp++ = CCL;
710 mask = 0;
711 ChSetWithCase(c, false);
712 for (n = 0; n < BITBLK; bittab[n++] = 0)
713 *mp++ = static_cast<char>(mask ^ bittab[n]);
714 }
715 }
716 break;
717 }
718 sp = lp;
719 }
720 if (tagi > 0)
721 return badpat((posix ? "Unmatched (" : "Unmatched \\("));
722 *mp = END;
723 sta = OKP;
724 return 0;
725 }
726
727 /*
728 * RESearch::Execute:
729 * execute nfa to find a match.
730 *
731 * special cases: (nfa[0])
732 * BOL
733 * Match only once, starting from the
734 * beginning.
735 * CHR
736 * First locate the character without
737 * calling PMatch, and if found, call
738 * PMatch for the remaining string.
739 * END
740 * RESearch::Compile failed, poor luser did not
741 * check for it. Fail fast.
742 *
743 * If a match is found, bopat[0] and eopat[0] are set
744 * to the beginning and the end of the matched fragment,
745 * respectively.
746 *
747 */
748 int RESearch::Execute(CharacterIndexer &ci, int lp, int endp) {
749 unsigned char c;
750 int ep = NOTFOUND;
751 char *ap = nfa;
752
753 bol = lp;
754 failure = 0;
755
756 Clear();
757
758 switch (*ap) {
759
760 case BOL: /* anchored: match from BOL only */
761 ep = PMatch(ci, lp, endp, ap);
762 break;
763 case EOL: /* just searching for end of line normal path doesn't work */
764 if (*(ap+1) == END) {
765 lp = endp;
766 ep = lp;
767 break;
768 } else {
769 return 0;
770 }
771 case CHR: /* ordinary char: locate it fast */
772 c = *(ap+1);
773 while ((lp < endp) && (ci.CharAt(lp) != c))
774 lp++;
775 if (lp >= endp) /* if EOS, fail, else fall thru. */
776 return 0;
777 default: /* regular matching all the way. */
778 while (lp < endp) {
779 ep = PMatch(ci, lp, endp, ap);
780 if (ep != NOTFOUND)
781 break;
782 lp++;
783 }
784 break;
785 case END: /* munged automaton. fail always */
786 return 0;
787 }
788 if (ep == NOTFOUND)
789 return 0;
790
791 bopat[0] = lp;
792 eopat[0] = ep;
793 return 1;
794 }
795
796 /*
797 * PMatch: internal routine for the hard part
798 *
799 * This code is partly snarfed from an early grep written by
800 * David Conroy. The backref and tag stuff, and various other
801 * innovations are by oz.
802 *
803 * special case optimizations: (nfa[n], nfa[n+1])
804 * CLO ANY
805 * We KNOW .* will match everything upto the
806 * end of line. Thus, directly go to the end of
807 * line, without recursive PMatch calls. As in
808 * the other closure cases, the remaining pattern
809 * must be matched by moving backwards on the
810 * string recursively, to find a match for xy
811 * (x is ".*" and y is the remaining pattern)
812 * where the match satisfies the LONGEST match for
813 * x followed by a match for y.
814 * CLO CHR
815 * We can again scan the string forward for the
816 * single char and at the point of failure, we
817 * execute the remaining nfa recursively, same as
818 * above.
819 *
820 * At the end of a successful match, bopat[n] and eopat[n]
821 * are set to the beginning and end of subpatterns matched
822 * by tagged expressions (n = 1 to 9).
823 */
824
825 extern void re_fail(char *,char);
826
827 #define isinset(x,y) ((x)[((y)&BLKIND)>>3] & bitarr[(y)&BITIND])
828
829 /*
830 * skip values for CLO XXX to skip past the closure
831 */
832
833 #define ANYSKIP 2 /* [CLO] ANY END */
834 #define CHRSKIP 3 /* [CLO] CHR chr END */
835 #define CCLSKIP 34 /* [CLO] CCL 32 bytes END */
836
837 int RESearch::PMatch(CharacterIndexer &ci, int lp, int endp, char *ap) {
838 int op, c, n;
839 int e; /* extra pointer for CLO */
840 int bp; /* beginning of subpat... */
841 int ep; /* ending of subpat... */
842 int are; /* to save the line ptr. */
843
844 while ((op = *ap++) != END)
845 switch (op) {
846
847 case CHR:
848 if (ci.CharAt(lp++) != *ap++)
849 return NOTFOUND;
850 break;
851 case ANY:
852 if (lp++ >= endp)
853 return NOTFOUND;
854 break;
855 case CCL:
856 c = ci.CharAt(lp++);
857 if (!isinset(ap,c))
858 return NOTFOUND;
859 ap += BITBLK;
860 break;
861 case BOL:
862 if (lp != bol)
863 return NOTFOUND;
864 break;
865 case EOL:
866 if (lp < endp)
867 return NOTFOUND;
868 break;
869 case BOT:
870 bopat[*ap++] = lp;
871 break;
872 case EOT:
873 eopat[*ap++] = lp;
874 break;
875 case BOW:
876 if (lp!=bol && iswordc(ci.CharAt(lp-1)) || !iswordc(ci.CharAt(lp)))
877 return NOTFOUND;
878 break;
879 case EOW:
880 if (lp==bol || !iswordc(ci.CharAt(lp-1)) || iswordc(ci.CharAt(lp)))
881 return NOTFOUND;
882 break;
883 case REF:
884 n = *ap++;
885 bp = bopat[n];
886 ep = eopat[n];
887 while (bp < ep)
888 if (ci.CharAt(bp++) != ci.CharAt(lp++))
889 return NOTFOUND;
890 break;
891 case CLO:
892 are = lp;
893 switch (*ap) {
894
895 case ANY:
896 while (lp < endp)
897 lp++;
898 n = ANYSKIP;
899 break;
900 case CHR:
901 c = *(ap+1);
902 while ((lp < endp) && (c == ci.CharAt(lp)))
903 lp++;
904 n = CHRSKIP;
905 break;
906 case CCL:
907 while ((lp < endp) && isinset(ap+1,ci.CharAt(lp)))
908 lp++;
909 n = CCLSKIP;
910 break;
911 default:
912 failure = true;
913 //re_fail("closure: bad nfa.", *ap);
914 return NOTFOUND;
915 }
916
917 ap += n;
918
919 while (lp >= are) {
920 if ((e = PMatch(ci, lp, endp, ap)) != NOTFOUND)
921 return e;
922 --lp;
923 }
924 return NOTFOUND;
925 default:
926 //re_fail("RESearch::Execute: bad nfa.", static_cast<char>(op));
927 return NOTFOUND;
928 }
929 return lp;
930 }
931
932 /*
933 * RESearch::Substitute:
934 * substitute the matched portions of the src in dst.
935 *
936 * & substitute the entire matched pattern.
937 *
938 * \digit substitute a subpattern, with the given tag number.
939 * Tags are numbered from 1 to 9. If the particular
940 * tagged subpattern does not exist, null is substituted.
941 */
942 int RESearch::Substitute(CharacterIndexer &ci, char *src, char *dst) {
943 unsigned char c;
944 int pin;
945 int bp;
946 int ep;
947
948 if (!*src || !bopat[0])
949 return 0;
950
951 while ((c = *src++) != 0) {
952 switch (c) {
953
954 case '&':
955 pin = 0;
956 break;
957
958 case '\\':
959 c = *src++;
960 if (c >= '0' && c <= '9') {
961 pin = c - '0';
962 break;
963 }
964
965 default:
966 *dst++ = c;
967 continue;
968 }
969
970 if ((bp = bopat[pin]) != 0 && (ep = eopat[pin]) != 0) {
971 while (ci.CharAt(bp) && bp < ep)
972 *dst++ = ci.CharAt(bp++);
973 if (bp < ep)
974 return 0;
975 }
976 }
977 *dst = '\0';
978 return 1;
979 }
980