aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/deprecated/split/delim_string_iter.h
blob: 14fab35726f5bdc68a22ec10cfb8b8a2cd1c23f6 (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
#pragma once

#include <util/generic/algorithm.h>
#include <util/generic/strbuf.h>
#include <util/generic/yexception.h>
#include <util/string/cast.h>
#include <util/system/yassert.h>

#include <iterator>

class TDelimStringIter {
public:
    using value_type = TStringBuf; 
    using difference_type = ptrdiff_t; 
    using pointer = const TStringBuf*; 
    using reference = const TStringBuf&; 
    using iterator_category = std::forward_iterator_tag; 
 
    inline TDelimStringIter(const char* begin, const char* strEnd, TStringBuf delim)
        : TDelimStringIter(TStringBuf(begin, strEnd), delim)
    {
    }

    inline TDelimStringIter(TStringBuf str, TStringBuf delim)
        : IsValid(true)
        , Str(str)
        , Delim(delim)
    {
        UpdateCurrent();
    }

    inline TDelimStringIter()
        : IsValid(false)
    {
    }

    inline explicit operator bool() const {
        return IsValid;
    }

    // NOTE: this is a potentially unsafe operation (no overrun check)
    inline TDelimStringIter& operator++() {
        if (Current.end() != Str.end()) {
            Str.Skip(Current.length() + Delim.length());
            UpdateCurrent();
        } else {
            Str.Clear();
            Current.Clear();
            IsValid = false;
        }
        return *this;
    }

    inline void operator+=(size_t n) {
        for (; n > 0; --n) {
            ++(*this);
        }
    }

    inline bool operator==(const TDelimStringIter& rhs) const {
        return (IsValid == rhs.IsValid) && (!IsValid || (Current.begin() == rhs.Current.begin()));
    }

    inline bool operator!=(const TDelimStringIter& rhs) const {
        return !(*this == rhs);
    }

    inline TStringBuf operator*() const {
        return Current;
    }

    inline const TStringBuf* operator->() const {
        return &Current;
    }

    // Get & advance
    template <class T>
    inline bool TryNext(T& t) {
        if (IsValid) {
            t = FromString<T>(Current);
            operator++();
            return true;
        } else {
            return false;
        }
    }

    template <class T>
    inline TDelimStringIter& Next(T& t) // Get & advance
    {
        if (!TryNext(t))
            ythrow yexception() << "No valid field";
        return *this;
    }

    template <class T>
    inline T GetNext() {
        T res;
        Next(res);
        return res;
    }

    inline const char* GetBegin() const {
        return Current.begin();
    }

    inline const char* GetEnd() const {
        return Current.end();
    }

    inline bool Valid() const {
        return IsValid;
    }

    // contents from next token to the end of string
    inline TStringBuf Cdr() const {
        return Str.SubStr(Current.length() + Delim.length());
    }

    inline TDelimStringIter IterEnd() const {
        return TDelimStringIter();
    }

private:
    inline void UpdateCurrent() {
        // it is much faster than TStringBuf::find
        size_t pos = std::search(Str.begin(), Str.end(), Delim.begin(), Delim.end()) - Str.begin();
        Current = Str.Head(pos);
    }

private:
    bool IsValid;

    TStringBuf Str;
    TStringBuf Current;
    TStringBuf Delim;
};

//example: for (TStringBuf field: TDelimStroka(line, "@@")) { ... }
struct TDelimStroka {
    TStringBuf S;
    TStringBuf Delim;

    inline TDelimStroka(TStringBuf s, TStringBuf delim)
        : S(s)
        , Delim(delim)
    {
    }

    inline TDelimStringIter begin() const {
        return TDelimStringIter(S, Delim);
    }

    inline TDelimStringIter end() const {
        return TDelimStringIter();
    }
};

inline TDelimStringIter begin_delim(const TString& str, TStringBuf delim) {
    return TDelimStringIter(str, delim);
}

inline TDelimStringIter begin_delim(TStringBuf str, TStringBuf delim) {
    return TDelimStringIter(str.begin(), str.end(), delim);
}

inline TDelimStringIter end_delim(const TString& /*str*/, TStringBuf /*delim*/) {
    return TDelimStringIter();
}

class TKeyValueDelimStringIter {
public:
    TKeyValueDelimStringIter(const TStringBuf str, const TStringBuf delim);
    bool Valid() const;
    TKeyValueDelimStringIter& operator++();
    const TStringBuf& Key() const;
    const TStringBuf& Value() const;

private:
    TDelimStringIter DelimIter;
    TStringBuf ChunkKey, ChunkValue;

private:
    void ReadKeyAndValue();
};