use boost::regex instead of std::regex
This commit is contained in:
parent
8d1336d7df
commit
6369a38800
6 changed files with 44 additions and 16 deletions
|
@ -1,4 +1,4 @@
|
||||||
find_package(Boost 1.54 REQUIRED)
|
find_package(Boost 1.54 COMPONENTS regex REQUIRED)
|
||||||
|
|
||||||
include_directories(
|
include_directories(
|
||||||
${CMAKE_SOURCE_DIR}/include
|
${CMAKE_SOURCE_DIR}/include
|
||||||
|
@ -18,3 +18,4 @@ set(SRC
|
||||||
visitor/render_section.cpp)
|
visitor/render_section.cpp)
|
||||||
|
|
||||||
add_library(mstch STATIC ${SRC})
|
add_library(mstch STATIC ${SRC})
|
||||||
|
target_link_libraries(mstch ${Boost_REGEX_LIBRARY})
|
|
@ -1,7 +1,7 @@
|
||||||
#include "render_context.hpp"
|
#include "render_context.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
#include "state/outside_section.hpp"
|
#include "state/outside_section.hpp"
|
||||||
#include <regex>
|
#include <boost/regex.hpp>
|
||||||
|
|
||||||
using namespace mstch;
|
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::string render_context::render(const std::string& t) {
|
||||||
std::ostringstream output;
|
std::ostringstream output;
|
||||||
auto re = std::regex("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
|
auto re = boost::regex("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
|
||||||
std::sregex_token_iterator it(t.begin(), t.end(), re, {-1, 0});
|
boost::sregex_token_iterator it(t.begin(), t.end(), re, {-1, 0});
|
||||||
for (; it != std::sregex_token_iterator(); ++it)
|
for (; it != boost::sregex_token_iterator(); ++it)
|
||||||
output << state.top()->render(*this, token(it->str()));
|
output << state.top()->render(*this, token(it->str()));
|
||||||
return output.str();
|
return output.str();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "token.hpp"
|
#include "token.hpp"
|
||||||
#include <boost/algorithm/string/trim.hpp>
|
#include <boost/algorithm/string/trim.hpp>
|
||||||
#include <regex>
|
#include <boost/regex.hpp>
|
||||||
|
|
||||||
using namespace mstch;
|
using namespace mstch;
|
||||||
|
|
||||||
|
@ -20,8 +20,8 @@ std::tuple<int,int,token::type> token::token_info(const std::string& inside) {
|
||||||
}
|
}
|
||||||
|
|
||||||
token::token(const std::string& raw_token): raw_val(raw_token) {
|
token::token(const std::string& raw_token): raw_val(raw_token) {
|
||||||
std::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
|
boost::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
|
||||||
if(std::regex_match(raw_token, token_match)) {
|
if(boost::regex_match(raw_token, token_match)) {
|
||||||
std::string inside{raw_token.substr(2, raw_token.size() - 4)};
|
std::string inside{raw_token.substr(2, raw_token.size() - 4)};
|
||||||
boost::trim(inside);
|
boost::trim(inside);
|
||||||
if (inside.size() > 0) {
|
if (inside.size() > 0) {
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
#include <regex>
|
#include <boost/regex.hpp>
|
||||||
#include <boost/algorithm/string/replace.hpp>
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
|
|
||||||
std::string mstch::strip_whitespace(const std::string& tmplt) {
|
std::string mstch::strip_whitespace(const std::string& tmplt) {
|
||||||
std::regex comment_match("\\{\\{![^\\}]*\\}\\}");
|
boost::regex comment_match("\\{\\{![^\\}]*\\}\\}");
|
||||||
std::regex tag_match("\\{{2}[ ]*[#|/|^|!|>]{1}[^\\}]*\\}{2}");
|
boost::regex tag_match("\\{{2}[ ]*[#|/|^|!|>]{1}[^\\}]*\\}{2}");
|
||||||
std::regex whitespace_match("^\\s*$");
|
boost::regex whitespace_match("^\\s*$");
|
||||||
std::ostringstream out;
|
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);) {
|
for(std::string line; std::getline(in, line);) {
|
||||||
std::string no_tags = std::regex_replace(line, tag_match, "");
|
std::string no_tags = boost::regex_replace(line, tag_match, "");
|
||||||
if (no_tags != line && std::regex_match(no_tags, whitespace_match))
|
if (no_tags != line && boost::regex_match(no_tags, whitespace_match))
|
||||||
out << std::regex_replace(line, std::regex("\\s"), "");
|
out << boost::regex_replace(line, boost::regex("\\s"), "");
|
||||||
else
|
else
|
||||||
out << line << (in.eof()?"":"\n");
|
out << line << (in.eof()?"":"\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,9 @@ include_directories(
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
${Boost_INCLUDE_DIR})
|
${Boost_INCLUDE_DIR})
|
||||||
|
|
||||||
|
add_executable(benchmark benchmark_main.cpp)
|
||||||
|
target_link_libraries(benchmark mstch)
|
||||||
|
|
||||||
add_executable(filetoheader filetoheader.cpp)
|
add_executable(filetoheader filetoheader.cpp)
|
||||||
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
|
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
|
||||||
set(filetoheader_exe ${CMAKE_CURRENT_BINARY_DIR}/filetoheader${CMAKE_EXECUTABLE_SUFFIX})
|
set(filetoheader_exe ${CMAKE_CURRENT_BINARY_DIR}/filetoheader${CMAKE_EXECUTABLE_SUFFIX})
|
||||||
|
|
24
test/benchmark_main.cpp
Normal file
24
test/benchmark_main.cpp
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#include "mstch/mstch.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::string comment_tmp{
|
||||||
|
"<div class=\"comments\"><h3>{{header}}</h3><ul>"
|
||||||
|
"{{#comments}}<li class=\"comment\"><h5>{{name}}</h5>"
|
||||||
|
"<p>{{body}}</p></li>{{/comments}}</ul></div>"
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue