summaryrefslogtreecommitdiff
path: root/src/utils/cpp/incremental_reader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/cpp/incremental_reader.cpp')
-rw-r--r--src/utils/cpp/incremental_reader.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/utils/cpp/incremental_reader.cpp b/src/utils/cpp/incremental_reader.cpp
index c1b5bc3a..a7bcc184 100644
--- a/src/utils/cpp/incremental_reader.cpp
+++ b/src/utils/cpp/incremental_reader.cpp
@@ -72,6 +72,29 @@ auto IncrementalReader::FromFile(std::size_t chunk_size,
}
}
+auto IncrementalReader::FromMemory(
+ std::size_t chunk_size,
+ gsl::not_null<std::string const*> const& data) noexcept
+ -> expected<IncrementalReader, std::string> {
+ if (chunk_size == 0) {
+ return unexpected<std::string>{
+ "IncrementalReader: the chunk size cannot be 0"};
+ }
+
+ try {
+ // Reading from memory doesn't require a buffer. The resulting chunks
+ // point at the content_ directly.
+ return IncrementalReader{chunk_size,
+ /*content_size=*/data->size(),
+ data,
+ /*buffer=*/std::string{}};
+ } catch (...) {
+ return unexpected<std::string>{
+ "IncrementalReader: Got an unknown exception during creation from "
+ "a string"};
+ }
+}
+
auto IncrementalReader::ReadChunk(std::size_t offset) const noexcept
-> expected<std::string_view, std::string> {
using Result = expected<std::string_view, std::string>;
@@ -79,6 +102,9 @@ auto IncrementalReader::ReadChunk(std::size_t offset) const noexcept
[this, offset](FileSource const& file) -> Result {
return ReadFromFile(file, offset);
},
+ [this, offset](MemorySource const& data) -> Result {
+ return ReadFromMemory(data, offset);
+ },
};
try {
@@ -119,6 +145,18 @@ auto IncrementalReader::ReadFromFile(FileSource const& file, std::size_t offset)
return std::string_view{buffer_.data(), read};
}
+auto IncrementalReader::ReadFromMemory(MemorySource const& data,
+ std::size_t offset) const
+ -> expected<std::string_view, std::string> {
+ if (data->empty()) {
+ // NOLINTNEXTLINE(bugprone-string-constructor,-warnings-as-errors)
+ return std::string_view{data->data(), 0};
+ }
+
+ std::size_t const read = std::min(chunk_size_, data->size() - offset);
+ return std::string_view{&data->at(offset), read};
+}
+
IncrementalReader::Iterator::Iterator(
gsl::not_null<IncrementalReader const*> const& owner,
std::size_t offset) noexcept