simplify code

This commit is contained in:
Daniel Sipka 2015-04-27 13:59:36 +02:00
parent c6325f5581
commit 4f35bcae25
12 changed files with 35 additions and 65 deletions

View File

@ -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<internal::object_t<boost::recursive_variant_>>,
std::map<const std::string, boost::recursive_variant_>,
std::vector<boost::recursive_variant_>>::type;

View File

@ -2,7 +2,6 @@
#include "mstch/mstch.hpp"
#include "render_context.hpp"
#include "utils.hpp"
using namespace mstch;

View File

@ -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;

View File

@ -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;

View File

@ -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 &section_name);
std::string render(render_context &context, const token &token) override;

View File

@ -3,7 +3,6 @@
#include "visitor/render_node.hpp"
#include "in_section.hpp"
#include "render_context.hpp"
#include "utils.hpp"
using namespace mstch;

View File

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

View File

@ -1,7 +1,6 @@
#pragma once
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "mstch/mstch.hpp"
#include "has_token.hpp"
@ -26,12 +25,12 @@ class get_token: public boost::static_visitor<const mstch::node&> {
};
template<>
inline const mstch::node& get_token::operator()<map>(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()<std::shared_ptr<object>>(
inline const mstch::node& get_token::operator()(
const std::shared_ptr<object>& object) const
{
return object->at(token);

View File

@ -1,7 +1,6 @@
#pragma once
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "mstch/mstch.hpp"
@ -22,14 +21,12 @@ class has_token: public boost::static_visitor<bool> {
};
template<>
inline bool has_token::operator()<map>(const map& map) const {
inline bool has_token::operator()(const map& map) const {
return map.count(token) == 1;
}
template<>
inline bool has_token::operator()<std::shared_ptr<object>>(
const std::shared_ptr<object>& object) const
{
inline bool has_token::operator()(const std::shared_ptr<object>& object) const {
return object->has(token);
}

View File

@ -1,7 +1,6 @@
#pragma once
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "mstch/mstch.hpp"
@ -9,38 +8,34 @@ namespace mstch {
class is_node_empty: public boost::static_visitor<bool> {
public:
template<class T> inline
bool operator()(const T& t) const {
template<class T>
inline bool operator()(const T& t) const {
return false;
}
};
template<>
inline bool is_node_empty::operator()<boost::blank>(
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()<int>(const int& value) const {
inline bool is_node_empty::operator()(const int& value) const {
return value == 0;
}
template<>
inline bool is_node_empty::operator()<bool>(const bool& value) const {
inline bool is_node_empty::operator()(const bool& value) const {
return !value;
}
template<>
inline bool is_node_empty::operator()<std::string>(
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()<array>(const array& array) const {
inline bool is_node_empty::operator()(const array& array) const {
return array.size() == 0;
}

View File

@ -1,7 +1,6 @@
#pragma once
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "mstch/mstch.hpp"
#include "utils.hpp"
@ -10,10 +9,7 @@ namespace mstch {
class render_node: public boost::static_visitor<std::string> {
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<std::string> {
};
template<>
inline std::string render_node::operator()<int>(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()<bool>(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()<lambda>(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()<std::string>(
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;
}

View File

@ -1,7 +1,6 @@
#pragma once
#include <boost/variant/static_visitor.hpp>
#include <boost/blank.hpp>
#include "render_context.hpp"
#include "mstch/mstch.hpp"
@ -32,7 +31,7 @@ class render_section: public boost::static_visitor<std::string> {
};
template<>
inline std::string render_section::operator()<lambda>(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()<lambda>(const lambda& fun) const {
}
template<>
inline std::string render_section::operator()<array>(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);