Skip to content

Commit d4d436f

Browse files
committed
fix: correct slop code to be use-case specific
1 parent 1472742 commit d4d436f

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/util.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
#include <cmath>
44
#include <sstream>
5+
#include <stdexcept>
56
#include <string>
7+
#include <type_traits>
68
#include <vector>
79

810
template <typename T> void split_response(const std::string& str, std::vector<T>& vec) {
11+
static_assert(std::is_integral_v<T>, "T must be an integral type.");
12+
913
std::stringstream ss(str);
1014
std::string item;
15+
1116
while (std::getline(ss, item, ',')) {
12-
if constexpr (std::is_floating_point_v<T>) {
13-
vec.push_back(static_cast<T>(std::stod(item)));
14-
} else if constexpr (std::is_integral_v<T>) {
15-
double item_double = std::stod(item);
16-
vec.push_back(static_cast<T>(std::floor(item_double)));
17-
} else {
18-
static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type.");
17+
try {
18+
vec.push_back(static_cast<T>(std::stoll(item)));
19+
} catch (const std::exception&) {
20+
throw std::runtime_error("Server call returned malformed response string: " + str);
1921
}
2022
}
2123
}

0 commit comments

Comments
 (0)