Clean up some comments around wildcard expansion

This commit is contained in:
ridiculousfish 2020-12-28 11:08:54 -08:00
parent 3c2cf6241b
commit df73964ced
2 changed files with 9 additions and 17 deletions

View file

@ -867,20 +867,14 @@ void wildcard_expander_t::expand_last_segment(const wcstring &base_dir, DIR *bas
/// wrappers around this one. /// wrappers around this one.
/// ///
/// This function traverses the relevant directory tree looking for matches, and recurses when /// This function traverses the relevant directory tree looking for matches, and recurses when
/// needed to handle wildcrards spanning multiple components and recursive wildcards. /// needed to handle wildcards spanning multiple components and recursive wildcards.
///
/// Because this function calls itself recursively with substrings, it's important that the
/// parameters be raw pointers instead of wcstring, which would be too expensive to construct for
/// all substrings.
/// ///
/// Args: /// Args:
/// base_dir: the "working directory" against which the wildcard is to be resolved /// base_dir: the "working directory" against which the wildcard is to be resolved
/// wc: the wildcard string itself, e.g. foo*bar/baz (where * is actually ANY_CHAR) /// wc: the wildcard string itself, e.g. foo*bar/baz (where * is actually ANY_CHAR)
/// prefix: the string that should be prepended for completions that replace their token. /// effective_prefix: the string that should be prepended for completions that replace their token.
// This is usually the same thing as the original wildcard, but for fuzzy matching, we /// This is usually the same thing as the original wildcard, but for fuzzy matching, we
// expand intermediate segments. effective_prefix is always either empty, or ends with a slash /// expand intermediate segments. effective_prefix is always either empty, or ends with a slash
// Note: this is only used when doing completions (for_completions is true), not
// expansions
void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc, void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc,
const wcstring &effective_prefix) { const wcstring &effective_prefix) {
assert(wc != nullptr); assert(wc != nullptr);
@ -890,10 +884,9 @@ void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc,
} }
// Get the current segment and compute interesting properties about it. // Get the current segment and compute interesting properties about it.
const size_t wc_len = std::wcslen(wc);
const wchar_t *const next_slash = std::wcschr(wc, L'/'); const wchar_t *const next_slash = std::wcschr(wc, L'/');
const bool is_last_segment = (next_slash == nullptr); const bool is_last_segment = (next_slash == nullptr);
const size_t wc_segment_len = next_slash ? next_slash - wc : wc_len; const size_t wc_segment_len = next_slash ? next_slash - wc : std::wcslen(wc);
const wcstring wc_segment = wcstring(wc, wc_segment_len); const wcstring wc_segment = wcstring(wc, wc_segment_len);
const bool segment_has_wildcards = const bool segment_has_wildcards =
wildcard_has(wc_segment, true /* internal, i.e. look for ANY_CHAR instead of ? */); wildcard_has(wc_segment, true /* internal, i.e. look for ANY_CHAR instead of ? */);
@ -949,7 +942,8 @@ void wildcard_expander_t::expand(const wcstring &base_dir, const wchar_t *wc,
effective_prefix + wc_segment + L'/'); effective_prefix + wc_segment + L'/');
} }
// Recursive wildcards require special handling. // If we have a recursive wildcard in this segment, we want to recurse into
// subdirectories.
size_t asr_idx = wc_segment.find(ANY_STRING_RECURSIVE); size_t asr_idx = wc_segment.find(ANY_STRING_RECURSIVE);
if (asr_idx != wcstring::npos) { if (asr_idx != wcstring::npos) {
// Construct a "head + any" wildcard for matching stuff in this directory, and an // Construct a "head + any" wildcard for matching stuff in this directory, and an

View file

@ -24,11 +24,9 @@ enum {
/// Expand the wildcard by matching against the filesystem. /// Expand the wildcard by matching against the filesystem.
/// ///
/// New strings are allocated using malloc and should be freed by the caller.
///
/// wildcard_expand works by dividing the wildcard into segments at each directory boundary. Each /// wildcard_expand works by dividing the wildcard into segments at each directory boundary. Each
/// segment is processed separatly. All except the last segment are handled by matching the wildcard /// segment is processed separately. All except the last segment are handled by matching the
/// segment against all subdirectories of matching directories, and recursively calling /// wildcard segment against all subdirectories of matching directories, and recursively calling
/// wildcard_expand for matches. On the last segment, matching is made to any file, and all matches /// wildcard_expand for matches. On the last segment, matching is made to any file, and all matches
/// are inserted to the list. /// are inserted to the list.
/// ///