aboutsummaryrefslogtreecommitdiff
path: root/libbutl/char-scanner.ixx
blob: ce57a05352b76cd56307cad24e1d5e06e36ec32f (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
// file      : libbutl/char-scanner.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

namespace butl
{
  inline auto char_scanner::
  get () -> xchar
  {
    if (unget_)
    {
      unget_ = false;
      return ungetc_;
    }
    else
    {
      xchar c (peek ());
      get (c);
      return c;
    }
  }

  inline void char_scanner::
  unget (const xchar& c)
  {
    // Because iostream::unget cannot work once eos is reached, we have to
    // provide our own implementation.
    //
    unget_ = true;
    ungetc_ = c;
  }

  inline auto char_scanner::
  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;
  }

  inline void char_scanner::
  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().

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

  inline std::uint64_t char_scanner::
  pos_ () const
  {
    return buf_ != nullptr ? buf_->tellg () : 0;
  }
}