Accelerate access to underlying std::istream streambuf

This commit is contained in:
Perry Kundert 2017-10-05 14:13:55 -07:00
parent f775922ca8
commit 184dab60e6

View file

@ -1417,6 +1417,7 @@ class input_stream_adapter : public input_adapter_protocol
} }
explicit input_stream_adapter(std::istream& i) explicit input_stream_adapter(std::istream& i)
: is(i) : is(i)
, sb(i.rdbuf())
{ {
// Ignore Byte Order Mark at start of input // Ignore Byte Order Mark at start of input
int c; int c;
@ -1448,18 +1449,19 @@ class input_stream_adapter : public input_adapter_protocol
int get_character() override int get_character() override
{ {
int c = is.rdbuf()->sbumpc(); // Avoided for performance: int c = is.get(); int c = sb->sbumpc(); // Avoided for performance: int c = is.get();
return c < 0 ? c : ( c & 0xFF ); // faster than == std::char_traits<char>::eof() return c < 0 ? c : ( c & 0xFF ); // faster than == std::char_traits<char>::eof()
} }
void unget_character() override void unget_character() override
{ {
is.rdbuf()->sungetc(); // Avoided for performance: is.unget(); sb->sungetc(); // Avoided for performance: is.unget();
} }
private: private:
/// the associated input stream /// the associated input stream
std::istream& is; std::istream& is;
std::streambuf *sb;
}; };
/// input adapter for buffer input /// input adapter for buffer input