Skip to content

Commit

Permalink
Improve trimming of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Feb 28, 2024
1 parent 90f5ccf commit de51669
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/passwordManager/passwords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,13 @@ bool exportCsv(const privacy::vector<passwordRecords> &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.
Expand Down
11 changes: 6 additions & 5 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ concept StringLike = std::same_as<T, std::basic_string<typename T::value_type,
/// \brief Trims space (whitespace) off the beginning and end of a string.
/// \param str the string to trim.
void stripString(StringLike auto &str) noexcept {
constexpr std::string_view space = " \t\n\r\f\v";

// Trim the leading space
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);
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 Gets a response string from user input.
Expand All @@ -99,6 +98,8 @@ void stripString(StringLike auto &str) noexcept {
std::string getResponseStr(const std::string &prompt) {
std::cout << prompt << std::endl;
char *tmp = readline("> ");
if (tmp == nullptr) return std::string{};

auto str = std::string{tmp};

// Trim leading and trailing spaces
Expand Down

0 comments on commit de51669

Please sign in to comment.