Skip to content

Commit

Permalink
Cast std::streamsize to std::size_t for fast stream copies.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jan 29, 2024
1 parent 12f8b7f commit e284f20
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
19 changes: 13 additions & 6 deletions include/bitcoin/system/impl/stream/iostream/iostream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#ifndef LIBBITCOIN_SYSTEM_STREAM_IOSTREAM_IOSTREAM_IPP
#define LIBBITCOIN_SYSTEM_STREAM_IOSTREAM_IOSTREAM_IPP

#include <algorithm>
#include <type_traits>
#include <bitcoin/system/define.hpp>
#include <bitcoin/system/math/math.hpp>

namespace libbitcoin {
namespace system {
Expand Down Expand Up @@ -157,35 +160,39 @@ template <typename Character>
void
iostream<Character>::read(char_type* data, std::streamsize count) NOEXCEPT
{
if (is_overflow(count))
const auto bytes = possible_narrow_sign_cast<size_t>(count);

if (is_overflow(bytes))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(position_, count, data);
std::copy_n(position_, bytes, data);
BC_POP_WARNING()

position_ += count;
position_ += bytes;
}

template <typename Character>
void
iostream<Character>::write(const char_type* data,
std::streamsize count) NOEXCEPT
{
if (is_overflow(count))
const auto bytes = possible_narrow_sign_cast<size_t>(count);

if (is_overflow(bytes))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(data, count, position_);
std::copy_n(data, bytes, position_);
BC_POP_WARNING()

position_ += count;
position_ += bytes;
}

template <typename Character>
Expand Down
8 changes: 5 additions & 3 deletions include/bitcoin/system/impl/stream/iostream/istream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,19 @@ template <typename Character>
void
istream<Character>::read(char_type* data, std::streamsize count) NOEXCEPT
{
if (is_overflow(count))
const auto bytes = possible_narrow_sign_cast<size_t>(count);

if (is_overflow(bytes))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(position_, count, data);
std::copy_n(position_, bytes, data);
BC_POP_WARNING()

position_ += count;
position_ += bytes;
}

// private
Expand Down
9 changes: 6 additions & 3 deletions include/bitcoin/system/impl/stream/iostream/ostream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <bitcoin/system/define.hpp>
#include <bitcoin/system/math/math.hpp>

namespace libbitcoin {
namespace system {
Expand Down Expand Up @@ -81,17 +82,19 @@ void
ostream<Character>::write(const char_type* data,
std::streamsize count) NOEXCEPT
{
if (is_overflow(count))
const auto bytes = possible_narrow_sign_cast<size_t>(count);

if (is_overflow(bytes))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(data, count, position_);
std::copy_n(data, bytes, position_);
BC_POP_WARNING()

position_ += count;
position_ += bytes;
}

template <typename Character>
Expand Down

0 comments on commit e284f20

Please sign in to comment.