From 61d678a6e35785eaaf492648df16f49753fd5ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Tue, 30 Dec 2025 13:45:56 +0100 Subject: [PATCH] refactor: use `DataStream::clear` in `::read` and `::ignore` When `DataStream` is fully consumed, both `read()` and `ignore()` reset it to an empty state by clearing the backing buffer and resetting the read position. Call `clear()` in both places instead of open-coding the same state transition. This keeps the behavior unchanged while documenting the fully-consumed reset in one place. Remove the unused `Compact()` method as well - it has been unused for a long time and can be added back if it is ever needed. --- src/streams.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/streams.h b/src/streams.h index d6d6d57fb93..4fc5879f67e 100644 --- a/src/streams.h +++ b/src/streams.h @@ -206,12 +206,6 @@ public: value_type* data() { return vch.data() + m_read_pos; } const value_type* data() const { return vch.data() + m_read_pos; } - inline void Compact() - { - vch.erase(vch.begin(), vch.begin() + m_read_pos); - m_read_pos = 0; - } - bool Rewind(std::optional n = std::nullopt) { // Total rewind if no size is passed @@ -243,8 +237,8 @@ public: } memcpy(dst.data(), &vch[m_read_pos], dst.size()); if (next_read_pos.value() == vch.size()) { - m_read_pos = 0; - vch.clear(); + // If fully consumed, reset to empty state. + clear(); return; } m_read_pos = next_read_pos.value(); @@ -258,8 +252,8 @@ public: throw std::ios_base::failure("DataStream::ignore(): end of data"); } if (next_read_pos.value() == vch.size()) { - m_read_pos = 0; - vch.clear(); + // If all bytes are ignored, reset to empty state. + clear(); return; } m_read_pos = next_read_pos.value();