From 99c498d3d753091c9cae4f26cad28d239f73e702 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 25 Aug 2019 13:37:06 -0700 Subject: [PATCH] Use move semantics in trim and history_item_t --- src/history.cpp | 6 ++---- src/history.h | 2 +- src/wcstringutil.cpp | 15 +++++++++------ src/wcstringutil.h | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/history.cpp b/src/history.cpp index 38e1a2ef1..ba12fe40c 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -171,10 +171,8 @@ bool history_item_t::merge(const history_item_t &item) { return result; } -history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident) - : creation_timestamp(when), identifier(ident) { - contents = trim(str); -} +history_item_t::history_item_t(wcstring str, time_t when, history_identifier_t ident) + : contents(trim(std::move(str))), creation_timestamp(when), identifier(ident) {} bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type, bool case_sensitive) const { diff --git a/src/history.h b/src/history.h index 42328e063..102c3a560 100644 --- a/src/history.h +++ b/src/history.h @@ -82,7 +82,7 @@ class history_item_t { path_list_t required_paths; public: - explicit history_item_t(const wcstring &str = wcstring(), time_t when = 0, + explicit history_item_t(wcstring str = wcstring(), time_t when = 0, history_identifier_t ident = 0); const wcstring &str() const { return contents; } diff --git a/src/wcstringutil.cpp b/src/wcstringutil.cpp index b0fac9809..3691f79e5 100644 --- a/src/wcstringutil.cpp +++ b/src/wcstringutil.cpp @@ -49,16 +49,19 @@ wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype) { return output; } -wcstring trim(const wcstring &input) { return trim(input, L"\t\v \r\n"); } +wcstring trim(wcstring input) { return trim(std::move(input), L"\t\v \r\n"); } -wcstring trim(const wcstring &input, const wchar_t *any_of) { - auto begin_offset = input.find_first_not_of(any_of); - if (begin_offset == wcstring::npos) { +wcstring trim(wcstring input, const wchar_t *any_of) { + wcstring result = std::move(input); + size_t suffix = result.find_last_not_of(any_of); + if (suffix == wcstring::npos) { return wcstring{}; } - auto end = input.cbegin() + input.find_last_not_of(any_of); + result.erase(suffix + 1); - wcstring result(input.begin() + begin_offset, end + 1); + auto prefix = result.find_first_not_of(any_of); + assert(prefix != wcstring::npos && "Should have one non-trimmed character"); + result.erase(0, prefix); return result; } diff --git a/src/wcstringutil.h b/src/wcstringutil.h index 4fbfe0608..54ce9ba2a 100644 --- a/src/wcstringutil.h +++ b/src/wcstringutil.h @@ -65,8 +65,8 @@ enum class ellipsis_type { wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype = ellipsis_type::Prettiest); -wcstring trim(const wcstring &input); -wcstring trim(const wcstring &input, const wchar_t *any_of); +wcstring trim(wcstring input); +wcstring trim(wcstring input, const wchar_t *any_of); /// Converts a string to lowercase. wcstring wcstolower(wcstring input);