xref: /dragonfly/contrib/grep/src/search.h (revision 66e72101)
1 /* search.c - searching subroutines using dfa, kwset and regex for grep.
2    Copyright 1992, 1998, 2000, 2007, 2009-2012 Free Software Foundation, Inc.
3 
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 3, or (at your option)
7    any later version.
8 
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.
13 
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
16    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17    02110-1301, USA.  */
18 
19 #ifndef GREP_SEARCH_H
20 #define GREP_SEARCH_H 1
21 
22 #include <config.h>
23 
24 #include <sys/types.h>
25 #include <stdint.h>
26 
27 #include "mbsupport.h"
28 
29 #include <wchar.h>
30 #include <wctype.h>
31 #include <regex.h>
32 
33 #include "system.h"
34 #include "error.h"
35 #include "grep.h"
36 #include "kwset.h"
37 #include "xalloc.h"
38 
39 /* This must be a signed type.  Each value is the difference in the size
40    of a character (in bytes) induced by converting to lower case.
41    The vast majority of values are 0, but a few are 1 or -1, so
42    technically, two bits may be sufficient.  */
43 typedef signed char mb_len_map_t;
44 
45 /* searchutils.c */
46 extern void kwsinit (kwset_t *);
47 
48 extern char *mbtolower (const char *, size_t *, mb_len_map_t **);
49 extern bool is_mb_middle (const char **, const char *, const char *, size_t);
50 
51 /* dfasearch.c */
52 extern void GEAcompile (char const *, size_t, reg_syntax_t);
53 extern size_t EGexecute (char const *, size_t, size_t *, char const *);
54 
55 /* kwsearch.c */
56 extern void Fcompile (char const *, size_t);
57 extern size_t Fexecute (char const *, size_t, size_t *, char const *);
58 
59 /* pcresearch.c */
60 extern void Pcompile (char const *, size_t);
61 extern size_t Pexecute (char const *, size_t, size_t *, char const *);
62 
63 /* Apply the MAP (created by mbtolower) to the lowercase-buffer-relative
64    *OFF and *LEN, converting them to be relative to the original buffer.  */
65 static inline void
66 mb_case_map_apply (mb_len_map_t const *map, size_t *off, size_t *len)
67 {
68   if (map)
69     {
70       intmax_t off_incr = 0;
71       intmax_t len_incr = 0;
72       size_t k;
73       for (k = 0; k < *off; k++)
74         off_incr += map[k];
75       for (k = *off; k < *off + *len; k++)
76         len_incr += map[k];
77       *off += off_incr;
78       *len += len_incr;
79     }
80 }
81 
82 #endif /* GREP_SEARCH_H */
83