From 86eb98393ab87989cc9752506adba9786c635c5a Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Thu, 20 Feb 2025 10:17:18 +0100 Subject: IncrementalReader: Support reading from memory --- src/utils/cpp/incremental_reader.hpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/utils/cpp/incremental_reader.hpp') diff --git a/src/utils/cpp/incremental_reader.hpp b/src/utils/cpp/incremental_reader.hpp index f8f6d32b..31c3cc96 100644 --- a/src/utils/cpp/incremental_reader.hpp +++ b/src/utils/cpp/incremental_reader.hpp @@ -79,6 +79,16 @@ class IncrementalReader final { std::filesystem::path const& path) noexcept -> expected; + /// \brief Create IncrementalReader that uses the given string as the source + /// of data. + /// \param chunk_size Size of chunk, must be greater than 0. + /// \param data String to read. + /// \return Configured reader on success or an error message on failure. + [[nodiscard]] static auto FromMemory( + std::size_t chunk_size, + gsl::not_null const& data) noexcept + -> expected; + [[nodiscard]] auto GetContentSize() const noexcept -> std::size_t { return content_size_; } @@ -100,7 +110,8 @@ class IncrementalReader final { private: using FileSource = std::shared_ptr; - using ContentSource = std::variant; + using MemorySource = gsl::not_null; + using ContentSource = std::variant; std::size_t chunk_size_; std::size_t content_size_; @@ -122,6 +133,10 @@ class IncrementalReader final { [[nodiscard]] auto ReadFromFile(FileSource const& file, std::size_t offset) const -> expected; + [[nodiscard]] auto ReadFromMemory(MemorySource const& data, + std::size_t offset) const + -> expected; + /// \brief Obtain offset corresponding to the end of content. The content /// size is shifted by 1 character to properly handle empty sources. [[nodiscard]] auto GetEndOffset() const noexcept -> std::size_t { -- cgit v1.2.3