diff --git a/src/app/qt/element/QtSearchBox.cpp b/src/app/qt/element/QtSearchBox.cpp index 129bcdae..03a21ff7 100644 --- a/src/app/qt/element/QtSearchBox.cpp +++ b/src/app/qt/element/QtSearchBox.cpp @@ -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>(tokens, ""); + m_query = utility::join(tokens, ""); } setText(m_query); diff --git a/src/lib/data/SearchIndex.cpp b/src/lib/data/SearchIndex.cpp index ec26a381..d50a60d4 100644 --- a/src/lib/data/SearchIndex.cpp +++ b/src/lib/data/SearchIndex.cpp @@ -400,17 +400,17 @@ SearchIndex::SearchNode* SearchIndex::getNode(const std::string& fullName) const std::vector SearchIndex::findFuzzyMatches(const std::string& query) const { - std::vector names = utility::split>(query, '\"'); + std::deque 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); diff --git a/src/lib/data/graph/token_component/TokenComponentName.cpp b/src/lib/data/graph/token_component/TokenComponentName.cpp index fc363bed..802f6ea0 100644 --- a/src/lib/data/graph/token_component/TokenComponentName.cpp +++ b/src/lib/data/graph/token_component/TokenComponentName.cpp @@ -57,7 +57,7 @@ std::shared_ptr TokenComponentNameCached::copy() const const std::string& TokenComponentNameCached::getName() const { - return utility::split>(m_fullName, SearchIndex::DELIMITER).back(); + return utility::split(m_fullName, SearchIndex::DELIMITER).back(); } std::string TokenComponentNameCached::getFullName() const diff --git a/src/lib/data/query/QueryToken.cpp b/src/lib/data/query/QueryToken.cpp index 0a5e1864..44b6b10b 100644 --- a/src/lib/data/query/QueryToken.cpp +++ b/src/lib/data/query/QueryToken.cpp @@ -6,7 +6,7 @@ QueryToken::QueryToken(const std::string& name) { - std::deque names = utility::split>(name, DELIMITER); + std::deque names = utility::split(name, DELIMITER); m_tokenName = names.front(); names.pop_front(); diff --git a/src/lib/data/query/QueryTree.cpp b/src/lib/data/query/QueryTree.cpp index 8c6e8536..e37ae7fe 100644 --- a/src/lib/data/query/QueryTree.cpp +++ b/src/lib/data/query/QueryTree.cpp @@ -8,12 +8,11 @@ std::deque QueryTree::tokenizeQuery(const std::string& query) { - std::deque tokensTmp = - utility::split>(query, QueryOperator::getOperator(QueryOperator::OPERATOR_NONE)); + std::deque tokensTmp = utility::split(query, QueryOperator::getOperator(QueryOperator::OPERATOR_NONE)); for (const std::pair& p : QueryOperator::getOperatorTypeMap()) { - tokensTmp = utility::tokenize>(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 tokens = tokenizeQuery(query); - m_query = utility::join>(tokens, ' '); + m_query = utility::join(tokens, ' '); m_root = buildTree(tokens, nullptr); } diff --git a/src/lib/utility/text/Dictionary.cpp b/src/lib/utility/text/Dictionary.cpp index 2cfbc2cf..e431ca45 100644 --- a/src/lib/utility/text/Dictionary.cpp +++ b/src/lib/utility/text/Dictionary.cpp @@ -32,7 +32,7 @@ Id Dictionary::getWordId(const std::string& word) std::deque Dictionary::getWordIds(const std::string& wordList, const std::string& delimiter) { - std::deque words = utility::split>(wordList, delimiter); + std::deque words = utility::split(wordList, delimiter); std::deque ids; for (const std::string& word: words) diff --git a/src/lib/utility/utilityString.cpp b/src/lib/utility/utilityString.cpp index 5e0f8afe..2854880d 100644 --- a/src/lib/utility/utilityString.cpp +++ b/src/lib/utility/utilityString.cpp @@ -2,6 +2,103 @@ namespace utility { + std::deque split(const std::string& str, char delimiter) + { + return split(str, std::string(1, delimiter)); + } + + std::deque split(const std::string& str, const std::string& delimiter) + { + size_t pos = 0; + size_t oldPos = 0; + std::deque 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& list, char delimiter) + { + return join(list, std::string(1, delimiter)); + } + + std::string join(const std::deque& 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 tokenize(const std::string& str, char delimiter) + { + return tokenize(str, std::string(1, delimiter)); + } + + std::deque tokenize(const std::string& str, const std::string& delimiter) + { + size_t pos = 0; + size_t oldPos = 0; + std::deque 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 tokenize(const std::deque& list, char delimiter) + { + return tokenize(list, std::string(1, delimiter)); + } + + std::deque tokenize(const std::deque& list, const std::string& delimiter) + { + std::deque c; + + for (std::string str : list) + { + if (str.size()) + { + std::deque 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); diff --git a/src/lib/utility/utilityString.h b/src/lib/utility/utilityString.h index 28f4533d..730b3b70 100644 --- a/src/lib/utility/utilityString.h +++ b/src/lib/utility/utilityString.h @@ -1,144 +1,26 @@ #ifndef UTILITY_STRING_H #define UTILITY_STRING_H +#include #include #include -#include namespace utility { - template - ContainerType split(const std::string& str, char delimiter); + std::deque split(const std::string& str, char delimiter); + std::deque split(const std::string& str, const std::string& delimiter); - template - ContainerType split(const std::string& str, const std::string& delimiter); + std::string join(const std::deque& list, char delimiter); + std::string join(const std::deque& list, const std::string& delimiter); - template - std::string join(const ContainerType& list, char delimiter); - - template - std::string join(const ContainerType& list, const std::string& delimiter); - - template - ContainerType tokenize(const std::string& str, char delimiter); - - template - ContainerType tokenize(const std::string& str, const std::string& delimiter); - - template - ContainerType tokenize(const ContainerType& list, char delimiter); - - template - ContainerType tokenize(const ContainerType& list, const std::string& delimiter); + std::deque tokenize(const std::string& str, char delimiter); + std::deque tokenize(const std::string& str, const std::string& delimiter); + std::deque tokenize(const std::deque& list, char delimiter); + std::deque tokenize(const std::deque& 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 -ContainerType utility::split(const std::string& str, char delimiter) -{ - return split(str, std::string(1, delimiter)); -} - -template -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 -std::string utility::join(const ContainerType& list, char delimiter) -{ - return join(list, std::string(1, delimiter)); -} - -template -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 -ContainerType utility::tokenize(const std::string& str, char delimiter) -{ - return tokenize(str, std::string(1, delimiter)); -} - -template -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 -ContainerType utility::tokenize(const ContainerType& list, char delimiter) -{ - return tokenize(list, std::string(1, delimiter)); -} - -template -ContainerType utility::tokenize(const ContainerType& list, const std::string& delimiter) -{ - ContainerType c; - - for (std::string str : list) - { - if (str.size()) - { - ContainerType c2 = tokenize(str, delimiter); - c.insert(c.end(), c2.begin(), c2.end()); - } - } - - return c; -} - #endif // UTILITY_STRING_H diff --git a/src/test/UtilityStringTestSuite.h b/src/test/UtilityStringTestSuite.h index 4e29f849..4227538e 100644 --- a/src/test/UtilityStringTestSuite.h +++ b/src/test/UtilityStringTestSuite.h @@ -7,153 +7,153 @@ class UtilityStringTestSuite : public CxxTest::TestSuite public: void test_split_with_char_delimiter() { - std::vector result = utility::split >("A,B,C", ','); + std::deque 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 result = utility::split >("A->B>C", "->"); + std::deque 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 result = utility::split >("", "->"); + std::deque 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 result = utility::split >("A:B:C", ";"); + std::deque 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 result = utility::split >("A::B:C", ':'); + std::deque 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 result = utility::split >(":B:C", ':'); + std::deque 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 result = utility::split >("B:C:", ':'); + std::deque 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 list; + std::deque list; list.push_back("A"); list.push_back("B"); list.push_back("C"); - std::string result = utility::join >(list, ','); + std::string result = utility::join(list, ','); TS_ASSERT_EQUALS(result, "A,B,C"); } void test_join_with_string_delimiter() { - std::vector list; + std::deque list; list.push_back("A"); list.push_back("B"); list.push_back("C"); - std::string result = utility::join >(list, "=="); + std::string result = utility::join(list, "=="); TS_ASSERT_EQUALS(result, "A==B==C"); } void test_join_on_empty_list() { - std::vector list; - std::string result = utility::join >(list, ','); + std::deque list; + std::string result = utility::join(list, ','); TS_ASSERT_EQUALS(result, ""); } void test_join_with_empty_strings_in_list() { - std::vector list; + std::deque list; list.push_back("A"); list.push_back(""); list.push_back(""); - std::string result = utility::join >(list, ':'); + std::string result = utility::join(list, ':'); TS_ASSERT_EQUALS(result, "A::"); } void test_tokenize_with_string() { - std::vector result = utility::tokenize >("A->B->C", "->"); + std::deque 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 result = utility::tokenize >("->B", "->"); + std::deque 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 result = utility::tokenize >("C+", '+'); + std::deque 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 result = utility::tokenize >("A->B=C->D", "->"); - result = utility::tokenize>(result, "="); + std::deque 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()