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