From 9f995a129c97cfe95ca06957bd15922d124ff01b Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 13 Feb 2024 01:47:09 +0100 Subject: [PATCH] add Boost.Regex support --- CMakeLists.txt | 18 +++++++++++++++++- src/regex.h | 43 +++++++++++++++++++++++++++++++++++++++++++ src/webhook_push.cpp | 10 +++++----- 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/regex.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ba6f31..59e34f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ set(DTRANSLATEBOT_HEADERS src/database_core.h src/database_file.h src/message_queue.h + src/regex.h src/settings.h src/settings_types.h src/slashcommands.h @@ -49,6 +50,18 @@ set(DTRANSLATEBOT_SOURCES # dtranslatebot Module Path list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +# Boost C++ Libraries +option(WITH_BOOST "Build with Boost C++ Libraries" OFF) +if (WITH_BOOST) + find_package(Boost COMPONENTS regex) + if (Boost_regex_FOUND) + list(APPEND DTRANSLATEBOT_LIBRARIES + Boost::regex + ) + set(DTRANSLATEBOT_USE_BOOST_REGEX TRUE) + endif() +endif() + # D++ Discord API Library for Bots find_package(DPP REQUIRED) @@ -58,10 +71,13 @@ find_package(Threads REQUIRED) # dtranslatebot Target + Installs add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES}) +target_compile_definitions(dtranslatebot PRIVATE + $<$:DTRANSLATEBOT_USE_BOOST_REGEX> +) if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914) target_compile_options(dtranslatebot PRIVATE $<$:/Zc:__cplusplus>) endif() -target_link_libraries(dtranslatebot PRIVATE Threads::Threads ${DPP_LIBRARIES}) +target_link_libraries(dtranslatebot PRIVATE Threads::Threads ${DPP_LIBRARIES} ${DTRANSLATEBOT_LIBRARIES}) target_include_directories(dtranslatebot PRIVATE ${DPP_INCLUDE_DIR}) set_target_properties(dtranslatebot PROPERTIES CXX_STANDARD 17 diff --git a/src/regex.h b/src/regex.h new file mode 100644 index 0000000..3f2e3db --- /dev/null +++ b/src/regex.h @@ -0,0 +1,43 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2023-2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#ifndef REGEX_H +#define REGEX_H + +#ifdef DTRANSLATEBOT_USE_BOOST_REGEX +#include +#else +#include +#endif +#include + +namespace bot { +#ifdef DTRANSLATEBOT_USE_BOOST_REGEX + using boost::regex; + using boost::regex_match; + using boost::match_results; + typedef boost::match_results svmatch; +#else + using std::regex; + using std::regex_match; + using std::match_results; + typedef std::match_results svmatch; +#endif +} + +#endif // REGEX_H diff --git a/src/webhook_push.cpp b/src/webhook_push.cpp index c9f5a81..1e4a0f7 100644 --- a/src/webhook_push.cpp +++ b/src/webhook_push.cpp @@ -17,7 +17,7 @@ *****************************************************************************/ #include -#include +#include "regex.h" #include "webhook_push.h" using namespace std::string_literals; using namespace std::string_view_literals; @@ -40,16 +40,16 @@ void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster message_v = message_v.substr(1333 + pos); } else { - std::match_results match; - if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*(\\.|\\?|\\!|\\。)\\s.*$"s))) { + bot::svmatch match; + if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*(\\.|\\?|\\!|\\。)\\s.*$"s))) { json_body["content"] = message_v.substr(0, 1334 + match.position(1)); message_v = message_v.substr(1334 + match.position(1)); } - else if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*(\\,)\\s.*$"s))) { + else if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*(\\,)\\s.*$"s))) { json_body["content"] = message_v.substr(0, 1334 + match.position(1)); message_v = message_v.substr(1334 + match.position(1)); } - else if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*()\\s.*$"s))) { + else if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*()\\s.*$"s))) { json_body["content"] = message_v.substr(0, 1334 + match.position(1)); message_v = message_v.substr(1334 + match.position(1)); }