mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-05-02 19:00:05 +02:00
moved string stuff to string_utils.h/.cpp
This commit is contained in:
parent
81a49babda
commit
b7aac059c3
@ -100,40 +100,6 @@ T minusOneIfDifferent(const std::vector<T>& container)
|
||||
|
||||
|
||||
|
||||
//TODO!(Erik)Should try to move away from using this in the slsDetectorPackage
|
||||
inline
|
||||
std::string concatenateIfDifferent(std::vector<std::string> container)
|
||||
{
|
||||
if (allEqual(container)) {
|
||||
return container.front();
|
||||
} else {
|
||||
std::string result;
|
||||
for (const auto& s : container)
|
||||
result += s + "+";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
inline
|
||||
std::vector<std::string> split(const std::string& strToSplit, char delimeter)
|
||||
{
|
||||
std::stringstream ss(strToSplit);
|
||||
std::string item;
|
||||
std::vector<std::string> splittedStrings;
|
||||
while (std::getline(ss, item, delimeter)) {
|
||||
splittedStrings.push_back(item);
|
||||
}
|
||||
return splittedStrings;
|
||||
}
|
||||
|
||||
inline
|
||||
std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
|
||||
std::string ret;
|
||||
for (const auto& s : vec)
|
||||
if (!s.empty())
|
||||
ret += s + "+";
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
|
||||
#endif // CONTAINER_UTILS_H
|
||||
|
@ -1,11 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
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 <size_t array_size>
|
||||
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<std::string> split(const std::string& strToSplit, char delimeter);
|
||||
|
||||
/*
|
||||
Concatenate the non empty strings in the vector using +
|
||||
*/
|
||||
std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec);
|
||||
|
||||
/*
|
||||
Concatenate strings using + if the strings are different
|
||||
*/
|
||||
std::string concatenateIfDifferent(std::vector<std::string> container);
|
||||
|
||||
}; // namespace sls
|
||||
|
@ -1,9 +1,41 @@
|
||||
|
||||
// #include <cstring>
|
||||
// namespace sls{
|
||||
#include <sstream>
|
||||
#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);
|
||||
// }
|
||||
|
||||
// };
|
||||
std::vector<std::string> split(const std::string& strToSplit, char delimeter)
|
||||
{
|
||||
std::stringstream ss(strToSplit);
|
||||
std::string item;
|
||||
std::vector<std::string> splittedStrings;
|
||||
while (std::getline(ss, item, delimeter)) {
|
||||
splittedStrings.push_back(item);
|
||||
}
|
||||
return splittedStrings;
|
||||
}
|
||||
|
||||
|
||||
std::string concatenateNonEmptyStrings(const std::vector<std::string>& vec){
|
||||
std::string ret;
|
||||
for (const auto& s : vec)
|
||||
if (!s.empty())
|
||||
ret += s + '+';
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string concatenateIfDifferent(std::vector<std::string> container)
|
||||
{
|
||||
if (allEqual(container)) {
|
||||
return container.front();
|
||||
} else {
|
||||
std::string result;
|
||||
for (const auto& s : container)
|
||||
result += s + '+';
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
@ -104,57 +104,3 @@ TEST_CASE("minus one does not have side effects"){
|
||||
REQUIRE(v[0]==1);
|
||||
}
|
||||
|
||||
TEST_CASE("Concat") {
|
||||
std::vector<std::string> v{"one", "one", "one"};
|
||||
std::vector<std::string> 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<std::string> 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<std::string> vec{"", "", ""};
|
||||
REQUIRE(vec.size()==3);
|
||||
auto ret = sls::concatenateNonEmptyStrings(vec);
|
||||
REQUIRE(ret.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("concatenate non empty strings with one element"){
|
||||
std::vector<std::string> vec{"", "hej", "", "", ""};
|
||||
REQUIRE(vec.size()==5);
|
||||
auto ret = sls::concatenateNonEmptyStrings(vec);
|
||||
REQUIRE(ret=="hej+");
|
||||
}
|
@ -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<std::string> v{"one", "one", "one"};
|
||||
std::vector<std::string> 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<std::string> 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<std::string> vec{"", "", ""};
|
||||
REQUIRE(vec.size()==3);
|
||||
auto ret = sls::concatenateNonEmptyStrings(vec);
|
||||
REQUIRE(ret.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("concatenate non empty strings with one element"){
|
||||
std::vector<std::string> vec{"", "hej", "", "", ""};
|
||||
REQUIRE(vec.size()==5);
|
||||
auto ret = sls::concatenateNonEmptyStrings(vec);
|
||||
REQUIRE(ret=="hej+");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user