From b6e02f4224975a6425f62095bc35478e8866db77 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 24 May 2017 13:26:13 +0200 Subject: Various improvements to char_scanner --- libbutl/char-scanner.cxx | 68 +++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) (limited to 'libbutl/char-scanner.cxx') diff --git a/libbutl/char-scanner.cxx b/libbutl/char-scanner.cxx index cbc2503..42a72cc 100644 --- a/libbutl/char-scanner.cxx +++ b/libbutl/char-scanner.cxx @@ -14,44 +14,44 @@ namespace butl peek () -> xchar { if (unget_) - return buf_; - else - { - if (eos_) - return xchar (xchar::traits_type::eof (), line, column); - else - { - xchar::int_type v (is_.peek ()); + return ungetc_; - if (v == xchar::traits_type::eof ()) - eos_ = true; - else if (crlf_ && v == 0x0D) - { - is_.get (); - xchar::int_type v1 (is_.peek ()); + if (unpeek_) + return unpeekc_; - if (v1 != '\n') - { - unget_ = true; - buf_ = '\n'; - } + if (eos_) + return xchar (xchar::traits_type::eof (), line, column); - v = '\n'; - } + xchar::int_type v (is_.peek ()); - return xchar (v, line, column); + if (v == xchar::traits_type::eof ()) + eos_ = true; + else if (crlf_ && v == 0x0D) + { + is_.get (); + xchar::int_type v1 (is_.peek ()); + + if (v1 != '\n') + { + // We need to make sure subsequent calls to peek() return newline. + // + unpeek_ = true; + unpeekc_ = xchar ('\n', line, column); } + + v = '\n'; } + + return xchar (v, line, column); } - auto char_scanner:: - get () -> xchar + void char_scanner:: + get (const xchar& c) { if (unget_) - { unget_ = false; - return buf_; - } + else if (unpeek_) + unpeek_ = false; else { // When is_.get () returns eof, the failbit is also set (stupid, @@ -60,8 +60,6 @@ namespace butl // 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 (); @@ -74,18 +72,6 @@ namespace butl 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; - } } -- cgit v1.1