mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibPDF: Add Reader::try_read for easier error propagation
This will allow us to use TRY(reader.try_read) instead of having to verify the result of reader.remaining() before calling read.read().
This commit is contained in:
parent
1b90ea7d3a
commit
c592b889bf
Notes:
sideshowbarker
2024-07-17 02:35:27 +09:00
Author: https://github.com/rtobar Commit: https://github.com/SerenityOS/serenity/commit/c592b889bf Pull-request: https://github.com/SerenityOS/serenity/pull/17082
1 changed files with 11 additions and 0 deletions
|
@ -12,6 +12,7 @@
|
|||
#include <AK/ScopeGuard.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibPDF/Error.h>
|
||||
|
||||
namespace PDF {
|
||||
|
||||
|
@ -59,6 +60,16 @@ public:
|
|||
return value;
|
||||
}
|
||||
|
||||
template<typename T = char>
|
||||
PDFErrorOr<T> try_read()
|
||||
{
|
||||
if (sizeof(T) + m_offset >= m_bytes.size()) {
|
||||
auto message = DeprecatedString::formatted("Cannot read {} bytes at offset {} of ReadonlyBytes of size {}", sizeof(T), m_offset, m_bytes.size());
|
||||
return Error { Error::Type::Parse, message };
|
||||
}
|
||||
return read<T>();
|
||||
}
|
||||
|
||||
char peek(size_t shift = 0) const
|
||||
{
|
||||
auto offset = m_offset + shift * (m_forwards ? 1 : -1);
|
||||
|
|
Loading…
Reference in a new issue