* also handle replace string case-insensitively in case-insensitive mode
* leaner code
This commit is contained in:
Juergen Spitzmueller 2024-07-06 09:24:07 +02:00
parent 7d2d26132b
commit 666304633a
2 changed files with 12 additions and 16 deletions

View File

@ -917,25 +917,20 @@ docstring const subst_string(docstring const & a,
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

View File

@ -195,6 +195,7 @@ std::string const subst(std::string const & a,
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);