2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2014,2015,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements regular expression wrappers.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
49 # include <sys/types.h>
50 // old Mac needs sys/types.h before regex.h
52 #elif HAVE_CXX11_REGEX
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/stringutil.h"
63 bool Regex::isSupported()
65 #if HAVE_POSIX_REGEX || HAVE_CXX11_REGEX
76 explicit Impl(const char *value
)
80 explicit Impl(const std::string
&value
)
82 compile(value
.c_str());
89 bool match(const char *value
) const
91 int rc
= regexec(®ex_
, value
, 0, nullptr, 0);
92 if (rc
!= 0 && rc
!= REG_NOMATCH
)
94 // TODO: Handle errors.
100 void compile(const char *value
)
102 std::string
buf(formatString("^%s$", value
));
103 int rc
= regcomp(®ex_
, buf
.c_str(), REG_EXTENDED
| REG_NOSUB
);
106 // TODO: Better error messages.
107 GMX_THROW(InvalidInputError(formatString(
108 "Error in regular expression \"%s\"", value
)));
114 #elif HAVE_CXX11_REGEX
118 explicit Impl(const char *value
)
119 try : regex_(value
, std::regex::nosubs
| std::regex::extended
)
122 catch (const std::regex_error
&)
124 // TODO: Better error messages.
125 GMX_THROW(InvalidInputError(formatString(
126 "Error in regular expression \"%s\"", value
)));
128 explicit Impl(const std::string
&value
)
129 try : regex_(value
, std::regex::nosubs
| std::regex::extended
)
132 catch (const std::regex_error
&)
134 // TODO: Better error messages.
135 GMX_THROW(InvalidInputError(formatString(
136 "Error in regular expression \"%s\"", value
.c_str())));
139 bool match(const char *value
) const
143 return std::regex_match(value
, regex_
);
145 catch (const std::regex_error
&)
147 // TODO: Handle errors.
159 explicit Impl(const char * /*value*/)
161 GMX_THROW(NotImplementedError(
162 "GROMACS is compiled without regular expression support"));
164 explicit Impl(const std::string
& /*value*/)
166 GMX_THROW(NotImplementedError(
167 "GROMACS is compiled without regular expression support"));
170 bool match(const char * /*value*/) const
172 // Should never be reached.
173 GMX_THROW(NotImplementedError(
174 "GROMACS is compiled without regular expression support"));
184 Regex::Regex(const char *value
)
185 : impl_(new Impl(value
))
189 Regex::Regex(const std::string
&value
)
190 : impl_(new Impl(value
))
199 bool regexMatch(const char *str
, const Regex
®ex
)
201 if (regex
.impl_
== nullptr)
205 return regex
.impl_
->match(str
);
208 bool regexMatch(const std::string
&str
, const Regex
®ex
)
210 return regexMatch(str
.c_str(), regex
);