From d43d497f88b44bcb9fbca6dc59935dc9e9d0424f Mon Sep 17 00:00:00 2001 From: pantor Date: Sat, 27 Jun 2020 22:06:13 +0200 Subject: [PATCH] fix stringview in included templates --- include/inja/environment.hpp | 4 +++- include/inja/parser.hpp | 15 ++++++--------- include/inja/renderer.hpp | 2 +- include/inja/template.hpp | 3 +++ single_include/inja/inja.hpp | 24 +++++++++++++----------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/inja/environment.hpp b/include/inja/environment.hpp index aa6058d..fb9996c 100644 --- a/include/inja/environment.hpp +++ b/include/inja/environment.hpp @@ -103,7 +103,9 @@ public: Template parse_template(const std::string &filename) { Parser parser(parser_config, lexer_config, template_storage); - return parser.parse_template(input_path + static_cast(filename)); + auto result = Template(parser.load_file(input_path + static_cast(filename))); + parser.parse_into_template(result, input_path + static_cast(filename)); + return result; } std::string render(nonstd::string_view input, const json &data) { return render(parse(input), data); } diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index 5da97d8..975d7b2 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -476,8 +476,9 @@ public: // sys::path::remove_dots(pathname, true, sys::path::Style::posix); if (config.search_included_templates_in_files && template_storage.find(pathname) == template_storage.end()) { - Template include_template = parse_template(pathname); + auto include_template = Template(load_file(pathname)); template_storage.emplace(pathname, include_template); + parse_into_template(template_storage.at(pathname), pathname); } // generate a reference node @@ -579,8 +580,7 @@ public: } Template parse(nonstd::string_view input, nonstd::string_view path) { - Template result; - result.content = static_cast(input); + auto result = Template(static_cast(input)); parse_into(result, path); return result; } @@ -589,15 +589,12 @@ public: return parse(input, "./"); } - Template parse_template(nonstd::string_view filename) { - Template result; - result.content = load_file(filename); - + void parse_into_template(Template& tmpl, nonstd::string_view filename) { nonstd::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1); // StringRef path = sys::path::parent_path(filename); - Parser(config, lexer.get_config(), template_storage).parse_into(result, path); - return result; + auto sub_parser = Parser(config, lexer.get_config(), template_storage); + sub_parser.parse_into(tmpl, path); } std::string load_file(nonstd::string_view filename) { diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index b4a62c8..44519f2 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -80,7 +80,7 @@ class Renderer { break; case Node::Flag::ValueLookupPointer: ptr_buffer += '/'; - ptr_buffer += node.str; // static_cast(node.view); + ptr_buffer += node.str; ptr = ptr_buffer; break; } diff --git a/include/inja/template.hpp b/include/inja/template.hpp index 8c6912a..7e807ac 100644 --- a/include/inja/template.hpp +++ b/include/inja/template.hpp @@ -18,6 +18,9 @@ struct Template { std::vector nodes; std::string content; + explicit Template() { } + explicit Template(const std::string& content): content(content) { } + /// Return number of variables (total number, not distinct ones) in the template int count_variables() { return std::count_if(nodes.cbegin(), nodes.cend(), [](const inja::Node &node) { diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index 65eda83..b9e7da0 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -2289,6 +2289,9 @@ struct Template { std::vector nodes; std::string content; + explicit Template() { } + explicit Template(const std::string& content): content(content) { } + /// Return number of variables (total number, not distinct ones) in the template int count_variables() { return std::count_if(nodes.cbegin(), nodes.cend(), [](const inja::Node &node) { @@ -2769,8 +2772,9 @@ public: // sys::path::remove_dots(pathname, true, sys::path::Style::posix); if (config.search_included_templates_in_files && template_storage.find(pathname) == template_storage.end()) { - Template include_template = parse_template(pathname); + auto include_template = Template(load_file(pathname)); template_storage.emplace(pathname, include_template); + parse_into_template(template_storage.at(pathname), pathname); } // generate a reference node @@ -2872,8 +2876,7 @@ public: } Template parse(nonstd::string_view input, nonstd::string_view path) { - Template result; - result.content = static_cast(input); + auto result = Template(static_cast(input)); parse_into(result, path); return result; } @@ -2882,15 +2885,12 @@ public: return parse(input, "./"); } - Template parse_template(nonstd::string_view filename) { - Template result; - result.content = load_file(filename); - + void parse_into_template(Template& tmpl, nonstd::string_view filename) { nonstd::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1); // StringRef path = sys::path::parent_path(filename); - Parser(config, lexer.get_config(), template_storage).parse_into(result, path); - return result; + auto sub_parser = Parser(config, lexer.get_config(), template_storage); + sub_parser.parse_into(tmpl, path); } std::string load_file(nonstd::string_view filename) { @@ -2992,7 +2992,7 @@ class Renderer { break; case Node::Flag::ValueLookupPointer: ptr_buffer += '/'; - ptr_buffer += node.str; // static_cast(node.view); + ptr_buffer += node.str; ptr = ptr_buffer; break; } @@ -3614,7 +3614,9 @@ public: Template parse_template(const std::string &filename) { Parser parser(parser_config, lexer_config, template_storage); - return parser.parse_template(input_path + static_cast(filename)); + auto result = Template(parser.load_file(input_path + static_cast(filename))); + parser.parse_into_template(result, input_path + static_cast(filename)); + return result; } std::string render(nonstd::string_view input, const json &data) { return render(parse(input), data); }