Skip to content

Commit 02b8355

Browse files
author
Lars Berscheid
committed
update single include file
1 parent a9d4ca9 commit 02b8355

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

single_include/inja/inja.hpp

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ struct Bytecode {
197197
uint32_t flags: 2;
198198

199199
json value;
200-
std::string_view str;
200+
std::string str;
201201

202202
Bytecode(): args(0), flags(0) {}
203203
explicit Bytecode(Op op, unsigned int args = 0): op(op), args(args), flags(0) {}
@@ -601,21 +601,28 @@ class Lexer {
601601

602602
Token scan_id() {
603603
for (;;) {
604-
if (m_pos >= m_in.size()) break;
604+
if (m_pos >= m_in.size()) {
605+
break;
606+
}
605607
char ch = m_in[m_pos];
606-
if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') break;
608+
if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') {
609+
break;
610+
}
607611
m_pos += 1;
608612
}
609613
return make_token(Token::Kind::Id);
610614
}
611615

612616
Token scan_number() {
613617
for (;;) {
614-
if (m_pos >= m_in.size()) break;
618+
if (m_pos >= m_in.size()) {
619+
break;
620+
}
615621
char ch = m_in[m_pos];
616622
// be very permissive in lexer (we'll catch errors when conversion happens)
617-
if (!std::isdigit(ch) && ch != '.' && ch != 'e' && ch != 'E' && ch != '+' && ch != '-')
623+
if (!std::isdigit(ch) && ch != '.' && ch != 'e' && ch != 'E' && ch != '+' && ch != '-') {
618624
break;
625+
}
619626
m_pos += 1;
620627
}
621628
return make_token(Token::Kind::Number);
@@ -626,12 +633,13 @@ class Lexer {
626633
for (;;) {
627634
if (m_pos >= m_in.size()) break;
628635
char ch = m_in[m_pos++];
629-
if (ch == '\\')
636+
if (ch == '\\') {
630637
escape = true;
631-
else if (!escape && ch == m_in[m_tok_start])
638+
} else if (!escape && ch == m_in[m_tok_start]) {
632639
break;
633-
else
640+
} else {
634641
escape = false;
642+
}
635643
}
636644
return make_token(Token::Kind::String);
637645
}
@@ -658,29 +666,9 @@ class Lexer {
658666

659667
namespace inja {
660668

661-
class Template {
662-
friend class Parser;
663-
friend class Renderer;
664-
669+
struct Template {
665670
std::vector<Bytecode> bytecodes;
666671
std::string content;
667-
668-
public:
669-
Template() {}
670-
Template(const Template& oth): bytecodes(oth.bytecodes), content(oth.content) {}
671-
Template(Template&& oth): bytecodes(std::move(oth.bytecodes)), content(std::move(oth.content)) {}
672-
673-
Template& operator=(const Template& oth) {
674-
bytecodes = oth.bytecodes;
675-
content = oth.content;
676-
return *this;
677-
}
678-
679-
Template& operator=(Template&& oth) {
680-
bytecodes = std::move(oth.bytecodes);
681-
content = std::move(oth.content);
682-
return *this;
683-
}
684672
};
685673

686674
using TemplateStorage = std::map<std::string, Template>;
@@ -1064,12 +1052,6 @@ class Parser {
10641052
}
10651053
// sys::path::remove_dots(pathname, true, sys::path::Style::posix);
10661054

1067-
// parse it only if it's new
1068-
// TemplateStorage::iterator included;
1069-
// bool is_new {true};
1070-
// std::tie(included, is_new) = m_included_templates.emplace(pathname);
1071-
// if (is_new) included->second = parse_template(pathname);
1072-
10731055
Template include_template = parse_template(pathname);
10741056
m_included_templates.emplace(pathname, include_template);
10751057

@@ -1322,18 +1304,20 @@ class Renderer {
13221304
std::vector<const json*>& get_args(const Bytecode& bc) {
13231305
m_tmp_args.clear();
13241306

1325-
bool hasImm = ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop);
1307+
bool has_imm = ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop);
13261308

13271309
// get args from stack
13281310
unsigned int pop_args = bc.args;
1329-
if (hasImm) --pop_args;
1311+
if (has_imm) {
1312+
pop_args -= 1;
1313+
}
13301314

13311315
for (auto i = std::prev(m_stack.end(), pop_args); i != m_stack.end(); i++) {
13321316
m_tmp_args.push_back(&(*i));
13331317
}
13341318

13351319
// get immediate arg
1336-
if (hasImm) {
1320+
if (has_imm) {
13371321
m_tmp_args.push_back(get_imm(bc));
13381322
}
13391323

@@ -1342,9 +1326,12 @@ class Renderer {
13421326

13431327
void pop_args(const Bytecode& bc) {
13441328
unsigned int popArgs = bc.args;
1345-
if ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop)
1346-
--popArgs;
1347-
for (unsigned int i = 0; i < popArgs; ++i) m_stack.pop_back();
1329+
if ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop) {
1330+
popArgs -= 1;
1331+
}
1332+
for (unsigned int i = 0; i < popArgs; ++i) {
1333+
m_stack.pop_back();
1334+
}
13481335
}
13491336

13501337
const json* get_imm(const Bytecode& bc) {
@@ -1463,11 +1450,13 @@ class Renderer {
14631450
const auto& bc = tmpl.bytecodes[i];
14641451

14651452
switch (bc.op) {
1466-
case Bytecode::Op::Nop:
1453+
case Bytecode::Op::Nop: {
14671454
break;
1468-
case Bytecode::Op::PrintText:
1455+
}
1456+
case Bytecode::Op::PrintText: {
14691457
os << bc.str;
14701458
break;
1459+
}
14711460
case Bytecode::Op::PrintValue: {
14721461
const json& val = *get_args(bc)[0];
14731462
if (val.is_string())
@@ -1478,9 +1467,10 @@ class Renderer {
14781467
pop_args(bc);
14791468
break;
14801469
}
1481-
case Bytecode::Op::Push:
1470+
case Bytecode::Op::Push: {
14821471
m_stack.emplace_back(*get_imm(bc));
14831472
break;
1473+
}
14841474
case Bytecode::Op::Upper: {
14851475
auto result = get_args(bc)[0]->get<std::string>();
14861476
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
@@ -1737,9 +1727,10 @@ class Renderer {
17371727
m_stack.emplace_back(std::move(result));
17381728
break;
17391729
}
1740-
case Bytecode::Op::Jump:
1730+
case Bytecode::Op::Jump: {
17411731
i = bc.args - 1; // -1 due to ++i in loop
17421732
break;
1733+
}
17431734
case Bytecode::Op::ConditionalJump: {
17441735
if (!truthy(m_stack.back())) {
17451736
i = bc.args - 1; // -1 due to ++i in loop

0 commit comments

Comments
 (0)