optimizations

This commit is contained in:
Daniel Sipka 2015-04-15 16:13:23 +02:00
parent bb21e64a51
commit 48fbaeb3c9
5 changed files with 49 additions and 37 deletions

View file

@ -1,36 +1,48 @@
#include "token.hpp"
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/predicate.hpp>
using namespace mstch;
std::tuple<int,int,token::type> token::token_info(const std::string& inside) {
switch (inside.at(0)) {
case '>': return std::make_tuple(1, 0, type::partial);
case '^': return std::make_tuple(1, 0, type::inverted_section_open);
case '/': return std::make_tuple(1, 0, type::section_close);
case '&': return std::make_tuple(1, 0, type::unescaped_variable);
case '#': return std::make_tuple(1, 0, type::section_open);
case '!': return std::make_tuple(1, 0, type::comment);
case '{':
if (inside.at(inside.size() - 1) == '}')
return std::make_tuple(1, 1, type::unescaped_variable);
default: return std::make_tuple(0, 0, type::variable);
token::type token::token_info(char c) {
switch (c) {
case '>': return type::partial;
case '^': return type::inverted_section_open;
case '/': return type::section_close;
case '&': return type::unescaped_variable;
case '#': return type::section_open;
case '!': return type::comment;
default: return type::variable;
}
}
token::token(bool is_tag, bool eol, bool ws_only, const std::string& raw_val):
raw_val(raw_val), eol(eol), ws_only(ws_only), marked(false)
eol(eol), ws_only(ws_only), marked(false)
{
if(is_tag) {
std::string inside{raw_val.substr(2, raw_val.size() - 4)};
boost::trim(inside);
if (inside.size() > 0) {
int lpad, rpad;
std::tie(lpad, rpad, type_val) = token_info(inside);
content_val = inside.substr(lpad, inside.size() - lpad - rpad);
boost::trim(content_val);
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;
}
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;