aboutsummaryrefslogtreecommitdiff
path: root/libbutl/char-scanner.ixx
blob: 57aefc2d4f92382d1f7101ebe470c6b9d416c6d4 (plain)
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
// file      : libbutl/char-scanner.ixx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

namespace butl
{
  template <typename V, std::size_t N>
  inline char_scanner<V, N>::
  char_scanner (std::istream& is, bool crlf, std::uint64_t l, std::uint64_t p)
      : char_scanner (is, validator_type (), crlf, l, p)
  {
  }

  template <typename V, std::size_t N>
  inline auto char_scanner<V, N>::
  peek (std::string& what) -> xchar
  {
    return peek (&what);
  }

  template <typename V, std::size_t N>
  inline auto char_scanner<V, N>::
  peek () -> xchar
  {
    return peek (nullptr /* what */);
  }

  template <typename V, std::size_t N>
  inline auto char_scanner<V, N>::
  get (std::string* what) -> xchar
  {
    if (ungetn_ != 0)
      return ungetb_[--ungetn_];
    else
    {
      xchar c (peek (what));
      get (c);
      return c;
    }
  }

  template <typename V, std::size_t N>
  inline auto char_scanner<V, N>::
  get (std::string& what) -> xchar
  {
    return get (&what);
  }

  template <typename V, std::size_t N>
  inline auto char_scanner<V, N>::
  get () -> xchar
  {
    return get (nullptr /* what */);
  }

  template <typename V, std::size_t N>
  inline void char_scanner<V, N>::
  unget (const xchar& c)
  {
    // Because iostream::unget cannot work once eos is reached, we have to
    // provide our own implementation.
    //
    assert (ungetn_ != N); // Make sure the buffer is not filled.

    ungetb_[ungetn_++] = c;
  }

  template <typename V, std::size_t N>
  inline auto char_scanner<V, N>::
  peek_ () -> int_type
  {
    if (gptr_ != egptr_)
      return *gptr_;

    int_type r (is_.peek ());

    // Update buffer pointers for the next chunk.
    //
    if (buf_ != nullptr)
    {
      gptr_ = buf_->gptr ();
      egptr_ = buf_->egptr ();
    }

    return r;
  }

  template <typename V, std::size_t N>
  inline void char_scanner<V, N>::
  get_ ()
  {
    int_type c;

    if (gptr_ != egptr_)
    {
      buf_->gbump (1);
      c = *gptr_++;
    }
    else
      c = is_.get (); // About as fast as ignore() and way faster than tellg().

    validated_ = false;

    if (save_ != nullptr && c != xchar::traits_type::eof ())
      save_->push_back (static_cast<char_type> (c));
  }

  template <typename V, std::size_t N>
  inline std::uint64_t char_scanner<V, N>::
  pos_ () const
  {
    return buf_ != nullptr ? buf_->tellg () : 0;
  }
}