Unify regex implementation
authorMark Abraham <mark.j.abraham@gmail.com>
Thu, 24 Jan 2019 15:11:20 +0000 (16:11 +0100)
committerChristian Blau <cblau@gwdg.de>
Fri, 25 Jan 2019 11:17:35 +0000 (12:17 +0100)
Now that we require compiler versions that all implement std::regex,
we can always have regex support and only use one implementation.

Refs #2831

Change-Id: I0118fc1aac127a5e2fcebd55a6bbef13b7c47762

CMakeLists.txt
src/config.h.cmakein
src/gromacs/selection/sm_keywords.cpp
src/gromacs/selection/tests/selectioncollection.cpp
src/gromacs/utility.h
src/gromacs/utility/gmxregex.cpp
src/gromacs/utility/gmxregex.h
src/gromacs/utility/tests/gmxregex.cpp

index f9e6cb5152adc6026f2045a114790d91bcf25979..ea9a2e856eb56e0c0b6125b43100c74c2346efc5 100644 (file)
@@ -370,10 +370,6 @@ check_include_files(sys/time.h   HAVE_SYS_TIME_H)
 check_include_files(io.h         HAVE_IO_H)
 check_include_files(sched.h      HAVE_SCHED_H)
 
-check_include_files(regex.h      HAVE_POSIX_REGEX)
-# TODO: It could be nice to inform the user if no regex support is found,
-# as selections won't be fully functional.
-
 include(CheckCXXSymbolExists)
 check_cxx_symbol_exists(gettimeofday      sys/time.h   HAVE_GETTIMEOFDAY)
 check_cxx_symbol_exists(sysconf           unistd.h     HAVE_SYSCONF)
@@ -668,8 +664,6 @@ gmx_test_sigusr1(HAVE_SIGUSR1)
 include(gmxTestPipes)
 gmx_test_pipes(HAVE_PIPES)
 
-check_include_file_cxx(regex     HAVE_CXX11_REGEX)
-
 include(gmxTestXDR)
 gmx_test_xdr(GMX_SYSTEM_XDR)
 # Darwin has system XDR, but it uses a three-argument flavour of
index 2723c0511756b5cb27af8358be7d799c07fd595f..bc7ac924b6805dffed15bcd68425995c7e933105 100644 (file)
 /* Define to 1 if xmmintrin.h is present, otherwise 0 */
 #cmakedefine01 HAVE_XMMINTRIN_H
 
-/* Define to 1 if you have the POSIX <regex.h> header file. */
-#cmakedefine01 HAVE_POSIX_REGEX
-
-/* Define to 1 if you have the C++11 <regex> header file. */
-#cmakedefine01 HAVE_CXX11_REGEX
-
 /* Define to 1 if you have the sysconf() function */
 #cmakedefine HAVE_SYSCONF
 
index 8f2bcf341d16f3df461059819c32f9b32568396e..fcf3023527d4637aa7b5e1df34865032cc54a853 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -215,13 +215,6 @@ class StringKeywordMatchItem
             }
             if (bRegExp)
             {
-                if (!gmx::Regex::isSupported())
-                {
-                    std::string message
-                        = gmx::formatString("No regular expression support, "
-                                            "cannot match \"%s\"", str);
-                    GMX_THROW(gmx::InvalidInputError(message));
-                }
                 regex_ = std::make_shared<gmx::Regex>(str);
             }
         }
index ba13fc5288a6be9dabb609f498f628376d4357a3..228f58f2186d40d8a3edaa138bb5fcd6df325b8d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -500,18 +500,6 @@ TEST_F(SelectionCollectionTest, HandlesInvalidRegularExpressions)
                      }, gmx::InvalidInputError);
 }
 
-TEST_F(SelectionCollectionTest, HandlesUnsupportedRegularExpressions)
-{
-    if (!gmx::Regex::isSupported())
-    {
-        ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
-        EXPECT_THROW_GMX({
-                             sc_.parseFromString("resname \"R[AD]\"");
-                             sc_.compile();
-                         }, gmx::InvalidInputError);
-    }
-}
-
 TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
 {
     EXPECT_THROW_GMX(sc_.parseFromString("mindist from atomnr 1 cutoff"),
@@ -1370,10 +1358,7 @@ TEST_F(SelectionCollectionDataTest, HandlesRegexMatching)
         "resname \"R[BD]\"",
         "resname ~ \"R[BD]\""
     };
-    if (gmx::Regex::isSupported())
-    {
-        runTest("simple.gro", selections);
-    }
+    runTest("simple.gro", selections);
 }
 
 
index 4651e0812b37663c24e979f56babcfb573a69da4..f6801feba288a74a20d2cdfb307ce334773f4d4e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
  * that needs to include omp.h for OpenMP functions.
  *
  * The header gmxregex.h declares gmx::Regex and regexMatch() for basic regular
- * expression matching using an interface similar to C++11.
+ * expression matching using an interface similar to C++11 std::regex.
  *
  * The header messagestringcollector.h declares a gmx::MessageStringCollector
  * class for composing messages with context information.
index 656a4b7419135641864c6768343d272c29dab13e..60f3bd92981980122c7b158525ed7af140428747 100644 (file)
 
 #include "gmxregex.h"
 
-#include "config.h"
-
-#if HAVE_POSIX_REGEX
-#    include <sys/types.h>
-// old Mac needs sys/types.h before regex.h
-#    include <regex.h>
-#else
-#    include <regex>
-#endif
+#include <regex>
 
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/stringutil.h"
 namespace gmx
 {
 
-// static
-bool Regex::isSupported() //TODO: Remove
-{
-    return true;
-}
-
-#if HAVE_POSIX_REGEX
-class Regex::Impl
-{
-    public:
-        explicit Impl(const char *value)
-        {
-            compile(value);
-        }
-        explicit Impl(const std::string &value)
-        {
-            compile(value.c_str());
-        }
-        ~Impl()
-        {
-            regfree(&regex_);
-        }
-
-        bool match(const char *value) const
-        {
-            int rc = regexec(&regex_, value, 0, nullptr, 0);
-            if (rc != 0 && rc != REG_NOMATCH)
-            {
-                // TODO: Handle errors.
-            }
-            return (rc == 0);
-        }
-
-    private:
-        void compile(const char *value)
-        {
-            std::string buf(formatString("^%s$", value));
-            int         rc = regcomp(&regex_, buf.c_str(), REG_EXTENDED | REG_NOSUB);
-            if (rc != 0)
-            {
-                // TODO: Better error messages.
-                GMX_THROW(InvalidInputError(formatString(
-                                                    "Error in regular expression \"%s\"", value)));
-            }
-        }
-
-        regex_t                 regex_;
-};
-#else
 class Regex::Impl
 {
     public:
@@ -148,7 +91,6 @@ class Regex::Impl
     private:
         std::regex              regex_;
 };
-#endif
 
 Regex::Regex()
     : impl_(nullptr)
index 432708844536f21702039ed1914d5f5e7012f9fa..5a6dcfd557dbb08d15b0b74e9f8a27730f78de3a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2013,2014,2018, by the GROMACS development team, led by
+ * Copyright (c) 2012,2013,2014,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -75,20 +75,12 @@ bool regexMatch(const std::string &str, const Regex &regex);
 //! \endcond
 
 /*! \libinternal \brief
- * Represents a regular expression.
+ * Represents a regular expression compatible with std::regex
  *
  * This class provides a simple interface for regular expression construction.
  * regexMatch() is used to match the regular expression against a string.
  * POSIX extended regular expression syntax is used.
  *
- * Currently, isSupported() will return true if either
- *
- *  -# POSIX regular expression header <regex.h> is available, or
- *  -# C++11 header \<regex> is available (e.g., new enough MSVC has this).
- *
- * In other cases, isSupported() returns false and calling other
- * constructors than the default constructor throws an exception.
- *
  * \see regexMatch()
  *
  * \inlibraryapi
@@ -97,13 +89,6 @@ bool regexMatch(const std::string &str, const Regex &regex);
 class Regex
 {
     public:
-        /*! \brief
-         * Returns true if regular expression support has been compiled in.
-         *
-         * Does not throw.
-         */
-        static bool isSupported();
-
         /*! \brief
          * Constructs a regular expression that matches nothing.
          *
index 95d6495474ffae4e2dd811188d11b0f891961ad0..40885751d0cde6dc7c0eccce8d848f2c7982f2a5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2018, by the GROMACS development team, led by
+ * Copyright (c) 2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 /*! \internal \file
  * \brief Tests for gmx::Regex
  *
- * These tests ensure that basic regex operations work. We have
- * two different underlying implementations, so we need to prove
- * to ourselves that these work the same on the range of operations
- * we try to use.
+ * These tests ensure that basic regex operations work.
  *
  * \author Mark Abraham <mark.j.abraham@gmail.com>
  * \ingroup module_utility
@@ -61,11 +58,6 @@ namespace
 
 TEST(RegexBasicTest, BasicMatchesWorkWhenSupported)
 {
-    if (!Regex::isSupported())
-    {
-        return;
-    }
-
     EXPECT_TRUE(regexMatch("dog", Regex("dog")));
     EXPECT_TRUE(regexMatch("dog", Regex("^dog")));
     EXPECT_TRUE(regexMatch("dog", Regex("dog$")));
@@ -83,11 +75,6 @@ TEST(RegexBasicTest, BasicMatchesWorkWhenSupported)
 
 TEST(RegexBasicTest, MatchesForCharacterClassesWorkWhenSupported)
 {
-    if (!Regex::isSupported())
-    {
-        return;
-    }
-
     EXPECT_TRUE(regexMatch("Dog", Regex("[[:alpha:]]+")));
     EXPECT_TRUE(regexMatch("dog", Regex("[[:lower:]]+")));
     EXPECT_TRUE(regexMatch("DOG", Regex("[[:upper:]]+")));