use boost::regex instead of std::regex

This commit is contained in:
Daniel Sipka 2015-04-13 10:18:58 +02:00
parent 8d1336d7df
commit 6369a38800
6 changed files with 44 additions and 16 deletions

View File

@ -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})

View File

@ -1,7 +1,7 @@
#include "render_context.hpp"
#include "utils.hpp"
#include "state/outside_section.hpp"
#include <regex>
#include <boost/regex.hpp>
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();
}

View File

@ -1,6 +1,6 @@
#include "token.hpp"
#include <boost/algorithm/string/trim.hpp>
#include <regex>
#include <boost/regex.hpp>
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) {
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) {

View File

@ -1,18 +1,18 @@
#include "utils.hpp"
#include <regex>
#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>
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");
}

View File

@ -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})

24
test/benchmark_main.cpp Normal file
View 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;
}