aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/deprecated/split/split_iterator.h
blob: 26e5865ef6fc07c7f1fe6e56a3a5c4339636ee10 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#pragma once 

#include <library/cpp/deprecated/kmp/kmp.h>
#include <util/string/cast.h>
#include <util/string/util.h>
#include <util/string/builder.h>

#include <util/system/yassert.h>
#include <util/system/defaults.h>
#include <util/generic/strbuf.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>

#include <cstdio>

template <typename T>
struct TNumPair {
    T Begin;
    T End;

    TNumPair() = default;

    TNumPair(T begin, T end)
        : Begin(begin)
        , End(end)
    {
        Y_ASSERT(begin <= end);
    }

    T Length() const {
        return End - Begin + 1;
    }

    bool operator==(const TNumPair& r) const {
        return (Begin == r.Begin) && (End == r.End);
    }

    bool operator!=(const TNumPair& r) const {
        return (Begin != r.Begin) || (End != r.End);
    }
};

using TSizeTRegion = TNumPair<size_t>;
using TUi32Region = TNumPair<ui32>;

template <>
inline TString ToString(const TUi32Region& r) {
    return TStringBuilder() << "(" << r.Begin << ", " << r.End << ")";
}

template <>
inline TUi32Region FromString(const TString& s) {
    TUi32Region result;
    sscanf(s.data(), "(%" PRIu32 ", %" PRIu32 ")", &result.Begin, &result.End);
    return result;
}

class TSplitDelimiters {
private:
    bool Delims[256];

public:
    explicit TSplitDelimiters(const char* s);

    Y_FORCE_INLINE bool IsDelimiter(ui8 ch) const {
        return Delims[ch];
    }
};

template <class Split>
class TSplitIterator;

class TSplitBase {
protected:
    const char* Str;
    size_t Len;

public:
    TSplitBase(const char* str, size_t length);
    TSplitBase(const TString& s);

    Y_FORCE_INLINE const char* GetString() const {
        return Str;
    }

    Y_FORCE_INLINE size_t GetLength() const {
        return Len;
    }

private:
    // we don't own Str, make sure that no one calls us with temporary object
    TSplitBase(TString&&) = delete;
};

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4512)
#endif

class TDelimitersSplit: public TSplitBase {
private:
    const TSplitDelimiters& Delimiters;

public:
    using TIterator = TSplitIterator<TDelimitersSplit>;
    friend class TSplitIterator<TDelimitersSplit>;

    TDelimitersSplit(const char* str, size_t length, const TSplitDelimiters& delimiters);
    TDelimitersSplit(const TString& s, const TSplitDelimiters& delimiters);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Delimiters, make sure that no one calls us with temporary object
    TDelimitersSplit(const char*, size_t, TSplitDelimiters&&) = delete;
    TDelimitersSplit(const TString&, TSplitDelimiters&&) = delete;
    TDelimitersSplit(TString&&, const TSplitDelimiters&) = delete;
};

class TDelimitersStrictSplit: public TSplitBase {
private:
    const TSplitDelimiters& Delimiters;

public:
    using TIterator = TSplitIterator<TDelimitersStrictSplit>;
    friend class TSplitIterator<TDelimitersStrictSplit>;

    TDelimitersStrictSplit(const char* str, size_t length, const TSplitDelimiters& delimiters);
    TDelimitersStrictSplit(const TString& s, const TSplitDelimiters& delimiters);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Delimiters, make sure that no one calls us with temporary object
    TDelimitersStrictSplit(const char*, size_t, TSplitDelimiters&&) = delete;
    TDelimitersStrictSplit(const TString&, TSplitDelimiters&&) = delete;
    TDelimitersStrictSplit(TString&&, const TSplitDelimiters&) = delete;
};

class TScreenedDelimitersSplit: public TSplitBase {
private:
    const TSplitDelimiters& Delimiters;
    const TSplitDelimiters& Screens;

public:
    using TIterator = TSplitIterator<TScreenedDelimitersSplit>;
    friend class TSplitIterator<TScreenedDelimitersSplit>;

    TScreenedDelimitersSplit(const char*, size_t, const TSplitDelimiters& delimiters, const TSplitDelimiters& screens);
    TScreenedDelimitersSplit(const TString& s, const TSplitDelimiters& delimiters, const TSplitDelimiters& screens);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Delimiters and Screens, make sure that no one calls us with temporary object
    TScreenedDelimitersSplit(TString&&, const TSplitDelimiters&, const TSplitDelimiters&) = delete;
    TScreenedDelimitersSplit(const TString&, TSplitDelimiters&&, const TSplitDelimiters&) = delete;
    TScreenedDelimitersSplit(const TString&, const TSplitDelimiters&, TSplitDelimiters&&) = delete;
};

class TDelimitersSplitWithoutTags: public TSplitBase {
private:
    const TSplitDelimiters& Delimiters;
    size_t SkipTag(size_t pos) const;
    size_t SkipDelimiters(size_t pos) const;

public:
    using TIterator = TSplitIterator<TDelimitersSplitWithoutTags>;
    friend class TSplitIterator<TDelimitersSplitWithoutTags>;

    TDelimitersSplitWithoutTags(const char* str, size_t length, const TSplitDelimiters& delimiters);
    TDelimitersSplitWithoutTags(const TString& s, const TSplitDelimiters& delimiters);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Delimiters, make sure that no one calls us with temporary object
    TDelimitersSplitWithoutTags(const char*, size_t, TSplitDelimiters&&) = delete;
    TDelimitersSplitWithoutTags(const TString&, TSplitDelimiters&&) = delete;
    TDelimitersSplitWithoutTags(TString&&, const TSplitDelimiters&) = delete;
};

class TCharSplit: public TSplitBase {
public:
    using TIterator = TSplitIterator<TCharSplit>;
    friend class TSplitIterator<TCharSplit>;

    TCharSplit(const char* str, size_t length);
    TCharSplit(const TString& s);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Str, make sure that no one calls us with temporary object
    TCharSplit(TString&&) = delete;
};

#ifdef _MSC_VER
#pragma warning(pop)
#endif

class TCharSplitWithoutTags: public TSplitBase {
private:
    size_t SkipTag(size_t pos) const;
    size_t SkipDelimiters(size_t pos) const;

public:
    using TIterator = TSplitIterator<TCharSplitWithoutTags>;
    friend class TSplitIterator<TCharSplitWithoutTags>;

    TCharSplitWithoutTags(const char* str, size_t length);
    TCharSplitWithoutTags(const TString& s);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Str, make sure that no one calls us with temporary object
    TCharSplitWithoutTags(TString&&) = delete;
};

class TSubstringSplitDelimiter {
public:
    TKMPMatcher Matcher;
    size_t Len;

    TSubstringSplitDelimiter(const TString& s);
};

class TSubstringSplit: public TSplitBase {
private:
    const TSubstringSplitDelimiter& Delimiter;

public:
    using TIterator = TSplitIterator<TSubstringSplit>;
    friend class TSplitIterator<TSubstringSplit>;

    TSubstringSplit(const char* str, size_t length, const TSubstringSplitDelimiter& delimiter);
    TSubstringSplit(const TString& str, const TSubstringSplitDelimiter& delimiter);
    TIterator Iterator() const;
    TSizeTRegion Next(size_t& pos) const;
    size_t Begin() const;

private:
    // we don't own Delimiters, make sure that no one calls us with temporary object
    TSubstringSplit(TString&&, const TSubstringSplitDelimiter&) = delete;
    TSubstringSplit(const TString&, TSubstringSplitDelimiter&&) = delete;
};

template <class TSplit>
class TSplitIterator {
protected:
    const TSplit& Split;
    size_t Pos;
    TString* CurrentStroka;

public:
    TSplitIterator(const TSplit& split)
        : Split(split)
        , Pos(Split.Begin())
        , CurrentStroka(nullptr)
    {
    }

    virtual ~TSplitIterator() {
        delete CurrentStroka;
    }

    inline TSizeTRegion Next() {
        Y_ENSURE(!Eof(), TStringBuf("eof reached"));
        return Split.Next(Pos);
    }

    TStringBuf NextTok() {
        if (Eof())
            return TStringBuf();
        TSizeTRegion region = Next();
        return TStringBuf(Split.Str + region.Begin, region.End - region.Begin);
    }

    const TString& NextString() {
        if (!CurrentStroka)
            CurrentStroka = new TString();
        TSizeTRegion region = Next();
        CurrentStroka->assign(Split.Str, region.Begin, region.Length() - 1);
        return *CurrentStroka;
    }

    inline bool Eof() const {
        return Pos >= Split.Len;
    }

    TString GetTail() const {
        return TString(Split.Str + Pos);
    }

    void Skip(size_t count) {
        for (size_t i = 0; i < count; ++i)
            Next();
    }
};

using TSplitTokens = TVector<TString>;

template <typename TSplit>
void Split(const TSplit& split, TSplitTokens* words) {
    words->clear();
    TSplitIterator<TSplit> it(split);
    while (!it.Eof())
        words->push_back(it.NextString());
}