Description: Port code to pcre2
Author: Nilesh Patra <nilesh@debian.org>
Forwarded: https://github.com/PLAST-software/plast-library/issues/10
Last-Update: Jan 24, 2022
--- a/src/alignment/filter/impl/AlignmentFilterOperator.cpp
+++ b/src/alignment/filter/impl/AlignmentFilterOperator.cpp
@@ -156,19 +156,16 @@
 ** REMARKS :
 *********************************************************************/
 AlignmentFilterRegexOperator::AlignmentFilterRegexOperator (const std::vector<std::string>& args)
-    : AlignmentFilterUnaryOperator<std::string>(args), _reg(0), _regExtra(0)
+    : AlignmentFilterUnaryOperator<std::string>(args), _reg(0)
 {
-    const char* pcreErrorStr = 0;
-    int pcreErrorOffset = 0;
+    int pcreErrorStr = 0;
+    PCRE2_SIZE pcreErrorOffset = 0;
 
     /** We compile the regexp. */
-    _reg = pcre_compile (_value.c_str(), 0, &pcreErrorStr, &pcreErrorOffset, NULL);
+    _reg = pcre2_compile ((PCRE2_SPTR)_value.c_str(), PCRE2_ZERO_TERMINATED, 0, &pcreErrorStr, &pcreErrorOffset, NULL);
 
-    /** We optimize the regex. */
-    if (_reg != 0)  {  _regExtra = pcre_study (_reg, 0, &pcreErrorStr);  }
-
-    DEBUG (("AlignmentFilterRegexOperator::AlignmentFilterRegexOperator  _value='%s'  _reg=%p  _regExtra=%p\n",
-        _value.c_str(), _reg, _regExtra
+    DEBUG (("AlignmentFilterRegexOperator::AlignmentFilterRegexOperator  _value='%s'  _reg=%p\n",
+        _value.c_str(), _reg
     ));
 }
 
@@ -182,8 +179,7 @@
 *********************************************************************/
 AlignmentFilterRegexOperator::~AlignmentFilterRegexOperator ()
 {
-    if (_reg)       {  pcre_free (_reg);        }
-    if (_regExtra)  {  pcre_free (_regExtra);   }
+    if (_reg)       {  pcre2_code_free (_reg);        }
 
     DEBUG (("AlignmentFilterRegexOperator::~AlignmentFilterRegexOperator\n"));
 }
--- a/src/alignment/filter/impl/AlignmentFilterOperator.hpp
+++ b/src/alignment/filter/impl/AlignmentFilterOperator.hpp
@@ -28,7 +28,8 @@
 #include <alignment/filter/api/IAlignmentFilter.hpp>
 #include <designpattern/impl/Property.hpp>
 #include <stdarg.h>
-#include <pcre/pcre.h>
+#define PCRE2_CODE_UNIT_WIDTH 8
+#include <pcre2.h>
 
 /********************************************************************************/
 namespace alignment {
@@ -196,14 +197,12 @@
     /** */
     AlignmentFilterRegexOperator (const std::vector<std::string>& args);
 
-    AlignmentFilterRegexOperator () : AlignmentFilterUnaryOperator<std::string>(), _reg(0), _regExtra(0) {}
+    AlignmentFilterRegexOperator () : AlignmentFilterUnaryOperator<std::string>(), _reg(0) {}
 
     ~AlignmentFilterRegexOperator ();
 
 protected:
-    pcre*       _reg;
-    pcre_extra* _regExtra;
-
+    pcre2_code *_reg;
 };
 
 /********************************************************************************/
@@ -248,10 +247,11 @@
         bool isOk (const core::Alignment& a) const { \
             if (_reg==0)  { return false; } \
             std::string tmp(regexp); /* Not optimal but we may call with const char* or std::string. */ \
+            pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(_reg, NULL); \
             switch(_operator) \
             { \
-                case HOLD:      return pcre_exec (_reg, _regExtra, tmp.c_str(), tmp.size(), 0, 0, NULL, 0) >= 0; \
-                case NO_HOLD:   return pcre_exec (_reg, _regExtra, tmp.c_str(), tmp.size(), 0, 0, NULL, 0) < 0;  \
+                case HOLD:      return pcre2_match (_reg, (PCRE2_SPTR)tmp.c_str(), tmp.size(), 0, 0, match_data, NULL) >= 0; \
+                case NO_HOLD:   return pcre2_match (_reg, (PCRE2_SPTR)tmp.c_str(), tmp.size(), 0, 0, match_data, NULL) < 0;  \
                 default:        return false; \
             } \
         } \
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -46,7 +46,7 @@
 include_directories   (${PROJECT_BINARY_DIR}/include  ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
 add_library           (PlastLibrary SHARED  ${PlastLibraryFiles} )
 add_executable        (plast ${PlastCmdFiles})
-target_link_libraries (plast pthread)
+target_link_libraries (plast pthread -lpcre2-8)
 
 ################################################################################
 # OS specific stuffs
