From 5037a17fbac9dc897cdb7bbdd0fcba1f32e5945f Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 3 Oct 2021 16:47:35 +0200 Subject: [PATCH] utils: introduce trim() --- utils.cpp | 17 +++++++++++++++++ utils.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/utils.cpp b/utils.cpp index 7783e6d..d936f7a 100644 --- a/utils.cpp +++ b/utils.cpp @@ -181,3 +181,20 @@ std::string utils::toISODate(time_t t) } return std::string{result}; } + +std::string utils::trim(const std::string &str) +{ + std::string_view chars = " \t\n\r"; + std::string_view view = str; + auto n = view.find_first_not_of(chars); + if(n != std::string_view::npos) + { + view.remove_prefix(n); + } + n = view.find_last_not_of(chars); + if(n != std::string_view::npos) + { + view.remove_suffix(view.size() - n - 1); + } + return std::string{view}; +} diff --git a/utils.h b/utils.h index d19be1b..5173a4e 100644 --- a/utils.h +++ b/utils.h @@ -93,5 +93,7 @@ template inline std::string toString(const T &v) return std::string(v.begin(), v.end()); } +std::string trim(const std::string &str); + } // namespace utils #endif