Commit 678550aa authored by s_kleplj's avatar s_kleplj
Browse files

Did some bug fixes and optimizations

parent c89587e6
......@@ -9,6 +9,7 @@ bool macroprocessor_impl::process(
std::string& output
) {
std::vector<std::string_view> parsed;
bool is_def = false;
for (
auto [i, j] = std::tuple{s.begin(), s.end()};;
......@@ -18,28 +19,36 @@ bool macroprocessor_impl::process(
if (i == s.end()) {
parsed.emplace_back(&*j, &*i - &*j);
break;
} else if (*i == ' ') {
} else if (*i == ' ' || *i == '=') {
if (*i == '=') {
is_def = true;
}
parsed.emplace_back(&*j, &*i - &*j);
j = s.end();
}
} else {
if (i == s.end()) {
break;
} else if (*i == '=') {
is_def = true;
} else if (*i != ' ') {
j = i;
}
}
}
}
if (parsed.size() > 1 && parsed[1] == "=") {
if (parsed.size() != 2) {
if (is_def) {
if (parsed.size() != 1) {
auto tmp = std::vector<word*> {};
tmp.reserve(parsed.size() - 2);
tmp.reserve(parsed.size() - 1);
for (auto i = parsed.begin() + 2; i != parsed.end(); i++) {
for (auto i = parsed.begin() + 1; i != parsed.end(); ++i) {
auto it = words_.find(*i);
if (it != words_.end()) {
tmp.emplace_back(&it->second);
} else {
auto w_it = words_.emplace(parsed[0], std::string{parsed[0]}).first;
auto w_it = words_.emplace(*i, std::string{*i}).first;
const_cast<std::string_view&>(w_it->first) = w_it->second.name_;
tmp.emplace_back(&w_it->second);
}
......@@ -70,17 +79,40 @@ bool macroprocessor_impl::process(
return false;
} else {
std::size_t length = 0;
for (auto&& p : parsed) {
auto it = words_.find(p);
if (it != words_.end()) {
output.reserve(output.size() + it->second.cached_length);
it->second.append_to(output);
std::size_t w_length = it->second.update_length() + 1;
if (w_length > 0) {
length += w_length + 1;
}
} else {
length += p.size() + 1;
}
}
output.reserve(length);
for (auto&& p : parsed) {
auto it = words_.find(p);
if (it != words_.end()) {
if (it->second.length > 0) {
it->second.append_to(output);
output += ' ';
}
} else {
output += p;
output += ' ';
}
}
if (output.size() > 0) {
output.pop_back();
}
return true;
}
......
......@@ -41,37 +41,56 @@ class macroprocessor_impl {
struct word {
word(std::string&& name) noexcept :
name_{std::move(name)},
cached_length{name_.size()},
value_{}
value_{std::vector{this}}
{}
word(std::string&& name, std::vector<word*>&& vector) noexcept :
name_{std::move(name)},
value_{std::move(vector)},
cached_length{0}
{
for (const word* i : vector) {
cached_length += i->cached_length;
}
}
value_{std::move(vector)}
{}
void append_to(std::string& output) {
if (cached_length != 0) {
if (value_.size() != 0) {
cached_length = 0;
for (word* w : value_) {
if (length == 0) {
return;
}
if (value_.size() == 1 && value_[0] == this) {
output += name_;
} else {
for (word* w : value_) {
if (w->length > 0) {
w->append_to(output);
cached_length += w->cached_length;
output += ' ';
}
}
output.pop_back();
}
}
std::size_t update_length() {
if (value_.size() == 1 && value_[0] == this) {
length = name_.size();
} else {
length = 0;
for (word* w : value_) {
std::size_t w_length = w->update_length();
if (w_length > 0) {
length += w_length + 1;
}
} else {
output += name_;
}
if (length > 0) {
--length;
}
}
return length;
}
std::string name_;
std::vector<word*> value_;
std::size_t cached_length;
std::size_t length = 0;
};
std::map<std::string_view, word> words_;
......
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