summaryrefslogtreecommitdiffhomepage
path: root/compat/format.hpp
blob: 1cb4f365d3a9f6979d8fd42409ec3d92fe0aa11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include "compat/assert.hpp"
#include <fmt/core.h>
#include <fmt/compile.h>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Containers/String.h>

namespace fmt {

template<> struct formatter<Corrade::Containers::StringView> {
  template<typename ParseContext> static constexpr auto parse(ParseContext& ctx) { return ctx.begin(); }
  template<typename FormatContext> auto format(Corrade::Containers::StringView const& s, FormatContext& ctx);
};

template<> struct formatter<Corrade::Containers::String> {
  template<typename ParseContext> static constexpr auto parse(ParseContext& ctx) { return ctx.begin(); }
  template<typename FormatContext> auto format(Corrade::Containers::String const& s, FormatContext& ctx);
};

} // namespace fmt

#if !FMT_USE_NONTYPE_TEMPLATE_ARGS
namespace floormat::detail::fmt {
template<size_t N>
struct fmt_string final {
    static constexpr size_t size = N;
    char data[N];

    template <size_t... Is>
    consteval fmt_string(const char (&arr)[N]) noexcept {
        for (auto i = 0uz; i < N; i++)
            data[i] = arr[i];
    }
};
} // namespace floormat::detail::fmt
#endif

#if !FMT_USE_NONTYPE_TEMPLATE_ARGS
template<::floormat::detail::fmt::fmt_string s>
consteval auto operator""_cf() noexcept
{
    return FMT_COMPILE(s.data);
}
#else
using namespace fmt::literals;
#endif

namespace floormat {

template<size_t N, typename Fmt, typename... Xs>
size_t snformat(char(&buf)[N], Fmt&& fmt, Xs&&... args)
{
    constexpr size_t n = N > 0 ? N - 1 : 0;
    auto result = fmt::format_to_n(buf, n, forward<Fmt>(fmt), forward<Xs>(args)...);
    const auto len = n < result.size ? n : result.size;
    fm_assert(len > 0);
#if 0
    if constexpr(N > 0)
        buf[len] = '\0';
#else
    buf[len] = '\0';
#endif
    return result.size;
}

} // namespace floormat

template<typename FormatContext>
auto fmt::formatter<Corrade::Containers::StringView>::format(Corrade::Containers::StringView const& s, FormatContext& ctx) {
  return fmt::format_to(ctx.out(), "{}"_cf, basic_string_view<char>{s.data(), s.size()});
}

template<typename FormatContext>
auto fmt::formatter<Corrade::Containers::String>::format(Corrade::Containers::String const& s, FormatContext& ctx) {
  return fmt::format_to(ctx.out(), "{}"_cf, basic_string_view<char>{s.data(), s.size()});
}