|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if not STDX_FMT_FREESTANDING |
| 4 | + |
| 5 | +#include <fmt/compile.h> |
| 6 | +#include <fmt/format.h> |
| 7 | + |
| 8 | +#include <string_view> |
| 9 | + |
| 10 | +#define STDX_FMT_COMPILE(Fmt) FMT_COMPILE(std::string_view{Fmt}) |
| 11 | + |
| 12 | +#else |
| 13 | + |
| 14 | +#include <stdx/compiler.hpp> |
| 15 | +#include <stdx/concepts.hpp> |
| 16 | +#include <stdx/ranges.hpp> |
| 17 | + |
| 18 | +#include <cstddef> |
| 19 | + |
| 20 | +#define STDX_FMT_COMPILE(X) [] { return X; } |
| 21 | + |
| 22 | +namespace stdx { |
| 23 | +inline namespace v1 { |
| 24 | +namespace fmt { |
| 25 | + |
| 26 | +namespace detail { |
| 27 | +struct fmt_spec { |
| 28 | + int len{}; |
| 29 | + int base{}; |
| 30 | + bool well_formed{true}; |
| 31 | +}; |
| 32 | + |
| 33 | +CONSTEVAL auto parse_fmt_spec(auto fmtstr) -> fmt_spec { |
| 34 | + constexpr auto fmt = fmtstr(); |
| 35 | + auto i = fmt.begin(); |
| 36 | + while (*i != '{') { |
| 37 | + ++i; |
| 38 | + } |
| 39 | + ++i; |
| 40 | + if (*i == '}') { |
| 41 | + return {.len = 2, .base = 10}; |
| 42 | + } else if (*i++ == ':' and *i++ == 'x' and *i == '}') { |
| 43 | + return {.len = 4, .base = 16}; |
| 44 | + } |
| 45 | + return {.well_formed = false}; |
| 46 | +} |
| 47 | + |
| 48 | +template <stdx::range R> |
| 49 | +CONSTEVAL auto formatted_size(fmt_spec, R const &r) -> std::size_t { |
| 50 | + return r.size(); |
| 51 | +} |
| 52 | + |
| 53 | +CONSTEVAL auto formatted_size(fmt_spec, char const *str) -> std::size_t { |
| 54 | + std::size_t sz{}; |
| 55 | + while (*str) { |
| 56 | + ++sz; |
| 57 | + ++str; |
| 58 | + } |
| 59 | + return sz; |
| 60 | +} |
| 61 | + |
| 62 | +CONSTEVAL auto formatted_size(fmt_spec, char) -> std::size_t { return 1; } |
| 63 | + |
| 64 | +template <stdx::integral I> |
| 65 | +CONSTEVAL auto formatted_size(fmt_spec s, I i) -> std::size_t { |
| 66 | + if (i == 0) { |
| 67 | + return 1; |
| 68 | + } else { |
| 69 | + std::size_t sz{}; |
| 70 | + if (i < 0) { |
| 71 | + ++sz; |
| 72 | + } |
| 73 | + |
| 74 | + while (i != 0) { |
| 75 | + ++sz; |
| 76 | + i /= s.base; |
| 77 | + } |
| 78 | + return sz; |
| 79 | + } |
| 80 | +} |
| 81 | +} // namespace detail |
| 82 | + |
| 83 | +CONSTEVAL auto formatted_size(auto fmtstr, auto v) -> std::size_t { |
| 84 | + constexpr auto spec = detail::parse_fmt_spec(fmtstr); |
| 85 | + static_assert( |
| 86 | + spec.well_formed, |
| 87 | + "Freestanding fmt implementation does not support that format " |
| 88 | + "specifier"); |
| 89 | + |
| 90 | + return fmtstr().size() - spec.len + detail::formatted_size(spec, v); |
| 91 | +} |
| 92 | + |
| 93 | +namespace detail { |
| 94 | +template <typename It, stdx::range R> |
| 95 | +CONSTEVAL auto format_to(fmt_spec, It dest, R const &r) -> It { |
| 96 | + for (auto const c : r) { |
| 97 | + *dest++ = c; |
| 98 | + } |
| 99 | + return dest; |
| 100 | +} |
| 101 | + |
| 102 | +template <typename It> |
| 103 | +CONSTEVAL auto format_to(fmt_spec, It dest, char const *str) -> It { |
| 104 | + while (*str) { |
| 105 | + *dest++ = *str++; |
| 106 | + } |
| 107 | + return dest; |
| 108 | +} |
| 109 | + |
| 110 | +template <typename It> |
| 111 | +CONSTEVAL auto format_to(fmt_spec, It dest, char c) -> It { |
| 112 | + *dest++ = c; |
| 113 | + return dest; |
| 114 | +} |
| 115 | + |
| 116 | +template <typename It, stdx::integral I> |
| 117 | +CONSTEVAL auto format_to(fmt_spec s, It dest, I i) -> It { |
| 118 | + if (i == 0) { |
| 119 | + *dest++ = '0'; |
| 120 | + } else { |
| 121 | + auto abs = []<typename T>(T v) -> T { return v < 0 ? -v : v; }; |
| 122 | + auto to_digit = [](auto n) -> char { return "0123456789abcdef"[n]; }; |
| 123 | + |
| 124 | + if (i < 0) { |
| 125 | + *dest++ = '-'; |
| 126 | + } |
| 127 | + |
| 128 | + auto b = dest; |
| 129 | + while (i != 0) { |
| 130 | + auto digit = to_digit(abs(i % s.base)); |
| 131 | + i /= s.base; |
| 132 | + *dest++ = digit; |
| 133 | + } |
| 134 | + |
| 135 | + auto e = dest - 1; |
| 136 | + while (b < e) { |
| 137 | + auto tmp = *b; |
| 138 | + *b++ = *e; |
| 139 | + *e-- = tmp; |
| 140 | + } |
| 141 | + } |
| 142 | + return dest; |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace detail |
| 146 | + |
| 147 | +template <typename It> |
| 148 | +CONSTEVAL auto format_to(It dest, auto fmtstr, auto v) -> void { |
| 149 | + constexpr auto fmt = fmtstr(); |
| 150 | + constexpr auto spec = detail::parse_fmt_spec(fmtstr); |
| 151 | + |
| 152 | + // copy to opening { |
| 153 | + auto src = fmt.begin(); |
| 154 | + while (*src != '{') { |
| 155 | + *dest++ = *src++; |
| 156 | + } |
| 157 | + |
| 158 | + // skip fmt spec |
| 159 | + src += spec.len; |
| 160 | + |
| 161 | + // format the value into the buffer |
| 162 | + dest = detail::format_to(spec, dest, v); |
| 163 | + |
| 164 | + // copy the rest of src |
| 165 | + while (*src) { |
| 166 | + *dest++ = *src++; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +} // namespace fmt |
| 171 | +} // namespace v1 |
| 172 | +} // namespace stdx |
| 173 | + |
| 174 | +#endif |
0 commit comments