From 1efb81456bce624e319d10f9b3f86240bed1039b Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 26 Jan 2017 16:14:50 -0800 Subject: [PATCH] Use std::move instead of swap in a few places where it improves clarity --- src/common.cpp | 4 ++-- src/common.h | 7 ++++--- src/complete.cpp | 2 +- src/env_universal_common.cpp | 6 +++--- src/exec.cpp | 4 ++-- src/expand.cpp | 2 +- src/highlight.cpp | 2 +- src/iothread.cpp | 4 ++-- src/parse_execution.cpp | 13 +++++-------- src/parse_util.cpp | 4 ++-- src/path.cpp | 2 +- src/reader.cpp | 2 +- src/tokenizer.cpp | 2 +- 13 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index 4f987eac5..22aa8e440 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1330,7 +1330,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in // Return the string by reference, and then success. if (!errored) { - output_str->swap(result); + *output_str = std::move(result); } return !errored; } @@ -1340,7 +1340,7 @@ bool unescape_string_in_place(wcstring *str, unescape_flags_t escape_special) { wcstring output; bool success = unescape_string_internal(str->c_str(), str->size(), &output, escape_special); if (success) { - str->swap(output); + *str = std::move(output); } return success; } diff --git a/src/common.h b/src/common.h index fbd32edf4..38ddfc0cb 100644 --- a/src/common.h +++ b/src/common.h @@ -598,15 +598,16 @@ class scoped_push { public: explicit scoped_push(T *r) : ref(r), saved_value(*r), restored(false) {} - scoped_push(T *r, const T &new_value) : ref(r), saved_value(*r), restored(false) { - *r = new_value; + scoped_push(T *r, T new_value) : ref(r), restored(false) { + saved_value = std::move(*ref); + *ref = std::move(new_value); } ~scoped_push() { restore(); } void restore() { if (!restored) { - std::swap(*ref, saved_value); + *ref = std::move(saved_value); restored = true; } } diff --git a/src/complete.cpp b/src/complete.cpp index c2aa77d4b..c81de5f1e 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -1624,7 +1624,7 @@ wcstring_list_t complete_get_wrap_chain(const wcstring &command) { wcstring target; while (!to_visit.empty()) { // Grab the next command to visit, put it in target. - target.swap(to_visit.back()); + target = std::move(to_visit.back()); to_visit.pop_back(); // Try inserting into visited. If it was already present, we skip it; this is how we avoid diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index aaafe746c..a37f845a9 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -389,13 +389,13 @@ void env_universal_t::acquire_variables(var_table_t *vars_to_acquire) { // source entry in vars since we are about to get rid of this->vars entirely. var_entry_t &src = src_iter->second; var_entry_t &dst = (*vars_to_acquire)[key]; - dst.val.swap(src.val); + dst.val = std::move(src.val); dst.exportv = src.exportv; } } // We have constructed all the callbacks and updated vars_to_acquire. Acquire it! - this->vars.swap(*vars_to_acquire); + this->vars = std::move(*vars_to_acquire); } void env_universal_t::load_from_fd(int fd, callback_data_list_t *callbacks) { @@ -844,7 +844,7 @@ void env_universal_t::parse_message_internal(const wcstring &msgstr, var_table_t if (unescape_string(tmp + 1, &val, 0)) { var_entry_t &entry = (*vars)[key]; entry.exportv = exportv; - entry.val.swap(val); // acquire the value + entry.val = std::move(val); // acquire the value } } else { debug(1, PARSE_ERR, msg); diff --git a/src/exec.cpp b/src/exec.cpp index 386a534e5..9a03af75e 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -291,8 +291,8 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t *out_chain, // Now either return success, or clean up. if (success) { - out_chain->swap(result_chain); - out_opened_fds->swap(opened_fds); + *out_chain = std::move(result_chain); + *out_opened_fds = std::move(opened_fds); } else { result_chain.clear(); io_cleanup_fds(opened_fds); diff --git a/src/expand.cpp b/src/expand.cpp index 7ab2a10ba..2196e4d79 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -820,7 +820,7 @@ static int expand_variables(const wcstring &instr, std::vector *ou } // string_values is the new var_item_list. - var_item_list.swap(string_values); + var_item_list = std::move(string_values); } } diff --git a/src/highlight.cpp b/src/highlight.cpp index 7d2fffaaf..ac5909613 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -237,7 +237,7 @@ bool plain_statement_get_expanded_command(const wcstring &src, const parse_node_ if (tree.command_for_plain_statement(plain_statement, src, &cmd) && expand_one(cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS)) { // Success, return the expanded string by reference. - out_cmd->swap(cmd); + *out_cmd = std::move(cmd); return true; } return false; diff --git a/src/iothread.cpp b/src/iothread.cpp index 7fe6c9caf..c366e2c79 100644 --- a/src/iothread.cpp +++ b/src/iothread.cpp @@ -293,7 +293,7 @@ static void iothread_service_main_thread_requests(void) { std::queue request_queue; { scoped_lock queue_lock(s_main_thread_request_q_lock); - std::swap(request_queue, s_main_thread_request_queue); + request_queue.swap(s_main_thread_request_queue); } if (!request_queue.empty()) { @@ -326,7 +326,7 @@ static void iothread_service_result_queue() { std::queue result_queue; { scoped_lock queue_lock(s_result_queue_lock); - std::swap(result_queue, s_result_queue); + result_queue.swap(s_result_queue); } // Perform each completion in order. We are responsibile for cleaning them up. diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index feac256d4..ea7ab6e40 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -948,14 +948,11 @@ parse_execution_result_t parse_execution_context_t::determine_arguments( } } - // Now copy over any expanded arguments. Do it using swap() to avoid extra allocations; this + // Now copy over any expanded arguments. Use std::move() to avoid extra allocations; this // is called very frequently. - size_t old_arg_count = out_arguments->size(); - size_t new_arg_count = arg_expanded.size(); - out_arguments->resize(old_arg_count + new_arg_count); - for (size_t i = 0; i < new_arg_count; i++) { - wcstring &new_arg = arg_expanded.at(i).completion; - out_arguments->at(old_arg_count + i).swap(new_arg); + out_arguments->reserve(out_arguments->size() + arg_expanded.size()); + for (completion_t &new_arg : arg_expanded) { + out_arguments->push_back(std::move(new_arg.completion)); } } @@ -1035,7 +1032,7 @@ bool parse_execution_context_t::determine_io_chain(const parse_node_t &statement } if (out_chain && !errored) { - out_chain->swap(result); + *out_chain = std::move(result); } return !errored; } diff --git a/src/parse_util.cpp b/src/parse_util.cpp index 07ee1ad6c..361214fcd 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -1293,11 +1293,11 @@ parser_test_error_bits_t parse_util_detect_errors(const wcstring &buff_src, if (has_unclosed_block || has_unclosed_quote) res |= PARSER_TEST_INCOMPLETE; if (out_errors != NULL) { - out_errors->swap(parse_errors); + *out_errors = std::move(parse_errors); } if (out_tree != NULL) { - out_tree->swap(node_tree); + *out_tree = std::move(node_tree); } return res; diff --git a/src/path.cpp b/src/path.cpp index 6465822ed..1aa0056ea 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -72,7 +72,7 @@ static bool path_get_path_core(const wcstring &cmd, wcstring *out_path, continue; } if (S_ISREG(buff.st_mode)) { - if (out_path) out_path->swap(nxt_path); + if (out_path) *out_path = std::move(nxt_path); return true; } err = EACCES; diff --git a/src/reader.cpp b/src/reader.cpp index 33021d25e..5e80108df 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -625,7 +625,7 @@ bool reader_data_t::expand_abbreviation_as_necessary(size_t cursor_backtrack) { // lengths. size_t new_buff_pos = el->position + new_cmdline.size() - el->text.size(); - el->text.swap(new_cmdline); + el->text = std::move(new_cmdline); update_buff_pos(el, new_buff_pos); data->command_line_changed(el); result = true; diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 126269940..d39e27bcb 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -609,7 +609,7 @@ wcstring tok_first(const wcstring &str) { tokenizer_t t(str.c_str(), TOK_SQUASH_ERRORS); tok_t token; if (t.next(&token) && token.type == TOK_STRING) { - result.swap(token.text); + result = std::move(token.text); } return result; }