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

#include <butl/char-scanner>

#include <istream>

using namespace std;

namespace butl
{
  auto char_scanner::
  peek () -> xchar
  {
    if (unget_)
      return buf_;
    else
    {
      if (eos_)
        return xchar (xchar::traits_type::eof (), line, column);
      else
      {
        xchar::int_type v (is_.peek ());

        if (v == xchar::traits_type::eof ())
          eos_ = true;

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

  auto char_scanner::
  get () -> xchar
  {
    if (unget_)
    {
      unget_ = false;
      return buf_;
    }
    else
    {
      // 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).
      //
      xchar c (peek ());

      if (!eos (c))
      {
        is_.get ();

        if (crlf_ && c == 0x0D)
        {
          xchar c1 (peek ());

          if (c1 == '\n')
          {
            is_.get ();
            c = c1;
          }
        }

        if (c == '\n')
        {
          line++;
          column = 1;
        }
        else
          column++;
      }

      return c;
    }
  }

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