|
| 1 | +#include "prometheus/iovector.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <iterator> |
| 5 | +#include <memory> |
| 6 | +#include <numeric> |
| 7 | + |
| 8 | +namespace prometheus { |
| 9 | + |
| 10 | +bool IOVector::empty() const { return data.empty() || !size(); } |
| 11 | + |
| 12 | +std::size_t IOVector::size() const { |
| 13 | + return std::accumulate(begin(data), end(data), std::size_t{0}, |
| 14 | + [](std::size_t size, const ByteVector& chunk) { |
| 15 | + return size + chunk.size(); |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +std::size_t IOVector::copy(std::size_t offset, void* buffer, |
| 20 | + std::size_t bufferSize) const { |
| 21 | + std::size_t copied = 0; |
| 22 | + for (const auto& chunk : data) { |
| 23 | + if (offset >= chunk.size()) { |
| 24 | + offset -= chunk.size(); |
| 25 | + continue; |
| 26 | + } |
| 27 | + |
| 28 | + auto chunkSize = std::min(chunk.size() - offset, bufferSize - copied); |
| 29 | + std::copy_n(chunk.data() + offset, chunkSize, |
| 30 | + reinterpret_cast<std::uint8_t*>(buffer) + copied); |
| 31 | + copied += chunkSize; |
| 32 | + offset = 0; |
| 33 | + |
| 34 | + if (copied == bufferSize) { |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + return copied; |
| 39 | +} |
| 40 | + |
| 41 | +void IOVector::add(const std::string& str, std::size_t chunkSize) { |
| 42 | + std::size_t size = str.size(); |
| 43 | + std::size_t offset = 0; |
| 44 | + |
| 45 | + while (size > 0U) { |
| 46 | + if (data.empty() || data.back().size() >= chunkSize) { |
| 47 | + data.emplace_back(); |
| 48 | + data.back().reserve(chunkSize); |
| 49 | + } |
| 50 | + auto&& chunk = data.back(); |
| 51 | + const auto toAdd = std::min(size, chunkSize - chunk.size()); |
| 52 | + chunk.insert(chunk.end(), str.data() + offset, str.data() + offset + toAdd); |
| 53 | + |
| 54 | + size -= toAdd; |
| 55 | + offset += toAdd; |
| 56 | + } |
| 57 | +} |
| 58 | +} // namespace prometheus |
0 commit comments