From b7aac059c3cb935e3e0b473bd40171d7fbb8a6b2 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 9 Jan 2019 11:19:16 +0100 Subject: [PATCH] moved string stuff to string_utils.h/.cpp --- slsSupportLib/include/container_utils.h | 34 --------------- slsSupportLib/include/string_utils.h | 25 ++++++++++- slsSupportLib/src/string_utils.cpp | 44 ++++++++++++++++--- tests/src/test-container_utils.cpp | 54 ----------------------- tests/src/test-string_utils.cpp | 58 ++++++++++++++++++++++++- 5 files changed, 118 insertions(+), 97 deletions(-) diff --git a/slsSupportLib/include/container_utils.h b/slsSupportLib/include/container_utils.h index 2b327b0f3..163e8f196 100644 --- a/slsSupportLib/include/container_utils.h +++ b/slsSupportLib/include/container_utils.h @@ -100,40 +100,6 @@ T minusOneIfDifferent(const std::vector& container) -//TODO!(Erik)Should try to move away from using this in the slsDetectorPackage -inline -std::string concatenateIfDifferent(std::vector container) -{ - if (allEqual(container)) { - return container.front(); - } else { - std::string result; - for (const auto& s : container) - result += s + "+"; - return result; - } -} -inline -std::vector split(const std::string& strToSplit, char delimeter) -{ - std::stringstream ss(strToSplit); - std::string item; - std::vector splittedStrings; - while (std::getline(ss, item, delimeter)) { - splittedStrings.push_back(item); - } - return splittedStrings; -} - -inline -std::string concatenateNonEmptyStrings(const std::vector& vec){ - std::string ret; - for (const auto& s : vec) - if (!s.empty()) - ret += s + "+"; - return ret; -} - } // namespace sls #endif // CONTAINER_UTILS_H diff --git a/slsSupportLib/include/string_utils.h b/slsSupportLib/include/string_utils.h index b5f8adc08..c6fda9542 100644 --- a/slsSupportLib/include/string_utils.h +++ b/slsSupportLib/include/string_utils.h @@ -1,11 +1,16 @@ +#pragma once + +#include +#include + namespace sls { /* Implementation of a safe string copy function for setting fields in for example the multi sls detector. It tries to copy the size of the destination from the source, stopping on '\0'. -Warning this would truncate the source string and should be used with care. -Still this is better than strcpy... +Warning this will truncate the source string and should be used with care. +Still this is better than strcpy and a buffer overflow... */ template void strcpy_safe(char (&destination)[array_size], const char *source) { @@ -13,5 +18,21 @@ void strcpy_safe(char (&destination)[array_size], const char *source) { destination[array_size - 1] = '\0'; } +/* +Split a string using the specified delimeter and return a vector of strings. +TODO! Look into switching to absl or a string_view based implementation. Current +implementation should not be used in a performance critical place. +*/ +std::vector split(const std::string& strToSplit, char delimeter); + +/* +Concatenate the non empty strings in the vector using + +*/ +std::string concatenateNonEmptyStrings(const std::vector& vec); + +/* +Concatenate strings using + if the strings are different +*/ +std::string concatenateIfDifferent(std::vector container); }; // namespace sls diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 9429c84b9..1a631bcf7 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -1,9 +1,41 @@ -// #include -// namespace sls{ +#include +#include "string_utils.h" +#include "container_utils.h" +namespace sls{ -// void strcpy_safe(char *dst, const char *src, size_t size){ -// strncpy(dst, src, size); -// } -// }; \ No newline at end of file +std::vector split(const std::string& strToSplit, char delimeter) +{ + std::stringstream ss(strToSplit); + std::string item; + std::vector splittedStrings; + while (std::getline(ss, item, delimeter)) { + splittedStrings.push_back(item); + } + return splittedStrings; +} + + +std::string concatenateNonEmptyStrings(const std::vector& vec){ + std::string ret; + for (const auto& s : vec) + if (!s.empty()) + ret += s + '+'; + return ret; +} + +std::string concatenateIfDifferent(std::vector container) +{ + if (allEqual(container)) { + return container.front(); + } else { + std::string result; + for (const auto& s : container) + result += s + '+'; + return result; + } +} + + +}; \ No newline at end of file diff --git a/tests/src/test-container_utils.cpp b/tests/src/test-container_utils.cpp index 4f1704319..d4572f131 100644 --- a/tests/src/test-container_utils.cpp +++ b/tests/src/test-container_utils.cpp @@ -104,57 +104,3 @@ TEST_CASE("minus one does not have side effects"){ REQUIRE(v[0]==1); } -TEST_CASE("Concat") { - std::vector v{"one", "one", "one"}; - std::vector v2{"one", "one", "one"}; - auto r = sls::concatenateIfDifferent(v); - REQUIRE(r == std::string("one")); - r.clear(); - - // make sure we didn't modify the string - REQUIRE(v == v2); - - SECTION("add a different value"){ - v.push_back("two"); - REQUIRE(v!=v2); - REQUIRE( sls::concatenateIfDifferent(v) == "one+one+one+two+"); - } -} - -TEST_CASE("split a string with end delimiter"){ - std::string s("abra+kadabra+"); - auto r =sls::split(s, '+'); - REQUIRE(r.size()==2); - REQUIRE(r[0]=="abra"); - REQUIRE(r[1]=="kadabra"); -} - -TEST_CASE("split a string without end delimiter"){ - std::string s("abra+kadabra+filibom"); - auto r =sls::split(s, '+'); - REQUIRE(r.size()==3); - REQUIRE(r[0]=="abra"); - REQUIRE(r[1]=="kadabra"); - REQUIRE(r[2]=="filibom"); -} - -TEST_CASE("concatenate non empty strings"){ - std::vector vec{"hej", "kalas", "", "foto"}; - REQUIRE(vec.size()==4); - auto ret = sls::concatenateNonEmptyStrings(vec); - REQUIRE(ret == "hej+kalas+foto+"); -} - -TEST_CASE("concatenate non empty strings with only emty"){ - std::vector vec{"", "", ""}; - REQUIRE(vec.size()==3); - auto ret = sls::concatenateNonEmptyStrings(vec); - REQUIRE(ret.empty()); -} - -TEST_CASE("concatenate non empty strings with one element"){ - std::vector vec{"", "hej", "", "", ""}; - REQUIRE(vec.size()==5); - auto ret = sls::concatenateNonEmptyStrings(vec); - REQUIRE(ret=="hej+"); -} \ No newline at end of file diff --git a/tests/src/test-string_utils.cpp b/tests/src/test-string_utils.cpp index 86b2ad140..5bce9d2d3 100644 --- a/tests/src/test-string_utils.cpp +++ b/tests/src/test-string_utils.cpp @@ -30,5 +30,61 @@ TEST_CASE("copy a long string"){ sls::strcpy_safe(dst, src); REQUIRE(dst[0]=='s'); REQUIRE(dst[1]=='o'); - REQUIRE(dst[3]=='\0'); + REQUIRE(dst[2]=='\0'); + } + +TEST_CASE("Concat") { + std::vector v{"one", "one", "one"}; + std::vector v2{"one", "one", "one"}; + auto r = sls::concatenateIfDifferent(v); + REQUIRE(r == std::string("one")); + r.clear(); + + // make sure we didn't modify the string + REQUIRE(v == v2); + + SECTION("add a different value"){ + v.push_back("two"); + REQUIRE(v!=v2); + REQUIRE( sls::concatenateIfDifferent(v) == "one+one+one+two+"); + } +} + +TEST_CASE("split a string with end delimiter"){ + std::string s("abra+kadabra+"); + auto r =sls::split(s, '+'); + REQUIRE(r.size()==2); + REQUIRE(r[0]=="abra"); + REQUIRE(r[1]=="kadabra"); +} + +TEST_CASE("split a string without end delimiter"){ + std::string s("abra+kadabra+filibom"); + auto r =sls::split(s, '+'); + REQUIRE(r.size()==3); + REQUIRE(r[0]=="abra"); + REQUIRE(r[1]=="kadabra"); + REQUIRE(r[2]=="filibom"); +} + +TEST_CASE("concatenate non empty strings"){ + std::vector vec{"hej", "kalas", "", "foto"}; + REQUIRE(vec.size()==4); + auto ret = sls::concatenateNonEmptyStrings(vec); + REQUIRE(ret == "hej+kalas+foto+"); +} + +TEST_CASE("concatenate non empty strings with only emty"){ + std::vector vec{"", "", ""}; + REQUIRE(vec.size()==3); + auto ret = sls::concatenateNonEmptyStrings(vec); + REQUIRE(ret.empty()); +} + +TEST_CASE("concatenate non empty strings with one element"){ + std::vector vec{"", "hej", "", "", ""}; + REQUIRE(vec.size()==5); + auto ret = sls::concatenateNonEmptyStrings(vec); + REQUIRE(ret=="hej+"); +} \ No newline at end of file