1 /* Pattern Matcher for Fixed String search.
2 Copyright (C) 1992, 1998, 2000, 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
35 # define IN_CTYPE_DOMAIN(c) 1
37 # define IN_CTYPE_DOMAIN(c) isascii(c)
39 #define ISALNUM(C) (IN_CTYPE_DOMAIN (C) && isalnum (C))
42 Fcompile (const char *pattern
, size_t pattern_size
,
43 bool match_icase
, bool match_words
, bool match_lines
,
46 struct compiled_kwset
*ckwset
;
47 const char *beg
, *lim
, *err
;
49 ckwset
= (struct compiled_kwset
*) xmalloc (sizeof (struct compiled_kwset
));
50 kwsinit (ckwset
, match_icase
, match_words
, match_lines
, eolbyte
);
55 for (lim
= beg
; lim
< pattern
+ pattern_size
&& *lim
!= '\n'; ++lim
)
57 if ((err
= kwsincr (ckwset
->kwset
, beg
, lim
- beg
)) != NULL
)
58 error (exit_failure
, 0, err
);
59 if (lim
< pattern
+ pattern_size
)
63 while (beg
< pattern
+ pattern_size
);
65 if ((err
= kwsprep (ckwset
->kwset
)) != NULL
)
66 error (exit_failure
, 0, err
);
71 Fexecute (const void *compiled_pattern
, const char *buf
, size_t buf_size
,
72 size_t *match_size
, bool exact
)
74 struct compiled_kwset
*ckwset
= (struct compiled_kwset
*) compiled_pattern
;
75 register const char *beg
, *try, *end
;
77 char eol
= ckwset
->eolbyte
;
78 struct kwsmatch kwsmatch
;
82 mb_properties
= check_multibyte_string (buf
, buf_size
);
83 #endif /* MBS_SUPPORT */
85 for (beg
= buf
; beg
<= buf
+ buf_size
; ++beg
)
88 kwsexec (ckwset
->kwset
, beg
, buf
+ buf_size
- beg
, &kwsmatch
);
89 if (offset
== (size_t) -1)
94 #endif /* MBS_SUPPORT */
98 if (MB_CUR_MAX
> 1 && mb_properties
[offset
+beg
-buf
] == 0)
99 continue; /* It is a part of multibyte character. */
100 #endif /* MBS_SUPPORT */
102 len
= kwsmatch
.size
[0];
108 free (mb_properties
);
109 #endif /* MBS_SUPPORT */
112 if (ckwset
->match_lines
)
114 if (beg
> buf
&& beg
[-1] != eol
)
116 if (beg
+ len
< buf
+ buf_size
&& beg
[len
] != eol
)
120 else if (ckwset
->match_words
)
121 for (try = beg
; len
; )
123 if (try > buf
&& IS_WORD_CONSTITUENT ((unsigned char) try[-1]))
125 if (try + len
< buf
+ buf_size
126 && IS_WORD_CONSTITUENT ((unsigned char) try[len
]))
128 offset
= kwsexec (ckwset
->kwset
, beg
, --len
, &kwsmatch
);
129 if (offset
== (size_t) -1)
133 free (mb_properties
);
134 #endif /* MBS_SUPPORT */
138 len
= kwsmatch
.size
[0];
149 free (mb_properties
);
150 #endif /* MBS_SUPPORT */
154 end
= memchr (beg
+ len
, eol
, (buf
+ buf_size
) - (beg
+ len
));
156 while (buf
< beg
&& beg
[-1] != eol
)
158 *match_size
= end
- beg
;
161 free (mb_properties
);
162 #endif /* MBS_SUPPORT */
167 Ffree (void *compiled_pattern
)
169 struct compiled_kwset
*ckwset
= (struct compiled_kwset
*) compiled_pattern
;
171 free (ckwset
->trans
);
175 matcher_t matcher_fgrep
=