Просмотр исходного кода

HPCC-15077 Use C++11 regex if boost regex not found and not disabled

Signed-off-by: Mark Kelly <mark.kelly@lexisnexis.com>
Mark Kelly 9 лет назад
Родитель
Сommit
a9948f3b4c

+ 13 - 0
cmake_modules/commonSetup.cmake

@@ -59,6 +59,7 @@ IF ("${COMMONSETUP_DONE}" STREQUAL "")
   option(USE_OPENLDAP "Enable OpenLDAP support (requires OpenLDAP)" ON)
   option(USE_ICU "Enable unicode support (requires ICU)" ON)
   option(USE_BOOST_REGEX "Configure use of boost regex" ON)
+  option(USE_C11_REGEX "Configure use of c++11 std::regex" ON)
   option(Boost_USE_STATIC_LIBS "Use boost_regex static library for RPM BUILD" OFF)
   option(USE_OPENSSL "Configure use of OpenSSL" ON)
   option(USE_ZLIB "Configure use of zlib" ON)
@@ -774,10 +775,22 @@ IF ("${COMMONSETUP_DONE}" STREQUAL "")
       if(USE_BOOST_REGEX)
         find_package(BOOST_REGEX)
         if (BOOST_REGEX_FOUND)
+          message(STATUS "BOOST_REGEX enabled")
           add_definitions (-D_USE_BOOST_REGEX)
         else()
           message(FATAL_ERROR "BOOST_REGEX requested but package not found")
         endif()
+      else(USE_BOOST_REGEX)
+        if (USE_C11_REGEX)
+          if ((NOT CMAKE_COMPILER_IS_GNUCC) OR (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.1))
+            message(STATUS "C11_REGEX enabled")
+            add_definitions (-D_USE_C11_REGEX)
+          else()
+            message(STATUS "C11_REGEX requested but not supported on this platform")
+          endif()
+        else(USE_C11_REGEX)
+          message(STATUS "NO REGEX requested")
+        endif(USE_C11_REGEX)
       endif(USE_BOOST_REGEX)
 
       if(USE_OPENSSL)

+ 0 - 1
esp/logging/loggingagent/espserverloggingagent/CMakeLists.txt

@@ -33,7 +33,6 @@ include_directories (
     ${HPCC_SOURCE_DIR}/system/include
     ${HPCC_SOURCE_DIR}/system/jlib
     ${HPCC_SOURCE_DIR}/system/xmllib
-    ${HPCC_SOURCE_DIR}/system/boost/include
     ${HPCC_SOURCE_DIR}/system/security/shared
     ${HPCC_SOURCE_DIR}/esp/bindings
     ${HPCC_SOURCE_DIR}/esp/bindings/http/client

+ 0 - 1
esp/logging/loggingmanager/CMakeLists.txt

@@ -38,7 +38,6 @@ set ( SRCS
 include_directories (
     ${HPCC_SOURCE_DIR}/esp/platform
     ${HPCC_SOURCE_DIR}/system/jlib
-    ${HPCC_SOURCE_DIR}/system/boost/include
     ${HPCC_SOURCE_DIR}/system/security/shared
     ${HPCC_SOURCE_DIR}/system/xmllib
     ${HPCC_SOURCE_DIR}/system/include

+ 8 - 6
rtl/eclrtl/CMakeLists.txt

@@ -68,13 +68,15 @@ include_directories (
 
 ADD_DEFINITIONS( -D_USRDLL -DECLRTL_EXPORTS )
 
-if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)
-    set_source_files_properties(eclrtl.cpp PROPERTIES COMPILE_FLAGS -std=c++98)
-endif ()
+if (USE_BOOST_REGEX)
+    if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)
+        set_source_files_properties(eclrtl.cpp PROPERTIES COMPILE_FLAGS -std=c++98)
+    endif ()
 
-if (WIN32)
-else ()
-    ADD_DEFINITIONS( -DBOOST_DYN_LINK )
+    if (WIN32)
+    else ()
+        ADD_DEFINITIONS( -DBOOST_DYN_LINK )
+    endif ()
 endif ()
 
 HPCC_ADD_LIBRARY( eclrtl SHARED ${SRCS} )

+ 57 - 16
rtl/eclrtl/eclrtl.cpp

@@ -16,8 +16,10 @@
 ############################################################################## */
 
 #include "limits.h"
-#ifdef _USE_BOOST_REGEX
+#if defined(_USE_BOOST_REGEX)
 #include "boost/regex.hpp" // must precede platform.h ; n.b. this uses a #pragma comment(lib, ...) to link the appropriate .lib in MSVC
+#elif defined(_USE_C11_REGEX)
+#include <regex>
 #endif
 #include "platform.h"
 #include <math.h>
@@ -4830,18 +4832,34 @@ int rtlNewSearchUtf8Table(unsigned count, unsigned elemlen, char * * table, unsi
 }
 
 //---------------------------------------------------------------------------
-#ifdef _USE_BOOST_REGEX
+#if defined(_USE_BOOST_REGEX) || defined(_USE_C11_REGEX)
+
+#if defined(_USE_BOOST_REGEX)
+using boost::regex;
+using boost::regex_search;
+using boost::regex_replace;
+using boost::regex_iterator;
+using boost::cmatch;
+using boost::match_results;
+#else
+using std::regex;
+using std::regex_search;
+using std::regex_replace;
+using std::regex_iterator;
+using std::cmatch;
+using std::match_results;
+#endif
 
 class CStrRegExprFindInstance : implements IStrRegExprFindInstance
 {
 private:
     bool            matched;
-    const boost::regex * regEx;
-    boost::cmatch   subs;
+    const regex * regEx;
+    cmatch   subs;
     char *          sample; //only required if findstr/findvstr will be called
 
 public:
-    CStrRegExprFindInstance(const boost::regex * _regEx, const char * _str, size32_t _from, size32_t _len, bool _keep)
+    CStrRegExprFindInstance(const regex * _regEx, const char * _str, size32_t _from, size32_t _len, bool _keep)
         : regEx(_regEx)
     {
         matched = false;
@@ -4853,16 +4871,20 @@ public:
                 sample = (char *)rtlMalloc(_len + 1);  //required for findstr
                 memcpy(sample, _str + _from, _len);
                 sample[_len] = (char)NULL;
-                matched = boost::regex_search(sample, subs, *regEx);
+                matched = regex_search(sample, subs, *regEx);
             }
             else
             {
-                matched = boost::regex_search(_str + _from, _str + _len, subs, *regEx);
+                matched = regex_search(_str + _from, _str + _len, subs, *regEx);
             }
         }
         catch (const std::runtime_error & e)
         {
+#if defined(_USE_BOOST_REGEX)
             throw MakeStringException(0, "Error in regex search: %s (regex: %s)", e.what(), regEx->str().c_str());
+#else
+            throw MakeStringException(0, "Error in regex search: %s", e.what());
+#endif
         }
 
     }
@@ -4914,19 +4936,30 @@ public:
 class CCompiledStrRegExpr : implements ICompiledStrRegExpr
 {
 private:
-    boost::regex    regEx;
+    regex    regEx;
 
 public:
     CCompiledStrRegExpr(const char * _regExp, bool _isCaseSensitive = false) 
     {
         try
         {
+#if defined(_USE_BOOST_REGEX)
             if (_isCaseSensitive)
-                regEx.assign(_regExp, boost::regbase::perl);
+                regEx.assign(_regExp, regex::perl);
             else
-                regEx.assign(_regExp, boost::regbase::perl | boost::regbase::icase);                
+                regEx.assign(_regExp, regex::perl | regex::icase);
+#else
+            if (_isCaseSensitive)
+                regEx.assign(_regExp, regex::ECMAScript);
+            else
+                regEx.assign(_regExp, regex::ECMAScript | regex::icase);
+#endif
         }
+#if defined(_USE_BOOST_REGEX)
         catch(const boost::bad_expression & e)
+#else
+        catch(const std::regex_error & e)
+#endif
         {
             StringBuffer msg;
             msg.append("Bad regular expression: ").append(e.what()).append(": ").append(_regExp);
@@ -4944,11 +4977,19 @@ public:
         try
         {
 //          tgt = boost::regex_merge(src, cre->regEx, fmt, boost::format_perl); //Algorithm regex_merge has been renamed regex_replace, existing code will continue to compile, but new code should use regex_replace instead.
-            tgt = boost::regex_replace(src, regEx, fmt, boost::format_perl);
+#if defined(_USE_BOOST_REGEX)
+            tgt = regex_replace(src, regEx, fmt, boost::format_perl);
+#else
+            tgt = regex_replace(src, regEx, fmt);
+#endif
         }
         catch(const std::runtime_error & e)
         {
+#if defined(_USE_BOOST_REGEX)
             throw MakeStringException(0, "Error in regex replace: %s (regex: %s)", e.what(), regEx.str().c_str());
+#else
+            throw MakeStringException(0, "Error in regex replace: %s", e.what());
+#endif
         }
         outlen = tgt.length();
         out = (char *)rtlMalloc(outlen);
@@ -4967,11 +5008,11 @@ public:
         size32_t outBytes = 0;
         const char * search_end = _search+_srcLen;
 
-        boost::regex_iterator<const char *> cur(_search, search_end, regEx);
-        boost::regex_iterator<const char *> end; // Default contructor creates an end of list marker
+        regex_iterator<const char *> cur(_search, search_end, regEx);
+        regex_iterator<const char *> end; // Default contructor creates an end of list marker
         for (; cur != end; ++cur)
         {
-            const boost::match_results<const char *> &match = *cur;
+            const match_results<const char *> &match = *cur;
             if (match[0].first==search_end) break;
 
             const size32_t lenBytes = match[0].second - match[0].first;
@@ -5197,7 +5238,7 @@ ECLRTL_API void rtlDestroyUStrRegExprFindInstance(IUStrRegExprFindInstance * fin
         delete (CUStrRegExprFindInstance*)findInst;
 }
 
-#else // _USE_BOOST_REGEX not set
+#else // _USE_BOOST_REGEX or _USE_C11_REGEX not set
 ECLRTL_API ICompiledStrRegExpr * rtlCreateCompiledStrRegExpr(const char * regExpr, bool isCaseSensitive)
 {
     UNIMPLEMENTED_X("Boost regex disabled");
@@ -5223,7 +5264,7 @@ ECLRTL_API void rtlDestroyCompiledUStrRegExpr(ICompiledUStrRegExpr * compiledExp
 ECLRTL_API void rtlDestroyUStrRegExprFindInstance(IUStrRegExprFindInstance * findInst)
 {
 }
-#endif
+#endif // _USE_BOOST_REGEX or _USE_C11_REGEX
 //---------------------------------------------------------------------------
 
 ECLRTL_API int rtlQueryLocalFailCode(IException * e)