Revert "Replace some simple loops with STL algorithms"

This commit was problematic for a few reasons:

1. It silently changed the behavior of argparse, by switching which
characters were replaced with `_` from non-alphanumeric to punctuation.
This is a potentially breaking change and there doesn't appear to be any
justification for it.

2. It combines a one-line if with a multi-line else which we should try
   to avoid.

This reverts commit 63bd4eda55.
This reverts commit 4f835a0f0f.
This commit is contained in:
ridiculousfish 2022-04-09 12:05:19 -07:00
parent 735962d306
commit 1c7b934402
3 changed files with 16 additions and 10 deletions

View file

@ -692,10 +692,12 @@ 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 punctuation chars rather than calling
// 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;
std::replace_if(long_flag.begin(), long_flag.end(), iswpunct, L'_');
for (auto &pos : long_flag) {
if (!iswalnum(pos)) pos = L'_';
}
vars.set(var_name_prefix + long_flag, ENV_LOCAL, opt_spec->vals);
}
}

View file

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

View file

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