[LyX/master] Improve 0f35e3141bc5b6ba

Juergen Spitzmueller spitz at lyx.org
Sat Jul 6 07:26:43 UTC 2024


commit 666304633ac99adf52f5b69f093ac32875b4dd99
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sat Jul 6 09:24:07 2024 +0200

    Improve 0f35e3141bc5b6ba
    
    * also handle replace string case-insensitively in case-insensitive mode
    * leaner code
---
 src/support/lstrings.cpp | 27 +++++++++++----------------
 src/support/lstrings.h   |  1 +
 2 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp
index 600885f80c..5d0c37694d 100644
--- a/src/support/lstrings.cpp
+++ b/src/support/lstrings.cpp
@@ -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
diff --git a/src/support/lstrings.h b/src/support/lstrings.h
index b406f30994..715d9b0b02 100644
--- a/src/support/lstrings.h
+++ b/src/support/lstrings.h
@@ -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);


More information about the lyx-cvs mailing list