From 127609f392f071609bf22dce31546e42edb09ca5 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Wed, 15 Apr 2015 22:32:27 +0200 Subject: [PATCH] optimizations --- src/render_context.cpp | 16 ++++++++-------- src/token.cpp | 43 +++++++++++++++--------------------------- src/token.hpp | 25 ++++++++++++------------ src/utils.cpp | 12 ++++++++++++ src/utils.hpp | 4 ++++ 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/render_context.cpp b/src/render_context.cpp index 6036ece..e272eeb 100644 --- a/src/render_context.cpp +++ b/src/render_context.cpp @@ -31,7 +31,8 @@ render_context::render_context( delim_start{"{{"}, delim_end{"}}"} { - state.push(std::unique_ptr(new state::outside_section)); + state.push(std::unique_ptr( + new state::outside_section)); } const mstch::node& render_context::find_node( @@ -79,7 +80,7 @@ void render_context::tokenize(const std::string& t, std::vector& toks) { else pstate = parse_state::start; } else if(pstate == parse_state::in_del) { - if (*it== '{') { + if (*it == '{') { pstate = parse_state::in_esccontent; } else if (*it == delim_end[0]) { pstate = parse_state::in_del_end; @@ -97,7 +98,7 @@ void render_context::tokenize(const std::string& t, std::vector& toks) { pstate = parse_state::start; toks.push_back({false, false, ws_only, {tok_start, tok_end}}); toks.push_back({true, false, false, - {tok_end +delim_start.size(), it - delim_end.size() +1}}); + {tok_end+delim_start.size(), it-delim_end.size()+1}}); ws_only = true; tok_start = it + 1; } else { @@ -116,19 +117,18 @@ void render_context::strip_whitespace(std::vector& tokens) { if (type != token::type::text && type != token::type::variable && type != token::type::unescaped_variable) has_tag = true; - else if (!(*it).is_ws_only()) + else if (!(*it).ws_only()) non_space = true; - if ((*it).is_eol()) { + if ((*it).eol()) { if (has_tag && !non_space) for (auto line_it = line_begin; line_it != it + 1; ++line_it) - if ((*line_it).is_ws_only()) - (*line_it).mark(); + if ((*line_it).ws_only()) (*line_it).mark(); non_space = has_tag = false; line_begin = it + 1; } } for (auto it = tokens.begin(); it != tokens.end();) - ((*it).is_marked())?(it = tokens.erase(it)):(++it); + ((*it).marked())?(it = tokens.erase(it)):(++it); } std::string render_context::render(const std::string& tmplt) { diff --git a/src/token.cpp b/src/token.cpp index 409369d..2374aed 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -1,4 +1,5 @@ #include "token.hpp" +#include "utils.hpp" using namespace mstch; @@ -14,37 +15,23 @@ token::type token::token_info(char c) { } } -token::token(bool is_tag, bool eol, bool ws_only, const std::string& raw_val): - eol(eol), ws_only(ws_only), marked(false) +token::token(bool is_tag, bool eol, bool ws_only, const std::string& str): + m_eol(eol), m_ws_only(ws_only), m_marked(false) { if(is_tag) { - auto content_begin = raw_val.begin(), content_end = raw_val.end(); - parse_state state = parse_state::prews; - if(*content_begin == '{' && *(content_end - 1) == '}') { - state = parse_state::postws; - type_val = type::unescaped_variable; - ++content_begin; - --content_end; + if(str[0] == '{' && str[str.size() - 1] == '}') { + m_type = type::unescaped_variable; + m_content = {first_not_ws(str.begin() + 1, str.end()), + first_not_ws(str.rbegin() + 1, str.rend()) + 1}; + } else { + auto first = first_not_ws(str.begin(), str.end()); + m_type = token_info(*first); + if(m_type != type::variable) + first = first_not_ws(first + 1, str.end()); + m_content = {first, first_not_ws(str.rbegin(), str.rend()) + 1}; } - for(auto it = content_begin; it != content_end;) { - if(state == parse_state::prews && *it != ' ') { - state = parse_state::postws; - if((type_val = token_info(*it++)) == type::variable) { - state = parse_state::content; - content_begin = it -1; - } - } else if(state == parse_state::postws && *it != ' ') { - content_begin = it++; - state = parse_state::content; - } else if(state == parse_state::content && *it == ' ') { - content_end = it; - } else { - ++it; - } - } - content_val = {content_begin, content_end}; } else { - type_val = type::text; - content_val = raw_val; + m_type = type::text; + m_content = str; } } diff --git a/src/token.hpp b/src/token.hpp index 2e5bdd1..4a4f572 100644 --- a/src/token.hpp +++ b/src/token.hpp @@ -10,20 +10,19 @@ namespace mstch { text, variable, section_open, section_close, inverted_section_open, unescaped_variable, comment, partial }; - token(bool is_tag, bool eol, bool ws_only, const std::string& raw_val); - type token_type() const { return type_val; }; - const std::string& content() const { return content_val; }; - bool is_eol() const { return eol; } - bool is_ws_only() const { return ws_only; } - bool is_marked() const { return marked; } - void mark() { marked = true; }; + token(bool is_tag, bool eol, bool ws_only, const std::string& str); + type token_type() const { return m_type; }; + const std::string& content() const { return m_content; }; + bool eol() const { return m_eol; } + bool ws_only() const { return m_ws_only; } + bool marked() const { return m_marked; } + void mark() { m_marked = true; }; private: - enum class parse_state { prews, postws, content }; - type type_val; - std::string content_val; - bool eol; - bool ws_only; - bool marked; + type m_type; + std::string m_content; + bool m_eol; + bool m_ws_only; + bool m_marked; type token_info(char c); }; } diff --git a/src/utils.cpp b/src/utils.cpp index 558c2c7..b8a8895 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,6 +2,18 @@ #include +mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) { + for(auto it = begin; it != end; ++it) + if(*it != ' ') return it; + return end; +} + +mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) { + for(auto rit = begin; rit != end; ++rit) + if(*rit != ' ') return --(rit.base()); + return --(end.base()); +} + std::string mstch::html_escape(std::string str) { boost::replace_all(str, "&", "&"); boost::replace_all(str, "'", "'"); diff --git a/src/utils.hpp b/src/utils.hpp index ff7cdaa..21fd7eb 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -4,6 +4,10 @@ #include namespace mstch { + using citer = std::string::const_iterator; + using criter = std::string::const_reverse_iterator; + citer first_not_ws(citer begin, citer end); + citer first_not_ws(criter begin, criter end); std::string html_escape(std::string str); }