cpp : more robust code and added more documentation

This commit is contained in:
saundersp
2024-04-27 21:08:33 +02:00
parent 45f0f6ab8e
commit c7d21e1014
10 changed files with 355 additions and 319 deletions

View File

@ -1,19 +1,17 @@
#include "toolbox.hpp"
#include <numeric>
#include <algorithm>
static constexpr uint64_t u64(const double& n) noexcept { return static_cast<uint64_t>(n); }
inline static constexpr uint64_t u64(const double& n) noexcept { return static_cast<uint64_t>(n); }
static const constexpr size_t N_TIMES = 11;
static const constexpr std::array<const char*, N_TIMES> time_formats = { "ns", "µs", "ms", "s", "m", "h", "j", "w", "M", "y", "c" };
static const constexpr std::array<const char*, N_TIMES> time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y", "c" };
static const constexpr std::array<uint64_t, N_TIMES> time_numbers = { 1, u64(1e3), u64(1e6), u64(1e9), u64(6e10), u64(36e11), u64(864e11),
u64(6048e11), u64(26784e11), u64(31536e12), u64(31536e14) };
/**
* @brief Format the time in seconds in human readable format.
*
* @param time Time in seconds
* @return std::string The formatted human readable string.
* @param time number of seconds
* @return The formatted human readable string.
*/
std::string format_time(uint64_t time) noexcept {
if (time == 0)
@ -21,8 +19,8 @@ std::string format_time(uint64_t time) noexcept {
std::string s = "";
uint64_t res;
for (int i = N_TIMES - 1; i >= 3; --i) {
const uint64_t time_number = time_numbers[i] / 1e9; // Converting nanosecond timestamp to second
for (int32_t i = N_TIMES - 1; i >= 3; --i) {
const uint64_t time_number = time_numbers[i] / u64(1e9); // Converting nanosecond timestamp to second
if (time >= time_number) {
res = time / time_number;
time %= time_number;
@ -30,8 +28,8 @@ std::string format_time(uint64_t time) noexcept {
}
}
if (s.back() == ' ')
s.pop_back();
// Remove trailing character
s.pop_back();
return s;
}
@ -48,7 +46,7 @@ std::string format_time_ns(uint64_t time) noexcept {
std::string s = "";
uint64_t res;
for (int i = N_TIMES - 1; i >= 0; --i) {
for (int32_t i = N_TIMES - 1; i >= 0; --i) {
if (time >= time_numbers[i]) {
res = time / time_numbers[i];
time %= time_numbers[i];
@ -56,8 +54,8 @@ std::string format_time_ns(uint64_t time) noexcept {
}
}
if (s.back() == ' ')
s.pop_back();
// Remove trailing character
s.pop_back();
return s;
}
@ -71,7 +69,7 @@ static const constexpr uint64_t total_bytes = u64(1)<<(10 * (N_BYTES - 1));
* See more : https://en.wikipedia.org/wiki/JEDEC_memory_standards
*
* @param bytes Number of bytes
* @return std::string JEDEC compliant formatted number of bytes
* @return JEDEC compliant formatted number of bytes
*/
std::string format_byte_size(uint64_t bytes) noexcept {
if (bytes == 0)
@ -95,6 +93,13 @@ std::string format_byte_size(uint64_t bytes) noexcept {
return s;
}
/**
* @brief Format a number with a separator (i.e. 1000 as 1,000)
*
* @param k number to format
* @param separator used between each thouand
* @return Formatted number
*/
std::string thousand_sep(uint64_t k, const char& separator) noexcept {
const std::string n = std::to_string(k);
const uint64_t st_size = n.length() + (n.length() - 1) / 3;
@ -110,5 +115,13 @@ std::string thousand_sep(uint64_t k, const char& separator) noexcept {
}
return s;
}
//uint64_t len = n.length(), dlen = 3;
//while (len > dlen) {
// n.insert(len - dlen, 1, separator);
// dlen += 4;
// len += 1;
//}
//return n;
}