Make history_search_type_t an enum class

This commit is contained in:
ridiculousfish 2019-11-02 18:56:16 -07:00
parent be35b858c5
commit 52e900690b
5 changed files with 27 additions and 26 deletions

View File

@ -141,17 +141,17 @@ static int parse_cmd_opts(history_cmd_opts_t &opts, int *optind, //!OCLINT(high
break; break;
} }
case 'p': { 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; opts.history_search_type_defined = true;
break; break;
} }
case 'c': { 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; opts.history_search_type_defined = true;
break; break;
} }
case 'e': { case 'e': {
opts.search_type = HISTORY_SEARCH_TYPE_EXACT; opts.search_type = history_search_type_t::exact;
opts.history_search_type_defined = true; opts.history_search_type_defined = true;
break; break;
} }
@ -242,8 +242,8 @@ int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// Establish appropriate defaults. // Establish appropriate defaults.
if (opts.hist_cmd == HIST_UNDEF) opts.hist_cmd = HIST_SEARCH; if (opts.hist_cmd == HIST_UNDEF) opts.hist_cmd = HIST_SEARCH;
if (!opts.history_search_type_defined) { 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_SEARCH) opts.search_type = history_search_type_t::contains_glob;
if (opts.hist_cmd == HIST_DELETE) opts.search_type = HISTORY_SEARCH_TYPE_EXACT; if (opts.hist_cmd == HIST_DELETE) opts.search_type = history_search_type_t::exact;
} }
int status = STATUS_CMD_OK; 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 // 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 // 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. // 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")); streams.err.append_format(_(L"builtin history delete only supports --exact\n"));
status = STATUS_INVALID_ARGS; status = STATUS_INVALID_ARGS;
break; break;

View File

@ -3593,7 +3593,7 @@ void history_tests_t::test_history() {
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);
// Items matching "alpha", case-insensitive. // 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; }); set_expected([](const wcstring &s) { return wcstolower(s).find(L"alpha") != wcstring::npos; });
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);
@ -3603,23 +3603,23 @@ void history_tests_t::test_history() {
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);
// Items starting with "be", case-sensitive. // 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); }); set_expected([](const wcstring &s) { return string_prefixes_string(L"be", s); });
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);
// Items starting with "be", case-insensitive. // 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( set_expected(
[](const wcstring &s) { return string_prefixes_string_case_insensitive(L"be", s); }); [](const wcstring &s) { return string_prefixes_string_case_insensitive(L"be", s); });
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);
// Items exactly matching "alph", case-sensitive. // 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"; }); set_expected([](const wcstring &s) { return s == L"alph"; });
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);
// Items exactly matching "alph", case-insensitive. // 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"; }); set_expected([](const wcstring &s) { return wcstolower(s) == L"alph"; });
test_history_matches(searcher, expected, __LINE__); test_history_matches(searcher, expected, __LINE__);

View File

@ -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; const wcstring &content_to_match = case_sensitive ? contents : contents_lower;
switch (type) { switch (type) {
case HISTORY_SEARCH_TYPE_EXACT: { case history_search_type_t::exact: {
return term == content_to_match; return term == content_to_match;
} }
case HISTORY_SEARCH_TYPE_CONTAINS: { case history_search_type_t::contains: {
return content_to_match.find(term) != wcstring::npos; 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); 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); wcstring wcpattern1 = parse_util_unescape_wildcards(term);
if (wcpattern1.front() != ANY_STRING) wcpattern1.insert(0, 1, ANY_STRING); if (wcpattern1.front() != ANY_STRING) wcpattern1.insert(0, 1, ANY_STRING);
if (wcpattern1.back() != ANY_STRING) wcpattern1.push_back(ANY_STRING); if (wcpattern1.back() != ANY_STRING) wcpattern1.push_back(ANY_STRING);
return wildcard_match(content_to_match, wcpattern1); 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); wcstring wcpattern2 = parse_util_unescape_wildcards(term);
if (wcpattern2.back() != ANY_STRING) wcpattern2.push_back(ANY_STRING); if (wcpattern2.back() != ANY_STRING) wcpattern2.push_back(ANY_STRING);
return wildcard_match(content_to_match, wcpattern2); return wildcard_match(content_to_match, wcpattern2);

View File

@ -44,17 +44,17 @@ class environment_t;
typedef std::vector<wcstring> path_list_t; typedef std::vector<wcstring> path_list_t;
enum history_search_type_t { enum class history_search_type_t {
// Search for commands exactly matching the given string. // Search for commands exactly matching the given string.
HISTORY_SEARCH_TYPE_EXACT = 1, exact,
// Search for commands containing the given string. // Search for commands containing the given string.
HISTORY_SEARCH_TYPE_CONTAINS, contains,
// Search for commands starting with the given string. // Search for commands starting with the given string.
HISTORY_SEARCH_TYPE_PREFIX, prefix,
// Search for commands containing the given glob pattern. // Search for commands containing the given glob pattern.
HISTORY_SEARCH_TYPE_CONTAINS_GLOB, contains_glob,
// Search for commands starting with the given glob pattern. // Search for commands starting with the given glob pattern.
HISTORY_SEARCH_TYPE_PREFIX_GLOB prefix_glob,
}; };
typedef uint64_t history_identifier_t; typedef uint64_t history_identifier_t;
@ -229,7 +229,7 @@ class history_search_t {
wcstring canon_term_; wcstring canon_term_;
// Our search type. // 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. // Our flags.
history_search_flags_t flags_{0}; history_search_flags_t flags_{0};
@ -264,7 +264,7 @@ class history_search_t {
// Constructor. // Constructor.
history_search_t(history_t &hist, const wcstring &str, 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_search_flags_t flags = 0)
: history_(&hist), orig_term_(str), canon_term_(str), search_type_(type), flags_(flags) { : history_(&hist), orig_term_(str), canon_term_(str), search_type_(type), flags_(flags) {
if (ignores_case()) { if (ignores_case()) {

View File

@ -297,7 +297,8 @@ class reader_history_search_t {
mode_ = mode; mode_ = mode;
// We can skip dedup in history_search_t because we do it ourselves in skips_. // We can skip dedup in history_search_t because we do it ourselves in skips_.
search_ = history_search_t( 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); history_search_no_dedup);
} }
@ -1298,7 +1299,7 @@ static std::function<autosuggestion_result_t(void)> get_autosuggestion_performer
return nothing; 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()) { while (!reader_test_should_cancel() && searcher.go_backwards()) {
history_item_t item = searcher.current_item(); history_item_t item = searcher.current_item();