From 6369a38800a6a0f1f19f291a3c678d1eff7cb1ac Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Mon, 13 Apr 2015 10:18:58 +0200 Subject: [PATCH] use boost::regex instead of std::regex --- src/CMakeLists.txt | 3 ++- src/render_context.cpp | 8 ++++---- src/token.cpp | 6 +++--- src/utils.cpp | 16 ++++++++-------- test/CMakeLists.txt | 3 +++ test/benchmark_main.cpp | 24 ++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 test/benchmark_main.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 95fc2e1..9d8cb99 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -find_package(Boost 1.54 REQUIRED) +find_package(Boost 1.54 COMPONENTS regex REQUIRED) include_directories( ${CMAKE_SOURCE_DIR}/include @@ -18,3 +18,4 @@ set(SRC visitor/render_section.cpp) add_library(mstch STATIC ${SRC}) +target_link_libraries(mstch ${Boost_REGEX_LIBRARY}) \ No newline at end of file diff --git a/src/render_context.cpp b/src/render_context.cpp index 82dc6d0..ea7097c 100644 --- a/src/render_context.cpp +++ b/src/render_context.cpp @@ -1,7 +1,7 @@ #include "render_context.hpp" #include "utils.hpp" #include "state/outside_section.hpp" -#include +#include using namespace mstch; @@ -57,9 +57,9 @@ const mstch::node& render_context::get_node(const std::string& token) { std::string render_context::render(const std::string& t) { std::ostringstream output; - auto re = std::regex("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}"); - std::sregex_token_iterator it(t.begin(), t.end(), re, {-1, 0}); - for (; it != std::sregex_token_iterator(); ++it) + auto re = boost::regex("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}"); + boost::sregex_token_iterator it(t.begin(), t.end(), re, {-1, 0}); + for (; it != boost::sregex_token_iterator(); ++it) output << state.top()->render(*this, token(it->str())); return output.str(); } diff --git a/src/token.cpp b/src/token.cpp index e5b88fe..18b4d6d 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -1,6 +1,6 @@ #include "token.hpp" #include -#include +#include using namespace mstch; @@ -20,8 +20,8 @@ std::tuple token::token_info(const std::string& inside) { } token::token(const std::string& raw_token): raw_val(raw_token) { - std::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}"); - if(std::regex_match(raw_token, token_match)) { + boost::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}"); + if(boost::regex_match(raw_token, token_match)) { std::string inside{raw_token.substr(2, raw_token.size() - 4)}; boost::trim(inside); if (inside.size() > 0) { diff --git a/src/utils.cpp b/src/utils.cpp index c19d3d4..a7b323d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,18 +1,18 @@ #include "utils.hpp" -#include +#include #include std::string mstch::strip_whitespace(const std::string& tmplt) { - std::regex comment_match("\\{\\{![^\\}]*\\}\\}"); - std::regex tag_match("\\{{2}[ ]*[#|/|^|!|>]{1}[^\\}]*\\}{2}"); - std::regex whitespace_match("^\\s*$"); + boost::regex comment_match("\\{\\{![^\\}]*\\}\\}"); + boost::regex tag_match("\\{{2}[ ]*[#|/|^|!|>]{1}[^\\}]*\\}{2}"); + boost::regex whitespace_match("^\\s*$"); std::ostringstream out; - std::istringstream in(std::regex_replace(tmplt, comment_match, "{{!}}")); + std::istringstream in(boost::regex_replace(tmplt, comment_match, "{{!}}")); for(std::string line; std::getline(in, line);) { - std::string no_tags = std::regex_replace(line, tag_match, ""); - if (no_tags != line && std::regex_match(no_tags, whitespace_match)) - out << std::regex_replace(line, std::regex("\\s"), ""); + std::string no_tags = boost::regex_replace(line, tag_match, ""); + if (no_tags != line && boost::regex_match(no_tags, whitespace_match)) + out << boost::regex_replace(line, boost::regex("\\s"), ""); else out << line << (in.eof()?"":"\n"); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dfe7dd8..0632e81 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,6 +6,9 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIR}) +add_executable(benchmark benchmark_main.cpp) +target_link_libraries(benchmark mstch) + add_executable(filetoheader filetoheader.cpp) target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY}) set(filetoheader_exe ${CMAKE_CURRENT_BINARY_DIR}/filetoheader${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/benchmark_main.cpp b/test/benchmark_main.cpp new file mode 100644 index 0000000..3c17e28 --- /dev/null +++ b/test/benchmark_main.cpp @@ -0,0 +1,24 @@ +#include "mstch/mstch.hpp" + +int main() { + std::string comment_tmp{ + "

{{header}}

    " + "{{#comments}}
  • {{name}}
    " + "

    {{body}}

  • {{/comments}}
" + }; + auto comment_view = mstch::object{ + {"header", std::string{"My Post Comments"}}, + {"comments", mstch::array{ + mstch::object{{"name", std::string{"Joe"}}, {"body", std::string{"Thanks for this post!"}}}, + mstch::object{{"name", std::string{"Sam"}}, {"body", std::string{"Thanks for this post!"}}}, + mstch::object{{"name", std::string{"Heather"}}, {"body", std::string{"Thanks for this post!"}}}, + mstch::object{{"name", std::string{"Kathy"}}, {"body", std::string{"Thanks for this post!"}}}, + mstch::object{{"name", std::string{"George"}}, {"body", std::string{"Thanks for this post!"}}} + }} + }; + + for(int i = 0; i < 5000; i++) + mstch::render(comment_tmp, comment_view); + + return 0; +}