utility: removed templates from utilityString functions to use std::deque by default
This commit is contained in:
@@ -133,7 +133,7 @@ void QtSearchBox::onSearchCompletionHighlighted(const QModelIndex& index)
|
||||
std::string match = m_matches[index.row()].encodeForQuery();
|
||||
tokens.push_back(match);
|
||||
|
||||
m_query = utility::join<std::deque<std::string>>(tokens, "");
|
||||
m_query = utility::join(tokens, "");
|
||||
}
|
||||
|
||||
setText(m_query);
|
||||
|
||||
@@ -400,17 +400,17 @@ SearchIndex::SearchNode* SearchIndex::getNode(const std::string& fullName) const
|
||||
|
||||
std::vector<SearchIndex::SearchMatch> SearchIndex::findFuzzyMatches(const std::string& query) const
|
||||
{
|
||||
std::vector<std::string> names = utility::split<std::vector<std::string>>(query, '\"');
|
||||
std::deque<std::string> names = utility::split(query, '\"');
|
||||
|
||||
if (names.size() == 3 && names[0].size() == 0)
|
||||
if (names.size() == 3 && names.at(0).size() == 0)
|
||||
{
|
||||
SearchNode* node = getNode(names[1]);
|
||||
SearchNode* node = getNode(names.at(1));
|
||||
if (!node)
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "Couldn't find node with name " << names[1] << " in the SearchIndex.");
|
||||
LOG_ERROR_STREAM(<< "Couldn't find node with name " << names.at(1) << " in the SearchIndex.");
|
||||
}
|
||||
|
||||
return node->findFuzzyMatches(names[2]);
|
||||
return node->findFuzzyMatches(names.at(2));
|
||||
}
|
||||
|
||||
return m_root.findFuzzyMatches(query);
|
||||
|
||||
@@ -57,7 +57,7 @@ std::shared_ptr<TokenComponent> TokenComponentNameCached::copy() const
|
||||
|
||||
const std::string& TokenComponentNameCached::getName() const
|
||||
{
|
||||
return utility::split<std::deque<std::string>>(m_fullName, SearchIndex::DELIMITER).back();
|
||||
return utility::split(m_fullName, SearchIndex::DELIMITER).back();
|
||||
}
|
||||
|
||||
std::string TokenComponentNameCached::getFullName() const
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
QueryToken::QueryToken(const std::string& name)
|
||||
{
|
||||
std::deque<std::string> names = utility::split<std::deque<std::string>>(name, DELIMITER);
|
||||
std::deque<std::string> names = utility::split(name, DELIMITER);
|
||||
|
||||
m_tokenName = names.front();
|
||||
names.pop_front();
|
||||
|
||||
@@ -8,12 +8,11 @@
|
||||
|
||||
std::deque<std::string> QueryTree::tokenizeQuery(const std::string& query)
|
||||
{
|
||||
std::deque<std::string> tokensTmp =
|
||||
utility::split<std::deque<std::string>>(query, QueryOperator::getOperator(QueryOperator::OPERATOR_NONE));
|
||||
std::deque<std::string> tokensTmp = utility::split(query, QueryOperator::getOperator(QueryOperator::OPERATOR_NONE));
|
||||
|
||||
for (const std::pair<char, QueryOperator::OperatorType>& p : QueryOperator::getOperatorTypeMap())
|
||||
{
|
||||
tokensTmp = utility::tokenize<std::deque<std::string>>(tokensTmp, p.first);
|
||||
tokensTmp = utility::tokenize(tokensTmp, p.first);
|
||||
}
|
||||
|
||||
char operatorToken = QueryOperator::getOperator(QueryOperator::OPERATOR_TOKEN);
|
||||
@@ -49,7 +48,7 @@ QueryTree::QueryTree(const std::string& query)
|
||||
{
|
||||
std::deque<std::string> tokens = tokenizeQuery(query);
|
||||
|
||||
m_query = utility::join<std::deque<std::string>>(tokens, ' ');
|
||||
m_query = utility::join(tokens, ' ');
|
||||
|
||||
m_root = buildTree(tokens, nullptr);
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ Id Dictionary::getWordId(const std::string& word)
|
||||
|
||||
std::deque<Id> Dictionary::getWordIds(const std::string& wordList, const std::string& delimiter)
|
||||
{
|
||||
std::deque<std::string> words = utility::split<std::deque<std::string>>(wordList, delimiter);
|
||||
std::deque<std::string> words = utility::split(wordList, delimiter);
|
||||
std::deque<Id> ids;
|
||||
|
||||
for (const std::string& word: words)
|
||||
|
||||
@@ -2,6 +2,103 @@
|
||||
|
||||
namespace utility
|
||||
{
|
||||
std::deque<std::string> split(const std::string& str, char delimiter)
|
||||
{
|
||||
return split(str, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
std::deque<std::string> split(const std::string& str, const std::string& delimiter)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t oldPos = 0;
|
||||
std::deque<std::string> c;
|
||||
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, oldPos);
|
||||
c.push_back(str.substr(oldPos, pos - oldPos));
|
||||
oldPos = pos + delimiter.size();
|
||||
}
|
||||
while (pos != std::string::npos);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
std::string join(const std::deque<std::string>& list, char delimiter)
|
||||
{
|
||||
return join(list, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
std::string join(const std::deque<std::string>& list, const std::string& delimiter)
|
||||
{
|
||||
std::stringstream ss;
|
||||
bool first = true;
|
||||
for (const std::string& str : list)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
ss << delimiter;
|
||||
}
|
||||
first = false;
|
||||
|
||||
ss << str;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::deque<std::string> tokenize(const std::string& str, char delimiter)
|
||||
{
|
||||
return tokenize(str, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
std::deque<std::string> tokenize(const std::string& str, const std::string& delimiter)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t oldPos = 0;
|
||||
std::deque<std::string> c;
|
||||
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, oldPos);
|
||||
|
||||
if (pos != oldPos)
|
||||
{
|
||||
c.push_back(str.substr(oldPos, pos - oldPos));
|
||||
}
|
||||
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
c.push_back(str.substr(pos, delimiter.size()));
|
||||
}
|
||||
|
||||
oldPos = pos + delimiter.size();
|
||||
}
|
||||
while (pos != std::string::npos && oldPos < str.size());
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
std::deque<std::string> tokenize(const std::deque<std::string>& list, char delimiter)
|
||||
{
|
||||
return tokenize(list, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
std::deque<std::string> tokenize(const std::deque<std::string>& list, const std::string& delimiter)
|
||||
{
|
||||
std::deque<std::string> c;
|
||||
|
||||
for (std::string str : list)
|
||||
{
|
||||
if (str.size())
|
||||
{
|
||||
std::deque<std::string> c2 = tokenize(str, delimiter);
|
||||
c.insert(c.end(), c2.begin(), c2.end());
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
std::string substrAfter(const std::string& str, char delimiter)
|
||||
{
|
||||
size_t pos = str.find(delimiter);
|
||||
|
||||
@@ -1,144 +1,26 @@
|
||||
#ifndef UTILITY_STRING_H
|
||||
#define UTILITY_STRING_H
|
||||
|
||||
#include <deque>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace utility
|
||||
{
|
||||
template<typename ContainerType>
|
||||
ContainerType split(const std::string& str, char delimiter);
|
||||
std::deque<std::string> split(const std::string& str, char delimiter);
|
||||
std::deque<std::string> split(const std::string& str, const std::string& delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType split(const std::string& str, const std::string& delimiter);
|
||||
std::string join(const std::deque<std::string>& list, char delimiter);
|
||||
std::string join(const std::deque<std::string>& list, const std::string& delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
std::string join(const ContainerType& list, char delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
std::string join(const ContainerType& list, const std::string& delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType tokenize(const std::string& str, char delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType tokenize(const std::string& str, const std::string& delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType tokenize(const ContainerType& list, char delimiter);
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType tokenize(const ContainerType& list, const std::string& delimiter);
|
||||
std::deque<std::string> tokenize(const std::string& str, char delimiter);
|
||||
std::deque<std::string> tokenize(const std::string& str, const std::string& delimiter);
|
||||
std::deque<std::string> tokenize(const std::deque<std::string>& list, char delimiter);
|
||||
std::deque<std::string> tokenize(const std::deque<std::string>& list, const std::string& delimiter);
|
||||
|
||||
std::string substrAfter(const std::string& str, char delimiter);
|
||||
|
||||
bool isPrefix(const std::string& prefix, const std::string& text);
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType utility::split(const std::string& str, char delimiter)
|
||||
{
|
||||
return split<ContainerType>(str, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType utility::split(const std::string& str, const std::string& delimiter)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t oldPos = 0;
|
||||
ContainerType c;
|
||||
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, oldPos);
|
||||
c.push_back(str.substr(oldPos, pos - oldPos));
|
||||
oldPos = pos + delimiter.size();
|
||||
}
|
||||
while (pos != std::string::npos);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
std::string utility::join(const ContainerType& list, char delimiter)
|
||||
{
|
||||
return join<ContainerType>(list, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
std::string utility::join(const ContainerType& list, const std::string& delimiter)
|
||||
{
|
||||
std::stringstream ss;
|
||||
bool first = true;
|
||||
for (const std::string& str : list)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
ss << delimiter;
|
||||
}
|
||||
first = false;
|
||||
|
||||
ss << str;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType utility::tokenize(const std::string& str, char delimiter)
|
||||
{
|
||||
return tokenize<ContainerType>(str, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType utility::tokenize(const std::string& str, const std::string& delimiter)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t oldPos = 0;
|
||||
ContainerType c;
|
||||
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, oldPos);
|
||||
|
||||
if (pos != oldPos)
|
||||
{
|
||||
c.push_back(str.substr(oldPos, pos - oldPos));
|
||||
}
|
||||
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
c.push_back(str.substr(pos, delimiter.size()));
|
||||
}
|
||||
|
||||
oldPos = pos + delimiter.size();
|
||||
}
|
||||
while (pos != std::string::npos && oldPos < str.size());
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType utility::tokenize(const ContainerType& list, char delimiter)
|
||||
{
|
||||
return tokenize<ContainerType>(list, std::string(1, delimiter));
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
ContainerType utility::tokenize(const ContainerType& list, const std::string& delimiter)
|
||||
{
|
||||
ContainerType c;
|
||||
|
||||
for (std::string str : list)
|
||||
{
|
||||
if (str.size())
|
||||
{
|
||||
ContainerType c2 = tokenize<ContainerType>(str, delimiter);
|
||||
c.insert(c.end(), c2.begin(), c2.end());
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
#endif // UTILITY_STRING_H
|
||||
|
||||
@@ -7,153 +7,153 @@ class UtilityStringTestSuite : public CxxTest::TestSuite
|
||||
public:
|
||||
void test_split_with_char_delimiter()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >("A,B,C", ',');
|
||||
std::deque<std::string> result = utility::split("A,B,C", ',');
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 3);
|
||||
TS_ASSERT_EQUALS(result[0], "A");
|
||||
TS_ASSERT_EQUALS(result[1], "B");
|
||||
TS_ASSERT_EQUALS(result[2], "C");
|
||||
TS_ASSERT_EQUALS(result.at(0), "A");
|
||||
TS_ASSERT_EQUALS(result.at(1), "B");
|
||||
TS_ASSERT_EQUALS(result.at(2), "C");
|
||||
}
|
||||
|
||||
void test_split_with_string_delimiter()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >("A->B>C", "->");
|
||||
std::deque<std::string> result = utility::split("A->B>C", "->");
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 2);
|
||||
TS_ASSERT_EQUALS(result[0], "A");
|
||||
TS_ASSERT_EQUALS(result[1], "B>C");
|
||||
TS_ASSERT_EQUALS(result.at(0), "A");
|
||||
TS_ASSERT_EQUALS(result.at(1), "B>C");
|
||||
}
|
||||
|
||||
void test_split_on_empty_string()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >("", "->");
|
||||
std::deque<std::string> result = utility::split("", "->");
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 1);
|
||||
TS_ASSERT_EQUALS(result[0], "");
|
||||
TS_ASSERT_EQUALS(result.at(0), "");
|
||||
}
|
||||
|
||||
void test_split_with_unused_delimiter()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >("A:B:C", ";");
|
||||
std::deque<std::string> result = utility::split("A:B:C", ";");
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 1);
|
||||
TS_ASSERT_EQUALS(result[0], "A:B:C");
|
||||
TS_ASSERT_EQUALS(result.at(0), "A:B:C");
|
||||
}
|
||||
|
||||
void test_split_with_delimiters_next_to_each()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >("A::B:C", ':');
|
||||
std::deque<std::string> result = utility::split("A::B:C", ':');
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 4);
|
||||
TS_ASSERT_EQUALS(result[0], "A");
|
||||
TS_ASSERT_EQUALS(result[1], "");
|
||||
TS_ASSERT_EQUALS(result[2], "B");
|
||||
TS_ASSERT_EQUALS(result[3], "C");
|
||||
TS_ASSERT_EQUALS(result.at(0), "A");
|
||||
TS_ASSERT_EQUALS(result.at(1), "");
|
||||
TS_ASSERT_EQUALS(result.at(2), "B");
|
||||
TS_ASSERT_EQUALS(result.at(3), "C");
|
||||
}
|
||||
|
||||
void test_split_with_delimiter_at_start()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >(":B:C", ':');
|
||||
std::deque<std::string> result = utility::split(":B:C", ':');
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 3);
|
||||
TS_ASSERT_EQUALS(result[0], "");
|
||||
TS_ASSERT_EQUALS(result[1], "B");
|
||||
TS_ASSERT_EQUALS(result[2], "C");
|
||||
TS_ASSERT_EQUALS(result.at(0), "");
|
||||
TS_ASSERT_EQUALS(result.at(1), "B");
|
||||
TS_ASSERT_EQUALS(result.at(2), "C");
|
||||
}
|
||||
|
||||
void test_split_with_delimiter_at_end()
|
||||
{
|
||||
std::vector<std::string> result = utility::split<std::vector<std::string> >("B:C:", ':');
|
||||
std::deque<std::string> result = utility::split("B:C:", ':');
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 3);
|
||||
TS_ASSERT_EQUALS(result[0], "B");
|
||||
TS_ASSERT_EQUALS(result[1], "C");
|
||||
TS_ASSERT_EQUALS(result[2], "");
|
||||
TS_ASSERT_EQUALS(result.at(0), "B");
|
||||
TS_ASSERT_EQUALS(result.at(1), "C");
|
||||
TS_ASSERT_EQUALS(result.at(2), "");
|
||||
}
|
||||
|
||||
void test_join_with_char_delimiter()
|
||||
{
|
||||
std::vector<std::string> list;
|
||||
std::deque<std::string> list;
|
||||
list.push_back("A");
|
||||
list.push_back("B");
|
||||
list.push_back("C");
|
||||
|
||||
std::string result = utility::join<std::vector<std::string> >(list, ',');
|
||||
std::string result = utility::join(list, ',');
|
||||
TS_ASSERT_EQUALS(result, "A,B,C");
|
||||
}
|
||||
|
||||
void test_join_with_string_delimiter()
|
||||
{
|
||||
std::vector<std::string> list;
|
||||
std::deque<std::string> list;
|
||||
list.push_back("A");
|
||||
list.push_back("B");
|
||||
list.push_back("C");
|
||||
|
||||
std::string result = utility::join<std::vector<std::string> >(list, "==");
|
||||
std::string result = utility::join(list, "==");
|
||||
TS_ASSERT_EQUALS(result, "A==B==C");
|
||||
}
|
||||
|
||||
void test_join_on_empty_list()
|
||||
{
|
||||
std::vector<std::string> list;
|
||||
std::string result = utility::join<std::vector<std::string> >(list, ',');
|
||||
std::deque<std::string> list;
|
||||
std::string result = utility::join(list, ',');
|
||||
TS_ASSERT_EQUALS(result, "");
|
||||
}
|
||||
|
||||
void test_join_with_empty_strings_in_list()
|
||||
{
|
||||
std::vector<std::string> list;
|
||||
std::deque<std::string> list;
|
||||
list.push_back("A");
|
||||
list.push_back("");
|
||||
list.push_back("");
|
||||
|
||||
std::string result = utility::join<std::vector<std::string> >(list, ':');
|
||||
std::string result = utility::join(list, ':');
|
||||
TS_ASSERT_EQUALS(result, "A::");
|
||||
}
|
||||
|
||||
void test_tokenize_with_string()
|
||||
{
|
||||
std::vector<std::string> result = utility::tokenize<std::vector<std::string> >("A->B->C", "->");
|
||||
std::deque<std::string> result = utility::tokenize("A->B->C", "->");
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 5);
|
||||
TS_ASSERT_EQUALS(result[0], "A");
|
||||
TS_ASSERT_EQUALS(result[1], "->");
|
||||
TS_ASSERT_EQUALS(result[2], "B");
|
||||
TS_ASSERT_EQUALS(result[3], "->");
|
||||
TS_ASSERT_EQUALS(result[4], "C");
|
||||
TS_ASSERT_EQUALS(result.at(0), "A");
|
||||
TS_ASSERT_EQUALS(result.at(1), "->");
|
||||
TS_ASSERT_EQUALS(result.at(2), "B");
|
||||
TS_ASSERT_EQUALS(result.at(3), "->");
|
||||
TS_ASSERT_EQUALS(result.at(4), "C");
|
||||
}
|
||||
|
||||
void test_tokenize_with_string_and_delimiter_at_start()
|
||||
{
|
||||
std::vector<std::string> result = utility::tokenize<std::vector<std::string> >("->B", "->");
|
||||
std::deque<std::string> result = utility::tokenize("->B", "->");
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 2);
|
||||
TS_ASSERT_EQUALS(result[0], "->");
|
||||
TS_ASSERT_EQUALS(result[1], "B");
|
||||
TS_ASSERT_EQUALS(result.at(0), "->");
|
||||
TS_ASSERT_EQUALS(result.at(1), "B");
|
||||
}
|
||||
|
||||
void test_tokenize_with_string_and_delimiter_at_end()
|
||||
{
|
||||
std::vector<std::string> result = utility::tokenize<std::vector<std::string> >("C+", '+');
|
||||
std::deque<std::string> result = utility::tokenize("C+", '+');
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 2);
|
||||
TS_ASSERT_EQUALS(result[0], "C");
|
||||
TS_ASSERT_EQUALS(result[1], "+");
|
||||
TS_ASSERT_EQUALS(result.at(0), "C");
|
||||
TS_ASSERT_EQUALS(result.at(1), "+");
|
||||
}
|
||||
|
||||
void test_tokenize_with_vector()
|
||||
void test_tokenize_with_deque()
|
||||
{
|
||||
std::vector<std::string> result = utility::tokenize<std::vector<std::string> >("A->B=C->D", "->");
|
||||
result = utility::tokenize<std::vector<std::string>>(result, "=");
|
||||
std::deque<std::string> result = utility::tokenize("A->B=C->D", "->");
|
||||
result = utility::tokenize(result, "=");
|
||||
|
||||
TS_ASSERT_EQUALS(result.size(), 7);
|
||||
TS_ASSERT_EQUALS(result[0], "A");
|
||||
TS_ASSERT_EQUALS(result[1], "->");
|
||||
TS_ASSERT_EQUALS(result[2], "B");
|
||||
TS_ASSERT_EQUALS(result[3], "=");
|
||||
TS_ASSERT_EQUALS(result[4], "C");
|
||||
TS_ASSERT_EQUALS(result[5], "->");
|
||||
TS_ASSERT_EQUALS(result[6], "D");
|
||||
TS_ASSERT_EQUALS(result.at(0), "A");
|
||||
TS_ASSERT_EQUALS(result.at(1), "->");
|
||||
TS_ASSERT_EQUALS(result.at(2), "B");
|
||||
TS_ASSERT_EQUALS(result.at(3), "=");
|
||||
TS_ASSERT_EQUALS(result.at(4), "C");
|
||||
TS_ASSERT_EQUALS(result.at(5), "->");
|
||||
TS_ASSERT_EQUALS(result.at(6), "D");
|
||||
}
|
||||
|
||||
void test_substr_after_with_single_delimiter_occurence()
|
||||
|
||||
Reference in New Issue
Block a user