From cdfdc994f17e910e28c2aae5a5c614d94d7749da Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 16 Feb 2018 21:26:15 -0800 Subject: [PATCH] Clean up completion_t constructors Use some move semantics and default implementations. --- src/complete.cpp | 49 ++++++++++++++---------------------------------- src/complete.h | 11 +++++++---- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/complete.cpp b/src/complete.cpp index 7ebd5eeae..fda527a10 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -195,8 +195,6 @@ const option_list_t &completion_entry_t::get_options() const { return options; } -completion_t::~completion_t() {} - /// Clear the COMPLETE_AUTO_SPACE flag, and set COMPLETE_NO_SPACE appropriately depending on the /// suffix of the string. static complete_flags_t resolve_auto_space(const wcstring &comp, complete_flags_t flags) { @@ -210,25 +208,18 @@ static complete_flags_t resolve_auto_space(const wcstring &comp, complete_flags_ } /// completion_t functions. Note that the constructor resolves flags! -completion_t::completion_t(const wcstring &comp, const wcstring &desc, string_fuzzy_match_t mat, +completion_t::completion_t(wcstring comp, wcstring desc, string_fuzzy_match_t mat, complete_flags_t flags_val) - : completion(comp), description(desc), match(mat), flags(resolve_auto_space(comp, flags_val)) {} + : completion(std::move(comp)), + description(std::move(desc)), + match(mat), + flags(resolve_auto_space(completion, flags_val)) {} -completion_t::completion_t(const completion_t &him) - : completion(him.completion), - description(him.description), - match(him.match), - flags(him.flags) {} - -completion_t &completion_t::operator=(const completion_t &him) { - if (this != &him) { - this->completion = him.completion; - this->description = him.description; - this->match = him.match; - this->flags = him.flags; - } - return *this; -} +completion_t::completion_t(const completion_t &him) = default; +completion_t::completion_t(completion_t &&him) = default; +completion_t &completion_t::operator=(const completion_t &him) = default; +completion_t &completion_t::operator=(completion_t &&him) = default; +completion_t::~completion_t() = default; bool completion_t::is_naturally_less_than(const completion_t &a, const completion_t &b) { // For this to work, stable_sort must be used because results aren't interchangeable. @@ -371,22 +362,10 @@ static void autoloaded_completion_removed(const wcstring &cmd) { static autoload_t completion_autoloader(L"fish_complete_path", autoloaded_completion_removed); /// Create a new completion entry. -void append_completion(std::vector *completions, const wcstring &comp, - const wcstring &desc, complete_flags_t flags, string_fuzzy_match_t match) { - // If we just constructed the completion and used push_back, we would get two string copies. Try - // to avoid that by making a stubby completion in the vector first, and then copying our string - // in. Note that completion_t's constructor will munge 'flags' so it's important that we pass - // those to the constructor. - // - // Nasty hack for #1241 - since the constructor needs the completion string to resolve - // AUTO_SPACE, and we aren't providing it with the completion, we have to do the resolution - // ourselves. We should get this resolving out of the constructor. - assert(completions != NULL); - const wcstring empty; - completions->push_back(completion_t(empty, empty, match, resolve_auto_space(comp, flags))); - completion_t *last = &completions->back(); - last->completion = comp; - last->description = desc; +void append_completion(std::vector *completions, wcstring comp, wcstring desc, + complete_flags_t flags, string_fuzzy_match_t match) { + completions->emplace_back(std::move(comp), std::move(desc), match, + resolve_auto_space(comp, flags)); } /// Test if the specified script returns zero. The result is cached, so that if multiple completions diff --git a/src/complete.h b/src/complete.h index a2cda9a9a..8e4e8b3cd 100644 --- a/src/complete.h +++ b/src/complete.h @@ -69,13 +69,16 @@ class completion_t { /// signal that this completion is case insensitive. complete_flags_t flags; - // Construction. Note: defining these so that they are not inlined reduces the executable size. - explicit completion_t(const wcstring &comp, const wcstring &desc = wcstring(), + // Construction. + explicit completion_t(wcstring comp, wcstring desc = wcstring(), string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact), complete_flags_t flags_val = 0); completion_t(const completion_t &); completion_t &operator=(const completion_t &); + completion_t(completion_t &&); + completion_t &operator=(completion_t &&); + // Compare two completions. No operating overlaoding to make this always explicit (there's // potentially multiple ways to compare completions). // @@ -176,8 +179,8 @@ bool complete_is_valid_argument(const wcstring &str, const wcstring &opt, const /// \param comp The completion string /// \param desc The description of the completion /// \param flags completion flags -void append_completion(std::vector *completions, const wcstring &comp, - const wcstring &desc = wcstring(), int flags = 0, +void append_completion(std::vector *completions, wcstring comp, + wcstring desc = wcstring(), int flags = 0, string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact)); /// Function used for testing.