diff --git a/include/inja/environment.hpp b/include/inja/environment.hpp index e102046..9e6f7c4 100644 --- a/include/inja/environment.hpp +++ b/include/inja/environment.hpp @@ -34,13 +34,13 @@ class Environment { public: Environment() : Environment("") {} - explicit Environment(const std::string &global_path) : input_path(global_path), output_path(global_path) {} + explicit Environment(const std::string& global_path) : input_path(global_path), output_path(global_path) {} - Environment(const std::string &input_path, const std::string &output_path) + Environment(const std::string& input_path, const std::string& output_path) : input_path(input_path), output_path(output_path) {} /// Sets the opener and closer for template statements - void set_statement(const std::string &open, const std::string &close) { + void set_statement(const std::string& open, const std::string& close) { lexer_config.statement_open = open; lexer_config.statement_open_no_lstrip = open + "+"; lexer_config.statement_open_force_lstrip = open + "-"; @@ -50,13 +50,13 @@ public: } /// Sets the opener for template line statements - void set_line_statement(const std::string &open) { + void set_line_statement(const std::string& open) { lexer_config.line_statement = open; lexer_config.update_open_chars(); } /// Sets the opener and closer for template expressions - void set_expression(const std::string &open, const std::string &close) { + void set_expression(const std::string& open, const std::string& close) { lexer_config.expression_open = open; lexer_config.expression_open_force_lstrip = open + "-"; lexer_config.expression_close = close; @@ -65,7 +65,7 @@ public: } /// Sets the opener and closer for template comments - void set_comment(const std::string &open, const std::string &close) { + void set_comment(const std::string& open, const std::string& close) { lexer_config.comment_open = open; lexer_config.comment_open_force_lstrip = open + "-"; lexer_config.comment_close = close; @@ -98,68 +98,68 @@ public: return parser.parse(input); } - Template parse_template(const std::string &filename) { + Template parse_template(const std::string& filename) { Parser parser(parser_config, lexer_config, template_storage, function_storage); auto result = Template(parser.load_file(input_path + static_cast(filename))); parser.parse_into_template(result, input_path + static_cast(filename)); return result; } - Template parse_file(const std::string &filename) { + Template parse_file(const std::string& filename) { return parse_template(filename); } - std::string render(std::string_view input, const json &data) { return render(parse(input), data); } + std::string render(std::string_view input, const json& data) { return render(parse(input), data); } - std::string render(const Template &tmpl, const json &data) { + std::string render(const Template& tmpl, const json& data) { std::stringstream os; render_to(os, tmpl, data); return os.str(); } - std::string render_file(const std::string &filename, const json &data) { + std::string render_file(const std::string& filename, const json& data) { return render(parse_template(filename), data); } - std::string render_file_with_json_file(const std::string &filename, const std::string &filename_data) { + std::string render_file_with_json_file(const std::string& filename, const std::string& filename_data) { const json data = load_json(filename_data); return render_file(filename, data); } - void write(const std::string &filename, const json &data, const std::string &filename_out) { + void write(const std::string& filename, const json& data, const std::string& filename_out) { std::ofstream file(output_path + filename_out); file << render_file(filename, data); file.close(); } - void write(const Template &temp, const json &data, const std::string &filename_out) { + void write(const Template& temp, const json& data, const std::string& filename_out) { std::ofstream file(output_path + filename_out); file << render(temp, data); file.close(); } - void write_with_json_file(const std::string &filename, const std::string &filename_data, - const std::string &filename_out) { + void write_with_json_file(const std::string& filename, const std::string& filename_data, + const std::string& filename_out) { const json data = load_json(filename_data); write(filename, data, filename_out); } - void write_with_json_file(const Template &temp, const std::string &filename_data, const std::string &filename_out) { + void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) { const json data = load_json(filename_data); write(temp, data, filename_out); } - std::ostream &render_to(std::ostream &os, const Template &tmpl, const json &data) { + std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) { Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data); return os; } - std::string load_file(const std::string &filename) { + std::string load_file(const std::string& filename) { Parser parser(parser_config, lexer_config, template_storage, function_storage); return parser.load_file(input_path + filename); } - json load_json(const std::string &filename) { + json load_json(const std::string& filename) { std::ifstream file; file.open(input_path + filename); if (file.fail()) { @@ -173,28 +173,28 @@ public: /*! @brief Adds a variadic callback */ - void add_callback(const std::string &name, const CallbackFunction &callback) { + void add_callback(const std::string& name, const CallbackFunction& callback) { add_callback(name, -1, callback); } /*! @brief Adds a variadic void callback */ - void add_void_callback(const std::string &name, const VoidCallbackFunction &callback) { + void add_void_callback(const std::string& name, const VoidCallbackFunction& callback) { add_void_callback(name, -1, callback); } /*! @brief Adds a callback with given number or arguments */ - void add_callback(const std::string &name, int num_args, const CallbackFunction &callback) { + void add_callback(const std::string& name, int num_args, const CallbackFunction& callback) { function_storage.add_callback(name, num_args, callback); } /*! @brief Adds a void callback with given number or arguments */ - void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback) { + void add_void_callback(const std::string& name, int num_args, const VoidCallbackFunction& callback) { function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); }); } @@ -202,7 +202,7 @@ public: * Then, a template can be rendered in another template using the * include "" syntax. */ - void include_template(const std::string &name, const Template &tmpl) { + void include_template(const std::string& name, const Template& tmpl) { template_storage[name] = tmpl; } @@ -217,14 +217,14 @@ public: /*! @brief render with default settings to a string */ -inline std::string render(std::string_view input, const json &data) { +inline std::string render(std::string_view input, const json& data) { return Environment().render(input, data); } /*! @brief render with default settings to the given output stream */ -inline void render_to(std::ostream &os, std::string_view input, const json &data) { +inline void render_to(std::ostream& os, std::string_view input, const json& data) { Environment env; env.render_to(os, env.parse(input), data); } diff --git a/include/inja/function_storage.hpp b/include/inja/function_storage.hpp index 90c3955..1a6641f 100644 --- a/include/inja/function_storage.hpp +++ b/include/inja/function_storage.hpp @@ -7,9 +7,9 @@ namespace inja { -using Arguments = std::vector; -using CallbackFunction = std::function; -using VoidCallbackFunction = std::function; +using Arguments = std::vector; +using CallbackFunction = std::function; +using VoidCallbackFunction = std::function; /*! * \brief Class for builtin functions and user-defined callbacks. @@ -69,7 +69,7 @@ public: }; struct FunctionData { - explicit FunctionData(const Operation &op, const CallbackFunction &cb = CallbackFunction{}) : operation(op), callback(cb) {} + explicit FunctionData(const Operation& op, const CallbackFunction& cb = CallbackFunction{}) : operation(op), callback(cb) {} const Operation operation; const CallbackFunction callback; }; @@ -114,7 +114,7 @@ public: function_storage.emplace(std::make_pair(static_cast(name), num_args), FunctionData { op }); } - void add_callback(std::string_view name, int num_args, const CallbackFunction &callback) { + void add_callback(std::string_view name, int num_args, const CallbackFunction& callback) { function_storage.emplace(std::make_pair(static_cast(name), num_args), FunctionData { Operation::Callback, callback }); } diff --git a/include/inja/lexer.hpp b/include/inja/lexer.hpp index 7bcbf56..5f45da1 100644 --- a/include/inja/lexer.hpp +++ b/include/inja/lexer.hpp @@ -35,7 +35,7 @@ class Lexer { Number, }; - const LexerConfig &config; + const LexerConfig& config; State state; MinusState minus_state; @@ -270,7 +270,7 @@ class Lexer { } public: - explicit Lexer(const LexerConfig &config) : config(config), state(State::Text), minus_state(MinusState::Number) {} + explicit Lexer(const LexerConfig& config) : config(config), state(State::Text), minus_state(MinusState::Number) {} SourceLocation current_position() const { return get_source_location(m_in, tok_start); @@ -424,7 +424,7 @@ public: } } - const LexerConfig &get_config() const { + const LexerConfig& get_config() const { return config; } }; diff --git a/include/inja/node.hpp b/include/inja/node.hpp index da77065..2df0054 100644 --- a/include/inja/node.hpp +++ b/include/inja/node.hpp @@ -101,7 +101,7 @@ class LiteralNode : public ExpressionNode { public: const json value; - explicit LiteralNode(const json& value, size_t pos) : ExpressionNode(pos), value(value) { } + explicit LiteralNode(std::string_view data_text, size_t pos) : ExpressionNode(pos), value(json::parse(data_text)) { } void accept(NodeVisitor& v) const { v.visit(*this); diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index e3c26e7..61e2aa8 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -23,11 +23,11 @@ namespace inja { * \brief Class for parsing an inja Template. */ class Parser { - const ParserConfig &config; + const ParserConfig& config; Lexer lexer; - TemplateStorage &template_storage; - const FunctionStorage &function_storage; + TemplateStorage& template_storage; + const FunctionStorage& function_storage; Token tok, peek_tok; bool have_peek_tok {false}; @@ -48,7 +48,7 @@ class Parser { std::stack for_statement_stack; std::stack block_statement_stack; - inline void throw_parser_error(const std::string &message) const { + inline void throw_parser_error(const std::string& message) const { INJA_THROW(ParserError(message, lexer.current_position())); } @@ -70,7 +70,7 @@ class Parser { inline void add_literal(const char* content_ptr) { std::string_view data_text(literal_start.data(), tok.text.data() - literal_start.data() + tok.text.size()); - arguments.emplace_back(std::make_shared(json::parse(data_text), data_text.data() - content_ptr)); + arguments.emplace_back(std::make_shared(data_text, data_text.data() - content_ptr)); } inline void add_operator() { @@ -638,8 +638,8 @@ class Parser { public: - explicit Parser(const ParserConfig &parser_config, const LexerConfig &lexer_config, - TemplateStorage &template_storage, const FunctionStorage &function_storage) + explicit Parser(const ParserConfig& parser_config, const LexerConfig& lexer_config, + TemplateStorage& template_storage, const FunctionStorage& function_storage) : config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) { } Template parse(std::string_view input, std::string_view path) { diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index 2e369c4..67dc09d 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -325,12 +325,12 @@ class Renderer : public NodeVisitor { make_result(get_arguments<1>(node)[0]->get() % 2 == 0); } break; case Op::Exists: { - auto &&name = get_arguments<1>(node)[0]->get_ref(); + auto &&name = get_arguments<1>(node)[0]->get_ref(); make_result(data_input->contains(json::json_pointer(DataNode::convert_dot_to_ptr(name)))); } break; case Op::ExistsInObject: { const auto args = get_arguments<2>(node); - auto &&name = args[1]->get_ref(); + auto &&name = args[1]->get_ref(); make_result(args[0]->find(name) != args[0]->end()); } break; case Op::First: { @@ -338,10 +338,10 @@ class Renderer : public NodeVisitor { data_eval_stack.push(result); } break; case Op::Float: { - make_result(std::stod(get_arguments<1>(node)[0]->get_ref())); + make_result(std::stod(get_arguments<1>(node)[0]->get_ref())); } break; case Op::Int: { - make_result(std::stoi(get_arguments<1>(node)[0]->get_ref())); + make_result(std::stoi(get_arguments<1>(node)[0]->get_ref())); } break; case Op::Last: { const auto result = &get_arguments<1>(node)[0]->back(); @@ -350,7 +350,7 @@ class Renderer : public NodeVisitor { case Op::Length: { const auto val = get_arguments<1>(node)[0]; if (val->is_string()) { - make_result(val->get_ref().length()); + make_result(val->get_ref().length()); } else { make_result(val->size()); } diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index 715a218..0273fd4 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -94,9 +94,9 @@ namespace inja { namespace inja { -using Arguments = std::vector; -using CallbackFunction = std::function; -using VoidCallbackFunction = std::function; +using Arguments = std::vector; +using CallbackFunction = std::function; +using VoidCallbackFunction = std::function; /*! * \brief Class for builtin functions and user-defined callbacks. @@ -156,7 +156,7 @@ public: }; struct FunctionData { - explicit FunctionData(const Operation &op, const CallbackFunction &cb = CallbackFunction{}) : operation(op), callback(cb) {} + explicit FunctionData(const Operation& op, const CallbackFunction& cb = CallbackFunction{}) : operation(op), callback(cb) {} const Operation operation; const CallbackFunction callback; }; @@ -201,7 +201,7 @@ public: function_storage.emplace(std::make_pair(static_cast(name), num_args), FunctionData { op }); } - void add_callback(std::string_view name, int num_args, const CallbackFunction &callback) { + void add_callback(std::string_view name, int num_args, const CallbackFunction& callback) { function_storage.emplace(std::make_pair(static_cast(name), num_args), FunctionData { Operation::Callback, callback }); } @@ -442,7 +442,7 @@ class LiteralNode : public ExpressionNode { public: const json value; - explicit LiteralNode(const json& value, size_t pos) : ExpressionNode(pos), value(value) { } + explicit LiteralNode(std::string_view data_text, size_t pos) : ExpressionNode(pos), value(json::parse(data_text)) { } void accept(NodeVisitor& v) const { v.visit(*this); @@ -1027,7 +1027,7 @@ class Lexer { Number, }; - const LexerConfig &config; + const LexerConfig& config; State state; MinusState minus_state; @@ -1262,7 +1262,7 @@ class Lexer { } public: - explicit Lexer(const LexerConfig &config) : config(config), state(State::Text), minus_state(MinusState::Number) {} + explicit Lexer(const LexerConfig& config) : config(config), state(State::Text), minus_state(MinusState::Number) {} SourceLocation current_position() const { return get_source_location(m_in, tok_start); @@ -1416,7 +1416,7 @@ public: } } - const LexerConfig &get_config() const { + const LexerConfig& get_config() const { return config; } }; @@ -1441,11 +1441,11 @@ namespace inja { * \brief Class for parsing an inja Template. */ class Parser { - const ParserConfig &config; + const ParserConfig& config; Lexer lexer; - TemplateStorage &template_storage; - const FunctionStorage &function_storage; + TemplateStorage& template_storage; + const FunctionStorage& function_storage; Token tok, peek_tok; bool have_peek_tok {false}; @@ -1466,7 +1466,7 @@ class Parser { std::stack for_statement_stack; std::stack block_statement_stack; - inline void throw_parser_error(const std::string &message) const { + inline void throw_parser_error(const std::string& message) const { INJA_THROW(ParserError(message, lexer.current_position())); } @@ -1488,7 +1488,7 @@ class Parser { inline void add_literal(const char* content_ptr) { std::string_view data_text(literal_start.data(), tok.text.data() - literal_start.data() + tok.text.size()); - arguments.emplace_back(std::make_shared(json::parse(data_text), data_text.data() - content_ptr)); + arguments.emplace_back(std::make_shared(data_text, data_text.data() - content_ptr)); } inline void add_operator() { @@ -2056,8 +2056,8 @@ class Parser { public: - explicit Parser(const ParserConfig &parser_config, const LexerConfig &lexer_config, - TemplateStorage &template_storage, const FunctionStorage &function_storage) + explicit Parser(const ParserConfig& parser_config, const LexerConfig& lexer_config, + TemplateStorage& template_storage, const FunctionStorage& function_storage) : config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) { } Template parse(std::string_view input, std::string_view path) { @@ -2426,12 +2426,12 @@ class Renderer : public NodeVisitor { make_result(get_arguments<1>(node)[0]->get() % 2 == 0); } break; case Op::Exists: { - auto &&name = get_arguments<1>(node)[0]->get_ref(); + auto &&name = get_arguments<1>(node)[0]->get_ref(); make_result(data_input->contains(json::json_pointer(DataNode::convert_dot_to_ptr(name)))); } break; case Op::ExistsInObject: { const auto args = get_arguments<2>(node); - auto &&name = args[1]->get_ref(); + auto &&name = args[1]->get_ref(); make_result(args[0]->find(name) != args[0]->end()); } break; case Op::First: { @@ -2439,10 +2439,10 @@ class Renderer : public NodeVisitor { data_eval_stack.push(result); } break; case Op::Float: { - make_result(std::stod(get_arguments<1>(node)[0]->get_ref())); + make_result(std::stod(get_arguments<1>(node)[0]->get_ref())); } break; case Op::Int: { - make_result(std::stoi(get_arguments<1>(node)[0]->get_ref())); + make_result(std::stoi(get_arguments<1>(node)[0]->get_ref())); } break; case Op::Last: { const auto result = &get_arguments<1>(node)[0]->back(); @@ -2451,7 +2451,7 @@ class Renderer : public NodeVisitor { case Op::Length: { const auto val = get_arguments<1>(node)[0]; if (val->is_string()) { - make_result(val->get_ref().length()); + make_result(val->get_ref().length()); } else { make_result(val->size()); } @@ -2762,13 +2762,13 @@ class Environment { public: Environment() : Environment("") {} - explicit Environment(const std::string &global_path) : input_path(global_path), output_path(global_path) {} + explicit Environment(const std::string& global_path) : input_path(global_path), output_path(global_path) {} - Environment(const std::string &input_path, const std::string &output_path) + Environment(const std::string& input_path, const std::string& output_path) : input_path(input_path), output_path(output_path) {} /// Sets the opener and closer for template statements - void set_statement(const std::string &open, const std::string &close) { + void set_statement(const std::string& open, const std::string& close) { lexer_config.statement_open = open; lexer_config.statement_open_no_lstrip = open + "+"; lexer_config.statement_open_force_lstrip = open + "-"; @@ -2778,13 +2778,13 @@ public: } /// Sets the opener for template line statements - void set_line_statement(const std::string &open) { + void set_line_statement(const std::string& open) { lexer_config.line_statement = open; lexer_config.update_open_chars(); } /// Sets the opener and closer for template expressions - void set_expression(const std::string &open, const std::string &close) { + void set_expression(const std::string& open, const std::string& close) { lexer_config.expression_open = open; lexer_config.expression_open_force_lstrip = open + "-"; lexer_config.expression_close = close; @@ -2793,7 +2793,7 @@ public: } /// Sets the opener and closer for template comments - void set_comment(const std::string &open, const std::string &close) { + void set_comment(const std::string& open, const std::string& close) { lexer_config.comment_open = open; lexer_config.comment_open_force_lstrip = open + "-"; lexer_config.comment_close = close; @@ -2826,68 +2826,68 @@ public: return parser.parse(input); } - Template parse_template(const std::string &filename) { + Template parse_template(const std::string& filename) { Parser parser(parser_config, lexer_config, template_storage, function_storage); auto result = Template(parser.load_file(input_path + static_cast(filename))); parser.parse_into_template(result, input_path + static_cast(filename)); return result; } - Template parse_file(const std::string &filename) { + Template parse_file(const std::string& filename) { return parse_template(filename); } - std::string render(std::string_view input, const json &data) { return render(parse(input), data); } + std::string render(std::string_view input, const json& data) { return render(parse(input), data); } - std::string render(const Template &tmpl, const json &data) { + std::string render(const Template& tmpl, const json& data) { std::stringstream os; render_to(os, tmpl, data); return os.str(); } - std::string render_file(const std::string &filename, const json &data) { + std::string render_file(const std::string& filename, const json& data) { return render(parse_template(filename), data); } - std::string render_file_with_json_file(const std::string &filename, const std::string &filename_data) { + std::string render_file_with_json_file(const std::string& filename, const std::string& filename_data) { const json data = load_json(filename_data); return render_file(filename, data); } - void write(const std::string &filename, const json &data, const std::string &filename_out) { + void write(const std::string& filename, const json& data, const std::string& filename_out) { std::ofstream file(output_path + filename_out); file << render_file(filename, data); file.close(); } - void write(const Template &temp, const json &data, const std::string &filename_out) { + void write(const Template& temp, const json& data, const std::string& filename_out) { std::ofstream file(output_path + filename_out); file << render(temp, data); file.close(); } - void write_with_json_file(const std::string &filename, const std::string &filename_data, - const std::string &filename_out) { + void write_with_json_file(const std::string& filename, const std::string& filename_data, + const std::string& filename_out) { const json data = load_json(filename_data); write(filename, data, filename_out); } - void write_with_json_file(const Template &temp, const std::string &filename_data, const std::string &filename_out) { + void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) { const json data = load_json(filename_data); write(temp, data, filename_out); } - std::ostream &render_to(std::ostream &os, const Template &tmpl, const json &data) { + std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) { Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data); return os; } - std::string load_file(const std::string &filename) { + std::string load_file(const std::string& filename) { Parser parser(parser_config, lexer_config, template_storage, function_storage); return parser.load_file(input_path + filename); } - json load_json(const std::string &filename) { + json load_json(const std::string& filename) { std::ifstream file; file.open(input_path + filename); if (file.fail()) { @@ -2901,28 +2901,28 @@ public: /*! @brief Adds a variadic callback */ - void add_callback(const std::string &name, const CallbackFunction &callback) { + void add_callback(const std::string& name, const CallbackFunction& callback) { add_callback(name, -1, callback); } /*! @brief Adds a variadic void callback */ - void add_void_callback(const std::string &name, const VoidCallbackFunction &callback) { + void add_void_callback(const std::string& name, const VoidCallbackFunction& callback) { add_void_callback(name, -1, callback); } /*! @brief Adds a callback with given number or arguments */ - void add_callback(const std::string &name, int num_args, const CallbackFunction &callback) { + void add_callback(const std::string& name, int num_args, const CallbackFunction& callback) { function_storage.add_callback(name, num_args, callback); } /*! @brief Adds a void callback with given number or arguments */ - void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback) { + void add_void_callback(const std::string& name, int num_args, const VoidCallbackFunction& callback) { function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); }); } @@ -2930,7 +2930,7 @@ public: * Then, a template can be rendered in another template using the * include "" syntax. */ - void include_template(const std::string &name, const Template &tmpl) { + void include_template(const std::string& name, const Template& tmpl) { template_storage[name] = tmpl; } @@ -2945,14 +2945,14 @@ public: /*! @brief render with default settings to a string */ -inline std::string render(std::string_view input, const json &data) { +inline std::string render(std::string_view input, const json& data) { return Environment().render(input, data); } /*! @brief render with default settings to the given output stream */ -inline void render_to(std::ostream &os, std::string_view input, const json &data) { +inline void render_to(std::ostream& os, std::string_view input, const json& data) { Environment env; env.render_to(os, env.parse(input), data); }