From 5f130c2d6d17d8e84b86438cf9c2d1e0d623b052 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Wed, 15 Jan 2020 11:09:09 -0800 Subject: [PATCH] Give wildcard_expand_result_t a real return value Use an enum instead of an int. --- src/expand.cpp | 19 +++++++++++-------- src/wildcard.cpp | 14 ++++++++------ src/wildcard.h | 12 +++++++++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/expand.cpp b/src/expand.cpp index 565913931..1f677e5a7 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -1010,15 +1010,18 @@ expand_result_t expander_t::stage_wildcards(wcstring path_to_expand, result = expand_result_t::wildcard_no_match; std::vector expanded; for (const auto &effective_working_dir : effective_working_dirs) { - int local_wc_res = + wildcard_expand_result_t expand_res = wildcard_expand_string(path_to_expand, effective_working_dir, flags, &expanded); - if (local_wc_res > 0) { - // Something matched,so overall we matched. - result = expand_result_t::wildcard_match; - } else if (local_wc_res < 0) { - // Cancellation - result = expand_result_t::error; - break; + switch (expand_res) { + case wildcard_expand_result_t::match: + // Something matched,so overall we matched. + result = expand_result_t::wildcard_match; + break; + case wildcard_expand_result_t::no_match: + break; + case wildcard_expand_result_t::cancel: + result = expand_result_t::error; + break; } } diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 1f4f9db4e..d1b2eb649 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -645,11 +645,11 @@ class wildcard_expander_t { // Do wildcard expansion. This is recursive. void expand(const wcstring &base_dir, const wchar_t *wc, const wcstring &prefix); - int status_code() const { + wildcard_expand_result_t status_code() const { if (this->did_interrupt) { - return -1; + return wildcard_expand_result_t::cancel; } - return this->did_add ? 1 : 0; + return this->did_add ? wildcard_expand_result_t::match : wildcard_expand_result_t::no_match; } }; @@ -898,8 +898,10 @@ void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc, } } -int wildcard_expand_string(const wcstring &wc, const wcstring &working_directory, - expand_flags_t flags, std::vector *output) { +wildcard_expand_result_t wildcard_expand_string(const wcstring &wc, + const wcstring &working_directory, + expand_flags_t flags, + std::vector *output) { assert(output != nullptr); // Fuzzy matching only if we're doing completions. assert(flags.get(expand_flag::for_completions) || !flags.get(expand_flag::fuzzy_match)); @@ -916,7 +918,7 @@ int wildcard_expand_string(const wcstring &wc, const wcstring &working_directory // embedded nulls are never allowed in a filename, so we just check for them and return 0 (no // matches) if there is an embedded null. if (wc.find(L'\0') != wcstring::npos) { - return 0; + return wildcard_expand_result_t::no_match; } // Compute the prefix and base dir. The prefix is what we prepend for filesystem operations diff --git a/src/wildcard.h b/src/wildcard.h index dd4b7827e..506e8a4a7 100644 --- a/src/wildcard.h +++ b/src/wildcard.h @@ -41,9 +41,15 @@ enum { /// executables_only /// \param out The list in which to put the output /// -/// \return 1 if matches where found, 0 otherwise. Return -1 on abort (I.e. ^C was pressed). -int wildcard_expand_string(const wcstring &wc, const wcstring &working_directory, - expand_flags_t flags, std::vector *out); +enum class wildcard_expand_result_t { + no_match, /// The wildcard did not match. + match, /// The wildcard did match. + cancel, /// Expansion was cancelled (e.g. control-C). +}; +wildcard_expand_result_t wildcard_expand_string(const wcstring &wc, + const wcstring &working_directory, + expand_flags_t flags, + std::vector *out); /// Test whether the given wildcard matches the string. Does not perform any I/O. ///