Use move semantics in trim and history_item_t

This commit is contained in:
ridiculousfish 2019-08-25 13:37:06 -07:00
parent dd34bf0ba6
commit 99c498d3d7
4 changed files with 14 additions and 13 deletions

View File

@ -171,10 +171,8 @@ bool history_item_t::merge(const history_item_t &item) {
return result; return result;
} }
history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident) history_item_t::history_item_t(wcstring str, time_t when, history_identifier_t ident)
: creation_timestamp(when), identifier(ident) { : contents(trim(std::move(str))), creation_timestamp(when), identifier(ident) {}
contents = trim(str);
}
bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type, bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type,
bool case_sensitive) const { bool case_sensitive) const {

View File

@ -82,7 +82,7 @@ class history_item_t {
path_list_t required_paths; path_list_t required_paths;
public: 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); history_identifier_t ident = 0);
const wcstring &str() const { return contents; } const wcstring &str() const { return contents; }

View File

@ -49,16 +49,19 @@ wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype) {
return output; 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) { wcstring trim(wcstring input, const wchar_t *any_of) {
auto begin_offset = input.find_first_not_of(any_of); wcstring result = std::move(input);
if (begin_offset == wcstring::npos) { size_t suffix = result.find_last_not_of(any_of);
if (suffix == wcstring::npos) {
return wcstring{}; 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; return result;
} }

View File

@ -65,8 +65,8 @@ enum class ellipsis_type {
wcstring truncate(const wcstring &input, int max_len, wcstring truncate(const wcstring &input, int max_len,
ellipsis_type etype = ellipsis_type::Prettiest); ellipsis_type etype = ellipsis_type::Prettiest);
wcstring trim(const wcstring &input); wcstring trim(wcstring input);
wcstring trim(const wcstring &input, const wchar_t *any_of); wcstring trim(wcstring input, const wchar_t *any_of);
/// Converts a string to lowercase. /// Converts a string to lowercase.
wcstring wcstolower(wcstring input); wcstring wcstolower(wcstring input);