Commit 7b1811a4 authored by Dubský Jan's avatar Dubský Jan
Browse files

Solution 1.03 (Speed: 1.07)

parent 88f4fb08
......@@ -8,11 +8,11 @@
namespace macrosol {
void macroprocessor_impl::apply_defines(std::string_view token, std::back_insert_iterator<std::string>& out) {
void macroprocessor_impl::apply_defines(std::string_view token, std::string& out) {
auto it = defines.find(token);
if (it == defines.end()) {
out = std::copy(token.begin(), token.end(), out);
*out = ' ';
out.append(token.begin(), token.end());
out.push_back(' ');
return;
}
// std::string_view(&t.front(), t.length())
......@@ -40,8 +40,7 @@ bool macroprocessor_impl::process(const std::string& s, std::string& output) {
}
assert(output.size() == 0);
auto out = std::back_insert_iterator(output);
for (auto&& t : tokens) apply_defines(t, out);
for (auto&& t : tokens) apply_defines(t, output);
if (output.size()) output.pop_back();
return true;
}
......@@ -83,22 +82,26 @@ void macroprocessor_impl::macro::update(const std::vector<std::string_view>& tok
assert(tokens[1] == "=");
right.resize(tokens.size() - 2);
std::copy(tokens.begin() + 2, tokens.end(), right.begin());
right_len = 0;
for (auto&& r : right) right_len += r.size();
}
macroprocessor_impl::macro::macro(macro&& m) {
this->left = m.left;
this->left_len = m.left_len;
m.left = 0;
m.left_len = 0;
this->right = std::move(m.right);
this->left = m.left;
this->left_len = m.left_len;
m.left = 0;
this->right = std::move(m.right);
this->right_len = m.right_len;
m.left_len = m.right_len = 0;
}
macroprocessor_impl::macro& macroprocessor_impl::macro::operator=(macro&& m) {
this->left = m.left;
this->left_len = m.left_len;
m.left = 0;
m.left_len = 0;
this->right = std::move(m.right);
this->left = m.left;
this->left_len = m.left_len;
m.left = 0;
this->right = std::move(m.right);
this->right_len = m.right_len;
m.left_len = m.right_len = 0;
return *this;
}
......
......@@ -52,12 +52,13 @@ private:
char* left;
size_t left_len;
std::vector<std::string> right;
size_t right_len;
};
std::unordered_map<std::string_view, macro> defines;
std::vector<std::string_view> parse_tokens(const std::string& s);
void apply_defines(std::string_view token, std::back_insert_iterator<std::string>& out);
void apply_defines(std::string_view token, std::string& out);
};
template <typename policy>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment