mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-09 18:31:04 +00:00
Improve 0f35e3141b
* also handle replace string case-insensitively in case-insensitive mode * leaner code
This commit is contained in:
parent
7d2d26132b
commit
666304633a
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user