xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/gdb_regex.cc (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Copyright (C) 2011-2024 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include "gdb_regex.h"
19 #include "gdbsupport/def-vector.h"
20 
21 compiled_regex::compiled_regex (const char *regex, int cflags,
22 				const char *message)
23 {
24   gdb_assert (regex != NULL);
25   gdb_assert (message != NULL);
26 
27   int code = regcomp (&m_pattern, regex, cflags);
28   if (code != 0)
29     {
30       size_t length = regerror (code, &m_pattern, NULL, 0);
31       gdb::def_vector<char> err (length);
32 
33       regerror (code, &m_pattern, err.data (), length);
34       error (("%s: %s"), message, err.data ());
35     }
36 }
37 
38 compiled_regex::~compiled_regex ()
39 {
40   regfree (&m_pattern);
41 }
42 
43 int
44 compiled_regex::exec (const char *string, size_t nmatch,
45 		      regmatch_t pmatch[], int eflags) const
46 {
47   return regexec (&m_pattern, string, nmatch, pmatch, eflags);
48 }
49 
50 int
51 compiled_regex::search (const char *string,
52 			int length, int start, int range,
53 			struct re_registers *regs)
54 {
55   return re_search (&m_pattern, string, length, start, range, regs);
56 }
57