// Then, we temporarily make all " and " strings to ampersands in order
// to handle them later on a per-char level. Note that arbitrary casing
// ("And", "AND", "aNd", ...) is allowed in bibtex (#10465).
- static regex const and_reg("(.* )([aA][nN][dD])( .*)");
- smatch sub;
- string res = to_utf8(iname);
- while (regex_match(res, sub, and_reg))
- res = sub.str(1) + "&" + sub.str(3);
- iname = from_utf8(res);
+ iname = subst(iname, from_ascii(" and "), from_ascii(" & "), false);
// Now we traverse through the string and replace the "&" by the proper
// output in- and outside groups
docstring name;
docstring const subst_string(docstring const & a,
- docstring const & oldstr, docstring const & newstr)
+ docstring const & oldstr, docstring const & newstr,
+ bool const case_sens)
{
LASSERT(!oldstr.empty(), return a);
docstring lstr = a;
size_t i = 0;
size_t const olen = oldstr.length();
- while ((i = lstr.find(oldstr, i)) != string::npos) {
- lstr.replace(i, olen, newstr);
- i += newstr.length(); // We need to be sure that we don't
- // use the same i over and over again.
+ if (case_sens)
+ while ((i = lstr.find(oldstr, i)) != string::npos) {
+ lstr.replace(i, olen, newstr);
+ i += newstr.length(); // We need to be sure that we don't
+ // use the same i over and over again.
+ }
+ else {
+ docstring lcstr = lowercase(lstr);
+ while ((i = lcstr.find(oldstr, i)) != string::npos) {
+ lstr.replace(i, olen, newstr);
+ i += newstr.length(); // We need to be sure that we don't
+ // use the same i over and over again.
+ lcstr = lowercase(lstr);
+ }
}
return lstr;
}
docstring const subst(docstring const & a,
- docstring const & oldstr, docstring const & newstr)
+ docstring const & oldstr, docstring const & newstr,
+ bool case_sens)
{
- return subst_string(a, oldstr, newstr);
+ return subst_string(a, oldstr, newstr, case_sens);
}
/// substitutes all instances of \a oldstr with \a newstr
docstring const subst(docstring const & a,
- docstring const & oldstr, docstring const & newstr);
+ docstring const & oldstr, docstring const & newstr,
+ bool case_sens = true);
/// Count all occurrences of char \a chr inside \a str
int count_char(std::string const & str, char chr);