utility: removed templates from utilityString functions to use std::deque by default
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user