From 4f35bcae256fc665158afdae34c213740ec91415 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Mon, 27 Apr 2015 13:59:36 +0200 Subject: [PATCH] simplify code --- include/mstch/mstch.hpp | 2 +- src/mstch.cpp | 1 - src/render_context.cpp | 2 -- src/state/in_section.cpp | 1 - src/state/in_section.hpp | 4 +--- src/state/outside_section.cpp | 1 - src/template_type.cpp | 35 ++++++++++++++-------------------- src/visitor/get_token.hpp | 5 ++--- src/visitor/has_token.hpp | 7 ++----- src/visitor/is_node_empty.hpp | 19 +++++++----------- src/visitor/render_node.hpp | 16 +++++----------- src/visitor/render_section.hpp | 7 +++---- 12 files changed, 35 insertions(+), 65 deletions(-) diff --git a/include/mstch/mstch.hpp b/include/mstch/mstch.hpp index f52c21e..c35b5ac 100644 --- a/include/mstch/mstch.hpp +++ b/include/mstch/mstch.hpp @@ -84,7 +84,7 @@ class lambda { }; using node = boost::make_recursive_variant< - boost::blank, std::string, int, bool, lambda, + std::nullptr_t, std::string, int, bool, lambda, std::shared_ptr>, std::map, std::vector>::type; diff --git a/src/mstch.cpp b/src/mstch.cpp index 0b0ec7a..7c5c2ab 100644 --- a/src/mstch.cpp +++ b/src/mstch.cpp @@ -2,7 +2,6 @@ #include "mstch/mstch.hpp" #include "render_context.hpp" -#include "utils.hpp" using namespace mstch; diff --git a/src/render_context.cpp b/src/render_context.cpp index c71bd02..80380a1 100644 --- a/src/render_context.cpp +++ b/src/render_context.cpp @@ -1,8 +1,6 @@ #include "render_context.hpp" -#include "utils.hpp" #include "state/outside_section.hpp" #include "visitor/get_token.hpp" -#include "visitor/has_token.hpp" using namespace mstch; diff --git a/src/state/in_section.cpp b/src/state/in_section.cpp index 2a226a0..7bb6489 100644 --- a/src/state/in_section.cpp +++ b/src/state/in_section.cpp @@ -2,7 +2,6 @@ #include "outside_section.hpp" #include "visitor/is_node_empty.hpp" #include "visitor/render_section.hpp" -#include "utils.hpp" using namespace mstch; diff --git a/src/state/in_section.hpp b/src/state/in_section.hpp index e3a1918..5eaf3c9 100644 --- a/src/state/in_section.hpp +++ b/src/state/in_section.hpp @@ -10,9 +10,7 @@ namespace mstch { class in_section: public render_state { public: - enum class type { - inverted, normal - }; + enum class type { inverted, normal }; in_section(type type, const std::string §ion_name); std::string render(render_context &context, const token &token) override; diff --git a/src/state/outside_section.cpp b/src/state/outside_section.cpp index 7b22299..8c8ed93 100644 --- a/src/state/outside_section.cpp +++ b/src/state/outside_section.cpp @@ -3,7 +3,6 @@ #include "visitor/render_node.hpp" #include "in_section.hpp" #include "render_context.hpp" -#include "utils.hpp" using namespace mstch; diff --git a/src/template_type.cpp b/src/template_type.cpp index fabc4f0..1aa2af5 100644 --- a/src/template_type.cpp +++ b/src/template_type.cpp @@ -19,21 +19,20 @@ void template_type::process_text(citer begin, citer end) { } void template_type::tokenize(const std::string& tmplt) { - std::string open{"{{"}, close{"}}"}; + std::string o{"{{"}, c{"}}"}; citer beg = tmplt.begin(); for (unsigned long pos = 0; pos < tmplt.size();) { - auto to = tmplt.find(open, pos); - auto tc = tmplt.find(close, (to == std::string::npos)?to:(to + 1)); + auto to = tmplt.find(o, pos); + auto tc = tmplt.find(c, (to == std::string::npos)?to:(to + 1)); if (tc != std::string::npos && to != std::string::npos) { - if (*(beg + to + open.size()) == '{' && *(beg + tc + close.size()) == '}') + if (*(beg + to + o.size()) == '{' && *(beg + tc + c.size()) == '}') ++tc; process_text(beg + pos, beg + to); - pos = tc + close.size(); - tokens.push_back({{beg + to, beg + tc + close.size()}, - open.size(), close.size()}); - if (*(beg + to + open.size()) == '=' && *(beg + tc - 1) == '=') { - open = {beg + to + open.size() + 1, beg + tmplt.find(' ', to)}; - close = {beg + tmplt.find(' ', to) + 1, beg + tc - 1}; + pos = tc + c.size(); + tokens.push_back({{beg + to, beg + tc + c.size()}, o.size(), c.size()}); + if (*(beg + to + o.size()) == '=' && *(beg + tc - 1) == '=') { + o = {beg + to + o.size() + 1, beg + tmplt.find(' ', to)}; + c = {beg + tmplt.find(' ', to) + 1, beg + tc - 1}; } } else { process_text(beg + pos, tmplt.end()); @@ -43,7 +42,7 @@ void template_type::tokenize(const std::string& tmplt) { } void template_type::strip_whitespace() { - auto line_begin = tokens.begin(); + auto lbegin = tokens.begin(); bool has_tag = false, non_space = false; for (auto it = tokens.begin(); it != tokens.end(); ++it) { auto type = (*it).token_type(); @@ -53,17 +52,11 @@ void template_type::strip_whitespace() { else if (!(*it).ws_only()) non_space = true; if ((*it).eol()) { - if (has_tag && !non_space) { - auto line_it = line_begin; - for (; !(*line_it).eol(); ++line_it) - if ((*line_it).ws_only()) - line_it = tokens.erase(line_it); - if ((*line_it).ws_only()) - line_it = tokens.erase(line_it); - it = line_it - 1; - } + if (has_tag && !non_space) + for (auto c = lbegin; it != c-1; c = (*c).ws_only()?tokens.erase(c):++c) + it = (*c).eol()?c-1:it; non_space = has_tag = false; - line_begin = it + 1; + lbegin = it + 1; } } } diff --git a/src/visitor/get_token.hpp b/src/visitor/get_token.hpp index 7f16444..37d0c30 100644 --- a/src/visitor/get_token.hpp +++ b/src/visitor/get_token.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "mstch/mstch.hpp" #include "has_token.hpp" @@ -26,12 +25,12 @@ class get_token: public boost::static_visitor { }; template<> -inline const mstch::node& get_token::operator()(const map& map) const { +inline const mstch::node& get_token::operator()(const map& map) const { return map.at(token); } template<> -inline const mstch::node& get_token::operator()>( +inline const mstch::node& get_token::operator()( const std::shared_ptr& object) const { return object->at(token); diff --git a/src/visitor/has_token.hpp b/src/visitor/has_token.hpp index e01f91b..9702ce4 100644 --- a/src/visitor/has_token.hpp +++ b/src/visitor/has_token.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "mstch/mstch.hpp" @@ -22,14 +21,12 @@ class has_token: public boost::static_visitor { }; template<> -inline bool has_token::operator()(const map& map) const { +inline bool has_token::operator()(const map& map) const { return map.count(token) == 1; } template<> -inline bool has_token::operator()>( - const std::shared_ptr& object) const -{ +inline bool has_token::operator()(const std::shared_ptr& object) const { return object->has(token); } diff --git a/src/visitor/is_node_empty.hpp b/src/visitor/is_node_empty.hpp index 3214417..25b3c69 100644 --- a/src/visitor/is_node_empty.hpp +++ b/src/visitor/is_node_empty.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "mstch/mstch.hpp" @@ -9,38 +8,34 @@ namespace mstch { class is_node_empty: public boost::static_visitor { public: - template inline - bool operator()(const T& t) const { + template + inline bool operator()(const T& t) const { return false; } }; template<> -inline bool is_node_empty::operator()( - const boost::blank& blank) const -{ +inline bool is_node_empty::operator()(const std::nullptr_t& nul) const { return true; } template<> -inline bool is_node_empty::operator()(const int& value) const { +inline bool is_node_empty::operator()(const int& value) const { return value == 0; } template<> -inline bool is_node_empty::operator()(const bool& value) const { +inline bool is_node_empty::operator()(const bool& value) const { return !value; } template<> -inline bool is_node_empty::operator()( - const std::string& value) const -{ +inline bool is_node_empty::operator()(const std::string& value) const { return value == ""; } template<> -inline bool is_node_empty::operator()(const array& array) const { +inline bool is_node_empty::operator()(const array& array) const { return array.size() == 0; } diff --git a/src/visitor/render_node.hpp b/src/visitor/render_node.hpp index bddfd3c..3d69da5 100644 --- a/src/visitor/render_node.hpp +++ b/src/visitor/render_node.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "mstch/mstch.hpp" #include "utils.hpp" @@ -10,10 +9,7 @@ namespace mstch { class render_node: public boost::static_visitor { public: - enum class flag { - none, escape_html - }; - + enum class flag { none, escape_html }; render_node(flag p_flag = flag::none): m_flag(p_flag) { } @@ -27,24 +23,22 @@ class render_node: public boost::static_visitor { }; template<> -inline std::string render_node::operator()(const int& value) const { +inline std::string render_node::operator()(const int& value) const { return std::to_string(value); } template<> -inline std::string render_node::operator()(const bool& value) const { +inline std::string render_node::operator()(const bool& value) const { return value?"true":"false"; } template<> -inline std::string render_node::operator()(const lambda& value) const { +inline std::string render_node::operator()(const lambda& value) const { return (m_flag == flag::escape_html)?html_escape(value()):value(); } template<> -inline std::string render_node::operator()( - const std::string& value) const -{ +inline std::string render_node::operator()(const std::string& value) const { return (m_flag == flag::escape_html)?html_escape(value):value; } diff --git a/src/visitor/render_section.hpp b/src/visitor/render_section.hpp index c9d84bf..fd298bd 100644 --- a/src/visitor/render_section.hpp +++ b/src/visitor/render_section.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include "render_context.hpp" #include "mstch/mstch.hpp" @@ -32,7 +31,7 @@ class render_section: public boost::static_visitor { }; template<> -inline std::string render_section::operator()(const lambda& fun) const { +inline std::string render_section::operator()(const lambda& fun) const { std::string section_str; for(auto& token: section) section_str += token.raw(); @@ -42,10 +41,10 @@ inline std::string render_section::operator()(const lambda& fun) const { } template<> -inline std::string render_section::operator()(const array& array) const { +inline std::string render_section::operator()(const array& array) const { std::string out; if(m_flag == flag::keep_array) - out += render_context::push(ctx, array).render(section); + return render_context::push(ctx, array).render(section); else for (auto& item: array) out += visit(render_section(ctx, section, flag::keep_array), item);