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);
}
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).
wcstring long_flag = opt_spec->long_flag;
for (auto &pos : long_flag) {
if (!iswalnum(pos)) pos = L'_';
}
std::replace_if(long_flag.begin(), long_flag.end(), std::iswpunct, L'_');
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;
}
if (split->indexes.empty()) {
// No indexes, just increment if our variable is missing.
if (!split->var) retval++;
} else {
if (split->indexes.empty() && !split->var) retval++; // No indicies, increment if missing
else {
// Increment for every index out of range.
long varsize = split->varsize();
for (long idx : split->indexes) {
if (idx < 1 || idx > varsize) retval++;
}
retval += std::count_if(split->indexes.begin(), split->indexes.end(),
[varsize](long i) { return (i < 1 || i > varsize); });
}
}

View file

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