mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 10:07:59 +02:00
moved string stuff to string_utils.h/.cpp
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user