bool const case_sens)
{
LASSERT(!oldstr.empty(), return a);
- docstring lstr = a;
+ docstring res = a;
size_t i = 0;
size_t const olen = oldstr.length();
- 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);
- }
+ // string to be searched in
+ docstring se_str = case_sens ? res : lowercase(res);
+ // token to be searched within the above
+ docstring const se_tok = case_sens ? oldstr : lowercase(oldstr);
+ while ((i = se_str.find(se_tok, i)) != string::npos) {
+ res.replace(i, olen, newstr);
+ i += newstr.length(); // We need to be sure that we don't
+ // use the same i over and over again.
+ se_str = case_sens ? res : lowercase(res);
}
- return lstr;
+ return res;
}
} // namespace
std::string const & oldstr, std::string const & newstr);
/// substitutes all instances of \a oldstr with \a newstr
+/// If \p case_sens is false, \a a and \a oldstr are treated case-insensitive
docstring const subst(docstring const & a,
docstring const & oldstr, docstring const & newstr,
bool case_sens = true);