aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/wildcard.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-07-27 18:45:47 -0700
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-07-27 18:45:47 -0700
commit6e3231a9d705ee043cf827b22c807b0b96ea8893 (patch)
tree04cf44073d24084bf2e583ef808fe83caa6f0cf3 /src/wildcard.cpp
parent85dad6913e77d81730f3aea478695429f441ebd5 (diff)
Stop passing mutable references to completions around
Replace uses of vector<completion_t>& with vector<completion_t>* This makes it clear at the call site that the object may be mutated.
Diffstat (limited to 'src/wildcard.cpp')
-rw-r--r--src/wildcard.cpp60
1 files changed, 30 insertions, 30 deletions
diff --git a/src/wildcard.cpp b/src/wildcard.cpp
index 24769e80..82417770 100644
--- a/src/wildcard.cpp
+++ b/src/wildcard.cpp
@@ -211,7 +211,7 @@ static bool wildcard_complete_internal(const wcstring &orig,
bool is_first,
const wchar_t *desc,
wcstring(*desc_func)(const wcstring &),
- std::vector<completion_t> &out,
+ std::vector<completion_t> *out,
expand_flags_t expand_flags,
complete_flags_t flags)
{
@@ -316,17 +316,17 @@ static bool wildcard_complete_internal(const wcstring &orig,
/* Try all submatches */
for (size_t i=0; str[i] != L'\0'; i++)
{
- const size_t before_count = out.size();
+ const size_t before_count = out->size();
if (wildcard_complete_internal(orig, str + i, wc+1, false, desc, desc_func, out, expand_flags, flags))
{
res = true;
/* #929: if the recursive call gives us a prefix match, just stop. This is sloppy - what we really want to do is say, once we've seen a match of a particular type, ignore all matches of that type further down the string, such that the wildcard produces the "minimal match." */
bool has_prefix_match = false;
- const size_t after_count = out.size();
+ const size_t after_count = out->size();
for (size_t j = before_count; j < after_count; j++)
{
- if (out[j].match.type <= fuzzy_match_prefix)
+ if (out->at(j).match.type <= fuzzy_match_prefix)
{
has_prefix_match = true;
break;
@@ -354,13 +354,12 @@ bool wildcard_complete(const wcstring &str,
const wchar_t *wc,
const wchar_t *desc,
wcstring(*desc_func)(const wcstring &),
- std::vector<completion_t> &out,
+ std::vector<completion_t> *out,
expand_flags_t expand_flags,
complete_flags_t flags)
{
- bool res;
- res = wildcard_complete_internal(str, str.c_str(), wc, true, desc, desc_func, out, expand_flags, flags);
- return res;
+ assert(out != NULL);
+ return wildcard_complete_internal(str, str.c_str(), wc, true, desc, desc_func, out, expand_flags, flags);
}
@@ -515,12 +514,13 @@ static wcstring file_get_desc(const wcstring &filename,
\param wc the wildcard to match against
\param is_cmd whether we are performing command completion
*/
-static void wildcard_completion_allocate(std::vector<completion_t> &list,
+static void wildcard_completion_allocate(std::vector<completion_t> *list,
const wcstring &fullname,
const wcstring &completion,
const wchar_t *wc,
expand_flags_t expand_flags)
{
+ assert(list != NULL);
struct stat buf, lbuf;
wcstring sb;
wcstring munged_completion;
@@ -644,9 +644,9 @@ static bool test_flags(const wchar_t *filename, expand_flags_t flags)
}
/** Appends a completion to the completion list, if the string is missing from the set. */
-static void insert_completion_if_missing(const wcstring &str, std::vector<completion_t> &out, std::set<wcstring> &completion_set)
+static void insert_completion_if_missing(const wcstring &str, std::vector<completion_t> *out, std::set<wcstring> *completion_set)
{
- if (completion_set.insert(str).second)
+ if (completion_set->insert(str).second)
append_completion(out, str);
}
@@ -665,7 +665,7 @@ static void insert_completion_if_missing(const wcstring &str, std::vector<comple
static int wildcard_expand_internal(const wchar_t *wc,
const wchar_t * const base_dir,
expand_flags_t flags,
- std::vector<completion_t> &out,
+ std::vector<completion_t> *out,
std::set<wcstring> &completion_set,
std::set<file_id_t> &visited_files)
{
@@ -776,7 +776,7 @@ static int wildcard_expand_internal(const wchar_t *wc,
else
{
res = 1;
- insert_completion_if_missing(base_dir, out, completion_set);
+ insert_completion_if_missing(base_dir, out, &completion_set);
}
}
else
@@ -792,7 +792,7 @@ static int wildcard_expand_internal(const wchar_t *wc,
/* Test for matches before stating file, so as to minimize the number of calls to the much slower stat function. The only expand flag we care about is EXPAND_FUZZY_MATCH; we have no complete flags. */
std::vector<completion_t> test;
- if (wildcard_complete(name_str, wc, L"", NULL, test, flags & EXPAND_FUZZY_MATCH, 0))
+ if (wildcard_complete(name_str, wc, L"", NULL, &test, flags & EXPAND_FUZZY_MATCH, 0))
{
if (test_flags(long_name.c_str(), flags))
{
@@ -823,7 +823,7 @@ static int wildcard_expand_internal(const wchar_t *wc,
}
if (! skip)
{
- insert_completion_if_missing(long_name, out, completion_set);
+ insert_completion_if_missing(long_name, out, &completion_set);
}
res = 1;
}
@@ -959,17 +959,18 @@ static int wildcard_expand_internal(const wchar_t *wc,
}
-int wildcard_expand(const wchar_t *wc,
- const wchar_t *base_dir,
- expand_flags_t flags,
- std::vector<completion_t> &out)
+static int wildcard_expand(const wchar_t *wc,
+ const wchar_t *base_dir,
+ expand_flags_t flags,
+ std::vector<completion_t> *out)
{
- size_t c = out.size();
+ assert(out != NULL);
+ size_t c = out->size();
/* Make a set of used completion strings so we can do fast membership tests inside wildcard_expand_internal. Otherwise wildcards like '**' are very slow, because we end up with an N^2 membership test.
*/
std::set<wcstring> completion_set;
- for (std::vector<completion_t>::const_iterator iter = out.begin(); iter != out.end(); ++iter)
+ for (std::vector<completion_t>::const_iterator iter = out->begin(); iter != out->end(); ++iter)
{
completion_set.insert(iter->completion);
}
@@ -986,29 +987,28 @@ int wildcard_expand(const wchar_t *wc,
wc_base = wcstring(wc, (wc_base_ptr-wc)+1);
}
- for (size_t i=c; i<out.size(); i++)
+ for (size_t i=c; i<out->size(); i++)
{
- completion_t &c = out.at(i);
+ completion_t &c = out->at(i);
if (c.flags & COMPLETE_REPLACES_TOKEN)
{
- c.completion = format_string(L"%ls%ls%ls", base_dir, wc_base.c_str(), c.completion.c_str());
+ // completion = base_dir + wc_base + completion
+ c.completion.insert(0, wc_base);
+ c.completion.insert(0, base_dir);
}
}
}
return res;
}
-int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, expand_flags_t flags, std::vector<completion_t> &outputs)
+int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, expand_flags_t flags, std::vector<completion_t> *output)
{
+ assert(output != NULL);
/* Hackish fix for 1631. We are about to call c_str(), which will produce a string truncated at any embedded nulls. We could fix this by passing around the size, etc. However 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. This isn't quite right, e.g. it will fail for \0?, but that is an edge case. */
if (wc.find(L'\0') != wcstring::npos)
{
return 0;
}
- // PCA: not convinced this temporary variable is really necessary
- std::vector<completion_t> lst;
- int res = wildcard_expand(wc.c_str(), base_dir.c_str(), flags, lst);
- outputs.insert(outputs.end(), lst.begin(), lst.end());
- return res;
+ return wildcard_expand(wc.c_str(), base_dir.c_str(), flags, output);
}