io/BufferedReader: migrate from WritableBuffer to std::span

This commit is contained in:
Max Kellermann 2022-05-10 16:05:27 +02:00 committed by Max Kellermann
parent 957d3e51e0
commit 570755f05a
2 changed files with 17 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2014-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -35,8 +35,6 @@
#include <cstdint> #include <cstdint>
#include <stdexcept> #include <stdexcept>
#include <string.h>
bool bool
BufferedReader::Fill(bool need_more) BufferedReader::Fill(bool need_more)
{ {
@ -68,8 +66,8 @@ BufferedReader::ReadFull(std::size_t size)
{ {
while (true) { while (true) {
auto r = Read(); auto r = Read();
if (r.size >= size) if (r.size() >= size)
return r.data; return r.data();
if (!Fill(true)) if (!Fill(true))
throw std::runtime_error("Premature end of file"); throw std::runtime_error("Premature end of file");
@ -77,25 +75,22 @@ BufferedReader::ReadFull(std::size_t size)
} }
std::size_t std::size_t
BufferedReader::ReadFromBuffer(WritableBuffer<void> dest) noexcept BufferedReader::ReadFromBuffer(std::span<std::byte> dest) noexcept
{ {
auto src = Read(); const auto src = Read();
std::size_t nbytes = std::min(src.size, dest.size); std::size_t nbytes = std::min(src.size(), dest.size());
memcpy(dest.data, src.data, nbytes); std::copy_n(src.data(), nbytes, dest.data());
Consume(nbytes); Consume(nbytes);
return nbytes; return nbytes;
} }
void void
BufferedReader::ReadFull(WritableBuffer<void> _dest) BufferedReader::ReadFull(std::span<std::byte> dest)
{ {
auto dest = WritableBuffer<uint8_t>::FromVoid(_dest);
assert(dest.size == _dest.size);
while (true) { while (true) {
std::size_t nbytes = ReadFromBuffer(dest.ToVoid()); std::size_t nbytes = ReadFromBuffer(dest);
dest.skip_front(nbytes); dest = dest.subspan(nbytes);
if (dest.size == 0) if (dest.empty())
break; break;
if (!Fill(true)) if (!Fill(true))

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2014-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,12 +27,12 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef BUFFERED_READER_HXX #pragma once
#define BUFFERED_READER_HXX
#include "util/DynamicFifoBuffer.hxx" #include "util/DynamicFifoBuffer.hxx"
#include <cstddef> #include <cstddef>
#include <span>
class Reader; class Reader;
@ -64,7 +64,7 @@ public:
bool Fill(bool need_more); bool Fill(bool need_more);
[[gnu::pure]] [[gnu::pure]]
WritableBuffer<void> Read() const noexcept { std::span<std::byte> Read() const noexcept {
return buffer.Read().ToVoid(); return buffer.Read().ToVoid();
} }
@ -83,14 +83,14 @@ public:
* Read (and consume) data from the input buffer into the * Read (and consume) data from the input buffer into the
* given buffer. Does not attempt to refill the buffer. * given buffer. Does not attempt to refill the buffer.
*/ */
std::size_t ReadFromBuffer(WritableBuffer<void> dest) noexcept; std::size_t ReadFromBuffer(std::span<std::byte> dest) noexcept;
/** /**
* Read data into the given buffer and consume it from our * Read data into the given buffer and consume it from our
* buffer. Throw an exception if the request cannot be * buffer. Throw an exception if the request cannot be
* forfilled. * forfilled.
*/ */
void ReadFull(WritableBuffer<void> dest); void ReadFull(std::span<std::byte> dest);
char *ReadLine(); char *ReadLine();
@ -98,5 +98,3 @@ public:
return line_number; return line_number;
} }
}; };
#endif