2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2014, 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
48 #if defined(HAVE_POSIX_REGEX)
49 #include <sys/types.h>
50 // old Mac needs sys/types.h before regex.h
52 #define USE_POSIX_REGEX
53 #elif defined(HAVE_CXX11_REGEX)
55 #define USE_CXX11_REGEX
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/stringutil.h"
65 bool Regex::isSupported()
67 #if defined(USE_POSIX_REGEX) || defined(USE_CXX11_REGEX)
74 #if defined(USE_POSIX_REGEX)
78 explicit Impl(const char *value)
82 explicit Impl(const std::string &value)
84 compile(value.c_str());
91 bool match(const char *value) const
93 int rc = regexec(®ex_, value, 0, NULL, 0);
94 if (rc != 0 && rc != REG_NOMATCH)
96 // TODO: Handle errors.
102 void compile(const char *value)
104 std::string buf(formatString("^%s$", value));
105 int rc = regcomp(®ex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
108 // TODO: Better error messages.
109 GMX_THROW(InvalidInputError(formatString(
110 "Error in regular expression \"%s\"", value)));
116 #elif defined(USE_CXX11_REGEX)
120 explicit Impl(const char *value)
121 try : regex_(value, std::regex::nosubs | std::regex::extended)
124 catch (const std::regex_error &)
126 // TODO: Better error messages.
127 GMX_THROW(InvalidInputError(formatString(
128 "Error in regular expression \"%s\"", value)));
130 explicit Impl(const std::string &value)
131 try : regex_(value, std::regex::nosubs | std::regex::extended)
134 catch (const std::regex_error &)
136 // TODO: Better error messages.
137 GMX_THROW(InvalidInputError(formatString(
138 "Error in regular expression \"%s\"", value)));
141 bool match(const char *value) const
145 return std::regex_match(value, regex_);
147 catch (const std::regex_error &)
149 // TODO: Handle errors.
161 explicit Impl(const char * /*value*/)
163 GMX_THROW(NotImplementedError(
164 "Gromacs is compiled without regular expression support"));
166 explicit Impl(const std::string & /*value*/)
168 GMX_THROW(NotImplementedError(
169 "Gromacs is compiled without regular expression support"));
172 bool match(const char * /*value*/) const
174 // Should never be reached.
175 GMX_THROW(NotImplementedError(
176 "Gromacs is compiled without regular expression support"));
186 Regex::Regex(const char *value)
187 : impl_(new Impl(value))
191 Regex::Regex(const std::string &value)
192 : impl_(new Impl(value))
201 bool regexMatch(const char *str, const Regex ®ex)
203 if (regex.impl_.get() == NULL)
207 return regex.impl_->match(str);
210 bool regexMatch(const std::string &str, const Regex ®ex)
212 return regexMatch(str.c_str(), regex);