add Boost.Regex support

This commit is contained in:
Syping 2024-02-13 01:47:09 +01:00
parent 5b9c3ce38f
commit 9f995a129c
3 changed files with 65 additions and 6 deletions

View file

@ -25,6 +25,7 @@ set(DTRANSLATEBOT_HEADERS
src/database_core.h src/database_core.h
src/database_file.h src/database_file.h
src/message_queue.h src/message_queue.h
src/regex.h
src/settings.h src/settings.h
src/settings_types.h src/settings_types.h
src/slashcommands.h src/slashcommands.h
@ -49,6 +50,18 @@ set(DTRANSLATEBOT_SOURCES
# dtranslatebot Module Path # dtranslatebot Module Path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 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 # D++ Discord API Library for Bots
find_package(DPP REQUIRED) find_package(DPP REQUIRED)
@ -58,10 +71,13 @@ find_package(Threads REQUIRED)
# dtranslatebot Target + Installs # dtranslatebot Target + Installs
add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES}) add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES})
target_compile_definitions(dtranslatebot PRIVATE
$<$<BOOL:${DTRANSLATEBOT_USE_BOOST_REGEX}>:DTRANSLATEBOT_USE_BOOST_REGEX>
)
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914) if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
target_compile_options(dtranslatebot PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>) target_compile_options(dtranslatebot PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
endif() 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}) target_include_directories(dtranslatebot PRIVATE ${DPP_INCLUDE_DIR})
set_target_properties(dtranslatebot PROPERTIES set_target_properties(dtranslatebot PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17

43
src/regex.h Normal file
View file

@ -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 <boost/regex.hpp>
#else
#include <regex>
#endif
#include <string_view>
namespace bot {
#ifdef DTRANSLATEBOT_USE_BOOST_REGEX
using boost::regex;
using boost::regex_match;
using boost::match_results;
typedef boost::match_results<std::string_view::const_iterator> svmatch;
#else
using std::regex;
using std::regex_match;
using std::match_results;
typedef std::match_results<std::string_view::const_iterator> svmatch;
#endif
}
#endif // REGEX_H

View file

@ -17,7 +17,7 @@
*****************************************************************************/ *****************************************************************************/
#include <future> #include <future>
#include <regex> #include "regex.h"
#include "webhook_push.h" #include "webhook_push.h"
using namespace std::string_literals; using namespace std::string_literals;
using namespace std::string_view_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); message_v = message_v.substr(1333 + pos);
} }
else { else {
std::match_results<std::string_view::const_iterator> match; bot::svmatch match;
if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*(\\.|\\?|\\!|\\。)\\s.*$"s))) { 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)); json_body["content"] = message_v.substr(0, 1334 + match.position(1));
message_v = message_v.substr(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)); json_body["content"] = message_v.substr(0, 1334 + match.position(1));
message_v = message_v.substr(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)); json_body["content"] = message_v.substr(0, 1334 + match.position(1));
message_v = message_v.substr(1334 + match.position(1)); message_v = message_v.substr(1334 + match.position(1));
} }