| 4 | #include <string_view> |
| 5 | |
| 6 | class TextReader { |
| 7 | public: |
| 8 | explicit TextReader(std::string_view text); |
| 9 | |
| 10 | void NextChar(); |
| 11 | |
| 12 | void NextLine(); |
| 13 | |
| 14 | void SaveAndNext(); |
| 15 | |
| 16 | void Save(); |
| 17 | |
| 18 | int GetCurrentChar(); |
| 19 | |
| 20 | bool CheckNext1(int ch); |
| 21 | |
| 22 | bool CheckNext2(std::string_view set); |
| 23 | |
| 24 | std::size_t GetPos() const; |
| 25 | |
| 26 | TextRange GetTokenRange() const; |
| 27 | |
| 28 | std::string_view GetSaveText() const; |
| 29 | |
| 30 | void ResetBuffer(); |
| 31 | |
| 32 | std::size_t EatWhen(int ch); |
| 33 | |
| 34 | template<class Fn> |
| 35 | std::size_t EatWhile(Fn fn) { |
| 36 | std::size_t count = 0; |
| 37 | while (!IsEof() && fn(GetCurrentChar())) { |
| 38 | SaveAndNext(); |
| 39 | count++; |
| 40 | } |
| 41 | return count; |
| 42 | } |
| 43 | |
| 44 | bool IsEof() const; |
| 45 | |
| 46 | bool IsEol() const; |
| 47 | |
| 48 | bool HasSaveText() const; |
| 49 | private: |
| 50 | std::string_view _text; |
| 51 | |
| 52 | bool _hasSaveText; |
| 53 | std::size_t _buffStart; |
| 54 | std::size_t _buffIndex; |
| 55 | |
| 56 | bool _isEof; |
| 57 | std::size_t _currentIndex; |
| 58 | }; |