aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/detail/stream_counter.h
blob: 0f72cda0a21e8e1085408a8235ce8eba9d16df0e (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
#pragma once

#include <library/cpp/yson_pull/position_info.h> 

#include <cstddef>

namespace NYsonPull {
    namespace NDetail {
        template <bool EnableLinePositionInfo>
        class stream_counter;

        template <>
        class stream_counter<true> {
        private:
            size_t offset_ = 0;
            size_t line_ = 1;
            size_t column_ = 1;

        public:
            TPositionInfo info() const {
                return {offset_, line_, column_};
            }

            void update(const ui8* begin, const ui8* end) {
                offset_ += end - begin;
                for (auto current = begin; current != end; ++current) {
                    ++column_;
                    if (*current == '\n') { //TODO: memchr
                        ++line_;
                        column_ = 1;
                    }
                }
            }
        };

        template <>
        class stream_counter<false> {
        private:
            size_t offset_ = 0;

        public:
            TPositionInfo info() const {
                return {offset_, {}, {}};
            }

            void update(const ui8* begin, const ui8* end) {
                offset_ += end - begin;
            }
        };
    }
}