From 586ac3dfa7ddde84741772e76551dcdb1fbff75c Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 19 Nov 2019 13:46:47 -0800 Subject: [PATCH] [clang-tidy] Convert loops to range based Found with modernize-loop-convert Signed-off-by: Rosen Penev --- src/builtin.cpp | 8 ++++---- src/builtin_argparse.cpp | 4 ++-- src/builtin_bind.cpp | 7 +++---- src/builtin_builtin.cpp | 4 ++-- src/builtin_command.cpp | 2 +- src/builtin_complete.cpp | 17 ++++++----------- src/builtin_functions.cpp | 8 ++++---- src/builtin_read.cpp | 4 ++-- src/builtin_set.cpp | 3 +-- src/builtin_set_color.cpp | 2 +- src/builtin_string.cpp | 4 ++-- src/builtin_test.cpp | 4 ++-- src/common.cpp | 3 +-- src/complete.cpp | 4 ++-- src/env_universal_common.cpp | 4 ++-- src/event.cpp | 4 ++-- src/expand.cpp | 31 +++++++++++++++---------------- src/fish.cpp | 4 ++-- src/fish_key_reader.cpp | 4 ++-- src/highlight.cpp | 11 +++-------- src/history.cpp | 2 +- src/input.cpp | 3 +-- src/output.cpp | 3 +-- src/pager.cpp | 12 +++++------- src/reader.cpp | 16 ++++++++-------- src/screen.cpp | 17 ++++++++--------- src/wildcard.cpp | 5 ++--- 27 files changed, 85 insertions(+), 105 deletions(-) diff --git a/src/builtin.cpp b/src/builtin.cpp index f01c0a4dc..d888eff51 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -464,8 +464,8 @@ proc_status_t builtin_run(parser_t &parser, int job_pgid, wchar_t **argv, io_str wcstring_list_t builtin_get_names() { wcstring_list_t result; result.reserve(BUILTIN_COUNT); - for (size_t i = 0; i < BUILTIN_COUNT; i++) { - result.push_back(builtin_datas[i].name); + for (const auto &builtin_data : builtin_datas) { + result.push_back(builtin_data.name); } return result; } @@ -474,8 +474,8 @@ wcstring_list_t builtin_get_names() { void builtin_get_names(std::vector *list) { assert(list != nullptr); list->reserve(list->size() + BUILTIN_COUNT); - for (size_t i = 0; i < BUILTIN_COUNT; i++) { - append_completion(list, builtin_datas[i].name); + for (const auto &builtin_data : builtin_datas) { + append_completion(list, builtin_data.name); } } diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index c99ee7128..befb98722 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -671,8 +671,8 @@ static void set_argparse_result_vars(env_stack_t &vars, const argparse_cmd_opts_ // We do a simple replacement of all non alphanum chars rather than calling // escape_string(long_flag, 0, STRING_STYLE_VAR). wcstring long_flag = opt_spec->long_flag; - for (size_t pos = 0; pos < long_flag.size(); pos++) { - if (!iswalnum(long_flag[pos])) long_flag[pos] = L'_'; + for (auto &pos : long_flag) { + if (!iswalnum(pos)) pos = L'_'; } vars.set(var_name_prefix + long_flag, ENV_LOCAL, opt_spec->vals); } diff --git a/src/builtin_bind.cpp b/src/builtin_bind.cpp index e4b715d91..419688229 100644 --- a/src/builtin_bind.cpp +++ b/src/builtin_bind.cpp @@ -89,8 +89,7 @@ bool builtin_bind_t::list_one(const wcstring &seq, const wcstring &bind_mode, bo } // Now show the list of commands. - for (size_t i = 0; i < ecmds.size(); i++) { - const wcstring &ecmd = ecmds.at(i); + for (const auto &ecmd : ecmds) { const wcstring escaped_ecmd = escape_string(ecmd, ESCAPE_ALL); streams.out.push_back(' '); streams.out.append(escaped_ecmd); @@ -143,8 +142,8 @@ void builtin_bind_t::key_names(bool all, io_streams_t &streams) { void builtin_bind_t::function_names(io_streams_t &streams) { wcstring_list_t names = input_function_get_names(); - for (size_t i = 0; i < names.size(); i++) { - const wchar_t *seq = names.at(i).c_str(); + for (const auto &name : names) { + auto seq = name.c_str(); streams.out.append_format(L"%ls\n", seq); } } diff --git a/src/builtin_builtin.cpp b/src/builtin_builtin.cpp index eca1b4bd2..f04a9c32f 100644 --- a/src/builtin_builtin.cpp +++ b/src/builtin_builtin.cpp @@ -102,8 +102,8 @@ int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wcstring_list_t names = builtin_get_names(); std::sort(names.begin(), names.end()); - for (size_t i = 0; i < names.size(); i++) { - const wchar_t *el = names.at(i).c_str(); + for (const auto &name : names) { + auto el = name.c_str(); streams.out.append(el); streams.out.append(L"\n"); diff --git a/src/builtin_command.cpp b/src/builtin_command.cpp index 91cad034d..d63bdbefa 100644 --- a/src/builtin_command.cpp +++ b/src/builtin_command.cpp @@ -99,7 +99,7 @@ int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) { const wchar_t *command_name = argv[idx]; if (opts.all_paths) { wcstring_list_t paths = path_get_paths(command_name, parser.vars()); - for (auto path : paths) { + for (const auto &path : paths) { if (!opts.quiet) streams.out.append_format(L"%ls\n", path.c_str()); ++found; } diff --git a/src/builtin_complete.cpp b/src/builtin_complete.cpp index 28fb3d4f6..cbcd7f96a 100644 --- a/src/builtin_complete.cpp +++ b/src/builtin_complete.cpp @@ -291,10 +291,9 @@ int builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv) { false /* do not accept incomplete */)) { streams.err.append_format(L"%ls: Condition '%ls' contained a syntax error", cmd, condition); - for (size_t i = 0; i < errors.size(); i++) { + for (const auto &error : errors) { streams.err.append_format(L"\n%ls: ", cmd); - streams.err.append( - errors.at(i).describe(condition_string, parser.is_interactive())); + streams.err.append(error.describe(condition_string, parser.is_interactive())); } return STATUS_CMD_ERROR; } @@ -351,9 +350,7 @@ int builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv) { complete(do_complete_param, &comp, completion_request_t::fuzzy_match, parser.vars(), parser.shared()); - for (size_t i = 0; i < comp.size(); i++) { - const completion_t &next = comp.at(i); - + for (const auto &next : comp) { // Make a fake commandline, and then apply the completion to it. const wcstring faux_cmdline = token; size_t tmp_cursor = faux_cmdline.size(); @@ -414,11 +411,9 @@ int builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t **argv) { } // Handle wrap targets (probably empty). We only wrap commands, not paths. - for (size_t w = 0; w < wrap_targets.size(); w++) { - const wcstring &wrap_target = wrap_targets.at(w); - for (size_t i = 0; i < cmd_to_complete.size(); i++) { - (remove ? complete_remove_wrapper : complete_add_wrapper)(cmd_to_complete.at(i), - wrap_target); + for (const auto &wrap_target : wrap_targets) { + for (const auto &i : cmd_to_complete) { + (remove ? complete_remove_wrapper : complete_add_wrapper)(i, wrap_target); } } } diff --git a/src/builtin_functions.cpp b/src/builtin_functions.cpp index 04d684871..e901e15fe 100644 --- a/src/builtin_functions.cpp +++ b/src/builtin_functions.cpp @@ -368,14 +368,14 @@ int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) { bool is_screen = !streams.out_is_redirected && isatty(STDOUT_FILENO); if (is_screen) { wcstring buff; - for (size_t i = 0; i < names.size(); i++) { - buff.append(names.at(i)); + for (const auto &name : names) { + buff.append(name); buff.append(L", "); } streams.out.append(reformat_for_screen(buff)); } else { - for (size_t i = 0; i < names.size(); i++) { - streams.out.append(names.at(i).c_str()); + for (const auto &name : names) { + streams.out.append(name.c_str()); streams.out.append(L"\n"); } } diff --git a/src/builtin_read.cpp b/src/builtin_read.cpp index d743b3357..763ca26b3 100644 --- a/src/builtin_read.cpp +++ b/src/builtin_read.cpp @@ -516,8 +516,8 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) { } else { // Not array mode: assign each char to a separate var with the remainder being // assigned to the last var. - for (auto c = chars.begin(); c != chars.end() && vars_left(); ++c) { - vars.set_one(*var_ptr++, opts.place, *c); + for (const auto &c : chars) { + vars.set_one(*var_ptr++, opts.place, c); } } } else if (opts.array) { diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index 1798d2fc3..a179da5da 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -476,8 +476,7 @@ static int builtin_set_list(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, wcstring_list_t names = parser.vars().get_names(compute_scope(opts)); sort(names.begin(), names.end()); - for (size_t i = 0; i < names.size(); i++) { - const wcstring key = names.at(i); + for (const auto &key : names) { const wcstring e_key = escape_string(key, 0); streams.out.append(e_key); diff --git a/src/builtin_set_color.cpp b/src/builtin_set_color.cpp index 78788928c..a2fcd3ed1 100644 --- a/src/builtin_set_color.cpp +++ b/src/builtin_set_color.cpp @@ -36,7 +36,7 @@ class parser_t; static void print_colors(io_streams_t &streams) { outputter_t outp; - for (wcstring color_name : rgb_color_t::named_color_names()) { + for (const auto &color_name : rgb_color_t::named_color_names()) { if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) { rgb_color_t color = rgb_color_t(color_name); outp.set_color(color, rgb_color_t::none()); diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 9b2453305..46944e1e4 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -1100,8 +1100,8 @@ static int string_split_maybe0(parser_t &parser, io_streams_t &streams, int argc // If we are from the right, split_about gave us reversed strings, in reversed order! if (opts.right) { - for (size_t j = 0; j < splits.size(); j++) { - std::reverse(splits[j].begin(), splits[j].end()); + for (auto &split : splits) { + std::reverse(split.begin(), split.end()); } std::reverse(splits.begin(), splits.end()); } diff --git a/src/builtin_test.cpp b/src/builtin_test.cpp index 52d552a28..a3c9b640d 100644 --- a/src/builtin_test.cpp +++ b/src/builtin_test.cpp @@ -864,8 +864,8 @@ int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) { bool result = expr->evaluate(eval_errors); if (!eval_errors.empty()) { if (!should_suppress_stderr_for_tests()) { - for (size_t i = 0; i < eval_errors.size(); i++) { - streams.err.append_format(L"%ls\n", eval_errors.at(i).c_str()); + for (const auto &eval_error : eval_errors) { + streams.err.append_format(L"%ls\n", eval_error.c_str()); } // Add a backtrace but not the "see help" message // because this isn't about passing the wrong options. diff --git a/src/common.cpp b/src/common.cpp index 9fa1b5586..ef6a6a0ea 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -410,8 +410,7 @@ std::string wcs2string(const wcstring &input) { mbstate_t state = {}; char converted[MB_LEN_MAX]; - for (size_t i = 0; i < input.size(); i++) { - wchar_t wc = input[i]; + for (auto wc : input) { if (wc == INTERNAL_SEPARATOR) { ; // do nothing } else if (wc >= ENCODE_DIRECT_BASE && wc < ENCODE_DIRECT_BASE + 256) { diff --git a/src/complete.cpp b/src/complete.cpp index bed7593ac..dfb242619 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -269,8 +269,8 @@ void completions_sort_and_prioritize(std::vector *comps, completion_request_flags_t flags) { // Find the best match type. fuzzy_match_type_t best_type = fuzzy_match_none; - for (size_t i = 0; i < comps->size(); i++) { - best_type = std::min(best_type, comps->at(i).match.type); + for (const auto &comp : *comps) { + best_type = std::min(best_type, comp.match.type); } // If the best type is an exact match, reduce it to prefix match. Otherwise a tab completion // will only show one match if it matches a file exactly. (see issue #959). diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index d6933c080..5e71a0248 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -1041,8 +1041,8 @@ static wcstring get_machine_identifier() { unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {}; if (get_mac_address(mac_addr)) { result.reserve(2 * MAC_ADDRESS_MAX_LEN); - for (size_t i = 0; i < MAC_ADDRESS_MAX_LEN; i++) { - append_format(result, L"%02x", mac_addr[i]); + for (auto i : mac_addr) { + append_format(result, L"%02x", i); } } else if (!get_hostname_identifier(result)) { result.assign(L"nohost"); // fallback to a dummy value diff --git a/src/event.cpp b/src/event.cpp index 69f41a3aa..b78da1f15 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -192,8 +192,8 @@ wcstring event_get_desc(const event_t &evt) { #if 0 static void show_all_handlers(void) { std::fwprintf(stdout, L"event handlers:\n"); - for (event_list_t::const_iterator iter = events.begin(); iter != events.end(); ++iter) { - const event_t *foo = *iter; + for (const auto& event : events) { + auto foo = event; wcstring tmp = event_get_desc(foo); std::fwprintf(stdout, L" handler now %ls\n", tmp.c_str()); } diff --git a/src/expand.cpp b/src/expand.cpp index 1f4563337..37583ddb6 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -109,7 +109,7 @@ static void append_cmdsub_error(parse_error_list_t *errors, size_t source_start, error.text = vformat_string(fmt, va); va_end(va); - for (auto it : *errors) { + for (const auto &it : *errors) { if (error.text == it.text) return; } @@ -757,8 +757,8 @@ static void unexpand_tildes(const wcstring &input, const environment_t &vars, // We only operate on completions that replace their contents. If we don't have any, we're done. // In particular, empty vectors are common. bool has_candidate_completion = false; - for (size_t i = 0; i < completions->size(); i++) { - if (completions->at(i).flags & COMPLETE_REPLACES_TOKEN) { + for (const auto &completion : *completions) { + if (completion.flags & COMPLETE_REPLACES_TOKEN) { has_candidate_completion = true; break; } @@ -774,8 +774,7 @@ static void unexpand_tildes(const wcstring &input, const environment_t &vars, expand_tilde(home, vars); // Now for each completion that starts with home, replace it with the username_with_tilde. - for (size_t i = 0; i < completions->size(); i++) { - completion_t &comp = completions->at(i); + for (auto &comp : *completions) { if ((comp.flags & COMPLETE_REPLACES_TOKEN) && string_prefixes_string(home, comp.completion)) { comp.completion.replace(0, home.size(), username_with_tilde); @@ -817,15 +816,15 @@ static void remove_internal_separator(wcstring *str, bool conv) { // If conv is true, replace all instances of ANY_STRING with '*', // ANY_STRING_RECURSIVE with '*'. if (conv) { - for (size_t idx = 0; idx < str->size(); idx++) { - switch (str->at(idx)) { + for (auto &idx : *str) { + switch (idx) { case ANY_CHAR: { - str->at(idx) = L'?'; + idx = L'?'; break; } case ANY_STRING: case ANY_STRING_RECURSIVE: { - str->at(idx) = L'*'; + idx = L'*'; break; } default: { @@ -903,9 +902,9 @@ expand_result_t expander_t::stage_variables(wcstring input, std::vector expanded; - for (size_t wd_idx = 0; wd_idx < effective_working_dirs.size(); wd_idx++) { - int local_wc_res = wildcard_expand_string( - path_to_expand, effective_working_dirs.at(wd_idx), flags, &expanded); + for (const auto &effective_working_dir : effective_working_dirs) { + int local_wc_res = + wildcard_expand_string(path_to_expand, effective_working_dir, flags, &expanded); if (local_wc_res > 0) { // Something matched,so overall we matched. result = expand_result_t::wildcard_match; diff --git a/src/fish.cpp b/src/fish.cpp index 2a2fc964c..068358a77 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -252,8 +252,8 @@ int run_command_list(std::vector *cmds, const io_chain_t &io) { int res = 1; parser_t &parser = parser_t::principal_parser(); - for (size_t i = 0; i < cmds->size(); i++) { - const wcstring cmd_wcs = str2wcstring(cmds->at(i)); + for (const auto &cmd : *cmds) { + const wcstring cmd_wcs = str2wcstring(cmd); res = parser.eval(cmd_wcs, io, TOP); } diff --git a/src/fish_key_reader.cpp b/src/fish_key_reader.cpp index 18d33a940..2f127a81b 100644 --- a/src/fish_key_reader.cpp +++ b/src/fish_key_reader.cpp @@ -163,8 +163,8 @@ static void add_char_to_bind_command(wchar_t wc, std::vector &bind_char static void output_bind_command(std::vector &bind_chars) { if (!bind_chars.empty()) { std::fputws(L"bind ", stdout); - for (size_t i = 0; i < bind_chars.size(); i++) { - std::fputws(char_to_symbol(bind_chars[i], true), stdout); + for (auto bind_char : bind_chars) { + std::fputws(char_to_symbol(bind_char, true), stdout); } std::fputws(L" 'do something'\n", stdout); bind_chars.clear(); diff --git a/src/highlight.cpp b/src/highlight.cpp index c9375fb28..3f354f090 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -215,8 +215,7 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l // debug( 1, L"%ls -> %ls ->%ls", path, tilde, unescaped ); - for (size_t i = 0; i < path_with_magic.size(); i++) { - wchar_t c = path_with_magic.at(i); + for (auto c : path_with_magic) { switch (c) { case PROCESS_EXPAND_SELF: case VARIABLE_EXPAND: @@ -385,8 +384,7 @@ rgb_color_t highlight_get_color(const highlight_spec_t &highlight, bool is_backg static bool has_expand_reserved(const wcstring &str) { bool result = false; - for (size_t i = 0; i < str.size(); i++) { - wchar_t wc = str.at(i); + for (auto wc : str) { if (wc >= EXPAND_RESERVED_BASE && wc <= EXPAND_RESERVED_END) { result = true; break; @@ -1290,10 +1288,7 @@ highlighter_t::color_array_t highlighter_t::highlight() { } // If the cursor is over an argument, and that argument is a valid path, underline it. - for (parse_node_tree_t::const_iterator iter = parse_tree.begin(); iter != parse_tree.end(); - ++iter) { - const parse_node_t &node = *iter; - + for (const auto &node : parse_tree) { // Must be an argument with source. if (node.type != symbol_argument || !node.has_source()) continue; diff --git a/src/history.cpp b/src/history.cpp index 911d0d577..c4dd8a02c 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -709,7 +709,7 @@ bool history_impl_t::rewrite_to_temporary_file(int existing_fd, int dst_fd) cons int err = 0; std::string buffer; buffer.reserve(HISTORY_OUTPUT_BUFFER_SIZE + 128); - for (const auto &key_item : lru) { + for (const auto key_item : lru) { append_history_item_to_buffer(key_item.second, &buffer); err = flush_to_fd(&buffer, dst_fd, HISTORY_OUTPUT_BUFFER_SIZE); if (err) break; diff --git a/src/input.cpp b/src/input.cpp index c98600cff..404cbce71 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -543,8 +543,7 @@ std::vector input_mapping_set_t::get_names(bool user) cons std::vector result; result.reserve(local_list.size()); - for (size_t i = 0; i < local_list.size(); i++) { - const input_mapping_t &m = local_list.at(i); + for (const auto &m : local_list) { result.push_back((input_mapping_name_t){m.seq, m.mode}); } return result; diff --git a/src/output.cpp b/src/output.cpp index 75aa8bb43..69ce26297 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -435,8 +435,7 @@ rgb_color_t best_color(const std::vector &candidates, color_support } rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none(); - for (size_t i = 0; i < candidates.size(); i++) { - const rgb_color_t &color = candidates.at(i); + for (const auto &color : candidates) { if (first_rgb.is_none() && color.is_rgb()) { first_rgb = color; } diff --git a/src/pager.cpp b/src/pager.cpp index d36a908e6..51e96df39 100644 --- a/src/pager.cpp +++ b/src/pager.cpp @@ -324,8 +324,8 @@ static comp_info_list_t process_completions_into_infos(const completion_list_t & void pager_t::measure_completion_infos(comp_info_list_t *infos, const wcstring &prefix) const { size_t prefix_len = fish_wcswidth(prefix); - for (size_t i = 0; i < infos->size(); i++) { - comp_t *comp = &infos->at(i); + for (auto &info : *infos) { + comp_t *comp = &info; const wcstring_list_t &comp_strings = comp->comp; for (size_t j = 0; j < comp_strings.size(); j++) { @@ -359,9 +359,8 @@ bool pager_t::completion_info_passes_filter(const comp_t &info) const { } // Match against the completion strings. - for (size_t i = 0; i < info.comp.size(); i++) { - if (string_fuzzy_match_string(needle, prefix + info.comp.at(i), limit).type != - fuzzy_match_none) { + for (const auto &i : info.comp) { + if (string_fuzzy_match_string(needle, prefix + i, limit).type != fuzzy_match_none) { return true; } } @@ -372,8 +371,7 @@ bool pager_t::completion_info_passes_filter(const comp_t &info) const { // Update completion_infos from unfiltered_completion_infos, to reflect the filter. void pager_t::refilter_completions() { this->completion_infos.clear(); - for (size_t i = 0; i < this->unfiltered_completion_infos.size(); i++) { - const comp_t &info = this->unfiltered_completion_infos.at(i); + for (const auto &info : this->unfiltered_completion_infos) { if (this->completion_info_passes_filter(info)) { this->completion_infos.push_back(info); } diff --git a/src/reader.cpp b/src/reader.cpp index 4b4157eec..ebfe0c1ba 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -877,8 +877,8 @@ void reader_write_title(const wcstring &cmd, parser_t &parser, bool reset_cursor if (exec_subshell(fish_title_command, parser, lst, false /* ignore exit status */) != -1 && !lst.empty()) { std::fputws(L"\x1B]0;", stdout); - for (size_t i = 0; i < lst.size(); i++) { - std::fputws(lst.at(i).c_str(), stdout); + for (const auto &i : lst) { + std::fputws(i.c_str(), stdout); } ignore_result(write(STDOUT_FILENO, "\a", 1)); } @@ -897,8 +897,8 @@ void reader_data_t::exec_mode_prompt() { exec_subshell(MODE_PROMPT_FUNCTION_NAME, parser(), mode_indicator_list, false); // We do not support multiple lines in the mode indicator, so just concatenate all of // them. - for (size_t i = 0; i < mode_indicator_list.size(); i++) { - mode_prompt_buff += mode_indicator_list.at(i); + for (const auto &i : mode_indicator_list) { + mode_prompt_buff += i; } } } @@ -939,9 +939,9 @@ void reader_data_t::exec_prompt() { wcstring_list_t prompt_list; // Status is ignored. exec_subshell(right_prompt, parser(), prompt_list, apply_exit_status); - for (size_t i = 0; i < prompt_list.size(); i++) { + for (const auto &i : prompt_list) { // Right prompt does not support multiple lines, so just concatenate all of them. - right_prompt_buff += prompt_list.at(i); + right_prompt_buff += i; } } } @@ -1470,8 +1470,8 @@ static bool reader_can_replace(const wcstring &in, int flags) { /// Determine the best match type for a set of completions. static fuzzy_match_type_t get_best_match_type(const std::vector &comp) { fuzzy_match_type_t best_type = fuzzy_match_none; - for (size_t i = 0; i < comp.size(); i++) { - best_type = std::min(best_type, comp.at(i).match.type); + for (const auto &i : comp) { + best_type = std::min(best_type, i.match.type); } // If the best type is an exact match, reduce it to prefix match. Otherwise a tab completion // will only show one match if it matches a file exactly. (see issue #959). diff --git a/src/screen.cpp b/src/screen.cpp index d441e88d1..d665e856d 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -201,11 +201,11 @@ static bool is_color_escape_seq(const wchar_t *code, size_t *resulting_length) { set_background, }; - for (size_t p = 0; p < sizeof esc / sizeof *esc; p++) { - if (!esc[p]) continue; + for (auto p : esc) { + if (!p) continue; for (int k = 0; k < max_colors; k++) { - size_t esc_seq_len = try_sequence(tparm(const_cast(esc[p]), k), code); + size_t esc_seq_len = try_sequence(tparm(const_cast(p), k), code); if (esc_seq_len) { *resulting_length = esc_seq_len; return true; @@ -227,12 +227,12 @@ static bool is_visual_escape_seq(const wchar_t *code, size_t *resulting_length) exit_shadow_mode, enter_standout_mode, exit_standout_mode, enter_secure_mode, enter_dim_mode, enter_blink_mode, enter_alt_charset_mode, exit_alt_charset_mode}; - for (size_t p = 0; p < sizeof esc2 / sizeof *esc2; p++) { - if (!esc2[p]) continue; + for (auto p : esc2) { + if (!p) continue; // Test both padded and unpadded version, just to be safe. Most versions of tparm don't // actually seem to do anything these days. - size_t esc_seq_len = std::max(try_sequence(tparm(const_cast(esc2[p])), code), - try_sequence(esc2[p], code)); + size_t esc_seq_len = + std::max(try_sequence(tparm(const_cast(p)), code), try_sequence(p, code)); if (esc_seq_len) { *resulting_length = esc_seq_len; return true; @@ -892,8 +892,7 @@ static screen_layout_t compute_layout(screen_t *s, size_t screen_width, // Get the width of the first line, and if there is more than one line. bool multiline = false; size_t first_line_width = 0; - for (size_t i = 0; i < commandline.size(); i++) { - wchar_t c = commandline.at(i); + for (auto c : commandline) { if (c == L'\n') { multiline = true; break; diff --git a/src/wildcard.cpp b/src/wildcard.cpp index da31fcd0c..58b8c00e7 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -637,9 +637,8 @@ class wildcard_expander_t { assert(resolved_completions != nullptr); // Insert initial completions into our set to avoid duplicates. - for (std::vector::const_iterator iter = resolved_completions->begin(); - iter != resolved_completions->end(); ++iter) { - this->completion_set.insert(iter->completion); + for (const auto &resolved_completion : *resolved_completions) { + this->completion_set.insert(resolved_completion.completion); } }