Skip to content

Commit f7fbe73

Browse files
committed
Use char instead of uint8_t and remove unnecessary includes
Also going back to reinterpret_cast due to bugprone-bitwise-pointer-cast. I actually think they are right on this, using bit_cast makes it seems things are safer than they are. Maybe add an "unsafe_pointer_cast" in the future?
1 parent 9e0994b commit f7fbe73

25 files changed

Lines changed: 48 additions & 64 deletions

include/mdcomp/basic_decoder.hh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "mdcomp/ignore_unused_variable_warning.hh"
2424
#include "mdcomp/stream_utils.hh"
2525

26-
#include <bit>
2726
#include <cstddef>
2827
#include <cstdint>
2928
#include <iostream>
@@ -69,7 +68,8 @@ public:
6968
}
7069

7170
auto const size_bytes = count * sizeof(T);
72-
return std::bit_cast<T*>(::operator new[](size_bytes, std::align_val_t{Align}));
71+
return reinterpret_cast<T*>(
72+
::operator new[](size_bytes, std::align_val_t{Align}));
7373
}
7474

7575
void deallocate(T* pointer, [[maybe_unused]] std::size_t count_bytes) {
@@ -85,13 +85,13 @@ bool basic_decoder<Format, Pad, Args...>::encode(
8585
source.ignore(std::numeric_limits<std::streamsize>::max());
8686
auto full_size = static_cast<size_t>(source.gcount());
8787
source.seekg(start);
88-
std::vector<uint8_t, aligned_allocator<uint8_t>> data;
88+
std::vector<char, aligned_allocator<char>> data;
8989
if constexpr (Pad == pad_mode::pad_even) {
9090
data.resize(detail::round_up(full_size, 2U));
9191
} else {
9292
data.resize(full_size);
9393
}
94-
source.read(std::bit_cast<char*>(data.data()), std::ssize(data));
94+
source.read(data.data(), std::ssize(data));
9595
if constexpr (Pad == pad_mode::pad_even) {
9696
if (data.size() > full_size) {
9797
data.back() = 0;

include/mdcomp/bigendian_io.hh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ namespace detail {
6565
template <typename Iter>
6666
concept byte_input_iterator = requires() {
6767
requires std::input_iterator<Iter>;
68-
requires(std::is_same_v<std::iter_value_t<Iter>, char>)
69-
|| (std::is_same_v<std::iter_value_t<Iter>, uint8_t>);
68+
requires(
69+
std::is_same_v<std::iter_value_t<Iter>, char>
70+
|| std::is_same_v<std::iter_value_t<Iter>, uint8_t>);
7071
};
7172

7273
template <typename Iter>
7374
concept byte_output_iterator
74-
= (std::output_iterator<Iter, uint8_t>) || (std::output_iterator<Iter, char>);
75+
= std::output_iterator<Iter, uint8_t> || std::output_iterator<Iter, char>;
7576

7677
template <typename T>
7778
concept container = std::regular<T> && std::swappable<T>

include/mdcomp/comper.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "mdcomp/basic_decoder.hh"
2323
#include "mdcomp/moduled_adaptor.hh"
2424

25-
#include <cstdint>
2625
#include <iosfwd>
2726
#include <span>
2827

@@ -33,7 +32,7 @@ using moduled_comper = moduled_adaptor<comper, 4096U, 1U>;
3332
class comper : public basic_comper, public moduled_comper {
3433
friend basic_comper;
3534
friend moduled_comper;
36-
static bool encode(std::ostream& dest, std::span<uint8_t const> data);
35+
static bool encode(std::ostream& dest, std::span<char const> data);
3736

3837
public:
3938
using basic_comper::encode;

include/mdcomp/comperx.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "mdcomp/basic_decoder.hh"
2323
#include "mdcomp/moduled_adaptor.hh"
2424

25-
#include <cstdint>
2625
#include <iosfwd>
2726
#include <span>
2827

@@ -33,7 +32,7 @@ using moduled_comperx = moduled_adaptor<comperx, 4096U, 1U>;
3332
class comperx : public basic_comperx, public moduled_comperx {
3433
friend basic_comperx;
3534
friend moduled_comperx;
36-
static bool encode(std::ostream& dest, std::span<uint8_t const> data);
35+
static bool encode(std::ostream& dest, std::span<char const> data);
3736

3837
public:
3938
using basic_comperx::encode;

include/mdcomp/enigma.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "mdcomp/basic_decoder.hh"
2424
#include "mdcomp/moduled_adaptor.hh"
2525

26-
#include <cstdint>
2726
#include <iosfwd>
2827
#include <span>
2928

@@ -34,7 +33,7 @@ using moduled_enigma = moduled_adaptor<enigma, 4096U, 1U>;
3433
class enigma : public basic_enigma, public moduled_enigma {
3534
friend basic_enigma;
3635
friend moduled_enigma;
37-
static bool encode(std::ostream& dest, std::span<uint8_t const> data);
36+
static bool encode(std::ostream& dest, std::span<char const> data);
3837

3938
public:
4039
static bool encode(std::istream& source, std::ostream& dest);

include/mdcomp/kosinski.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "mdcomp/basic_decoder.hh"
2323
#include "mdcomp/moduled_adaptor.hh"
2424

25-
#include <cstdint>
2625
#include <iosfwd>
2726
#include <span>
2827

@@ -33,7 +32,7 @@ using moduled_kosinski = moduled_adaptor<kosinski, 4096U, 16U>;
3332
class kosinski : public basic_kosinski, public moduled_kosinski {
3433
friend basic_kosinski;
3534
friend moduled_kosinski;
36-
static bool encode(std::ostream& dest, std::span<uint8_t const> data);
35+
static bool encode(std::ostream& dest, std::span<char const> data);
3736

3837
public:
3938
using basic_kosinski::encode;

include/mdcomp/kosplus.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "mdcomp/basic_decoder.hh"
2323
#include "mdcomp/moduled_adaptor.hh"
2424

25-
#include <cstdint>
2625
#include <iosfwd>
2726
#include <span>
2827

@@ -33,7 +32,7 @@ using moduled_kosplus = moduled_adaptor<kosplus, 4096U, 1U>;
3332
class kosplus : public basic_kosplus, public moduled_kosplus {
3433
friend basic_kosplus;
3534
friend moduled_kosplus;
36-
static bool encode(std::ostream& dest, std::span<uint8_t const> data);
35+
static bool encode(std::ostream& dest, std::span<char const> data);
3736

3837
public:
3938
using basic_kosplus::encode;

include/mdcomp/lzkn1.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "mdcomp/basic_decoder.hh"
2323
#include "mdcomp/moduled_adaptor.hh"
2424

25-
#include <cstdint>
2625
#include <iosfwd>
2726
#include <span>
2827

@@ -33,7 +32,7 @@ using moduled_lzkn1 = moduled_adaptor<lzkn1, 4096U, 1U>;
3332
class lzkn1 : public basic_lzkn1, public moduled_lzkn1 {
3433
friend basic_lzkn1;
3534
friend moduled_lzkn1;
36-
static bool encode(std::ostream& dest, std::span<uint8_t const> data);
35+
static bool encode(std::ostream& dest, std::span<char const> data);
3736

3837
public:
3938
using basic_lzkn1::encode;

include/mdcomp/lzss.hh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <boost/container/static_vector.hpp>
2929

3030
#include <algorithm>
31-
#include <bit>
3231
#include <cassert>
3332
#include <concepts> // IWYU pragma: keep
3433
#include <cstddef>
@@ -237,7 +236,7 @@ namespace lzss {
237236
using symbolwise_data = typename Adaptor::adj_list_node::symbolwise_data;
238237
symbolwise_data data(length);
239238
source.read(
240-
std::bit_cast<char*>(data.data()),
239+
reinterpret_cast<char*>(data.data()),
241240
static_cast<std::streamsize>(length * sizeof(stream_t)));
242241
nodes.emplace_back(position, std::move(data), type);
243242
output_size += Adaptor::edge_size(nodes.back());
@@ -667,7 +666,7 @@ namespace lzss {
667666
};
668667

669668
template <adaptor_t Adaptor>
670-
auto find_optimal_parse(std::span<uint8_t const> data_in, Adaptor adaptor) noexcept {
669+
auto find_optimal_parse(std::span<char const> data_in, Adaptor adaptor) noexcept {
671670
ignore_unused_variable_warning(adaptor);
672671
using edge_type = typename Adaptor::edge_type;
673672
using stream_t = typename Adaptor::stream_t;
@@ -679,7 +678,7 @@ namespace lzss {
679678

680679
auto read_stream = [](data_t data, size_t offset) {
681680
auto const* pointer
682-
= std::bit_cast<uint8_t const*>(std::addressof(data[offset]));
681+
= reinterpret_cast<char const*>(std::addressof(data[offset]));
683682
return stream_endian_t::template read<stream_t>(pointer);
684683
};
685684

@@ -839,7 +838,7 @@ namespace lzss {
839838
static_cast<size_t>(std::min(length, distance)),
840839
boost::container::default_init_t{});
841840
dest.read(
842-
std::bit_cast<char*>(buffer.data()),
841+
reinterpret_cast<char*>(buffer.data()),
843842
static_cast<std::streamsize>(buffer.size() * sizeof(stream_t)));
844843

845844
if (length > distance) {
@@ -860,13 +859,13 @@ namespace lzss {
860859

861860
dest.seekp(pointer);
862861
dest.write(
863-
std::bit_cast<char const*>(buffer.data()),
862+
reinterpret_cast<char const*>(buffer.data()),
864863
static_cast<std::streamsize>(buffer.size() * sizeof(stream_t)));
865864
}
866865

867866
template <adaptor_t Adaptor>
868867
auto encode(
869-
std::ostream& dest, std::span<uint8_t const> data, Adaptor adaptor) noexcept {
868+
std::ostream& dest, std::span<char const> data, Adaptor adaptor) noexcept {
870869
using ostream_t = Adaptor::ostream_t;
871870

872871
// Compute optimal Comper parsing of input file.

include/mdcomp/moduled_adaptor.hh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "mdcomp/forge_span.hh"
2525
#include "mdcomp/stream_utils.hh"
2626

27-
#include <bit>
2827
#include <cstddef>
2928
#include <cstdint>
3029
#include <ios>
@@ -34,7 +33,6 @@
3433
#include <sstream>
3534
#include <vector>
3635

37-
3836
template <typename Format, size_t DefaultModuleSize, size_t DefaultModulePadding>
3937
class moduled_adaptor {
4038
public:
@@ -84,10 +82,10 @@ bool moduled_adaptor<Format, DefaultModuleSize, DefaultModulePadding>::moduled_e
8482
source.ignore(std::numeric_limits<std::streamsize>::max());
8583
auto full_size = source.gcount();
8684
source.seekg(location);
87-
std::vector<uint8_t> data;
85+
std::vector<char> data;
8886
data.resize(static_cast<size_t>(full_size));
8987
auto pointer = std::ranges::cbegin(data);
90-
source.read(std::bit_cast<char*>(data.data()), full_size);
88+
source.read(data.data(), full_size);
9189

9290
big_endian::write2(
9391
dest, static_cast<size_t>(full_size) & std::numeric_limits<uint16_t>::max());

0 commit comments

Comments
 (0)