-/// A map of symbols and their escaped equivalent needed within a regex.
-/// @note Beware of order
-Escapes const & get_regexp_escapes()
-{
- typedef std::pair<std::string, std::string> P;
-
- static Escapes escape_map;
- if (escape_map.empty()) {
- escape_map.push_back(P("$", "_x_$"));
- escape_map.push_back(P("{", "_x_{"));
- escape_map.push_back(P("}", "_x_}"));
- escape_map.push_back(P("[", "_x_["));
- escape_map.push_back(P("]", "_x_]"));
- escape_map.push_back(P("(", "_x_("));
- escape_map.push_back(P(")", "_x_)"));
- escape_map.push_back(P("+", "_x_+"));
- escape_map.push_back(P("*", "_x_*"));
- escape_map.push_back(P(".", "_x_."));
- escape_map.push_back(P("\\", "(?:\\\\|\\\\backslash)"));
- escape_map.push_back(P("~", "(?:\\\\textasciitilde|\\\\sim)"));
- escape_map.push_back(P("^", "(?:\\^|\\\\textasciicircum\\{\\}|\\\\textasciicircum|\\\\mathcircumflex)"));
- escape_map.push_back(P("_x_", "\\"));
- }
- return escape_map;
-}
-
-/// A map of lyx escaped strings and their unescaped equivalent.
-Escapes const & get_lyx_unescapes()
-{
- typedef std::pair<std::string, std::string> P;
-
- static Escapes escape_map;
- if (escape_map.empty()) {
- escape_map.push_back(P("\\%", "%"));
- escape_map.push_back(P("\\{", "{"));
- escape_map.push_back(P("\\}", "}"));
- escape_map.push_back(P("\\mathcircumflex ", "^"));
- escape_map.push_back(P("\\mathcircumflex", "^"));
- escape_map.push_back(P("\\backslash ", "\\"));
- escape_map.push_back(P("\\backslash", "\\"));
- escape_map.push_back(P("\\sim ", "~"));
- escape_map.push_back(P("\\sim", "~"));
- }
- return escape_map;
-}
-
-/// A map of escapes turning a regexp matching text to one matching latex.
-Escapes const & get_regexp_latex_escapes()
-{
- typedef std::pair<std::string, std::string> P;
-
- static Escapes escape_map;
- if (escape_map.empty()) {
- escape_map.push_back(P("\\\\", "(?:\\\\\\\\|\\\\backslash|\\\\textbackslash\\{\\}|\\\\textbackslash)"));
- escape_map.push_back(P("(<?!\\\\\\\\textbackslash)\\{", "\\\\\\{"));
- escape_map.push_back(P("(<?!\\\\\\\\textbackslash\\\\\\{)\\}", "\\\\\\}"));
- escape_map.push_back(P("\\[", "\\{\\[\\}"));
- escape_map.push_back(P("\\]", "\\{\\]\\}"));
- escape_map.push_back(P("\\^", "(?:\\^|\\\\textasciicircum\\{\\}|\\\\textasciicircum|\\\\mathcircumflex)"));
- escape_map.push_back(P("%", "\\\\\\%"));
- escape_map.push_back(P("#", "\\\\#"));
- }
- return escape_map;
-}
-
-/** @todo Probably the maps need to be migrated to regexps, in order to distinguish if
- ** the found occurrence were escaped.
- **/
-string apply_escapes(string s, Escapes const & escape_map)
-{
- LYXERR(Debug::FIND, "Escaping: '" << s << "'");
- Escapes::const_iterator it;
- for (it = escape_map.begin(); it != escape_map.end(); ++it) {
-// LYXERR(Debug::FIND, "Escaping " << it->first << " as " << it->second);
- unsigned int pos = 0;
- while (pos < s.length() && (pos = s.find(it->first, pos)) < s.length()) {
- s.replace(pos, it->first.length(), it->second);
- LYXERR(Debug::FIND, "After escape: " << s);
- pos += it->second.length();
-// LYXERR(Debug::FIND, "pos: " << pos);
+string string2regex(string in)
+{
+ static std::regex specialChars { R"([-[\]{}()*+?.,\^$|#\s\$\\])" };
+ string temp = std::regex_replace(in, specialChars, R"(\$&)" );
+ string temp2("");
+ size_t lastpos = 0;
+ size_t fl_pos = 0;
+ int offset = 1;
+ while (fl_pos < temp.size()) {
+ fl_pos = temp.find("\\\\foreignlanguage", lastpos + offset);
+ if (fl_pos == string::npos)
+ break;
+ offset = 16;
+ temp2 += temp.substr(lastpos, fl_pos - lastpos);
+ temp2 += "\\n";
+ lastpos = fl_pos;
+ }
+ if (lastpos == 0)
+ return(temp);
+ if (lastpos < temp.size()) {
+ temp2 += temp.substr(lastpos, temp.size() - lastpos);
+ }
+ return temp2;
+}
+
+string correctRegex(string t, bool withformat)
+{
+ /* Convert \backslash => \
+ * and \{, \}, \[, \] => {, }, [, ]
+ */
+ string s("");
+ regex wordre("(\\\\)*(\\\\((backslash|mathcircumflex) ?|[\\[\\]\\{\\}]))");
+ size_t lastpos = 0;
+ smatch sub;
+ bool backslashed = false;
+ for (sregex_iterator it(t.begin(), t.end(), wordre), end; it != end; ++it) {
+ sub = *it;
+ string replace;
+ if ((sub.position(2) - sub.position(0)) % 2 == 1) {
+ continue;
+ }
+ else {
+ if (sub.str(4) == "backslash") {
+ replace = "\\";
+ if (withformat) {
+ // transforms '\backslash \{' into '\{'
+ // and '\{' into '{'
+ string next = t.substr(sub.position(2) + sub.str(2).length(), 2);
+ if ((next == "\\{") || (next == "\\}")) {
+ replace = "";
+ backslashed = true;
+ }
+ }
+ }
+ else if (sub.str(4) == "mathcircumflex")
+ replace = "^";
+ else if (backslashed) {
+ backslashed = false;
+ if (withformat && (sub.str(3) == "{"))
+ replace = accents["braceleft"];
+ else if (withformat && (sub.str(3) == "}"))
+ replace = accents["braceright"];
+ else {
+ // else part should not exist
+ LASSERT(1, /**/);
+ }
+ }
+ else
+ replace = sub.str(3);