From de51669ede2ec81e754ab8e08c173cdecd0c34ca Mon Sep 17 00:00:00 2001 From: Ian Duncan <76043277+dr8co@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:30:03 +0300 Subject: [PATCH] Improve trimming of strings --- src/passwordManager/passwords.cpp | 11 +++++------ src/utils/utils.cpp | 11 ++++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/passwordManager/passwords.cpp b/src/passwordManager/passwords.cpp index d255d41..9bbf682 100644 --- a/src/passwordManager/passwords.cpp +++ b/src/passwordManager/passwords.cpp @@ -577,14 +577,13 @@ bool exportCsv(const privacy::vector &records, const std::strin /// \brief Trims space (whitespace) off the beginning and end of a string. /// \param str the string to trim. inline void trim(std::string &str) { - // Trim the leading space (my IDE finds the w-word offensive) - std::input_iterator auto it = std::ranges::find_if_not(str.begin(), str.end(), - [](const char c) { return std::isspace(c); }); - str.erase(str.begin(), it); + constexpr std::string_view space = " \t\n\r\f\v"; + + // Trim the leading space + str.erase(0, str.find_first_not_of(space)); // Trim the trailing space - it = std::ranges::find_if_not(str.rbegin(), str.rend(), [](const char c) { return std::isspace(c); }).base(); - str.erase(it, str.end()); + str.erase(str.find_last_not_of(space) + 1); } /// \brief Imports password records from a csv file. diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 8e47354..9fa3495 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -79,14 +79,13 @@ concept StringLike = std::same_as "); + if (tmp == nullptr) return std::string{}; + auto str = std::string{tmp}; // Trim leading and trailing spaces