Commit 552416bd authored by gabime's avatar gabime

fmt_helper cleanup

parent b5224130
......@@ -4,7 +4,8 @@
#pragma once
#include "chrono"
#include <chrono>
#include <type_traits>
#include "spdlog/fmt/fmt.h"
// Some fmt helpers to efficiently format and pad ints and strings
......@@ -41,88 +42,65 @@ inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
dest.append(i.data(), i.data() + i.size());
}
template<size_t Buffer_Size>
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
template<typename T, size_t Buffer_Size>
inline void append_uint(T n, unsigned int width, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
if (n > 99)
{
append_int(n, dest);
return;
}
if (n > 9) // 10-99
{
dest.push_back(static_cast<char>('0' + n / 10));
dest.push_back(static_cast<char>('0' + n % 10));
return;
}
if (n >= 0) // 0-9
static_assert(std::is_unsigned<T>::value, "append_uint must get unsigned T");
auto digits = fmt::internal::count_digits(n);
if(width > digits)
{
dest.push_back('0');
dest.push_back(static_cast<char>('0' + n));
return;
const char* zeroes = "0000000000000000000";
dest.append(zeroes, zeroes + width-digits);
}
// negatives (unlikely, but just in case, let fmt deal with it)
fmt::format_to(dest, "{:02}", n);
append_int(n, dest);
}
template<size_t Buffer_Size>
inline void pad3(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
if (n > 999)
if (n > 99)
{
append_int(n, dest);
return;
}
if (n > 99) // 100-999
{
dest.push_back(static_cast<char>('0' + n / 100));
pad2(n % 100, dest);
return;
}
if (n > 9) // 10-99
else if (n > 9) // 10-99
{
dest.push_back('0');
dest.push_back(static_cast<char>('0' + n / 10));
dest.push_back(static_cast<char>('0' + n % 10));
return;
}
if (n >= 0)
else if (n >= 0) // 0-9
{
dest.push_back('0');
dest.push_back('0');
dest.push_back(static_cast<char>('0' + n));
return;
}
// negatives (unlikely, but just in case let fmt deal with it)
fmt::format_to(dest, "{:03}", n);
else // negatives (unlikely, but just in case, let fmt deal with it)
{
fmt::format_to(dest, "{:02}", n);
}
}
template<size_t Buffer_Size>
inline void pad6(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
template<typename T, size_t Buffer_Size>
inline void pad3(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
if (n > 99999)
{
append_int(n, dest);
return;
}
pad3(static_cast<int>(n / 1000), dest);
pad3(static_cast<int>(n % 1000), dest);
append_uint(n, 3, dest);
}
template<size_t Buffer_Size>
inline void pad9(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
template<typename T, size_t Buffer_Size>
inline void pad6(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
if (n > 99999999)
{
append_int(n, dest);
return;
}
pad6(static_cast<int>(n / 1000), dest);
pad3(static_cast<int>(n % 1000), dest);
append_uint(n, 6, dest);
}
template<typename T, size_t Buffer_Size>
inline void pad9(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
append_uint(n, 9, dest);
}
// return fraction of a second of the given time_point.
// e.g.
// fraction<std::milliseconds>(tp) -> will return the millis part of the second
......
......@@ -452,7 +452,7 @@ public:
scoped_pad p(field_size, padinfo_, dest);
auto millis = fmt_helper::time_fraction<std::chrono::milliseconds>(msg.time);
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
}
};
......@@ -818,7 +818,7 @@ public:
fmt_helper::append_buf(cached_datetime_, dest);
auto millis = fmt_helper::time_fraction<milliseconds>(msg.time);
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
dest.push_back(']');
dest.push_back(' ');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment