mirror of
https://github.com/pantor/inja.git
synced 2026-02-17 09:03:58 +00:00
remove string view polyfill
This commit is contained in:
@@ -382,7 +382,7 @@ Inja uses exceptions to handle ill-formed template input. However, exceptions ca
|
||||
|
||||
## Supported compilers
|
||||
|
||||
Inja uses `string_view` from C++17, but includes the [polyfill](https://github.com/martinmoene/string-view-lite) from martinmoene. This way, the minimum version is C++11. Currently, the following compilers are tested:
|
||||
Inja uses the `string_view` feature of the C++17 STL. Currently, the following compilers are tested:
|
||||
|
||||
- GCC 7 - 11 (and possibly later)
|
||||
- Clang 4 - 12 (and possibly later)
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "string_view.hpp"
|
||||
#include "template.hpp"
|
||||
|
||||
namespace inja {
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "function_storage.hpp"
|
||||
#include "parser.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "string_view.hpp"
|
||||
#include "template.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
@@ -93,7 +93,7 @@ public:
|
||||
render_config.throw_at_missing_includes = will_throw;
|
||||
}
|
||||
|
||||
Template parse(nonstd::string_view input) {
|
||||
Template parse(std::string_view input) {
|
||||
Parser parser(parser_config, lexer_config, template_storage, function_storage);
|
||||
return parser.parse(input);
|
||||
}
|
||||
@@ -109,7 +109,7 @@ public:
|
||||
return parse_template(filename);
|
||||
}
|
||||
|
||||
std::string render(nonstd::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::stringstream os;
|
||||
@@ -217,14 +217,14 @@ public:
|
||||
/*!
|
||||
@brief render with default settings to a string
|
||||
*/
|
||||
inline std::string render(nonstd::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, nonstd::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);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||
#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "string_view.hpp"
|
||||
|
||||
namespace inja {
|
||||
|
||||
@@ -110,15 +110,15 @@ private:
|
||||
};
|
||||
|
||||
public:
|
||||
void add_builtin(nonstd::string_view name, int num_args, Operation op) {
|
||||
void add_builtin(std::string_view name, int num_args, Operation op) {
|
||||
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { op });
|
||||
}
|
||||
|
||||
void add_callback(nonstd::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<std::string>(name), num_args), FunctionData { Operation::Callback, callback });
|
||||
}
|
||||
|
||||
FunctionData find_function(nonstd::string_view name, int num_args) const {
|
||||
FunctionData find_function(std::string_view name, int num_args) const {
|
||||
auto it = function_storage.find(std::make_pair(static_cast<std::string>(name), num_args));
|
||||
if (it != function_storage.end()) {
|
||||
return it->second;
|
||||
|
||||
@@ -53,7 +53,6 @@ namespace inja {
|
||||
#include "exceptions.hpp"
|
||||
#include "parser.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "string_view.hpp"
|
||||
#include "template.hpp"
|
||||
|
||||
#endif // INCLUDE_INJA_INJA_HPP_
|
||||
|
||||
@@ -39,12 +39,12 @@ class Lexer {
|
||||
|
||||
State state;
|
||||
MinusState minus_state;
|
||||
nonstd::string_view m_in;
|
||||
std::string_view m_in;
|
||||
size_t tok_start;
|
||||
size_t pos;
|
||||
|
||||
|
||||
Token scan_body(nonstd::string_view close, Token::Kind closeKind, nonstd::string_view close_trim = nonstd::string_view(), bool trim = false) {
|
||||
Token scan_body(std::string_view close, Token::Kind closeKind, std::string_view close_trim = std::string_view(), bool trim = false) {
|
||||
again:
|
||||
// skip whitespace (except for \n as it might be a close)
|
||||
if (tok_start >= m_in.size()) {
|
||||
@@ -254,8 +254,8 @@ class Lexer {
|
||||
}
|
||||
}
|
||||
|
||||
static nonstd::string_view clear_final_line_if_whitespace(nonstd::string_view text) {
|
||||
nonstd::string_view result = text;
|
||||
static std::string_view clear_final_line_if_whitespace(std::string_view text) {
|
||||
std::string_view result = text;
|
||||
while (!result.empty()) {
|
||||
const char ch = result.back();
|
||||
if (ch == ' ' || ch == '\t') {
|
||||
@@ -276,7 +276,7 @@ public:
|
||||
return get_source_location(m_in, tok_start);
|
||||
}
|
||||
|
||||
void start(nonstd::string_view input) {
|
||||
void start(std::string_view input) {
|
||||
m_in = input;
|
||||
tok_start = 0;
|
||||
pos = 0;
|
||||
@@ -302,7 +302,7 @@ public:
|
||||
case State::Text: {
|
||||
// fast-scan to first open character
|
||||
const size_t open_start = m_in.substr(pos).find_first_of(config.open_chars);
|
||||
if (open_start == nonstd::string_view::npos) {
|
||||
if (open_start == std::string_view::npos) {
|
||||
// didn't find open, return remaining text as text token
|
||||
pos = m_in.size();
|
||||
return make_token(Token::Kind::Text);
|
||||
@@ -310,7 +310,7 @@ public:
|
||||
pos += open_start;
|
||||
|
||||
// try to match one of the opening sequences, and get the close
|
||||
nonstd::string_view open_str = m_in.substr(pos);
|
||||
std::string_view open_str = m_in.substr(pos);
|
||||
bool must_lstrip = false;
|
||||
if (inja::string_view::starts_with(open_str, config.expression_open)) {
|
||||
if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) {
|
||||
@@ -344,7 +344,7 @@ public:
|
||||
goto again;
|
||||
}
|
||||
|
||||
nonstd::string_view text = string_view::slice(m_in, tok_start, pos);
|
||||
std::string_view text = string_view::slice(m_in, tok_start, pos);
|
||||
if (must_lstrip) {
|
||||
text = clear_final_line_if_whitespace(text);
|
||||
}
|
||||
@@ -403,7 +403,7 @@ public:
|
||||
case State::CommentBody: {
|
||||
// fast-scan to comment close
|
||||
const size_t end = m_in.substr(pos).find(config.comment_close);
|
||||
if (end == nonstd::string_view::npos) {
|
||||
if (end == std::string_view::npos) {
|
||||
pos = m_in.size();
|
||||
return make_token(Token::Kind::Eof);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#define INCLUDE_INJA_NODE_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
#include "function_storage.hpp"
|
||||
#include "string_view.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
|
||||
@@ -113,10 +113,10 @@ public:
|
||||
const std::string name;
|
||||
const json::json_pointer ptr;
|
||||
|
||||
static std::string convert_dot_to_json_ptr(nonstd::string_view ptr_name) {
|
||||
static std::string convert_dot_to_json_ptr(std::string_view ptr_name) {
|
||||
std::string result;
|
||||
do {
|
||||
nonstd::string_view part;
|
||||
std::string_view part;
|
||||
std::tie(part, ptr_name) = string_view::split(ptr_name, '.');
|
||||
result.push_back('/');
|
||||
result.append(part.begin(), part.end());
|
||||
@@ -124,7 +124,7 @@ public:
|
||||
return result;
|
||||
}
|
||||
|
||||
explicit JsonNode(nonstd::string_view ptr_name, size_t pos) : ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_json_ptr(ptr_name))) { }
|
||||
explicit JsonNode(std::string_view ptr_name, size_t pos) : ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_json_ptr(ptr_name))) { }
|
||||
|
||||
void accept(NodeVisitor& v) const {
|
||||
v.visit(*this);
|
||||
@@ -150,7 +150,7 @@ public:
|
||||
std::vector<std::shared_ptr<ExpressionNode>> arguments;
|
||||
CallbackFunction callback;
|
||||
|
||||
explicit FunctionNode(nonstd::string_view name, size_t pos) : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(1) { }
|
||||
explicit FunctionNode(std::string_view name, size_t pos) : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(1) { }
|
||||
explicit FunctionNode(Op operation, size_t pos) : ExpressionNode(pos), operation(operation), number_args(1) {
|
||||
switch (operation) {
|
||||
case Op::Not: {
|
||||
|
||||
@@ -37,7 +37,7 @@ class Parser {
|
||||
size_t current_bracket_level {0};
|
||||
size_t current_brace_level {0};
|
||||
|
||||
nonstd::string_view json_literal_start;
|
||||
std::string_view json_literal_start;
|
||||
|
||||
BlockNode *current_block {nullptr};
|
||||
ExpressionListNode *current_expression_list {nullptr};
|
||||
@@ -70,7 +70,7 @@ class Parser {
|
||||
}
|
||||
|
||||
inline void add_json_literal(const char* content_ptr) {
|
||||
nonstd::string_view json_text(json_literal_start.data(), tok.text.data() - json_literal_start.data() + tok.text.size());
|
||||
std::string_view json_text(json_literal_start.data(), tok.text.data() - json_literal_start.data() + tok.text.size());
|
||||
arguments.emplace_back(std::make_shared<LiteralNode>(json::parse(json_text), json_text.data() - content_ptr));
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ class Parser {
|
||||
arguments.emplace_back(function);
|
||||
}
|
||||
|
||||
void add_to_template_storage(nonstd::string_view path, std::string& template_name) {
|
||||
void add_to_template_storage(std::string_view path, std::string& template_name) {
|
||||
if (template_storage.find(template_name) != template_storage.end()) {
|
||||
return;
|
||||
}
|
||||
@@ -366,7 +366,7 @@ class Parser {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_statement(Template &tmpl, Token::Kind closing, nonstd::string_view path) {
|
||||
bool parse_statement(Template &tmpl, Token::Kind closing, std::string_view path) {
|
||||
if (tok.kind != Token::Kind::Id) {
|
||||
return false;
|
||||
}
|
||||
@@ -568,7 +568,7 @@ class Parser {
|
||||
return true;
|
||||
}
|
||||
|
||||
void parse_into(Template &tmpl, nonstd::string_view path) {
|
||||
void parse_into(Template &tmpl, std::string_view path) {
|
||||
lexer.start(tmpl.content);
|
||||
current_block = &tmpl.root;
|
||||
|
||||
@@ -638,18 +638,18 @@ public:
|
||||
TemplateStorage &template_storage, const FunctionStorage &function_storage)
|
||||
: config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) { }
|
||||
|
||||
Template parse(nonstd::string_view input, nonstd::string_view path) {
|
||||
Template parse(std::string_view input, std::string_view path) {
|
||||
auto result = Template(static_cast<std::string>(input));
|
||||
parse_into(result, path);
|
||||
return result;
|
||||
}
|
||||
|
||||
Template parse(nonstd::string_view input) {
|
||||
Template parse(std::string_view input) {
|
||||
return parse(input, "./");
|
||||
}
|
||||
|
||||
void parse_into_template(Template& tmpl, nonstd::string_view filename) {
|
||||
nonstd::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1);
|
||||
void parse_into_template(Template& tmpl, std::string_view filename) {
|
||||
std::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1);
|
||||
|
||||
// StringRef path = sys::path::parent_path(filename);
|
||||
auto sub_parser = Parser(config, lexer.get_config(), template_storage, function_storage);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,8 +2,8 @@
|
||||
#define INCLUDE_INJA_TOKEN_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "string_view.hpp"
|
||||
|
||||
namespace inja {
|
||||
|
||||
@@ -50,10 +50,10 @@ struct Token {
|
||||
};
|
||||
|
||||
Kind kind {Kind::Unknown};
|
||||
nonstd::string_view text;
|
||||
std::string_view text;
|
||||
|
||||
explicit constexpr Token() = default;
|
||||
explicit constexpr Token(Kind kind, nonstd::string_view text) : kind(kind), text(text) {}
|
||||
explicit constexpr Token(Kind kind, std::string_view text) : kind(kind), text(text) {}
|
||||
|
||||
std::string describe() const {
|
||||
switch (kind) {
|
||||
|
||||
@@ -4,39 +4,40 @@
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
#include "exceptions.hpp"
|
||||
#include "string_view.hpp"
|
||||
|
||||
|
||||
namespace inja {
|
||||
|
||||
namespace string_view {
|
||||
inline nonstd::string_view slice(nonstd::string_view view, size_t start, size_t end) {
|
||||
inline std::string_view slice(std::string_view view, size_t start, size_t end) {
|
||||
start = std::min(start, view.size());
|
||||
end = std::min(std::max(start, end), view.size());
|
||||
return view.substr(start, end - start);
|
||||
}
|
||||
|
||||
inline std::pair<nonstd::string_view, nonstd::string_view> split(nonstd::string_view view, char Separator) {
|
||||
inline std::pair<std::string_view, std::string_view> split(std::string_view view, char Separator) {
|
||||
size_t idx = view.find(Separator);
|
||||
if (idx == nonstd::string_view::npos) {
|
||||
return std::make_pair(view, nonstd::string_view());
|
||||
if (idx == std::string_view::npos) {
|
||||
return std::make_pair(view, std::string_view());
|
||||
}
|
||||
return std::make_pair(slice(view, 0, idx), slice(view, idx + 1, nonstd::string_view::npos));
|
||||
return std::make_pair(slice(view, 0, idx), slice(view, idx + 1, std::string_view::npos));
|
||||
}
|
||||
|
||||
inline bool starts_with(nonstd::string_view view, nonstd::string_view prefix) {
|
||||
inline bool starts_with(std::string_view view, std::string_view prefix) {
|
||||
return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0);
|
||||
}
|
||||
} // namespace string_view
|
||||
|
||||
inline SourceLocation get_source_location(nonstd::string_view content, size_t pos) {
|
||||
inline SourceLocation get_source_location(std::string_view content, size_t pos) {
|
||||
// Get line and offset position (starts at 1:1)
|
||||
auto sliced = string_view::slice(content, 0, pos);
|
||||
std::size_t last_newline = sliced.rfind("\n");
|
||||
|
||||
if (last_newline == nonstd::string_view::npos) {
|
||||
if (last_newline == std::string_view::npos) {
|
||||
return {1, sliced.length() + 1};
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user