Skip to content
Snippets Groups Projects
Verified Commit 3927862d authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

EquationTags: misc implementation improvements

parent 9658d82c
Branches
No related tags found
No related merge requests found
Pipeline #7959 passed
......@@ -21,6 +21,7 @@
#include <regex>
#include <ostream>
#include <utility>
set<int>
EquationTags::getEqnsByKey(const string &key) const
......@@ -93,8 +94,7 @@ EquationTags::writeLatexOutput(ostream &output, int eqn) const
if (!exists(eqn))
return;
auto escape_special_latex_symbols
= [](string str)
auto escape_special_latex_symbols = [](string str)
{
const regex special_latex_chars (R"([&%$#_{}])");
const regex backslash (R"(\\)");
......@@ -108,36 +108,32 @@ EquationTags::writeLatexOutput(ostream &output, int eqn) const
return regex_replace(str, textbackslash, R"(\textbackslash{})");
};
bool wrote_eq_tag = false;
output << R"(\noindent[)";
for (const auto & [key, value] : eqn_tags.at(eqn))
for (bool wrote_eq_tag {false};
const auto & [key, value] : eqn_tags.at(eqn))
{
if (wrote_eq_tag)
if (exchange(wrote_eq_tag, true))
output << ", ";
output << escape_special_latex_symbols(key);
if (!value.empty())
output << "= `" << escape_special_latex_symbols(value) << "'";
wrote_eq_tag = true;
}
output << "]" << endl;
}
void
EquationTags::writeJsonAST(ostream &output, const int eqn) const
EquationTags::writeJsonAST(ostream &output, int eqn) const
{
if (!exists(eqn))
return;
output << R"(, "tags": {)";
bool wroteFirst = false;
for (const auto &[key, value] : eqn_tags.at(eqn))
for (bool wroteFirst {false};
const auto &[key, value] : eqn_tags.at(eqn))
{
if (wroteFirst)
if (exchange(wroteFirst, true))
output << ", ";
else
wroteFirst = true;
output << R"(")" << key << R"(": ")" << value << R"(")";
}
output << "}";
......
......@@ -63,11 +63,11 @@ public:
//! Various functions to get info from equation tags
//! Get equation tags for a given equation
map<string, string>
getTagsByEqn(const int eqn) const
getTagsByEqn(int eqn) const
{
if (auto it = eqn_tags.find(eqn); it != eqn_tags.end())
return it->second;
return map<string, string>{};
return {};
}
//! Get equations that have the given key
......@@ -104,23 +104,24 @@ public:
}
bool
exists(const int eqn) const
exists(int eqn) const
{
return eqn_tags.contains(eqn);
}
//! Returns true if equation tag with key exists for a given equation
bool
exists(const int eqn, const string &key) const
exists(int eqn, const string &key) const
{
return exists(eqn) && eqn_tags.at(eqn).contains(key);
auto it = eqn_tags.find(eqn);
return it != eqn_tags.end() && it->second.contains(key);
}
//! Various functions to write equation tags
void writeCheckSumInfo(ostream &output) const;
void writeOutput(ostream &output) const;
void writeLatexOutput(ostream &output, int eqn) const;
void writeJsonAST(ostream &output, const int eq) const;
void writeJsonAST(ostream &output, int eq) const;
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment