Make contains() more general

Allow contains() to find arbitrary values in arbitrary vectors, and adopt it
in place of std::find.
This commit is contained in:
ridiculousfish
2018-08-11 19:55:06 -07:00
parent b0dc715d18
commit 1473f952d4
9 changed files with 15 additions and 19 deletions

View File

@@ -75,8 +75,7 @@ static int check_for_mutually_exclusive_flags(const argparse_cmd_opts_t &opts, i
// We saw this option at least once. Check all the sets of mutually exclusive options to see
// if this option appears in any of them.
for (const auto &xarg_set : opts.exclusive_flag_sets) {
auto found = std::find(xarg_set.begin(), xarg_set.end(), opt_spec->short_flag);
if (found != xarg_set.end()) {
if (contains(xarg_set, opt_spec->short_flag)) {
// Okay, this option is in a mutually exclusive set of options. Check if any of the
// other mutually exclusive options have been seen.
for (const auto &xflag : xarg_set) {

View File

@@ -151,7 +151,7 @@ static bool find_job_by_name(const wchar_t *proc, std::vector<job_id_t> &ids) {
if (j->command_is_empty()) continue;
if (match_pid(j->command(), proc)) {
if (std::find(ids.begin(), ids.end(), j->job_id) == ids.end()) {
if (!contains(ids, j->job_id)) {
// If pids doesn't already have the pgid, add it.
ids.push_back(j->job_id);
}
@@ -163,7 +163,7 @@ static bool find_job_by_name(const wchar_t *proc, std::vector<job_id_t> &ids) {
if (p->actual_cmd.empty()) continue;
if (match_pid(p->actual_cmd, proc)) {
if (std::find(ids.begin(), ids.end(), j->job_id) == ids.end()) {
if (!contains(ids, j->job_id)) {
// If pids doesn't already have the pgid, add it.
ids.push_back(j->job_id);
}

View File

@@ -1842,10 +1842,6 @@ int string_fuzzy_match_t::compare(const string_fuzzy_match_t &rhs) const {
return 0; // equal
}
bool contains(const wcstring_list_t &list, const wcstring &str) {
return std::find(list.begin(), list.end(), str) != list.end();
}
wcstring_list_t split_string(const wcstring &val, wchar_t sep) {
wcstring_list_t out;
size_t pos = 0, end = val.size();

View File

@@ -17,6 +17,7 @@
#include <sys/ioctl.h> // IWYU pragma: keep
#endif
#include <algorithm>
#include <memory>
#include <mutex>
#include <sstream>
@@ -283,8 +284,11 @@ inline bool is_whitespace(const wchar_t *input) { return is_whitespace(wcstring(
/// See https://developer.gnome.org/glib/stable/glib-I18N.html#N-:CAPS
#define N_(wstr) wstr
/// Test if a list of stirngs contains a particular string.
bool contains(const wcstring_list_t &list, const wcstring &str);
/// Test if a vector contains a value.
template <typename T1, typename T2>
bool contains(const std::vector<T1> &vec, const T2 &val) {
return std::find(vec.begin(), vec.end(), val) != vec.end();
}
/// Print a stack trace to stderr.
void show_stackframe(const wchar_t msg_level, int frame_count = 100, int skip_levels = 0);

View File

@@ -1672,7 +1672,7 @@ bool complete_add_wrapper(const wcstring &command, const wcstring &new_target) {
wrapper_map_t &wraps = wrap_map();
wcstring_list_t *targets = &wraps[command];
// If it's already present, we do nothing.
if (std::find(targets->begin(), targets->end(), new_target) == targets->end()) {
if (!contains(*targets, new_target)) {
targets->push_back(new_target);
}
return true;
@@ -1689,8 +1689,7 @@ bool complete_remove_wrapper(const wcstring &command, const wcstring &target_to_
wrapper_map_t::iterator current_targets_iter = wraps.find(command);
if (current_targets_iter != wraps.end()) {
wcstring_list_t *targets = &current_targets_iter->second;
wcstring_list_t::iterator where =
std::find(targets->begin(), targets->end(), target_to_remove);
auto where = std::find(targets->begin(), targets->end(), target_to_remove);
if (where != targets->end()) {
targets->erase(where);
result = true;

View File

@@ -370,7 +370,7 @@ static void fix_colon_delimited_var(const wcstring &var_name) {
// See if there's any empties.
const wcstring empty = wcstring();
const wcstring_list_t &strs = paths->as_list();
if (std::find(strs.begin(), strs.end(), empty) != strs.end()) {
if (contains(strs, empty)) {
// Copy the list and replace empties with L"."
wcstring_list_t newstrs = strs;
std::replace(newstrs.begin(), newstrs.end(), empty, wcstring(L"."));

View File

@@ -338,8 +338,7 @@ static void event_fire_internal(const event_t &event) {
// Iterate over our list of matching events.
for (shared_ptr<event_t> &criterion : fire) {
// Only fire if this event is still present
if (std::find(s_event_handlers.begin(), s_event_handlers.end(), criterion) ==
s_event_handlers.end()) {
if (!contains(s_event_handlers, criterion)) {
continue;
}

View File

@@ -26,7 +26,7 @@ void kill_add(const wcstring &str) {
/// Remove first match for specified string from circular list.
static void kill_remove(const wcstring &s) {
ASSERT_IS_MAIN_THREAD();
kill_list_t::iterator iter = std::find(kill_list.begin(), kill_list.end(), s);
auto iter = std::find(kill_list.begin(), kill_list.end(), s);
if (iter != kill_list.end()) kill_list.erase(iter);
}

View File

@@ -167,8 +167,7 @@ job_id_t acquire_job_id() {
std::vector<bool> &consumed_job_ids = locker.value;
// Find the index of the first 0 slot.
std::vector<bool>::iterator slot =
std::find(consumed_job_ids.begin(), consumed_job_ids.end(), false);
auto slot = std::find(consumed_job_ids.begin(), consumed_job_ids.end(), false);
if (slot != consumed_job_ids.end()) {
// We found a slot. Note that slot 0 corresponds to job ID 1.
*slot = true;