Compare commits
28 Commits
3b9d9775bf
...
feature/sm
| Author | SHA1 | Date | |
|---|---|---|---|
| 045363c733 | |||
| 4d05a9a8fa | |||
| ee1153c393 | |||
| b29695e148 | |||
| 86930fd8b0 | |||
| 7796be7cd4 | |||
| 73a50cae0f | |||
| 91398ef23d | |||
| b5ecd73b18 | |||
| 826f3a54b3 | |||
| ea722ec5bf | |||
| 5ba1723295 | |||
| 8e51facd10 | |||
| 9bd1104e5f | |||
| 6ba83fd582 | |||
| 8f7b1cd4e3 | |||
| ec70a5bba1 | |||
| 1263dfee98 | |||
| 52992e255e | |||
| 8526ae1d91 | |||
| b8f59710d3 | |||
| 184815c75b | |||
| 74c4a4d582 | |||
| 2933676ea2 | |||
| 7115e09aca | |||
| f9099ce6ee | |||
| 4c3024e978 | |||
| a921676b77 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
build
|
build
|
||||||
cmake-build-debug
|
cmake-build-debug
|
||||||
cmake-build-release
|
cmake-build-release
|
||||||
|
cmake-build-relwithdebinfo
|
||||||
.idea
|
.idea
|
||||||
|
|||||||
@@ -11,8 +11,7 @@ During compile-time, the string to be formatted is preprocessed to the point onl
|
|||||||
have to be written (If they are not available at compile time).
|
have to be written (If they are not available at compile time).
|
||||||
|
|
||||||
For example `One number: {:03}; And another one: {:05.3}` is preprocessed into `One number: 000; And another one: 00.000`.
|
For example `One number: {:03}; And another one: {:05.3}` is preprocessed into `One number: 000; And another one: 00.000`.
|
||||||
This is returned as a `std::array<char, N>`, where `N` is automatically evaluated. The only code executed at compile
|
This is returned as a `std::array<char, N>`, where `N` is automatically evaluated. The only code executed at runtime then formats the numbers and writes them into their places in the array.
|
||||||
time then formats the numbers and writes them into their place in the array.
|
|
||||||
|
|
||||||
Disclaimer: The actual formatting code is largely shamelessly stolen from `fmtlib`.
|
Disclaimer: The actual formatting code is largely shamelessly stolen from `fmtlib`.
|
||||||
|
|
||||||
@@ -48,8 +47,11 @@ $ ctest --test-dir build/
|
|||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
|
For the compile time preprocessing of format strings non non-type template parameters are heavily relied upon,
|
||||||
|
which means C++20 is required.
|
||||||
|
|
||||||
Only a relatively limited subset of the `fmtlib` syntax is recognized (for now anyway). In particular,
|
Only a relatively limited subset of the `fmtlib` syntax is recognized (for now anyway). In particular,
|
||||||
there is no support for positional arguments, alignment, chrono format specs and custom const_format specifications.
|
float formatting in decimal and integer formatting in decimal, binary and hexadecimal are supported.
|
||||||
|
|
||||||
By nature of the library design, which forces compile-time preprocessing of the const_format string, no dynamic width or
|
By nature of the library design, which forces compile-time preprocessing of the const_format string, no dynamic width or
|
||||||
dynamic precision can be implemented.
|
dynamic precision can be implemented.
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ constexpr inline void format_arg(char* dest, const char* arg) {
|
|||||||
dest = dest + fmt_data.length - len;
|
dest = dest + fmt_data.length - len;
|
||||||
|
|
||||||
if (!std::is_constant_evaluated()) {
|
if (!std::is_constant_evaluated()) {
|
||||||
std::memcpy(dest, arg, len);
|
memcpy(dest, arg, len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (std::size_t i = 0; i < len; ++i) {
|
for (std::size_t i = 0; i < len; ++i) {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "stdlib.h"
|
||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -69,11 +70,21 @@ constexpr inline auto count_digits_base(uint64_t n) -> int {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
if (!std::is_constant_evaluated()) {
|
if constexpr (t_format_type == FormatType::x) {
|
||||||
return do_count_digits_decimal(n);
|
int result = 0;
|
||||||
}
|
|
||||||
|
|
||||||
return count_digits_decimal_fallback(n);
|
while (n) {
|
||||||
|
n = n >> 4;
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (result + count_digits_base<FormatType::b>(n));
|
||||||
|
} else {
|
||||||
|
if (!std::is_constant_evaluated()) {
|
||||||
|
return do_count_digits_decimal(n);
|
||||||
|
}
|
||||||
|
return count_digits_decimal_fallback(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,25 +96,53 @@ constexpr inline const char* digits2_base(size_t value) {
|
|||||||
if constexpr (t_format_type == FormatType::b) {
|
if constexpr (t_format_type == FormatType::b) {
|
||||||
return &"00011011"[value * 2];
|
return &"00011011"[value * 2];
|
||||||
} else {
|
} else {
|
||||||
return &"0001020304050607080910111213141516171819"
|
if constexpr (t_format_type == FormatType::x) {
|
||||||
"2021222324252627282930313233343536373839"
|
// clang-format off
|
||||||
"4041424344454647484950515253545556575859"
|
return &"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"
|
||||||
"6061626364656667686970717273747576777879"
|
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F"
|
||||||
"8081828384858687888990919293949596979899"[value * 2];
|
"404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F"
|
||||||
|
"606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F"
|
||||||
|
"808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F"
|
||||||
|
"A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
|
||||||
|
"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
|
||||||
|
"E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"[value * 2];
|
||||||
|
// clang-format on
|
||||||
|
} else {
|
||||||
|
return &"0001020304050607080910111213141516171819"
|
||||||
|
"2021222324252627282930313233343536373839"
|
||||||
|
"4041424344454647484950515253545556575859"
|
||||||
|
"6061626364656667686970717273747576777879"
|
||||||
|
"8081828384858687888990919293949596979899"[value * 2];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
constexpr inline void copy2(char* dst, const char* src) {
|
constexpr inline void copy2(char* dst, const char* src) {
|
||||||
|
if (!std::is_constant_evaluated()) {
|
||||||
|
memcpy(dst, src, 2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
*dst++ = static_cast<char>(*src++);
|
*dst++ = static_cast<char>(*src++);
|
||||||
*dst = static_cast<char>(*src);
|
*dst = static_cast<char>(*src);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <FormatType t_format_type>
|
||||||
|
consteval inline unsigned get_base_divisor() {
|
||||||
|
switch (t_format_type) {
|
||||||
|
case FormatType::b:
|
||||||
|
return 2;
|
||||||
|
case FormatType::x:
|
||||||
|
return 16;
|
||||||
|
default:
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <FormatType t_format_type, typename uint_t>
|
template <FormatType t_format_type, typename uint_t>
|
||||||
constexpr inline void format_base(char* out, uint_t value, int n_digits,
|
constexpr inline void format_base(char* out, uint_t value, int n_digits,
|
||||||
int size) {
|
int size) {
|
||||||
constexpr unsigned divisor = (t_format_type == FormatType::b) ? 2 : 10;
|
constexpr unsigned divisor = get_base_divisor<t_format_type>();
|
||||||
constexpr unsigned square_divisor = const_pow(divisor, 2);
|
constexpr unsigned square_divisor = const_pow(divisor, 2);
|
||||||
|
|
||||||
if (n_digits > size) {
|
if (n_digits > size) {
|
||||||
@@ -122,7 +161,7 @@ constexpr inline void format_base(char* out, uint_t value, int n_digits,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (value < divisor) {
|
if (value < divisor) {
|
||||||
*--out = static_cast<char>('0' + value);
|
*--out = digits2_base<t_format_type>(value*divisor)[0];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,7 +199,6 @@ constexpr std::pair<int_t, bool> get_abs_value(int_t value) {
|
|||||||
|
|
||||||
template <fmt_data_t t_fmt_node, std::unsigned_integral uint_t>
|
template <fmt_data_t t_fmt_node, std::unsigned_integral uint_t>
|
||||||
constexpr inline void format_int(char* out, uint_t value) {
|
constexpr inline void format_int(char* out, uint_t value) {
|
||||||
// format_decimal(out, value, count_digits(value), t_fmt_node.length);
|
|
||||||
format_base<t_fmt_node.type>(out, value,
|
format_base<t_fmt_node.type>(out, value,
|
||||||
count_digits_base<t_fmt_node.type>(value),
|
count_digits_base<t_fmt_node.type>(value),
|
||||||
t_fmt_node.length);
|
t_fmt_node.length);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
// clang-const_format off
|
// clang-format off
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// clang-const_format on
|
// clang-format on
|
||||||
|
|
||||||
|
|
||||||
namespace const_fmt { namespace const_fmt_detail {
|
namespace const_fmt { namespace const_fmt_detail {
|
||||||
|
|||||||
300
const_fmt/stdlib.h
Normal file
300
const_fmt/stdlib.h
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
#ifndef CONST_FMT_STDLIB_H
|
||||||
|
#define CONST_FMT_STDLIB_H
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Disclaimer: Very bad (partially at least) implementation of some features of
|
||||||
|
* the C++ Standard Library. Not meant as a full-on stdlib implementation, only
|
||||||
|
* for usage in this project (Underlined by the fact, that in this case the
|
||||||
|
* namespace std is actually const_fmt::std)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef CONST_FMT_NO_CPP_STDLIB
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace const_fmt { namespace std {
|
||||||
|
|
||||||
|
|
||||||
|
using size_t = uint16_t;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* type_traits
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// various
|
||||||
|
|
||||||
|
|
||||||
|
constexpr inline bool is_constant_evaluated() noexcept {
|
||||||
|
return __builtin_is_constant_evaluated();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct true_type {
|
||||||
|
constexpr static bool value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct false_type {
|
||||||
|
constexpr static bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// is_same
|
||||||
|
|
||||||
|
|
||||||
|
template <typename fist_t, typename second_t>
|
||||||
|
struct is_same : public false_type {};
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
struct is_same<type_t, type_t> : public true_type {};
|
||||||
|
|
||||||
|
|
||||||
|
// is_one_of
|
||||||
|
|
||||||
|
|
||||||
|
template <typename...>
|
||||||
|
struct is_one_of;
|
||||||
|
template <typename type_t>
|
||||||
|
struct is_one_of<type_t> {
|
||||||
|
constexpr static bool value = false;
|
||||||
|
};
|
||||||
|
template <typename type_t, typename first_t, typename... rest_t>
|
||||||
|
struct is_one_of<type_t, first_t, rest_t...> {
|
||||||
|
constexpr static bool value = std::is_same<type_t, first_t>::value ||
|
||||||
|
is_one_of<type_t, rest_t...>::value;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// remove_x
|
||||||
|
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
template <typename type_t> struct remove_cv { typedef type_t type; };
|
||||||
|
template <typename type_t> struct remove_cv<const type_t> { typedef type_t type; };
|
||||||
|
template <typename type_t> struct remove_cv<volatile type_t> { typedef type_t type; };
|
||||||
|
template <typename type_t> struct remove_cv<const volatile type_t> { typedef type_t type; };
|
||||||
|
|
||||||
|
template <typename type_t> struct remove_const { typedef type_t type; };
|
||||||
|
template <typename type_t> struct remove_const<const type_t> { typedef type_t type; };
|
||||||
|
|
||||||
|
template <typename type_t> struct remove_volatile { typedef type_t type; };
|
||||||
|
template <typename type_t> struct remove_volatile<volatile type_t> { typedef type_t type; };
|
||||||
|
|
||||||
|
template <typename type_t> struct remove_reference { using type = type_t; };
|
||||||
|
template <typename type_t> struct remove_reference<type_t &> { using type = type_t; };
|
||||||
|
template <typename type_t> struct remove_reference<type_t &&> { using type = type_t; };
|
||||||
|
|
||||||
|
template <typename type_t> using remove_reference_t = typename std::remove_reference<type_t>::type;
|
||||||
|
template <typename type_t> using remove_cv_t = typename std::remove_cv<type_t>::type;
|
||||||
|
template <typename type_t> using remove_const_t = typename std::remove_const<type_t>::type;
|
||||||
|
template <typename type_t> using remove_volatile_t = typename std::remove_volatile<type_t>::type;
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
|
||||||
|
// is_integral
|
||||||
|
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
using is_integral =
|
||||||
|
is_one_of<remove_cv_t<type_t>, bool, char, signed char, unsigned char,
|
||||||
|
wchar_t, char16_t, char32_t, short, unsigned short, int,
|
||||||
|
unsigned int, long, unsigned long, long long, unsigned long long>;
|
||||||
|
|
||||||
|
|
||||||
|
// is_signed
|
||||||
|
|
||||||
|
|
||||||
|
template <typename _Tp>
|
||||||
|
using is_signed_integer = is_one_of<remove_cv_t<_Tp>, signed char, signed short,
|
||||||
|
signed int, signed long, signed long long>;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// is_floating_point
|
||||||
|
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
using is_floating_point =
|
||||||
|
is_one_of<remove_cv_t<type_t>, float, double, long double>;
|
||||||
|
|
||||||
|
|
||||||
|
// make_unsigned
|
||||||
|
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
template <typename type_t> struct make_unsigned { using type = type_t; };
|
||||||
|
|
||||||
|
template <> struct make_unsigned<signed char> { using type = char; };
|
||||||
|
template <> struct make_unsigned<unsigned short> { using type = unsigned short; };
|
||||||
|
template <> struct make_unsigned<signed int> { using type = unsigned int; };
|
||||||
|
template <> struct make_unsigned<signed long> { using type = unsigned long; };
|
||||||
|
template <> struct make_unsigned<signed long long> { using type = unsigned long long; };
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
|
||||||
|
// value definitions
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
inline constexpr bool is_integral_v = is_integral<type_t>::value;
|
||||||
|
template <typename type_t>
|
||||||
|
inline constexpr bool is_signed_integer_v = is_signed_integer<type_t>::value;
|
||||||
|
template <typename type_t>
|
||||||
|
inline constexpr bool is_floating_point_v = is_floating_point<type_t>::value;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* concepts
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
concept integral = is_integral_v<type_t>;
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
concept signed_integral = is_signed_integer_v<type_t>;
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
concept unsigned_integral = integral<type_t> && !signed_integral<type_t>;
|
||||||
|
|
||||||
|
template <typename type_t>
|
||||||
|
concept floating_point = is_floating_point_v<type_t>;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* algorithm
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
template <typename input_t, typename output_t>
|
||||||
|
constexpr inline void copy(const input_t* start, const input_t* end, output_t* dest_start) {
|
||||||
|
auto temp = start;
|
||||||
|
while (temp != end)
|
||||||
|
*(dest_start++) = *(temp++);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* utility
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::remove_reference_t<T>&& move(T&& arg) noexcept {
|
||||||
|
return reinterpret_cast<std::remove_reference_t<T>&&>(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr inline void swap(T& t1, T& t2) {
|
||||||
|
T temp = std::move(t1);
|
||||||
|
t1 = std::move(t2);
|
||||||
|
t2 = std::move(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename first_t, typename second_t>
|
||||||
|
struct pair {
|
||||||
|
first_t first;
|
||||||
|
second_t second;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* array
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Is std::size_t really the best bet here?
|
||||||
|
template <typename data_t, std::size_t t_size>
|
||||||
|
class array {
|
||||||
|
public:
|
||||||
|
template <typename... args_t>
|
||||||
|
constexpr array(args_t... args) noexcept : m_data{args...} {
|
||||||
|
static_assert(sizeof...(args) == t_size, "Invalid number of arguments");
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr array() noexcept = default;
|
||||||
|
constexpr array(const array&) = default;
|
||||||
|
constexpr array(array&&) = default;
|
||||||
|
|
||||||
|
constexpr array& operator=(const array& other) = default;
|
||||||
|
constexpr array& operator=(array&& other) = default;
|
||||||
|
|
||||||
|
constexpr void swap(array<data_t, t_size>& other) noexcept {
|
||||||
|
for (int i = 0; i < t_size; ++i) {
|
||||||
|
using std::swap;
|
||||||
|
swap(m_data[i], other.m_data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
constexpr data_t& operator[](std::size_t index) noexcept {
|
||||||
|
return m_data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const data_t& operator[](std::size_t index) const noexcept {
|
||||||
|
return m_data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
using iterator = data_t*;
|
||||||
|
using const_iterator = const data_t*;
|
||||||
|
|
||||||
|
constexpr iterator begin() noexcept {
|
||||||
|
return &(m_data[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr iterator end() noexcept {
|
||||||
|
return (&(m_data[t_size - 1]) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const_iterator begin() const noexcept {
|
||||||
|
return &(m_data[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const_iterator end() const noexcept {
|
||||||
|
return (&(m_data[t_size - 1]) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const_iterator cbegin() const noexcept {
|
||||||
|
return &(m_data[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const_iterator cend() const noexcept {
|
||||||
|
return (&(m_data[t_size - 1]) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
data_t m_data[t_size];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}} // namespace const_fmt::std
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif // CONST_FMT_STDLIB_H
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
#define LOGGER_TYPES_H
|
#define LOGGER_TYPES_H
|
||||||
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
|
#include "stdlib.h"
|
||||||
|
|
||||||
|
|
||||||
namespace const_fmt { namespace const_fmt_detail {
|
namespace const_fmt { namespace const_fmt_detail {
|
||||||
@@ -19,8 +20,7 @@ template <std::size_t N>
|
|||||||
class ConstString {
|
class ConstString {
|
||||||
public:
|
public:
|
||||||
constexpr ConstString(const char (&content)[N]) noexcept {
|
constexpr ConstString(const char (&content)[N]) noexcept {
|
||||||
std::copy(std::begin(content), std::end(content),
|
std::copy(&content[0], (&content[N - 1] + 1), m_content.begin());
|
||||||
std::begin(m_content));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr char operator[](std::size_t index) const noexcept {
|
constexpr char operator[](std::size_t index) const noexcept {
|
||||||
|
|||||||
@@ -2,8 +2,9 @@
|
|||||||
#define LOGGER_UTILITY_H
|
#define LOGGER_UTILITY_H
|
||||||
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "stdlib.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
|
#define CONST_FMT_NO_CPP_STDLIB
|
||||||
|
|
||||||
#include <const_fmt/format.h>
|
#include <const_fmt/format.h>
|
||||||
#include <iostream>
|
//#include <iostream>
|
||||||
#include <string_view>
|
//#include <string_view>
|
||||||
|
|
||||||
using namespace const_fmt;
|
using namespace const_fmt;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
constexpr auto s = "This is an integer: {:08b}, and this is a float: {:09.4b}"_const_fmt(125u, -86.2);
|
constexpr auto s = "This is an integer: {:08x}, and this is a float: {:09.4b}"_const_fmt(122u, -86.2);
|
||||||
|
|
||||||
// Convert s (with a type of 'std::array<char, N>') into something
|
// Convert s (with a type of 'std::array<char, N>') into something
|
||||||
// writable to std::cout
|
// writable to std::cout
|
||||||
std::string_view sv{&s[0], s.size()};
|
// std::string_view sv{&s[0], s.size()};
|
||||||
|
//
|
||||||
std::cout << sv << std::endl;
|
// std::cout << sv << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,8 @@ endmacro()
|
|||||||
|
|
||||||
package_add_test(utility_test src/utility.cpp)
|
package_add_test(utility_test src/utility.cpp)
|
||||||
package_add_test(parse_test src/parse.cpp)
|
package_add_test(parse_test src/parse.cpp)
|
||||||
|
package_add_test(format_utility_test src/format_utility.cpp)
|
||||||
package_add_test(format_decimal_test src/format_decimal.cpp)
|
package_add_test(format_decimal_test src/format_decimal.cpp)
|
||||||
package_add_test(format_binary_test src/format_binary.cpp)
|
package_add_test(format_binary_test src/format_binary.cpp)
|
||||||
|
package_add_test(format_hex_test src/format_hex.cpp)
|
||||||
|
|
||||||
|
|||||||
59
test/src/format_hex.cpp
Normal file
59
test/src/format_hex.cpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#include <const_fmt/format.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace const_fmt;
|
||||||
|
using namespace const_fmt::const_fmt_detail;
|
||||||
|
|
||||||
|
|
||||||
|
TEST(FormatHex, positive_int) {
|
||||||
|
constexpr std::array<char, 8> control1 = {'0', '0', '0', '0',
|
||||||
|
'0', '0', '1', '0'};
|
||||||
|
constexpr std::array<char, 8> formatted1 = const_format<"{:08x}">(0x10);
|
||||||
|
|
||||||
|
constexpr std::array<char, 8> control2 = {' ', ' ', ' ', 'F',
|
||||||
|
'F', 'A', '7', '6'};
|
||||||
|
constexpr std::array<char, 8> formatted2 = const_format<"{:8x}">(0xffa76);
|
||||||
|
|
||||||
|
constexpr std::array<char, 8> control3 = {'0', '0', '0', '0',
|
||||||
|
'B', 'C', 'E', 'F'};
|
||||||
|
constexpr std::array<char, 8> formatted3 = const_format<"{:08.4x}">(0xbcef);
|
||||||
|
|
||||||
|
constexpr std::array<char, 4> control4 = {'A', 'D', '0', '1'};
|
||||||
|
constexpr std::array<char, 4> formatted4 = const_format<"{:4x}">(0xad01);
|
||||||
|
|
||||||
|
constexpr std::array<char, 4> control5 = {'f', 'f', 'f', 'f'};
|
||||||
|
constexpr std::array<char, 4> formatted5 = const_format<"{:4x}">(0x12345);
|
||||||
|
|
||||||
|
EXPECT_EQ(control1, formatted1);
|
||||||
|
EXPECT_EQ(control2, formatted2);
|
||||||
|
EXPECT_EQ(control3, formatted3);
|
||||||
|
EXPECT_EQ(control4, formatted4);
|
||||||
|
EXPECT_EQ(control5, formatted5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(FormatHex, negative_int) {
|
||||||
|
constexpr std::array<char, 8> control1 = {'-', '0', '0', '0',
|
||||||
|
'0', '0', '1', '0'};
|
||||||
|
constexpr std::array<char, 8> formatted1 = const_format<"{:08x}">(-0x10);
|
||||||
|
|
||||||
|
constexpr std::array<char, 8> control2 = {' ', ' ', '-', 'F',
|
||||||
|
'F', 'A', '7', '6'};
|
||||||
|
constexpr std::array<char, 8> formatted2 = const_format<"{:8x}">(-0xffa76);
|
||||||
|
|
||||||
|
constexpr std::array<char, 8> control3 = {'-', '0', '0', '0',
|
||||||
|
'B', 'C', 'E', 'F'};
|
||||||
|
constexpr std::array<char, 8> formatted3 = const_format<"{:08.4x}">(-0xbcef);
|
||||||
|
|
||||||
|
constexpr std::array<char, 4> control4 = {'-', 'A', 'D', '1'};
|
||||||
|
constexpr std::array<char, 4> formatted4 = const_format<"{:4x}">(-0xad1);
|
||||||
|
|
||||||
|
constexpr std::array<char, 4> control5 = {'-', 'f', 'f', 'f'};
|
||||||
|
constexpr std::array<char, 4> formatted5 = const_format<"{:04x}">(-0x1234);
|
||||||
|
|
||||||
|
EXPECT_EQ(control1, formatted1);
|
||||||
|
EXPECT_EQ(control2, formatted2);
|
||||||
|
EXPECT_EQ(control3, formatted3);
|
||||||
|
EXPECT_EQ(control4, formatted4);
|
||||||
|
EXPECT_EQ(control5, formatted5);
|
||||||
|
}
|
||||||
44
test/src/format_utility.cpp
Normal file
44
test/src/format_utility.cpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include <const_fmt/format_impl.h>
|
||||||
|
#include <limits>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace const_fmt;
|
||||||
|
using namespace const_fmt::const_fmt_detail;
|
||||||
|
|
||||||
|
|
||||||
|
TEST(FormatUtility, count_digits_base_decimal) {
|
||||||
|
constexpr unsigned length1 = count_digits_base<FormatType::d>(123);
|
||||||
|
constexpr unsigned length2 = count_digits_base<FormatType::d>(std::numeric_limits<uint64_t>::max());
|
||||||
|
constexpr unsigned length3 = count_digits_base<FormatType::d>(10000011);
|
||||||
|
constexpr unsigned length4 = count_digits_base<FormatType::d>(1);
|
||||||
|
|
||||||
|
EXPECT_EQ(length1, 3);
|
||||||
|
EXPECT_EQ(length2, 20);
|
||||||
|
EXPECT_EQ(length3, 8);
|
||||||
|
EXPECT_EQ(length4, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(FormatUtility, count_digits_base_binary) {
|
||||||
|
constexpr unsigned length1 = count_digits_base<FormatType::b>(0b1001);
|
||||||
|
constexpr unsigned length2 = count_digits_base<FormatType::b>(std::numeric_limits<uint64_t>::max());
|
||||||
|
constexpr unsigned length3 = count_digits_base<FormatType::b>(0b10000001);
|
||||||
|
constexpr unsigned length4 = count_digits_base<FormatType::b>(0b01);
|
||||||
|
|
||||||
|
EXPECT_EQ(length1, 4);
|
||||||
|
EXPECT_EQ(length2, 64);
|
||||||
|
EXPECT_EQ(length3, 8);
|
||||||
|
EXPECT_EQ(length4, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(FormatUtility, count_digits_base_hex) {
|
||||||
|
constexpr unsigned length1 = count_digits_base<FormatType::x>(0x123);
|
||||||
|
constexpr unsigned length2 = count_digits_base<FormatType::x>(std::numeric_limits<uint64_t>::max());
|
||||||
|
constexpr unsigned length3 = count_digits_base<FormatType::x>(0x1000000f);
|
||||||
|
constexpr unsigned length4 = count_digits_base<FormatType::x>(0x01);
|
||||||
|
|
||||||
|
EXPECT_EQ(length1, 3);
|
||||||
|
EXPECT_EQ(length2, 16);
|
||||||
|
EXPECT_EQ(length3, 8);
|
||||||
|
EXPECT_EQ(length4, 1);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user