accept comma separated rx_dbitlist

This commit is contained in:
Erik Frojdh
2026-05-20 16:08:39 +02:00
parent 83cd91b93c
commit ed8c885de0
3 changed files with 46 additions and 7 deletions
+1 -5
View File
@@ -1226,11 +1226,7 @@ std::string Caller::rx_dbitlist(int action) {
}
// 'none' option already covered as t is empty by default
else if (args[0] != "none") {
unsigned int ntrim = args.size();
t.resize(ntrim);
for (unsigned int i = 0; i < ntrim; ++i) {
t[i] = StringTo<int>(args[i]);
}
t = StringTo<std::vector<int>>(ToString(args));
}
det->setRxDbitList(t, std::vector<int>{det_id});
os << ToString(args) << '\n';
+36 -2
View File
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2021 Contributors to the SLS Detector Package
#pragma once
/**
* \file ToString.h
*
@@ -353,7 +352,9 @@ T StringTo(const std::string &f, const std::string &unit) {
}
}
template <typename T> T StringTo(const std::string &t) {
template <typename T,
std::enable_if_t<!is_container<T>::value, int> = 0>
T StringTo(const std::string &t) {
std::string tmp{t};
auto unit = RemoveUnit(tmp);
return StringTo<T>(tmp, unit);
@@ -398,6 +399,7 @@ ToString(const T &obj) {
return obj.str();
}
/** Convert vector of strings to vector of type T */
template <typename T>
std::vector<T> StringTo(const std::vector<std::string> &strings) {
std::vector<T> result;
@@ -407,4 +409,36 @@ std::vector<T> StringTo(const std::vector<std::string> &strings) {
return result;
}
/** Parse comma-separated string into vector type T (e.g.,
* StringTo<std::vector<int>>()) */
template <typename T, std::enable_if_t<is_container<T>::value &&
!std::is_same_v<T, std::string>,
int> = 0>
T StringTo(const std::string &s) {
using ElementType = typename T::value_type;
T res;
std::istringstream ss(s);
std::string item;
while (std::getline(ss, item, ',')) {
item.erase(std::remove_if(item.begin(), item.end(),
[](char c) {
return c == '[' || c == ']' || c == '"' ||
c == ' ';
}),
item.end());
try {
if (item.empty())
continue;
auto val = StringTo<ElementType>(item);
res.push_back(val);
} catch (const std::exception &e) {
throw RuntimeError("Could not convert '" + item +
"' to the container element type. " +
std::string(e.what()));
}
}
return res;
}
} // namespace sls
+9
View File
@@ -415,4 +415,13 @@ TEST_CASE("string to timingInfoDecoder") {
defs::timingInfoDecoder::SHINE);
}
TEST_CASE("Convert string to vector of ints") {
REQUIRE(StringTo<std::vector<int>>("1, 2, 3") == std::vector<int>{1, 2, 3});
REQUIRE(StringTo<std::vector<int>>(" 4 ,5,6 ") == std::vector<int>{4, 5, 6});
REQUIRE(StringTo<std::vector<int>>("[8]") == std::vector<int>{8});
REQUIRE(StringTo<std::vector<int>>("9, ") == std::vector<int>{9});
REQUIRE(StringTo<std::vector<int>>("") == std::vector<int>{});
}
} // namespace sls