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