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:
Rodrigo Tobar 2023-01-15 12:31:58 +08:00 committed by Andreas Kling
parent 1b90ea7d3a
commit c592b889bf
Notes: sideshowbarker 2024-07-17 02:35:27 +09:00

View file

@ -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);