From 1c7b93440227b7bdb4935c4b7fa8972b6119f96a Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 9 Apr 2022 12:05:19 -0700 Subject: [PATCH] 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 63bd4eda557f91505c6b690c3e8aef554dfcfe98. This reverts commit 4f835a0f0fb65f76dc06c03b4bb4c53544adef95. --- src/builtins/argparse.cpp | 6 ++++-- src/builtins/set.cpp | 11 +++++++---- src/topic_monitor.h | 9 +++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/builtins/argparse.cpp b/src/builtins/argparse.cpp index 10bd1dd33..996d1466a 100644 --- a/src/builtins/argparse.cpp +++ b/src/builtins/argparse.cpp @@ -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); } } diff --git a/src/builtins/set.cpp b/src/builtins/set.cpp index ad6384f58..0f72673d3 100644 --- a/src/builtins/set.cpp +++ b/src/builtins/set.cpp @@ -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++; + } } } diff --git a/src/topic_monitor.h b/src/topic_monitor.h index 4baa2e977..cacb8564f 100644 --- a/src/topic_monitor.h +++ b/src/topic_monitor.h @@ -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 {