use fmtlib instead of sstream in numToString and intToHex

This commit is contained in:
matcool 2024-11-15 16:43:36 -03:00
parent 5645399a9f
commit bebc7b4074

View file

@ -5,7 +5,6 @@
#include "../DefaultInclude.hpp" #include "../DefaultInclude.hpp"
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
@ -64,9 +63,7 @@ namespace geode {
template <typename T> template <typename T>
std::string intToHex(T i) { std::string intToHex(T i) {
std::stringstream stream; return fmt::format("{:#x}", i);
stream << std::showbase << std::setbase(16) << (uint64_t)i;
return stream.str();
} }
/** /**
@ -75,15 +72,16 @@ namespace geode {
* @param num Number to convert to string * @param num Number to convert to string
* @param precision Precision of the converted number * @param precision Precision of the converted number
* @returns Number as string * @returns Number as string
* @note Precision has no effect on integers
*/ */
template <class Num> template <class Num>
std::string numToString(Num num, size_t precision = 0) { std::string numToString(Num num, size_t precision = 0) {
std::stringstream ss; if constexpr (std::is_floating_point_v<Num>) {
if (precision) { if (precision) {
ss << std::fixed << std::setprecision(precision); return fmt::format("{:.{}f}", num, precision);
}
} }
ss << num; return fmt::to_string(num);
return ss.str();
} }
/** /**