4#ifndef GREM_CORE_DATA_READER_HPP
5#define GREM_CORE_DATA_READER_HPP
28template <
typename Self>
32 return readBytesInto<Allocation<byte>>(maxLength);
36 return readBytesInto<ArrayList<byte>>(maxLength);
40 return readBytesInto<Buffer<byte>>(maxLength);
44 return readBytesInto<String>(maxLength);
48 if (
const Optional<byte> result = peekByte()) {
49 return bit_cast<char>(*result);
55 return bit_cast<char>(readByte());
59 if (
const Optional<byte> result = tryReadByte()) {
60 return bit_cast<char>(*result);
67 if constexpr (
N == 1) {
78 if constexpr (
N == 1) {
79 if (
const Optional<byte> result = tryReadByte()) {
96 return bit_cast<Array<char, N>>(data);
102 if (!tryRead(data)) {
105 return bit_cast<Array<char, N>>(data);
110 while (result.size() < maxLength) {
111 const Optional<char> ch = tryReadChar();
112 if (!ch || *ch ==
'\n') {
116 if (peekChar() ==
'\n') {
121 result.push_back(*ch);
128 while (result.size() < maxLength) {
129 const Optional<char> ch = tryReadChar();
130 if (!ch || *ch ==
'\0') {
133 result.push_back(*ch);
139 if (
const Optional<byte> result = peekByte()) {
140 return bit_cast<int8_t>(*result);
146 return bit_cast<int8_t>(readByte());
150 if (
const Optional<byte> result = tryReadByte()) {
151 return bit_cast<int8_t>(*result);
157 if (
const Optional<byte> result = peekByte()) {
158 return bit_cast<uint8_t>(*result);
164 return bit_cast<uint8_t>(readByte());
168 if (
const Optional<byte> result = tryReadByte()) {
169 return bit_cast<uint8_t>(*result);
177 return bit_cast<int16_t>(data);
182 if (!tryRead(data)) {
185 return bit_cast<int16_t>(data);
196 if (!tryRead(data)) {
210 if (!tryRead(data)) {
219 return bit_cast<uint16_t>(data);
224 if (!tryRead(data)) {
227 return bit_cast<uint16_t>(data);
238 if (!tryRead(data)) {
252 if (!tryRead(data)) {
261 return bit_cast<int32_t>(data);
266 if (!tryRead(data)) {
269 return bit_cast<int32_t>(data);
280 if (!tryRead(data)) {
294 if (!tryRead(data)) {
303 return bit_cast<uint32_t>(data);
308 if (!tryRead(data)) {
311 return bit_cast<uint32_t>(data);
322 if (!tryRead(data)) {
336 if (!tryRead(data)) {
345 return bit_cast<int64_t>(data);
350 if (!tryRead(data)) {
353 return bit_cast<int64_t>(data);
364 if (!tryRead(data)) {
378 if (!tryRead(data)) {
387 return bit_cast<uint64_t>(data);
392 if (!tryRead(data)) {
395 return bit_cast<uint64_t>(data);
406 if (!tryRead(data)) {
420 if (!tryRead(data)) {
426 [[nodiscard]] int64_t readIntLEB128() {
432 result |=
static_cast<uint64_t
>(
byte & 0b01111111) << shift;
434 }
while (shift < 64 && (
byte & 0b10000000) != 0);
435 if (shift < 64 && (
byte & 0b01000000) != 0) {
436 result |= ~uint64_t{0} << shift;
438 return bit_cast<int64_t>(result);
441 [[nodiscard]] Optional<int64_t> tryReadIntLEB128() {
446 const Optional<uint8_t> nextByte = tryReadUInt8();
451 result |=
static_cast<uint64_t
>(
byte & 0b01111111) << shift;
453 }
while (shift < 64 && (
byte & 0b10000000) != 0);
454 if (shift < 64 && (
byte & 0b01000000) != 0) {
455 result |= ~uint64_t{0} << shift;
457 return bit_cast<int64_t>(result);
460 [[nodiscard]] uint64_t readUIntLEB128() {
466 result |=
static_cast<uint64_t
>(
byte & 0b01111111) << shift;
468 }
while (shift < 64 && (
byte & 0b10000000) != 0);
472 [[nodiscard]] Optional<uint64_t> tryReadUIntLEB128() {
477 const Optional<uint8_t> nextByte = tryReadUInt8();
482 result |=
static_cast<uint64_t
>(
byte & 0b01111111) << shift;
484 }
while (shift < 64 && (
byte & 0b10000000) != 0);
492 return bit_cast<float16_t>(data);
498 if (!tryRead(data)) {
501 return bit_cast<float16_t>(data);
514 if (!tryRead(data)) {
530 if (!tryRead(data)) {
537 static_assert(Limits<float32_t>::IS_IEC60559);
540 return bit_cast<float32_t>(data);
544 static_assert(Limits<float32_t>::IS_IEC60559);
546 if (!tryRead(data)) {
549 return bit_cast<float32_t>(data);
553 static_assert(Limits<float32_t>::IS_IEC60559);
560 static_assert(Limits<float32_t>::IS_IEC60559);
562 if (!tryRead(data)) {
569 static_assert(Limits<float32_t>::IS_IEC60559);
576 static_assert(Limits<float32_t>::IS_IEC60559);
578 if (!tryRead(data)) {
585 static_assert(Limits<float64_t>::IS_IEC60559);
587 if (!tryRead(data)) {
590 return bit_cast<float64_t>(data);
594 static_assert(Limits<float64_t>::IS_IEC60559);
601 static_assert(Limits<float64_t>::IS_IEC60559);
603 if (!tryRead(data)) {
610 static_assert(Limits<float64_t>::IS_IEC60559);
617 static_assert(Limits<float64_t>::IS_IEC60559);
619 if (!tryRead(data)) {
626 template <
typename Container>
628 return static_cast<Self*
>(
this)->
template readBytesInto<Container>(maxLength);
632 static_cast<Self*
>(
this)->read(data);
636 return static_cast<Self*
>(
this)->tryRead(data);
640 return static_cast<Self*
>(
this)->peekByte();
644 return static_cast<Self*
>(
this)->readByte();
648 static_cast<Self*
>(
this)->skipByte();
652 return static_cast<Self*
>(
this)->tryReadByte();
658class Reader :
private detail::ReaderBase<Reader> {
662 , readSomeImplementation(readSomeImplementation) {}
667 const size_t bytesRead =
min(input.
size(), data.
size());
669 memcpy(data.
data(), input.
data(), bytesRead);
670 input = input.
subspan(bytesRead);
677 std::streambuf& input = *
static_cast<std::streambuf*
>(context);
678 size_t totalBytesRead = 0;
679 while (!data.
empty()) {
680 const size_t size =
min(data.
size(),
size_t{Limits<std::streamsize>::MAX});
681 const std::streamsize bytesRead = input.sgetn(
reinterpret_cast<char*
>(data.
data()),
static_cast<std::streamsize
>(size));
682 if (bytesRead <= 0) {
685 totalBytesRead +=
static_cast<size_t>(bytesRead);
686 data = data.
subspan(
static_cast<size_t>(bytesRead));
688 return totalBytesRead;
692 :
Reader(*input.rdbuf()) {}
700 : context(std::exchange(other.context,
nullptr))
701 , readSomeImplementation(std::exchange(other.readSomeImplementation,
nullptr))
702 , writeOffset(std::exchange(other.writeOffset,
size_t{0}))
703 , readOffset(std::exchange(other.readOffset,
size_t{0})) {
704 if (readOffset < writeOffset) {
705 memcpy(buffer.data(), other.buffer.data() + readOffset, writeOffset - readOffset);
710 if (
this == &other) {
713 context = std::exchange(other.context,
nullptr);
714 readSomeImplementation = std::exchange(other.readSomeImplementation,
nullptr);
715 writeOffset = std::exchange(other.writeOffset,
size_t{0});
716 readOffset = std::exchange(other.readOffset,
size_t{0});
717 if (readOffset < writeOffset) {
718 memcpy(buffer.data(), other.buffer.data() + readOffset, writeOffset - readOffset);
723 [[nodiscard]]
bool eof() {
724 if (readOffset < writeOffset) {
727 writeOffset = readSomeImplementation(context, buffer);
729 return writeOffset != 0;
733 if (readOffset == writeOffset) {
734 writeOffset = readSomeImplementation(context, buffer);
737 const size_t bytesRead =
min(writeOffset - readOffset, data.
size());
739 memcpy(data.
data(), buffer.data() + readOffset, bytesRead);
740 readOffset += bytesRead;
747 while (!data.
empty()) {
748 const size_t bytesRead =
readSome(data);
749 if (bytesRead == 0) {
752 data = data.
subspan(bytesRead);
759 if (readOffset == writeOffset) {
760 writeOffset = readSomeImplementation(context, buffer);
763 const size_t bytesSkipped =
min(writeOffset - readOffset, maxLength);
764 readOffset += bytesSkipped;
770 while (maxLength > 0) {
771 const size_t bytesRead =
skipSome(maxLength);
772 if (bytesRead == 0) {
775 maxLength -= bytesRead;
783 throw std::length_error{
"Unexpected end of data."};
789 throw std::length_error{
"Unexpected end of data."};
799 const size_t bytesSkipped =
skipSome(n);
800 if (bytesSkipped == 0) {
809 if (readOffset < writeOffset) {
810 return buffer[readOffset];
812 writeOffset = readSomeImplementation(context, buffer);
814 if (writeOffset == 0) {
824 throw std::length_error{
"Unexpected end of input."};
829 throw std::length_error{
"Unexpected end of input."};
845 using ReaderBase::peekChar;
846 using ReaderBase::peekInt8;
847 using ReaderBase::peekUInt8;
848 using ReaderBase::readBytes;
849 using ReaderBase::readBytesIntoAllocation;
850 using ReaderBase::readBytesIntoArrayList;
851 using ReaderBase::readBytesIntoBuffer;
852 using ReaderBase::readBytesIntoString;
853 using ReaderBase::readChar;
854 using ReaderBase::readChars;
855 using ReaderBase::readFloat16BE;
856 using ReaderBase::readFloat16HE;
857 using ReaderBase::readFloat16LE;
858 using ReaderBase::readFloat32BE;
859 using ReaderBase::readFloat32HE;
860 using ReaderBase::readFloat32LE;
861 using ReaderBase::readFloat64BE;
862 using ReaderBase::readFloat64LE;
863 using ReaderBase::readInt16BE;
864 using ReaderBase::readInt16HE;
865 using ReaderBase::readInt16LE;
866 using ReaderBase::readInt32BE;
867 using ReaderBase::readInt32HE;
868 using ReaderBase::readInt32LE;
869 using ReaderBase::readInt64BE;
870 using ReaderBase::readInt64HE;
871 using ReaderBase::readInt64LE;
872 using ReaderBase::readInt8;
873 using ReaderBase::readIntLEB128;
874 using ReaderBase::readLine;
875 using ReaderBase::readNullTerminatedString;
876 using ReaderBase::readUInt16BE;
877 using ReaderBase::readUInt16HE;
878 using ReaderBase::readUInt16LE;
879 using ReaderBase::readUInt32BE;
880 using ReaderBase::readUInt32HE;
881 using ReaderBase::readUInt32LE;
882 using ReaderBase::readUInt64BE;
883 using ReaderBase::readUInt64HE;
884 using ReaderBase::readUInt64LE;
885 using ReaderBase::readUInt8;
886 using ReaderBase::readUIntLEB128;
887 using ReaderBase::tryReadBytes;
888 using ReaderBase::tryReadChar;
889 using ReaderBase::tryReadChars;
890 using ReaderBase::tryReadFloat16BE;
891 using ReaderBase::tryReadFloat16HE;
892 using ReaderBase::tryReadFloat16LE;
893 using ReaderBase::tryReadFloat32BE;
894 using ReaderBase::tryReadFloat32HE;
895 using ReaderBase::tryReadFloat32LE;
896 using ReaderBase::tryReadFloat64BE;
897 using ReaderBase::tryReadFloat64HE;
898 using ReaderBase::tryReadFloat64LE;
899 using ReaderBase::tryReadInt16BE;
900 using ReaderBase::tryReadInt16HE;
901 using ReaderBase::tryReadInt16LE;
902 using ReaderBase::tryReadInt32BE;
903 using ReaderBase::tryReadInt32HE;
904 using ReaderBase::tryReadInt32LE;
905 using ReaderBase::tryReadInt64BE;
906 using ReaderBase::tryReadInt64HE;
907 using ReaderBase::tryReadInt64LE;
908 using ReaderBase::tryReadInt8;
909 using ReaderBase::tryReadIntLEB128;
910 using ReaderBase::tryReadUInt16BE;
911 using ReaderBase::tryReadUInt16HE;
912 using ReaderBase::tryReadUInt16LE;
913 using ReaderBase::tryReadUInt32BE;
914 using ReaderBase::tryReadUInt32HE;
915 using ReaderBase::tryReadUInt32LE;
916 using ReaderBase::tryReadUInt64BE;
917 using ReaderBase::tryReadUInt64HE;
918 using ReaderBase::tryReadUInt64LE;
919 using ReaderBase::tryReadUInt8;
920 using ReaderBase::tryReadUIntLEB128;
925 template <
typename Container>
926 [[nodiscard]] Container readBytesInto(
size_t maxLength) {
927 static_assert(
sizeof(
typename Container::value_type) ==
sizeof(byte));
930 result.resize(
min(buffer.
size(), maxLength));
933 while (
length < maxLength) {
934 result.resize(
min(
length + buffer.size(), maxLength));
936 if (bytesRead == 0) {
947 size_t (*readSomeImplementation)(
void* context, Span<byte> data);
948 size_t writeOffset = 0;
949 size_t readOffset = 0;
950 Array<byte, 1024> buffer;
963 return input->empty();
971 const size_t bytesRead =
min(data.
size(), input->size());
973 memcpy(data.
data(), input->data(), bytesRead);
974 *input = input->subspan(bytesRead);
984 const size_t bytesSkipped =
min(maxLength, input->size());
985 *input = input->subspan(bytesSkipped);
990 return input->first(
min(n, input->size()));
995 throw std::length_error{
"Unexpected end of data."};
1001 throw std::length_error{
"Unexpected end of data."};
1011 if (n > input->size()) {
1014 *input = input->subspan(n);
1020 if (input->empty()) {
1023 return input->front();
1030 throw std::length_error{
"Unexpected end of input."};
1035 throw std::length_error{
"Unexpected end of data."};
1041 *input = input->subspan(1);
1048 if (input->empty()) {
1051 *input = input->subspan(1);
1055 using ReaderBase::peekChar;
1056 using ReaderBase::peekInt8;
1057 using ReaderBase::peekUInt8;
1058 using ReaderBase::readBytes;
1059 using ReaderBase::readBytesIntoAllocation;
1060 using ReaderBase::readBytesIntoArrayList;
1061 using ReaderBase::readBytesIntoBuffer;
1062 using ReaderBase::readBytesIntoString;
1063 using ReaderBase::readChar;
1064 using ReaderBase::readChars;
1065 using ReaderBase::readFloat16BE;
1066 using ReaderBase::readFloat16HE;
1067 using ReaderBase::readFloat16LE;
1068 using ReaderBase::readFloat32BE;
1069 using ReaderBase::readFloat32HE;
1070 using ReaderBase::readFloat32LE;
1071 using ReaderBase::readFloat64BE;
1072 using ReaderBase::readFloat64LE;
1073 using ReaderBase::readInt16BE;
1074 using ReaderBase::readInt16HE;
1075 using ReaderBase::readInt16LE;
1076 using ReaderBase::readInt32BE;
1077 using ReaderBase::readInt32HE;
1078 using ReaderBase::readInt32LE;
1079 using ReaderBase::readInt64BE;
1080 using ReaderBase::readInt64HE;
1081 using ReaderBase::readInt64LE;
1082 using ReaderBase::readInt8;
1083 using ReaderBase::readIntLEB128;
1084 using ReaderBase::readLine;
1085 using ReaderBase::readNullTerminatedString;
1086 using ReaderBase::readUInt16BE;
1087 using ReaderBase::readUInt16HE;
1088 using ReaderBase::readUInt16LE;
1089 using ReaderBase::readUInt32BE;
1090 using ReaderBase::readUInt32HE;
1091 using ReaderBase::readUInt32LE;
1092 using ReaderBase::readUInt64BE;
1093 using ReaderBase::readUInt64HE;
1094 using ReaderBase::readUInt64LE;
1095 using ReaderBase::readUInt8;
1096 using ReaderBase::readUIntLEB128;
1097 using ReaderBase::tryReadBytes;
1098 using ReaderBase::tryReadChar;
1099 using ReaderBase::tryReadChars;
1100 using ReaderBase::tryReadFloat16BE;
1101 using ReaderBase::tryReadFloat16HE;
1102 using ReaderBase::tryReadFloat16LE;
1103 using ReaderBase::tryReadFloat32BE;
1104 using ReaderBase::tryReadFloat32HE;
1105 using ReaderBase::tryReadFloat32LE;
1106 using ReaderBase::tryReadFloat64BE;
1107 using ReaderBase::tryReadFloat64HE;
1108 using ReaderBase::tryReadFloat64LE;
1109 using ReaderBase::tryReadInt16BE;
1110 using ReaderBase::tryReadInt16HE;
1111 using ReaderBase::tryReadInt16LE;
1112 using ReaderBase::tryReadInt32BE;
1113 using ReaderBase::tryReadInt32HE;
1114 using ReaderBase::tryReadInt32LE;
1115 using ReaderBase::tryReadInt64BE;
1116 using ReaderBase::tryReadInt64HE;
1117 using ReaderBase::tryReadInt64LE;
1118 using ReaderBase::tryReadInt8;
1119 using ReaderBase::tryReadIntLEB128;
1120 using ReaderBase::tryReadUInt16BE;
1121 using ReaderBase::tryReadUInt16HE;
1122 using ReaderBase::tryReadUInt16LE;
1123 using ReaderBase::tryReadUInt32BE;
1124 using ReaderBase::tryReadUInt32HE;
1125 using ReaderBase::tryReadUInt32LE;
1126 using ReaderBase::tryReadUInt64BE;
1127 using ReaderBase::tryReadUInt64HE;
1128 using ReaderBase::tryReadUInt64LE;
1129 using ReaderBase::tryReadUInt8;
1130 using ReaderBase::tryReadUIntLEB128;
1135 template <
typename Container>
1136 [[nodiscard]] Container readBytesInto(
size_t maxLength) {
1137 static_assert(
sizeof(
typename Container::value_type) ==
sizeof(byte));
1140 if (!input->
empty()) {
1141 result.resize(
min(input->
size(), maxLength));
1142 memcpy(result.data(), input->
data(), result.size());
1148 Span<const byte>* input;
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition Reader.hpp:658
void skipByte()
Definition Reader.hpp:827
Reader & operator=(const Reader &)=delete
bool trySkipByte()
Definition Reader.hpp:841
GREM_ALWAYS_INLINE Reader(std::istream &input)
Definition Reader.hpp:691
GREM_ALWAYS_INLINE Reader(std::streambuf &input) noexcept
Definition Reader.hpp:675
byte readByte()
Definition Reader.hpp:820
size_t skipUntilEOF(size_t maxLength)
Definition Reader.hpp:768
GREM_ALWAYS_INLINE Reader(void *context, size_t(*readSomeImplementation)(void *context, Span< byte > data)) noexcept
Definition Reader.hpp:660
size_t skipSome(size_t maxLength)
Definition Reader.hpp:758
size_t readUntilEOF(Span< byte > data)
Definition Reader.hpp:745
bool eof()
Definition Reader.hpp:723
bool tryRead(Span< byte > data)
Definition Reader.hpp:793
Reader(const Reader &)=delete
void skip(size_t n)
Definition Reader.hpp:787
Optional< byte > peekByte()
Definition Reader.hpp:808
Optional< byte > tryReadByte()
Definition Reader.hpp:833
GREM_ALWAYS_INLINE Reader & operator=(Reader &&other) noexcept
Definition Reader.hpp:709
size_t readSome(Span< byte > data)
Definition Reader.hpp:732
GREM_ALWAYS_INLINE Reader(Reader &&other) noexcept
Definition Reader.hpp:699
bool trySkip(size_t n)
Definition Reader.hpp:797
GREM_ALWAYS_INLINE Reader(Span< const byte > &input) noexcept
Definition Reader.hpp:664
void read(Span< byte > data)
Definition Reader.hpp:781
size_t skipSome(size_t maxLength)
Definition Reader.hpp:979
void skip(size_t n)
Definition Reader.hpp:999
size_t readSome(Span< byte > data)
Definition Reader.hpp:966
Span< const byte > peek(size_t n) const noexcept
Definition Reader.hpp:989
bool trySkipByte()
Definition Reader.hpp:1047
void skipByte()
Definition Reader.hpp:1033
bool tryRead(Span< byte > data)
Definition Reader.hpp:1005
GREM_ALWAYS_INLINE bool eof()
Definition Reader.hpp:962
Optional< byte > tryReadByte()
Definition Reader.hpp:1039
GREM_ALWAYS_INLINE SpanReader(Span< const byte > &input) noexcept
Definition Reader.hpp:955
void read(Span< byte > data)
Definition Reader.hpp:993
size_t readUntilEOF(Span< byte > data)
Definition Reader.hpp:970
Optional< byte > peekByte()
Definition Reader.hpp:1019
bool trySkip(size_t n)
Definition Reader.hpp:1009
byte readByte()
Definition Reader.hpp:1026
size_t skipUntilEOF(size_t maxLength)
Definition Reader.hpp:983
constexpr bool empty() const noexcept
Definition Span.hpp:257
constexpr detail::subspan_type_t< T, EXTENT, Offset, N > subspan() const
Definition Span.hpp:284
constexpr size_type size() const noexcept
Definition Span.hpp:207
constexpr pointer data() const noexcept
Definition Span.hpp:203
@ N
'n'
Definition keyboard.hpp:84
auto asWritableBytes(Span< T, N > s) noexcept
Definition Span.hpp:368
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
float float32_t
Definition fundamentals.hpp:155
GREM_ALWAYS_INLINE constexpr T convertBigEndianToHostEndian(T value)
Convert a value in big-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:523
double float64_t
Definition fundamentals.hpp:157
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
GREM_ALWAYS_INLINE constexpr T convertLittleEndianToHostEndian(T value)
Convert a value in little-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:542
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
constexpr size_type size() const noexcept
Definition Array.hpp:139
static constexpr bool IS_IEC60559
Definition fundamentals.hpp:307
static constexpr T MAX
Definition fundamentals.hpp:29