Skip to content
Snippets Groups Projects
Commit 399897dc authored by Soeren Peters's avatar Soeren Peters
Browse files

Removed complete boost dependency in basics.

parent c71893e3
No related branches found
No related tags found
No related merge requests found
......@@ -53,7 +53,5 @@ target_include_directories(${library_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/Cor
target_include_directories(${library_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
include (${CMAKE_PATH}/3rd/mpi.cmake)
include (${CMAKE_PATH}/3rd/boost.cmake)
linkBoost ("")
vf_add_tests()
\ No newline at end of file
#include "StringUtil.h"
#include <boost/algorithm/string.hpp>
#include <regex>
#include <sstream>
std::string StringUtil::findAndReplace(const std::string &source, const std::string& find, const std::string& replace)
{
......@@ -97,11 +98,27 @@ std::vector<std::string> split(const std::string& str, char delim = ' ')
return list;
}
std::vector<std::string> StringUtil::split(const std::string& input, const std::string& delim/*= " "*/)
{
std::stringstream ss;
ss << "[" << delim << "]";
std::regex re(ss.str());
std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last; //the '-1' is what makes the regex split (-1 := what was not matched)
std::vector<std::string> tokens{first, last};
tokens.erase(std::remove_if(tokens.begin(), tokens.end(), [](std::string& token)
{
return token.empty();
}), tokens.end());
return tokens;
}
std::vector<int> StringUtil::toIntVector(const std::string& input)
{
std::vector<int> v;
std::vector<std::string> inputEntries;
boost::algorithm::split(inputEntries, input, boost::is_any_of("\t\n "));
inputEntries = split(input, " \n\t");
for(std::string entry : inputEntries)
if (entry != "")
v.push_back(toInt(entry));
......@@ -112,7 +129,7 @@ std::vector<unsigned int> StringUtil::toUintVector(const std::string & input)
{
std::vector<unsigned int> v;
std::vector<std::string> inputEntries;
boost::algorithm::split(inputEntries, input, boost::is_any_of("\t\n "));
inputEntries = split(input, " \n\t");
for(std::string entry : inputEntries)
if (entry != "")
v.push_back(toInt(entry));
......@@ -123,7 +140,7 @@ std::vector<bool> StringUtil::toBoolVector(const std::string & input)
{
std::vector<bool> v;
std::vector<std::string> inputEntries;
boost::algorithm::split(inputEntries, input, boost::is_any_of("\t\n "));
inputEntries = split(input, " \n\t");
for(std::string entry : inputEntries)
{
bool b = 0;
......@@ -136,20 +153,14 @@ std::vector<bool> StringUtil::toBoolVector(const std::string & input)
std::vector<std::string> StringUtil::toStringVector(const std::string & input)
{
std::vector<std::string> v;
std::vector<std::string> inputEntries;
boost::algorithm::split(inputEntries, input, boost::is_any_of("\t\n "));
for(std::string entry : inputEntries)
if (entry != "")
v.push_back(toString(entry));
return v;
return split(input, " \n\t");
}
BASICS_EXPORT std::vector<double> StringUtil::toDoubleVector(const std::string & input)
{
std::vector<double> v;
std::vector<std::string> inputEntries;
boost::algorithm::split(inputEntries, input, boost::is_any_of("\t\n "));
inputEntries = split(input, " \n\t");
for(std::string entry : inputEntries)
if (entry != "")
v.push_back(toDouble(entry));
......
......@@ -18,6 +18,7 @@ public:
static BASICS_EXPORT std::string findAndReplace(const std::string &source, const std::string& find, const std::string& replace);
static BASICS_EXPORT std::string makeUpper(const std::string& instring);
static BASICS_EXPORT std::string makeLower(const std::string& instring);
static BASICS_EXPORT std::vector<std::string> split(const std::string& input, const std::string& delim = " ");
static BASICS_EXPORT bool contains(const std::string& source, const char *find);
static BASICS_EXPORT std::string pad(const std::string& input, char pad, int length);
static BASICS_EXPORT std::string trim(const std::string &input, const std::string &trim = std::string(" \t\n"));
......
......@@ -36,5 +36,26 @@ TEST(StringUtilTest, toIntVector)
auto result = StringUtil::toIntVector(input);
ASSERT_THAT(result,testing::Eq(expected_result));
}
TEST(StringUtilTest, splitIntoStringsWithDelimeter)
{
const std::string input {"1 2\n3 4\t5"};
const std::string delimeter {" \n\t"};
std::vector<std::string> expected_result {"1", "2", "3", "4", "5"};
auto result = StringUtil::split(input, delimeter);
ASSERT_THAT(result,testing::Eq(expected_result));
}
TEST(StringUtilTest, toStringVector)
{
const std::string input {"1 2\n3 4\t5"};
std::vector<std::string> expected_result {"1", "2", "3", "4", "5"};
auto result = StringUtil::toStringVector(input);
ASSERT_THAT(result,testing::Eq(expected_result));
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment