aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/format_string-inl.h
blob: 67f9bad45c92621e23843d1ee72a21cbfcde6179 (plain) (blame)
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
#ifndef FORMAT_STRING_INL_H_
#error "Direct inclusion of this file is not allowed, include format_string.h"
// For the sake of sane code completion.
#include "format_string.h"
#endif

#include <algorithm>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class... TArgs>
template <class T>
    requires std::constructible_from<std::string_view, T>
consteval TBasicFormatString<TArgs...>::TBasicFormatString(const T& fmt)
    : Format_(fmt)
{
    CheckFormattability();
#if !defined(YT_DISABLE_FORMAT_STATIC_ANALYSIS)
    std::tie(Markers, Escapes) = NDetail::TFormatAnalyser::AnalyzeFormat<std::remove_cvref_t<TArgs>...>(Format_);
#else
    std::ranges::fill_n(std::ranges::begin(Escapes), 1, -1);
    if constexpr (sizeof...(TArgs) != 0) {
        std::ranges::fill_n(std::ranges::begin(Markers), 1, std::tuple{0, 0});
    }
#endif
}

template <class... TArgs>
TStringBuf TBasicFormatString<TArgs...>::Get() const noexcept
{
    return {Format_};
}

template <class... TArgs>
consteval void TBasicFormatString<TArgs...>::CheckFormattability()
{
#if !defined(NDEBUG) && !defined(YT_DISABLE_FORMAT_STATIC_ANALYSIS)
    using TTuple = std::tuple<std::remove_cvref_t<TArgs>...>;

    [] <size_t... Idx> (std::index_sequence<Idx...>) {
        ([] {
            if constexpr (!CFormattable<std::tuple_element_t<Idx, TTuple>>) {
                CrashCompilerClassIsNotFormattable<std::tuple_element_t<Idx, TTuple>>();
            }
        } (), ...);
    } (std::index_sequence_for<TArgs...>());
#endif
}

template <class... TArgs>
TBasicFormatString<TArgs...>::TBasicFormatString(TRuntimeFormat fmt)
    : Format_(fmt.Get())
{
    std::ranges::fill_n(std::ranges::begin(Escapes), 1, -1);
    if constexpr (sizeof...(TArgs) != 0) {
        std::ranges::fill_n(std::ranges::begin(Markers), 1, std::tuple{0, 0});
    }

    // NB(arkady-e1ppa): StaticFormat performs the
    // formattability check of the args in a way
    // that provides more useful information
    // than a simple static_assert with conjunction.
    // Additionally, the latter doesn't work properly
    // for older clang version.
    static constexpr auto argsChecker = [] {
        CheckFormattability();
        return 42;
    } ();
    Y_UNUSED(argsChecker);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT