diff --git a/include/utils.hpp b/include/utils.hpp index c213a1f..4ee0b0d 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -16,9 +17,29 @@ enum just { right, left }; std::string pad(std::string s, uint8_t len, just j = right); std::string center(std::string str, uint8_t len); -std::string pBytes(double bytes, bool isKbytes = false); std::string getFirstDir(const std::string_view& path); +inline std::string pNum(const auto num, const uint8_t decs = 0) { + static std::array buf; + auto [ptr, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), static_cast(num), std::chars_format::fixed, decs); + return {buf.data(), ptr}; +} + +/* converts large number in (k)bytes to human-readable numer with suffix and sane decimal */ +std::string pBytes(const auto bytes, const bool isKbytes = false) { + double bytes_d = bytes; + for (constexpr std::array suffix_list = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'}; const auto& suffix : suffix_list) { + if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes + bytes_d /= 1024.0; + if (bytes_d < 1024) { + if (bytes_d > 100) return pNum(bytes_d) + suffix; + if (bytes_d > 10) return pNum(bytes_d, 1) + suffix; + return pNum(bytes_d, 2) + suffix; + } + } + return "++++"; +} + /* reads file into a std::array buffer */ template inline ssize_t readFile(const std::string& path, std::array& buffer) { @@ -50,13 +71,6 @@ inline void fastReadNum(const std::string& path, std::array& buf, T& re result = static_cast(temp / Divider); } -inline std::string pNum(const auto num, const uint8_t decs = 0) { - static std::array buf; - auto [ptr, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), num, std::chars_format::fixed, decs); - return std::string(buf.data(), ptr); -} - - inline bool fileExists(const std::string_view& path) { return std::filesystem::exists(path); } diff --git a/src/utils.cpp b/src/utils.cpp index 490449b..f91805e 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,7 +1,5 @@ -#include #include #include -#include #include #include #include @@ -23,21 +21,6 @@ std::string center(std::string str, const uint8_t len) { return str; } -/* converts large number in (k)bytes to human-readable numer with suffix and sane decimal */ -std::string pBytes(double bytes, const bool isKbytes) { - for (constexpr std::array suffix_list = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'}; const auto& suffix : suffix_list) { - if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes - bytes /= 1024.0; - if (bytes < 1024) { - if (bytes > 100) return pNum(bytes) + suffix; - if (bytes > 10) return pNum(bytes, 1) + suffix; - return pNum(bytes, 2) + suffix; - } - } - return "++++"; -} - - /* finds first dir in path and returns it. don't refactor, you don't understand it, its fast enough, and you only use it on init */ std::string getFirstDir(const std::string_view& path) { for (const auto& entry : std::filesystem::directory_iterator(path))