3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements regular expression wrappers.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_utility
44 #if defined(HAVE_POSIX_REGEX)
45 // old Mac needs sys/types.h before regex.h
46 #include <sys/types.h>
48 #define USE_POSIX_REGEX
49 #elif defined(HAVE_CXX11_REGEX)
51 #define USE_CXX11_REGEX
54 #include "exceptions.h"
55 #include "stringutil.h"
61 bool Regex::isSupported()
63 #if defined(USE_POSIX_REGEX) || defined(USE_CXX11_REGEX)
70 #if defined(USE_POSIX_REGEX)
74 explicit Impl(const char *value)
78 explicit Impl(const std::string &value)
80 compile(value.c_str());
87 bool match(const char *value) const
89 int rc = regexec(®ex_, value, 0, NULL, 0);
90 if (rc != 0 && rc != REG_NOMATCH)
92 // TODO: Handle errors.
98 void compile(const char *value)
100 std::string buf(formatString("^%s$", value));
101 int rc = regcomp(®ex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
104 // TODO: Better error messages.
105 GMX_THROW(InvalidInputError(formatString(
106 "Error in regular expression \"%s\"", value)));
112 #elif defined(USE_CXX11_REGEX)
116 explicit Impl(const char *value)
117 try : regex_(value, std::regex::nosubs | std::regex::extended)
120 catch (const std::regex_error &)
122 // TODO: Better error messages.
123 GMX_THROW(InvalidInputError(formatString(
124 "Error in regular expression \"%s\"", value)));
126 explicit Impl(const std::string &value)
127 try : regex_(value, std::regex::nosubs | std::regex::extended)
130 catch (const std::regex_error &)
132 // TODO: Better error messages.
133 GMX_THROW(InvalidInputError(formatString(
134 "Error in regular expression \"%s\"", value)));
137 bool match(const char *value) const
141 return std::regex_match(value, regex_);
143 catch (const std::regex_error &)
145 // TODO: Handle errors.
157 explicit Impl(const char * /*value*/)
159 GMX_THROW(NotImplementedError(
160 "Gromacs is compiled without regular expression support"));
162 explicit Impl(const std::string & /*value*/)
164 GMX_THROW(NotImplementedError(
165 "Gromacs is compiled without regular expression support"));
168 bool match(const char * /*value*/) const
170 // Should never be reached.
171 GMX_THROW(NotImplementedError(
172 "Gromacs is compiled without regular expression support"));
182 Regex::Regex(const char *value)
183 : impl_(new Impl(value))
187 Regex::Regex(const std::string &value)
188 : impl_(new Impl(value))
197 bool regexMatch(const char *str, const Regex ®ex)
199 if (regex.impl_.get() == NULL)
203 return regex.impl_->match(str);
206 bool regexMatch(const std::string &str, const Regex ®ex)
208 return regexMatch(str.c_str(), regex);