Add prefix_match function and use it instead of adhoc code

This commit is contained in:
Maxime Coste 2013-09-23 21:16:25 +02:00
parent 5a02d38081
commit 5ae43acf94
9 changed files with 26 additions and 11 deletions

View File

@ -103,7 +103,7 @@ CandidateList BufferManager::complete_buffername(const String& prefix,
for (auto& buffer : m_buffers)
{
String name = buffer->display_name();
if (name.substr(0, real_prefix.length()) == real_prefix)
if (prefix_match(name, real_prefix))
result.push_back(escape(name));
}
// no prefix completion found, check regex matching

View File

@ -350,11 +350,10 @@ public:
m_prefix = line;
auto it = m_history_it;
// search for the previous history entry matching typed prefix
ByteCount prefix_length = m_prefix.length();
do
{
--it;
if (it->substr(0, prefix_length) == m_prefix)
if (prefix_match(*it, m_prefix))
{
m_history_it = it;
m_line_editor.reset(*it);
@ -368,11 +367,10 @@ public:
{
if (m_history_it != history.end())
{
ByteCount prefix_length = m_prefix.length();
// search for the next history entry matching typed prefix
++m_history_it;
while (m_history_it != history.end() and
m_history_it->substr(0, prefix_length) != m_prefix)
prefix_match(*m_history_it, m_prefix))
++m_history_it;
if (m_history_it != history.end())

View File

@ -337,7 +337,7 @@ Completions CommandManager::complete(const Context& context,
for (auto& command : m_commands)
{
if (command.first.substr(0, prefix.length()) == prefix)
if (prefix_match(command.first, prefix))
result.candidates.push_back(command.first);
}
std::sort(result.candidates.begin(), result.candidates.end());

View File

@ -74,7 +74,7 @@ String compact_path(const String& filename)
char cwd[1024];
getcwd(cwd, 1024);
String real_cwd = real_path(cwd) + '/';
if (real_filename.substr(0, real_cwd.length()) == real_cwd)
if (prefix_match(real_filename, real_cwd))
return real_filename.substr(real_cwd.length());
const char* home = getenv("HOME");
@ -302,7 +302,7 @@ std::vector<String> complete_filename(const String& prefix,
if (check_ignored_regex and boost::regex_match(filename.c_str(), ignored_regex))
continue;
const bool match_prefix = (filename.substr(0, fileprefix.length()) == fileprefix);
const bool match_prefix = prefix_match(filename, fileprefix);
const bool match_regex = not file_regex.empty() and
boost::regex_match(filename.c_str(), file_regex);

View File

@ -78,7 +78,7 @@ public:
continue;
String id_str = value.first;
if (id_str.substr(0, real_prefix.length()) == real_prefix)
if (prefix_match(id_str, real_prefix))
result.push_back(std::move(id_str));
}
return result;

View File

@ -73,8 +73,7 @@ CandidateList OptionManager::complete_option_name(const String& prefix,
for (auto& option : m_options)
{
const auto& name = option->name();
if (name.substr(0, real_prefix.length()) == real_prefix and
not contains(result, name))
if (prefix_match(name, real_prefix) and not contains(result, name))
result.push_back(name);
}
return result;

View File

@ -83,4 +83,15 @@ void option_from_string(const String& str, Regex& re)
}
}
bool prefix_match(const String& str, const String& prefix)
{
auto it = str.begin();
for (auto& c : prefix)
{
if (it ==str.end() or *it++ != c)
return false;
}
return true;
}
}

View File

@ -112,6 +112,8 @@ String to_string(const StronglyTypedNumber<RealType, ValueType>& val)
return to_string((ValueType)val);
}
bool prefix_match(const String& str, const String& prefix);
}
namespace std

View File

@ -136,6 +136,11 @@ void test_string()
String escaped = escape("youpi:matin:tchou:", ':', '\\');
kak_assert(escaped == "youpi\\:matin\\:tchou\\:");
kak_assert(prefix_match("tchou kanaky", "tchou"));
kak_assert(prefix_match("tchou kanaky", "tchou kanaky"));
kak_assert(prefix_match("tchou kanaky", "t"));
kak_assert(not prefix_match("tchou kanaky", "c"));
}
void test_keys()