This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch branch-2.7
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 63327872d312e660bc14cbcf01bc7ae6ae21c5f5
Author: Matteo Merli <mme...@apache.org>
AuthorDate: Tue Feb 9 17:08:44 2021 -0800

    [C++] Removed usages of boost::regex (#9533)
    
    * [C++] Removed usages of boost::regex
    
    * Fixed formatting
    
    * Fixed test
    
    * Fixed formatting
    
    (cherry picked from commit 8d0c36e2f5d6508582c0b63d0f8d21602ab944b1)
---
 pulsar-client-cpp/CMakeLists.txt                    | 11 ++++++++---
 pulsar-client-cpp/lib/NamedEntity.cc                | 19 ++++++++++++++++---
 pulsar-client-cpp/lib/NamedEntity.h                 |  9 ++-------
 pulsar-client-cpp/lib/PartitionedConsumerImpl.cc    | 11 +++++++----
 pulsar-client-cpp/lib/Url.cc                        | 17 +++++++++++++----
 pulsar-client-cpp/lib/auth/AuthAthenz.cc            | 10 ----------
 pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc      | 21 +++++++++++++++++++--
 pulsar-client-cpp/python/CMakeLists.txt             |  9 ---------
 .../tests/ConsumerConfigurationTest.cc              |  2 --
 pulsar-client-cpp/tests/ZTSClientTest.cc            |  2 +-
 10 files changed, 66 insertions(+), 45 deletions(-)

diff --git a/pulsar-client-cpp/CMakeLists.txt b/pulsar-client-cpp/CMakeLists.txt
index 59b1c00..5718cca 100644
--- a/pulsar-client-cpp/CMakeLists.txt
+++ b/pulsar-client-cpp/CMakeLists.txt
@@ -17,7 +17,7 @@
 # under the License.
 #
 
-cmake_minimum_required(VERSION 3.4) 
+cmake_minimum_required(VERSION 3.4)
 project (pulsar-cpp)
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
 
@@ -43,6 +43,9 @@ MESSAGE(STATUS "BUILD_TESTS:  " ${BUILD_TESTS})
 option(BUILD_PYTHON_WRAPPER "Build Pulsar Python wrapper" ON)
 MESSAGE(STATUS "BUILD_PYTHON_WRAPPER:  " ${BUILD_PYTHON_WRAPPER})
 
+option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF)
+MESSAGE(STATUS "BUILD_PERF_TOOLS:  " ${BUILD_PERF_TOOLS})
+
 option(LINK_STATIC "Link against static libraries" OFF)
 MESSAGE(STATUS "LINK_STATIC:  " ${LINK_STATIC})
 
@@ -283,7 +286,7 @@ if (NOT MSVC)
     )
 else()
     set(COMMON_LIBS
-    ${COMMON_LIBS} 
+    ${COMMON_LIBS}
     wldap32.lib
     Normaliz.lib)
 endif()
@@ -328,7 +331,9 @@ set(CLIENT_LIBS
 )
 
 add_subdirectory(lib)
-add_subdirectory(perf)
+if(BUILD_PERF_TOOLS)
+    add_subdirectory(perf)
+endif(BUILD_PERF_TOOLS)
 add_subdirectory(examples)
 
 if (BUILD_TESTS)
diff --git a/pulsar-client-cpp/lib/NamedEntity.cc 
b/pulsar-client-cpp/lib/NamedEntity.cc
index cd68e58..ad7c385 100644
--- a/pulsar-client-cpp/lib/NamedEntity.cc
+++ b/pulsar-client-cpp/lib/NamedEntity.cc
@@ -18,8 +18,21 @@
  */
 #include "NamedEntity.h"
 
-const boost::regex NamedEntity::pattern = boost::regex("^[-=:.\\w]*$");
-
 bool NamedEntity::checkName(const std::string& name) {
-    return boost::regex_match(name, pattern) ? true : false;
+    for (char c : name) {
+        switch (c) {
+            case '=':
+            case ':':
+            case ' ':
+            case '!':
+            case '\t':
+            case '\r':
+            case '\n':
+                return false;
+            default:
+                break;
+        }
+    }
+
+    return true;
 }
diff --git a/pulsar-client-cpp/lib/NamedEntity.h 
b/pulsar-client-cpp/lib/NamedEntity.h
index 4437602..14b73d6 100644
--- a/pulsar-client-cpp/lib/NamedEntity.h
+++ b/pulsar-client-cpp/lib/NamedEntity.h
@@ -16,16 +16,11 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef _PULSAR_NAMED_ENTITY_HEADER_
-#define _PULSAR_NAMED_ENTITY_HEADER_
+#pragma once
 
-#include <boost/regex.hpp>
+#include <string>
 
 class NamedEntity {
-   private:
-    static const boost::regex pattern;
-
    public:
     static bool checkName(const std::string& name);
 };
-#endif
diff --git a/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc 
b/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc
index bf12362..5571a6c 100644
--- a/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc
+++ b/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc
@@ -229,7 +229,8 @@ ConsumerConfiguration 
PartitionedConsumerImpl::getSinglePartitionConsumerConfig(
     
config.setBrokerConsumerStatsCacheTimeInMs(conf_.getBrokerConsumerStatsCacheTimeInMs());
 
     const auto shared_this = 
const_cast<PartitionedConsumerImpl*>(this)->shared_from_this();
-    
config.setMessageListener(std::bind(&PartitionedConsumerImpl::messageReceived, 
shared_this, _1, _2));
+    
config.setMessageListener(std::bind(&PartitionedConsumerImpl::messageReceived, 
shared_this,
+                                        std::placeholders::_1, 
std::placeholders::_2));
 
     // Apply total limit of receiver queue size across partitions
     // NOTE: if it's called by handleGetPartitions(), the queue size of new 
internal consumers may be smaller
@@ -250,8 +251,9 @@ ConsumerImplPtr 
PartitionedConsumerImpl::newInternalConsumer(unsigned int partit
                                                    internalListenerExecutor_, 
true, Partitioned);
 
     const auto shared_this = 
const_cast<PartitionedConsumerImpl*>(this)->shared_from_this();
-    consumer->getConsumerCreatedFuture().addListener(std::bind(
-        &PartitionedConsumerImpl::handleSinglePartitionConsumerCreated, 
shared_this, _1, _2, partition));
+    consumer->getConsumerCreatedFuture().addListener(
+        
std::bind(&PartitionedConsumerImpl::handleSinglePartitionConsumerCreated, 
shared_this,
+                  std::placeholders::_1, std::placeholders::_2, partition));
     consumer->setPartitionIndex(partition);
 
     LOG_DEBUG("Creating Consumer for single Partition - " << 
topicPartitionName << "SubName - "
@@ -573,7 +575,8 @@ void PartitionedConsumerImpl::runPartitionUpdateTask() {
 void PartitionedConsumerImpl::getPartitionMetadata() {
     using namespace std::placeholders;
     lookupServicePtr_->getPartitionMetadataAsync(topicName_)
-        .addListener(std::bind(&PartitionedConsumerImpl::handleGetPartitions, 
shared_from_this(), _1, _2));
+        .addListener(std::bind(&PartitionedConsumerImpl::handleGetPartitions, 
shared_from_this(),
+                               std::placeholders::_1, std::placeholders::_2));
 }
 
 void PartitionedConsumerImpl::handleGetPartitions(Result result,
diff --git a/pulsar-client-cpp/lib/Url.cc b/pulsar-client-cpp/lib/Url.cc
index 0924652..f31e1fc 100644
--- a/pulsar-client-cpp/lib/Url.cc
+++ b/pulsar-client-cpp/lib/Url.cc
@@ -18,9 +18,18 @@
  */
 #include "Url.h"
 
-#include <boost/regex.hpp>
+#include <map>
+
 #include <sstream>
 
+#ifdef PULSAR_USE_BOOST_REGEX
+#include <boost/regex.hpp>
+#define PULSAR_REGEX_NAMESPACE boost
+#else
+#include <regex>
+#define PULSAR_REGEX_NAMESPACE std
+#endif
+
 namespace pulsar {
 
 static const std::map<std::string, int> initDefaultPortsMap() {
@@ -39,14 +48,14 @@ static const std::map<std::string, int>& defaultPortsMap() {
 
 bool Url::parse(const std::string& urlStr, Url& url) {
     std::vector<std::string> values;
-    static const boost::regex expression(
+    static const PULSAR_REGEX_NAMESPACE::regex expression(
         //       proto                 host               port
         "^(\?:([^:/\?#]+)://)\?(\\w+[^/\?#:]*)(\?::(\\d+))\?"
         //       path                  file       parameters
         "(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)\?(\\\?(.*))\?");
 
-    boost::cmatch groups;
-    if (!boost::regex_match(urlStr.c_str(), groups, expression)) {
+    PULSAR_REGEX_NAMESPACE::cmatch groups;
+    if (!PULSAR_REGEX_NAMESPACE::regex_match(urlStr.c_str(), groups, 
expression)) {
         // Invalid url
         return false;
     }
diff --git a/pulsar-client-cpp/lib/auth/AuthAthenz.cc 
b/pulsar-client-cpp/lib/auth/AuthAthenz.cc
index 3141fb3..82d1276 100644
--- a/pulsar-client-cpp/lib/auth/AuthAthenz.cc
+++ b/pulsar-client-cpp/lib/auth/AuthAthenz.cc
@@ -18,16 +18,6 @@
  */
 #include <lib/auth/AuthAthenz.h>
 
-#include <string.h>
-#include <time.h>
-
-#include <openssl/sha.h>
-#include <openssl/rsa.h>
-#include <openssl/ec.h>
-#include <openssl/pem.h>
-
-#include <curl/curl.h>
-
 #include <boost/property_tree/json_parser.hpp>
 #include <boost/property_tree/ptree.hpp>
 namespace ptree = boost::property_tree;
diff --git a/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc 
b/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc
index 045f918..68a0b80 100644
--- a/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc
+++ b/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc
@@ -38,13 +38,18 @@
 #include <boost/property_tree/ptree.hpp>
 namespace ptree = boost::property_tree;
 
-#include <boost/regex.hpp>
 #include <boost/xpressive/xpressive.hpp>
 #include <boost/archive/iterators/base64_from_binary.hpp>
 #include <boost/archive/iterators/transform_width.hpp>
 
 #include <mutex>
 
+#ifdef PULSAR_USE_BOOST_REGEX
+#include <boost/regex.hpp>
+#else
+#include <regex>
+#endif
+
 DECLARE_LOG_OBJECT()
 
 namespace pulsar {
@@ -360,6 +365,8 @@ const std::string ZTSClient::getHeader() const { return 
roleHeader_; }
 PrivateKeyUri ZTSClient::parseUri(const char *uri) {
     PrivateKeyUri uriSt;
     // scheme mediatype[;base64] path file
+
+#ifdef PULSAR_USE_BOOST_REGEX
     static const boost::regex expression(
         
"^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)");
     boost::cmatch groups;
@@ -367,8 +374,18 @@ PrivateKeyUri ZTSClient::parseUri(const char *uri) {
         uriSt.scheme = groups.str(1);
         uriSt.mediaTypeAndEncodingType = groups.str(2);
         uriSt.data = groups.str(4);
-        uriSt.path = groups.str(3) + groups.str(4);
     }
+#else   // !PULSAR_USE_BOOST_REGEX
+    static const std::regex expression(
+        
R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?(\/[^?#]+)?)");
+    std::cmatch groups;
+    if (std::regex_match(uri, groups, expression)) {
+        uriSt.scheme = groups.str(1);
+        uriSt.mediaTypeAndEncodingType = groups.str(2);
+        uriSt.data = groups.str(3);
+        uriSt.path = groups.str(4);
+    }
+#endif  // PULSAR_USE_BOOST_REGEX
     return uriSt;
 }
 }  // namespace pulsar
diff --git a/pulsar-client-cpp/python/CMakeLists.txt 
b/pulsar-client-cpp/python/CMakeLists.txt
index 83bc63b..bea7a2c 100644
--- a/pulsar-client-cpp/python/CMakeLists.txt
+++ b/pulsar-client-cpp/python/CMakeLists.txt
@@ -67,15 +67,6 @@ if (APPLE)
                          ${Boost_PYTHON27-MT_LIBRARY_RELEASE}
                          ${Boost_PYTHON37-MT_LIBRARY_RELEASE}
                          ${Boost_PYTHON38-MT_LIBRARY_RELEASE})
-
-    if (LINK_STATIC)
-        # When linking statically on MacOS, include also libicu since it's now 
required by boost::regex
-        find_library(ICU_DATA REQUIRED NAMES libicudata.a PATHS 
/usr/local/opt/icu4c/lib)
-        find_library(ICU_I18N REQUIRED NAMES libicui18n.a PATHS 
/usr/local/opt/icu4c/lib)
-        find_library(ICU_UUC REQUIRED NAMES libicuuc.a PATHS 
/usr/local/opt/icu4c/lib)
-
-        set(ICU_LIBS ${ICU_DATA} ${ICU_I18N} ${ICU_UUC})
-    endif ()
 endif()
 
 message(STATUS "Using Boost Python libs: ${PYTHON_WRAPPER_LIBS}")
diff --git a/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc 
b/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc
index 42e8453..379bcdc 100644
--- a/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc
+++ b/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc
@@ -18,8 +18,6 @@
  */
 #include <pulsar/Client.h>
 #include <gtest/gtest.h>
-#include <boost/date_time/posix_time/ptime.hpp>
-#include <boost/bind.hpp>
 #include <lib/LogUtils.h>
 
 DECLARE_LOG_OBJECT()
diff --git a/pulsar-client-cpp/tests/ZTSClientTest.cc 
b/pulsar-client-cpp/tests/ZTSClientTest.cc
index 9eccf17..01c2e03 100644
--- a/pulsar-client-cpp/tests/ZTSClientTest.cc
+++ b/pulsar-client-cpp/tests/ZTSClientTest.cc
@@ -39,7 +39,7 @@ TEST(ZTSClientTest, testZTSClient) {
     {
         PrivateKeyUri uri = 
ZTSClientWrapper::parseUri("file:///path/to/private.key");
         ASSERT_EQ("file", uri.scheme);
-        ASSERT_EQ("///path/to/private.key", uri.path);
+        ASSERT_EQ("/path/to/private.key", uri.path);
     }
 
     {

Reply via email to