aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/raw_formatter.h
blob: 6956330883b4b119b032269880914bf6487b0ff3 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#pragma once

#include "guid.h"

#include <algorithm>
#include <array>

#include <util/generic/strbuf.h>

namespace NYT {

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

//! A dead-simple string formatter.
/*!
 *  This formatter is intended to be as simple as possible and async signal safe.
 *  This is the reason we do not use printf(): it does not meet signal-safety
 *  requirements.
 */

class TBaseFormatter
{
public:
    TBaseFormatter(char* buffer, int length)
        : Begin_(buffer)
        , Cursor_(buffer)
        , End_(buffer + length)
    { }

    //! Returns an underlying cursor.
    char* GetCursor()
    {
        return Cursor_;
    }

    //! Returns an pointer to the underlying buffer.
    const char* GetData() const
    {
        return Begin_;
    }

    //! Returns the number of bytes written in the buffer.
    int GetBytesWritten() const
    {
        return Cursor_ - Begin_;
    }

    //! Returns the number of bytes available in the buffer.
    int GetBytesRemaining() const
    {
        return End_ - Cursor_;
    }

    //! Advances the internal cursor #count symbols forward (assuming the data is already present).
    void Advance(int count)
    {
        Cursor_ += count;

        if (Cursor_ > End_) {
            Cursor_ = End_;
        }
    }

    //! Drops trailing #count symbols (assuming these are present).
    void Revert(int count)
    {
        Cursor_ -= count;
    }

    //! Appends the string and updates the internal cursor.
    void AppendString(const char* string)
    {
        while (*string != '\0' && Cursor_ < End_) {
            *Cursor_++ = *string++;
        }
    }

    //! Appends the string and updates the internal cursor.
    void AppendString(TStringBuf string)
    {
        size_t position = 0;
        while (position < string.length() && Cursor_ < End_) {
            *Cursor_++ = string[position++];
        }
    }

    //! Appends a single character and updates the internal cursor.
    void AppendChar(char ch)
    {
        if (Cursor_ < End_) {
            *Cursor_++ = ch;
        }
    }

    //! Formats |number| in base |radix| and updates the internal cursor.
    void AppendNumber(uintptr_t number, int radix = 10, int width = 0, char ch = ' ')
    {
        int digits = 0;

        if (radix == 16) {
            // Optimize output of hex numbers.

            uintptr_t reverse = 0;
            int length = 0;
            do {
                reverse <<= 4;
                reverse |= number & 0xf;
                number >>= 4;
                ++length;
            } while (number > 0);

            for (int index = 0; index < length && Cursor_ + digits < End_; ++index) {
                unsigned int modulus = reverse & 0xf;
                Cursor_[digits] = (modulus < 10 ? '0' + modulus : 'a' + modulus - 10);
                ++digits;
                reverse >>= 4;
            }
        } else {
            while (Cursor_ + digits < End_) {
                const int modulus = number % radix;
                number /= radix;
                Cursor_[digits] = (modulus < 10 ? '0' + modulus : 'a' + modulus - 10);
                ++digits;
                if (number == 0) {
                    break;
                }
            }

            // Reverse the bytes written.
            std::reverse(Cursor_, Cursor_ + digits);
        }

        if (digits < width) {
            auto delta = width - digits;
            std::copy(Cursor_, Cursor_ + digits, Cursor_ + delta);
            std::fill(Cursor_, Cursor_ + delta, ch);
            Cursor_ += width;
        } else {
            Cursor_ += digits;
        }
    }

    //! Formats |number| as hexadecimal number and updates the internal cursor.
    //! Padding will be added in front if needed.
    void AppendNumberAsHexWithPadding(uintptr_t number, int width)
    {
        char* begin = Cursor_;
        AppendString("0x");
        AppendNumber(number, 16);
        // Move to right and add padding in front if needed.
        if (Cursor_ < begin + width) {
            auto delta = begin + width - Cursor_;
            std::copy(begin, Cursor_, begin + delta);
            std::fill(begin, begin + delta, ' ');
            Cursor_ = begin + width;
        }
    }

    //! Formats |guid| and updates the internal cursor.
    void AppendGuid(TGuid guid)
    {
        if (Y_LIKELY(End_ - Cursor_ >= MaxGuidStringSize)) {
            // Fast path.
            Cursor_ = WriteGuidToBuffer(Cursor_, guid);
        } else {
            // Slow path.
            std::array<char, MaxGuidStringSize> buffer;
            auto* end = WriteGuidToBuffer(buffer.data(), guid);
            AppendString(TStringBuf(buffer.data(), end));
        }
    }

    //! Resets the underlying cursor.
    void Reset()
    {
        Cursor_ = Begin_;
    }

    TStringBuf GetBuffer() const
    {
        return {Begin_, Cursor_};
    }

private:
    char* const Begin_;
    char* Cursor_;
    char* const End_;

};

template <size_t N>
class TRawFormatter
    : public TBaseFormatter
{
public:
    TRawFormatter()
        : TBaseFormatter(Buffer_, N)
    { }

    TRawFormatter(char* buffer, int length)
        : TBaseFormatter(buffer, length)
    { }

private:
    char Buffer_[N];

};

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

} // namespace NYT