mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:47:35 +00:00
AK: Lower the requirements for InputStream::eof and rename it.
Consider the following snippet: void foo(InputStream& stream) { if(!stream.eof()) { u8 byte; stream >> byte; } } There is a very subtle bug in this snippet, for some input streams eof() might return false even if no more data can be read. In this case an error flag would be set on the stream. Until now I've always ensured that this is not the case, but this made the implementation of eof() unnecessarily complicated. InputFileStream::eof had to keep a ByteBuffer around just to make this possible. That meant a ton of unnecessary copies just to get a reliable eof(). In most cases it isn't actually necessary to have a reliable eof() implementation. In most other cases a reliable eof() is avaliable anyways because in some cases like InputMemoryStream it is very easy to implement.
This commit is contained in:
parent
8a21c528ad
commit
96edcbc27c
12 changed files with 61 additions and 89 deletions
|
@ -77,7 +77,7 @@ public:
|
|||
return nread;
|
||||
}
|
||||
|
||||
virtual bool read_or_error(Bytes bytes) override
|
||||
bool read_or_error(Bytes bytes) override
|
||||
{
|
||||
if (read(bytes) < bytes.size()) {
|
||||
set_fatal_error();
|
||||
|
@ -87,7 +87,9 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
virtual bool eof() const
|
||||
bool unreliable_eof() const override { return m_buffer_remaining == 0 && m_stream.unreliable_eof(); }
|
||||
|
||||
bool eof() const
|
||||
{
|
||||
if (m_buffer_remaining > 0)
|
||||
return false;
|
||||
|
@ -97,7 +99,7 @@ public:
|
|||
return m_buffer_remaining == 0;
|
||||
}
|
||||
|
||||
virtual bool discard_or_error(size_t count) override
|
||||
bool discard_or_error(size_t count) override
|
||||
{
|
||||
size_t ndiscarded = 0;
|
||||
while (ndiscarded < count) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue