aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/format_analyser.h
blob: 4afe3f1ca8b16bc5b8511d719dc072854343750b (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once

#include <array>
#include <string_view>

namespace NYT {

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

namespace NDetail {

struct TFormatAnalyser
{
public:
    template <class... TArgs>
    static consteval void ValidateFormat(std::string_view fmt);

private:
    // Non-constexpr function call will terminate compilation.
    // Purposefully undefined and non-constexpr/consteval
    static void CrashCompilerNotEnoughArguments(std::string_view msg);
    static void CrashCompilerTooManyArguments(std::string_view msg);
    static void CrashCompilerWrongTermination(std::string_view msg);
    static void CrashCompilerMissingTermination(std::string_view msg);
    static void CrashCompilerWrongFlagSpecifier(std::string_view msg);

    struct TSpecifiers
    {
        std::string_view Conversion;
        std::string_view Flags;
    };
    template <class TArg>
    static consteval auto GetSpecifiers();

    static constexpr char IntroductorySymbol = '%';
};

} // namespace NDetail

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

// Base used for flag checks for each type independently.
// Use it for overrides.
struct TFormatArgBase
{
public:
    // TODO(arkady-e1ppa): Consider more strict formatting rules.
    static constexpr std::array ConversionSpecifiers = {
            'v', '1', 'c', 's', 'd', 'i', 'o',
            'x', 'X', 'u', 'f', 'F', 'e', 'E',
            'a', 'A', 'g', 'G', 'n', 'p'
        };

    static constexpr std::array FlagSpecifiers = {
        '-', '+', ' ', '#', '0',
        '1', '2', '3', '4', '5',
        '6', '7', '8', '9',
        '*', '.', 'h', 'l', 'j',
        'z', 't', 'L', 'q', 'Q'
    };

    template <class T>
    static constexpr bool IsSpecifierList = requires (T t) {
        [] <size_t N> (std::array<char, N>) { } (t);
    };

    // Hot = |true| adds specifiers to the beggining
    // of a new array.
    template <bool Hot, size_t N, std::array<char, N> List, class TFrom = TFormatArgBase>
    static consteval auto ExtendConversion();

    template <bool Hot, size_t N, std::array<char, N> List, class TFrom = TFormatArgBase>
    static consteval auto ExtendFlags();

private:
    template <bool Hot, size_t N, std::array<char, N> List, auto* From>
    static consteval auto AppendArrays();
};

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

template <class T>
struct TFormatArg
    : public TFormatArgBase
{ };

// Semantic requirement:
// Said field must be constexpr.
template <class T>
concept CFormattable = requires {
    TFormatArg<T>::ConversionSpecifiers;
    requires TFormatArgBase::IsSpecifierList<decltype(TFormatArg<T>::ConversionSpecifiers)>;

    TFormatArg<T>::FlagSpecifiers;
    requires TFormatArgBase::IsSpecifierList<decltype(TFormatArg<T>::FlagSpecifiers)>;
};

} // namespace NYT

#define FORMAT_ANALYSER_INL_H_
#include "format_analyser-inl.h"
#undef FORMAT_ANALYSER_INL_H_