From 44d97e9e28d77d970cf82dcfdffe56540a2232e3 Mon Sep 17 00:00:00 2001 From: Teus Benschop Date: Tue, 5 Mar 2024 17:45:28 +0100 Subject: [PATCH] Optimize code a bit https://github.com/bibledit/cloud/issues/922 --- editor/styles.cpp | 89 +++++++++++++++++++++++------------------------ editor/styles.h | 4 +-- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/editor/styles.cpp b/editor/styles.cpp index f99c8b85d..c684f082f 100644 --- a/editor/styles.cpp +++ b/editor/styles.cpp @@ -35,72 +35,70 @@ #include #endif #pragma GCC diagnostic pop -using namespace std; -using namespace pugi; -string Editor_Styles::getRecentlyUsed (Webserver_Request& webserver_request) +std::string Editor_Styles::getRecentlyUsed (Webserver_Request& webserver_request) { - string bible = webserver_request.database_config_user()->getBible (); - string stylesheet = Database_Config_Bible::getEditorStylesheet (bible); + const std::string bible = webserver_request.database_config_user()->getBible (); + const std::string stylesheet = Database_Config_Bible::getEditorStylesheet (bible); // The recent styles. - string s_styles = webserver_request.database_config_user()->getRecentlyAppliedStyles (); - vector styles = filter::strings::explode (s_styles, ' '); - string fragment = translate("Select style") + ": "; + const std::string s_styles = webserver_request.database_config_user()->getRecentlyAppliedStyles (); + const std::vector styles = filter::strings::explode (s_styles, ' '); + std::string fragment = translate("Select style") + ": "; for (unsigned int i = 0; i < styles.size(); i++) { - if (i) fragment += " | "; - string marker = styles [i]; + if (i) fragment.append (" | "); + const std::string& marker = styles.at(i); Database_Styles_Item data = webserver_request.database_styles()->getMarkerData (stylesheet, marker); if (data.marker.empty ()) continue; - string name = data.name + " (" + marker + ")"; - string info = data.info; - xml_document document; - xml_node a_node = document.append_child("a"); + const std::string name = data.name + " (" + marker + ")"; + const std::string info = data.info; + pugi::xml_document document; + pugi::xml_node a_node = document.append_child("a"); a_node.append_attribute("href") = marker.c_str(); a_node.append_attribute("title") = info.c_str(); a_node.append_attribute("unselectable") = "on"; a_node.append_attribute("class") = "unselectable"; a_node.append_attribute("tabindex") = "-1"; a_node.text().set(name.c_str()); - stringstream ss; - document.print(ss, "", format_raw); + std::stringstream ss {}; + document.print(ss, "", pugi::format_raw); fragment.append (ss.str()); } // Links for cancelling and for all styles. - fragment += " "; - fragment += R"([)" + translate("cancel") + "]"; - fragment += " "; - fragment += R"([)" + translate("all") + "]"; + fragment.append(" "); + fragment.append(R"([)" + translate("cancel") + "]"); + fragment.append(" "); + fragment.append(R"([)" + translate("all") + "]"); return fragment; } -string Editor_Styles::getAll (Webserver_Request& webserver_request) +std::string Editor_Styles::getAll (Webserver_Request& webserver_request) { - string bible = webserver_request.database_config_user()->getBible (); - string stylesheet = Database_Config_Bible::getEditorStylesheet (bible); + const std::string bible = webserver_request.database_config_user()->getBible (); + const std::string stylesheet = Database_Config_Bible::getEditorStylesheet (bible); // The styles. - map data = webserver_request.database_styles()->getMarkersAndNames (stylesheet); + const std::map data = webserver_request.database_styles()->getMarkersAndNames (stylesheet); - vector lines; + std::vector lines{}; lines.push_back (R"(