Replace some simple loops with STL algorithms

src/builtins/argparce.cpp: replace_if
src/builtins/set.cpp: count_if
src/topic_monitor.h: any_of
This commit is contained in:
Aaron Gyes 2022-04-08 17:49:57 -07:00
parent 7d1d43744a
commit 4f835a0f0f
3 changed files with 10 additions and 16 deletions

View file

@ -692,12 +692,10 @@ static void set_argparse_result_vars(env_stack_t &vars, const argparse_cmd_opts_
vars.set(var_name_prefix + opt_spec->short_flag, ENV_LOCAL, opt_spec->vals); vars.set(var_name_prefix + opt_spec->short_flag, ENV_LOCAL, opt_spec->vals);
} }
if (!opt_spec->long_flag.empty()) { if (!opt_spec->long_flag.empty()) {
// We do a simple replacement of all non alphanum chars rather than calling // We do a simple replacement of punctuation chars rather than calling
// escape_string(long_flag, 0, STRING_STYLE_VAR). // escape_string(long_flag, 0, STRING_STYLE_VAR).
wcstring long_flag = opt_spec->long_flag; wcstring long_flag = opt_spec->long_flag;
for (auto &pos : long_flag) { std::replace_if(long_flag.begin(), long_flag.end(), std::iswpunct, L'_');
if (!iswalnum(pos)) pos = L'_';
}
vars.set(var_name_prefix + long_flag, ENV_LOCAL, opt_spec->vals); vars.set(var_name_prefix + long_flag, ENV_LOCAL, opt_spec->vals);
} }
} }

View file

@ -484,15 +484,12 @@ static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc,
return STATUS_CMD_ERROR; return STATUS_CMD_ERROR;
} }
if (split->indexes.empty()) { if (split->indexes.empty() && !split->var) retval++; // No indicies, increment if missing
// No indexes, just increment if our variable is missing. else {
if (!split->var) retval++;
} else {
// Increment for every index out of range. // Increment for every index out of range.
long varsize = split->varsize(); long varsize = split->varsize();
for (long idx : split->indexes) { retval += std::count_if(split->indexes.begin(), split->indexes.end(),
if (idx < 1 || idx > varsize) retval++; [varsize](long i) { return (i < 1 || i > varsize); });
}
} }
} }

View file

@ -102,11 +102,10 @@ class generation_list_t {
/// \return whether any topic is valid. /// \return whether any topic is valid.
bool any_valid() const { bool any_valid() const {
bool valid = false; auto arr = as_array();
for (auto gen : as_array()) { return std::any_of(arr.cbegin(), arr.cend(), [](generation_t gen) {
if (gen != invalid_generation) valid = true; return gen != invalid_generation;
} });
return valid;
} }
bool operator==(const generation_list_t &rhs) const { bool operator==(const generation_list_t &rhs) const {