From 061cb76fc48f493b187928f841083981e9733a28 Mon Sep 17 00:00:00 2001 From: Alex Maystrenko Date: Sat, 5 Feb 2022 20:47:29 +0100 Subject: [PATCH 1/3] make functions in header file inline added header guard now string utils can be used in any part of the project --- include/util/string.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/util/string.hpp b/include/util/string.hpp index d644b4c..c8c0209 100644 --- a/include/util/string.hpp +++ b/include/util/string.hpp @@ -1,15 +1,17 @@ +#pragma once + #include const std::string WHITESPACE = " \n\r\t\f\v"; -std::string ltrim(const std::string s) { +inline std::string ltrim(const std::string s) { size_t begin = s.find_first_not_of(WHITESPACE); return (begin == std::string::npos) ? "" : s.substr(begin); } -std::string rtrim(const std::string s) { +inline std::string rtrim(const std::string s) { size_t end = s.find_last_not_of(WHITESPACE); return (end == std::string::npos) ? "" : s.substr(0, end + 1); } -std::string trim(const std::string& s) { return rtrim(ltrim(s)); } +inline std::string trim(const std::string& s) { return rtrim(ltrim(s)); } From 2697d432a4c271f1cd6b701799842688c0e4f85e Mon Sep 17 00:00:00 2001 From: Alex Maystrenko Date: Sat, 5 Feb 2022 20:54:06 +0100 Subject: [PATCH 2/3] ltrim and rtrim take argument by const-ref --- include/util/string.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/util/string.hpp b/include/util/string.hpp index c8c0209..24a9b2b 100644 --- a/include/util/string.hpp +++ b/include/util/string.hpp @@ -4,12 +4,12 @@ const std::string WHITESPACE = " \n\r\t\f\v"; -inline std::string ltrim(const std::string s) { +inline std::string ltrim(const std::string& s) { size_t begin = s.find_first_not_of(WHITESPACE); return (begin == std::string::npos) ? "" : s.substr(begin); } -inline std::string rtrim(const std::string s) { +inline std::string rtrim(const std::string& s) { size_t end = s.find_last_not_of(WHITESPACE); return (end == std::string::npos) ? "" : s.substr(0, end + 1); } From f3819ee9549dd54b60168cf3b9ff84ac195d3542 Mon Sep 17 00:00:00 2001 From: Alex Maystrenko Date: Sat, 5 Feb 2022 21:01:07 +0100 Subject: [PATCH 3/3] remove duplicated string manipulation methods --- src/modules/wlr/taskbar.cpp | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/modules/wlr/taskbar.cpp b/src/modules/wlr/taskbar.cpp index ddc360b..55c9431 100644 --- a/src/modules/wlr/taskbar.cpp +++ b/src/modules/wlr/taskbar.cpp @@ -4,6 +4,7 @@ #include "glibmm/fileutils.h" #include "glibmm/refptr.h" #include "util/format.hpp" +#include "util/string.hpp" #include #include @@ -26,27 +27,6 @@ namespace waybar::modules::wlr { -/* String manipulation methods */ -const std::string WHITESPACE = " \n\r\t\f\v"; - -static std::string ltrim(const std::string& s) -{ - size_t start = s.find_first_not_of(WHITESPACE); - return (start == std::string::npos) ? "" : s.substr(start); -} - -static std::string rtrim(const std::string& s) -{ - size_t end = s.find_last_not_of(WHITESPACE); - return (end == std::string::npos) ? "" : s.substr(0, end + 1); -} - -static std::string trim(const std::string& s) -{ - return rtrim(ltrim(s)); -} - - /* Icon loading functions */ static std::vector search_prefix() {