This commit is contained in:
2020-06-23 09:30:17 +02:00
parent f6911c4238
commit 159b0a0367
4 changed files with 39 additions and 7 deletions

View File

@ -144,6 +144,24 @@ ToStringHex(const T &container) {
return os.str();
}
template <typename T>
typename std::enable_if<
is_container<T>::value &&
!std::is_same<typename T::value_type, std::string>::value,
std::string>::type
ToStringHex(const T &container, int width) {
std::ostringstream os;
os << '[';
if (!container.empty()) {
auto it = container.cbegin();
os << ToStringHex(*it++, width);
while (it != container.cend())
os << ", " << ToStringHex(*it++, width);
}
os << ']';
return os.str();
}
template <typename KeyType, typename ValueType>
std::string ToString(const std::map<KeyType, ValueType> &m) {
std::ostringstream os;

View File

@ -12,6 +12,7 @@
using sls::defs;
using sls::StringTo;
using sls::ToString;
using sls::ToStringHex;
using namespace sls::time;
TEST_CASE("Integer conversions", "[support]") {
@ -260,4 +261,16 @@ TEST_CASE("vector of dac index to string") {
std::vector<defs::dacIndex> daci{defs::VCASSH, defs::VTH2, defs::VRSHAPER};
auto r = ToString(daci);
REQUIRE(r == "[vcassh, vth2, vrshaper]");
}
TEST_CASE("int or uin64_t to a string in hex") {
REQUIRE(ToStringHex(65535) == "0xffff");
REQUIRE(ToStringHex(65535, 8) == "0x0000ffff");
REQUIRE(ToStringHex(8927748192) == "0x21422a060");
REQUIRE(ToStringHex(8927748192, 16) == "0x000000021422a060");
std::vector<int> temp{244, 65535, 1638582};
auto r = ToStringHex(temp);
REQUIRE(r == "[0xf4, 0xffff, 0x1900b6]");
r = ToStringHex(temp, 8);
REQUIRE(r == "[0x000000f4, 0x0000ffff, 0x001900b6]");
}