When investigating a completion bug I got confused by the API.
LongestCommonPrefix finds the longest common prefix of the strings in
the string list. Instead of returning that string through an output
argument, just return it by value.
llvm-svn: 367384
void Clear();
- void LongestCommonPrefix(std::string &common_prefix);
+ std::string LongestCommonPrefix();
void InsertStringAtIndex(size_t idx, const std::string &str);
size_t num_matches = request.GetNumberOfMatches();
if (num_matches > 0) {
- std::string common_prefix;
- matches.LongestCommonPrefix(common_prefix);
+ std::string common_prefix = matches.LongestCommonPrefix();
const size_t partial_name_len = request.GetCursorArgumentPrefix().size();
// If we matched a unique single command, add a space... Only do this if
// element 0, otherwise put an empty string in element 0.
std::string command_partial_str = request.GetCursorArgumentPrefix().str();
- std::string common_prefix;
- matches.LongestCommonPrefix(common_prefix);
+ std::string common_prefix = matches.LongestCommonPrefix();
const size_t partial_name_len = command_partial_str.size();
common_prefix.erase(0, partial_name_len);
void StringList::Clear() { m_strings.clear(); }
-void StringList::LongestCommonPrefix(std::string &common_prefix) {
- common_prefix.clear();
+std::string StringList::LongestCommonPrefix() {
if (m_strings.empty())
- return;
+ return {};
auto args = llvm::makeArrayRef(m_strings);
llvm::StringRef prefix = args.front();
}
prefix = prefix.take_front(count);
}
- common_prefix = prefix;
+ return prefix.str();
}
void StringList::InsertStringAtIndex(size_t idx, const char *str) {
TEST(StringListTest, LongestCommonPrefixEmpty) {
StringList s;
- std::string prefix = "this should be cleared";
- s.LongestCommonPrefix(prefix);
+ std::string prefix = s.LongestCommonPrefix();
EXPECT_EQ("", prefix);
}
s.AppendString("foo");
s.AppendString("foozar");
- std::string prefix = "this should be cleared";
- s.LongestCommonPrefix(prefix);
+ std::string prefix = s.LongestCommonPrefix();
EXPECT_EQ("foo", prefix);
}
StringList s;
s.AppendString("foo");
- std::string prefix = "this should be cleared";
- s.LongestCommonPrefix(prefix);
+ std::string prefix = s.LongestCommonPrefix();
EXPECT_EQ("foo", prefix);
}
s.AppendString("foo");
s.AppendString("foo");
- std::string prefix = "this should be cleared";
- s.LongestCommonPrefix(prefix);
+ std::string prefix = s.LongestCommonPrefix();
EXPECT_EQ("foo", prefix);
}
s.AppendString("2foo");
s.AppendString("3foozar");
- std::string prefix = "this should be cleared";
- s.LongestCommonPrefix(prefix);
+ std::string prefix = s.LongestCommonPrefix();
EXPECT_EQ("", prefix);
}