aboutsummaryrefslogtreecommitdiff
path: root/libbutl/char-scanner.txx
blob: 6e0063aad26073107755cf0c5f517283e46a5737 (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
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
// file      : libbutl/char-scanner.txx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <utility> // move

namespace butl
{
  template <typename V, std::size_t N>
  char_scanner<V, N>::
  char_scanner (std::istream& is,
                validator_type v,
                bool crlf,
                std::uint64_t l,
                std::uint64_t p)
      : line (l),
        column (1),
        position (p),
        is_ (is),
        val_ (std::move (v)),
        buf_ (dynamic_cast<bufstreambuf*> (is.rdbuf ())),
        gptr_ (nullptr),
        egptr_ (nullptr),
        crlf_ (crlf)
  {
  }

  template <typename V, std::size_t N>
  auto char_scanner<V, N>::
  peek (std::string* what) -> xchar
  {
    if (ungetn_ > 0)
      return ungetb_[ungetn_ - 1];

    if (unpeek_)
      return unpeekc_;

    if (eos_)
      return xchar (xchar::traits_type::eof (), line, column, position);

    int_type v (peek_ ());

    if (v == xchar::traits_type::eof ())
    {
      if (!decoded_)
      {
        if (what != nullptr)
          *what = "unexpected end of stream";

        v = xchar::invalid ();
      }

      eos_ = true;
    }
    else
    {
      auto valid = [what, this] (int_type v)
      {
        if (validated_)
          return true;

        char c (xchar::traits_type::to_char_type (v));
        std::pair<bool, bool> r (what != nullptr
                                 ? val_.validate (c, *what)
                                 : val_.validate (c));

        decoded_ = r.second;
        validated_ = true;
        return r.first;
      };

      if (!valid (v))
        v = xchar::invalid ();
      else if (crlf_ && v == '\r')
      {
        // Note that '\r' is a valid character (otherwise we won't be here),
        // so we don't validate it again below. We also postpone the
        // validation of the next non-'\r' character (except EOF) until the
        // next peek() call.
        //
        int_type v1;
        do
        {
          get_ ();       // Sets validated_ to false.
          v1 = peek_ ();
        }
        while (v1 == '\r');

        if (v1 != '\n')
        {
          // We need to make sure subsequent calls to peek() return newline.
          //
          unpeek_ = true;
          unpeekc_ = xchar ('\n', line, column, position);

          // Note that the previous character is decoded ('\r') and so EOF is
          // legitimate.
          //
          if (v1 == xchar::traits_type::eof ())
            eos_ = true;
        }

        v = '\n';
      }
    }

    return xchar (v, line, column, position);
  }

  template <typename V, std::size_t N>
  void char_scanner<V, N>::
  get (const xchar& c)
  {
    if (ungetn_ > 0)
      --ungetn_;
    else
    {
      if (unpeek_)
      {
        unpeek_ = false;
      }
      // When is_.get () returns eof, the failbit is also set (stupid,
      // isn't?) which may trigger an exception. To work around this
      // we will call peek() first and only call get() if it is not
      // eof. But we can only call peek() on eof once; any subsequent
      // calls will spoil the failbit (even more stupid).
      //
      else if (!eos (c))
        get_ ();

      if (!eos (c))
      {
        if (c == '\n')
        {
          line++;
          column = 1;
        }
        else if (decoded_) // The character is the last in a sequence?
          column++;

        position = pos_ ();
      }
    }
  }
}