]> git.lyx.org Git - lyx.git/commitdiff
Improve 0f35e3141bc5b6ba
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 6 Jul 2024 07:24:07 +0000 (09:24 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Sat, 6 Jul 2024 07:25:57 +0000 (09:25 +0200)
* also handle replace string case-insensitively in case-insensitive mode
* leaner code

src/support/lstrings.cpp
src/support/lstrings.h

index 600885f80c22f1d650d41a2580d4178b8541f9a9..5d0c37694d7c7a4ca6005dd0590a9ae202c53371 100644 (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
index b406f30994c39110dd5245a66384ffbc53de6988..715d9b0b0263471ff589405cc947886836ae09d7 100644 (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);