From 52e900690b31aee0bba436fdbd328bdeb5945493 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 2 Nov 2019 18:56:16 -0700 Subject: [PATCH] Make history_search_type_t an enum class --- src/builtin_history.cpp | 12 ++++++------ src/fish_tests.cpp | 10 +++++----- src/history.cpp | 10 +++++----- src/history.h | 16 ++++++++-------- src/reader.cpp | 5 +++-- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/builtin_history.cpp b/src/builtin_history.cpp index adfac2063..13040d3cf 100644 --- a/src/builtin_history.cpp +++ b/src/builtin_history.cpp @@ -141,17 +141,17 @@ static int parse_cmd_opts(history_cmd_opts_t &opts, int *optind, //!OCLINT(high break; } case 'p': { - opts.search_type = HISTORY_SEARCH_TYPE_PREFIX_GLOB; + opts.search_type = history_search_type_t::prefix_glob; opts.history_search_type_defined = true; break; } case 'c': { - opts.search_type = HISTORY_SEARCH_TYPE_CONTAINS_GLOB; + opts.search_type = history_search_type_t::contains_glob; opts.history_search_type_defined = true; break; } case 'e': { - opts.search_type = HISTORY_SEARCH_TYPE_EXACT; + opts.search_type = history_search_type_t::exact; opts.history_search_type_defined = true; break; } @@ -242,8 +242,8 @@ int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // Establish appropriate defaults. if (opts.hist_cmd == HIST_UNDEF) opts.hist_cmd = HIST_SEARCH; if (!opts.history_search_type_defined) { - if (opts.hist_cmd == HIST_SEARCH) opts.search_type = HISTORY_SEARCH_TYPE_CONTAINS_GLOB; - if (opts.hist_cmd == HIST_DELETE) opts.search_type = HISTORY_SEARCH_TYPE_EXACT; + if (opts.hist_cmd == HIST_SEARCH) opts.search_type = history_search_type_t::contains_glob; + if (opts.hist_cmd == HIST_DELETE) opts.search_type = history_search_type_t::exact; } int status = STATUS_CMD_OK; @@ -259,7 +259,7 @@ int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // TODO: Move this code to the history module and support the other search types // including case-insensitive matches. At this time we expect the non-exact deletions to // be handled only by the history function's interactive delete feature. - if (opts.search_type != HISTORY_SEARCH_TYPE_EXACT) { + if (opts.search_type != history_search_type_t::exact) { streams.err.append_format(_(L"builtin history delete only supports --exact\n")); status = STATUS_INVALID_ARGS; break; diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index d53eb3cb1..8db4808b3 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -3593,7 +3593,7 @@ void history_tests_t::test_history() { test_history_matches(searcher, expected, __LINE__); // Items matching "alpha", case-insensitive. - searcher = history_search_t(history, L"AlPhA", HISTORY_SEARCH_TYPE_CONTAINS, nocase); + searcher = history_search_t(history, L"AlPhA", history_search_type_t::contains, nocase); set_expected([](const wcstring &s) { return wcstolower(s).find(L"alpha") != wcstring::npos; }); test_history_matches(searcher, expected, __LINE__); @@ -3603,23 +3603,23 @@ void history_tests_t::test_history() { test_history_matches(searcher, expected, __LINE__); // Items starting with "be", case-sensitive. - searcher = history_search_t(history, L"be", HISTORY_SEARCH_TYPE_PREFIX, 0); + searcher = history_search_t(history, L"be", history_search_type_t::prefix, 0); set_expected([](const wcstring &s) { return string_prefixes_string(L"be", s); }); test_history_matches(searcher, expected, __LINE__); // Items starting with "be", case-insensitive. - searcher = history_search_t(history, L"be", HISTORY_SEARCH_TYPE_PREFIX, nocase); + searcher = history_search_t(history, L"be", history_search_type_t::prefix, nocase); set_expected( [](const wcstring &s) { return string_prefixes_string_case_insensitive(L"be", s); }); test_history_matches(searcher, expected, __LINE__); // Items exactly matching "alph", case-sensitive. - searcher = history_search_t(history, L"alph", HISTORY_SEARCH_TYPE_EXACT, 0); + searcher = history_search_t(history, L"alph", history_search_type_t::exact, 0); set_expected([](const wcstring &s) { return s == L"alph"; }); test_history_matches(searcher, expected, __LINE__); // Items exactly matching "alph", case-insensitive. - searcher = history_search_t(history, L"alph", HISTORY_SEARCH_TYPE_EXACT, nocase); + searcher = history_search_t(history, L"alph", history_search_type_t::exact, nocase); set_expected([](const wcstring &s) { return wcstolower(s) == L"alph"; }); test_history_matches(searcher, expected, __LINE__); diff --git a/src/history.cpp b/src/history.cpp index 88bbc7e58..120a89848 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -186,22 +186,22 @@ bool history_item_t::matches_search(const wcstring &term, enum history_search_ty const wcstring &content_to_match = case_sensitive ? contents : contents_lower; switch (type) { - case HISTORY_SEARCH_TYPE_EXACT: { + case history_search_type_t::exact: { return term == content_to_match; } - case HISTORY_SEARCH_TYPE_CONTAINS: { + case history_search_type_t::contains: { return content_to_match.find(term) != wcstring::npos; } - case HISTORY_SEARCH_TYPE_PREFIX: { + case history_search_type_t::prefix: { return string_prefixes_string(term, content_to_match); } - case HISTORY_SEARCH_TYPE_CONTAINS_GLOB: { + case history_search_type_t::contains_glob: { wcstring wcpattern1 = parse_util_unescape_wildcards(term); if (wcpattern1.front() != ANY_STRING) wcpattern1.insert(0, 1, ANY_STRING); if (wcpattern1.back() != ANY_STRING) wcpattern1.push_back(ANY_STRING); return wildcard_match(content_to_match, wcpattern1); } - case HISTORY_SEARCH_TYPE_PREFIX_GLOB: { + case history_search_type_t::prefix_glob: { wcstring wcpattern2 = parse_util_unescape_wildcards(term); if (wcpattern2.back() != ANY_STRING) wcpattern2.push_back(ANY_STRING); return wildcard_match(content_to_match, wcpattern2); diff --git a/src/history.h b/src/history.h index 9a5d1b81b..8711c0015 100644 --- a/src/history.h +++ b/src/history.h @@ -44,17 +44,17 @@ class environment_t; typedef std::vector path_list_t; -enum history_search_type_t { +enum class history_search_type_t { // Search for commands exactly matching the given string. - HISTORY_SEARCH_TYPE_EXACT = 1, + exact, // Search for commands containing the given string. - HISTORY_SEARCH_TYPE_CONTAINS, + contains, // Search for commands starting with the given string. - HISTORY_SEARCH_TYPE_PREFIX, + prefix, // Search for commands containing the given glob pattern. - HISTORY_SEARCH_TYPE_CONTAINS_GLOB, + contains_glob, // Search for commands starting with the given glob pattern. - HISTORY_SEARCH_TYPE_PREFIX_GLOB + prefix_glob, }; typedef uint64_t history_identifier_t; @@ -229,7 +229,7 @@ class history_search_t { wcstring canon_term_; // Our search type. - enum history_search_type_t search_type_ { HISTORY_SEARCH_TYPE_CONTAINS }; + enum history_search_type_t search_type_ { history_search_type_t::contains }; // Our flags. history_search_flags_t flags_{0}; @@ -264,7 +264,7 @@ class history_search_t { // Constructor. history_search_t(history_t &hist, const wcstring &str, - enum history_search_type_t type = HISTORY_SEARCH_TYPE_CONTAINS, + enum history_search_type_t type = history_search_type_t::contains, history_search_flags_t flags = 0) : history_(&hist), orig_term_(str), canon_term_(str), search_type_(type), flags_(flags) { if (ignores_case()) { diff --git a/src/reader.cpp b/src/reader.cpp index d27d52866..1d535278a 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -297,7 +297,8 @@ class reader_history_search_t { mode_ = mode; // We can skip dedup in history_search_t because we do it ourselves in skips_. search_ = history_search_t( - *hist, text, by_prefix() ? HISTORY_SEARCH_TYPE_PREFIX : HISTORY_SEARCH_TYPE_CONTAINS, + *hist, text, + by_prefix() ? history_search_type_t::prefix : history_search_type_t::contains, history_search_no_dedup); } @@ -1298,7 +1299,7 @@ static std::function get_autosuggestion_performer return nothing; } - history_search_t searcher(*history, search_string, HISTORY_SEARCH_TYPE_PREFIX); + history_search_t searcher(*history, search_string, history_search_type_t::prefix); while (!reader_test_should_cancel() && searcher.go_backwards()) { history_item_t item = searcher.current_item();