1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em ReadChar e ReadChar8

This commit is contained in:
Danilo Borges Santos 2024-12-09 17:37:20 -03:00
parent 98ee98d67a
commit a945f460bd
3 changed files with 20 additions and 14 deletions

View File

@ -43,7 +43,7 @@ namespace csharp {
} }
virtual int32_t PeekChar(); virtual int32_t PeekChar();
virtual int32_t Read(); virtual int32_t Read(bool twoBytesPerChar = false);
virtual uint8_t ReadByte() { virtual uint8_t ReadByte() {
return InternalReadByte(); return InternalReadByte();
@ -58,6 +58,7 @@ namespace csharp {
} }
virtual char ReadChar(); virtual char ReadChar();
virtual char ReadChar8();
virtual int16_t ReadInt16() { virtual int16_t ReadInt16() {
return ReadNumeric<int16_t>(); return ReadNumeric<int16_t>();
@ -183,7 +184,6 @@ namespace csharp {
std::shared_ptr<Stream> _stream; std::shared_ptr<Stream> _stream;
bool _leaveOpen; bool _leaveOpen;
bool _disposed{false}; bool _disposed{false};
bool _2BytesPerChar{ false };
std::vector<uint8_t> _auxBuffer; std::vector<uint8_t> _auxBuffer;
}; };

View File

@ -20,6 +20,7 @@ namespace xna {
auto reader = csharp::BinaryReader(stream); auto reader = csharp::BinaryReader(stream);
auto bo = reader.ReadBoolean(); //reader.ReadChar() auto bo = reader.ReadBoolean(); //reader.ReadChar()
auto x = reader.ReadChar(); //x auto x = reader.ReadChar(); //x
auto cedilha = reader.ReadChar8(); //ç
auto sb = reader.ReadSByte(); //127 auto sb = reader.ReadSByte(); //127
auto by = reader.ReadByte(); //255 auto by = reader.ReadByte(); //255
auto i16 = reader.ReadInt16(); //32767 auto i16 = reader.ReadInt16(); //32767

View File

@ -20,7 +20,7 @@ namespace csharp {
return ch; return ch;
} }
int32_t BinaryReader::Read() { int32_t BinaryReader::Read(bool twoBytesPerChar) {
if (_disposed) if (_disposed)
throw InvalidOperationException(); throw InvalidOperationException();
@ -36,7 +36,7 @@ namespace csharp {
char singleChar = '\0'; char singleChar = '\0';
while (charsRead == 0) { while (charsRead == 0) {
numBytes = _2BytesPerChar ? 2 : 1; numBytes = twoBytesPerChar ? 2 : 1;
auto r = _stream->ReadByte(); auto r = _stream->ReadByte();
@ -46,7 +46,8 @@ namespace csharp {
} }
if (numBytes == 2) if (numBytes == 2)
{ {
r |= _stream->ReadByte(); auto r2 = _stream->ReadByte();
r |= r2;
if (r == -1) if (r == -1)
{ {
@ -92,6 +93,17 @@ namespace csharp {
return static_cast<char>(value); return static_cast<char>(value);
} }
char BinaryReader::ReadChar8() {
const auto value = Read(true);
if (value == -1)
{
throw EndOfStreamException(SR::IO_EOF_ReadBeyondEOF);
}
return static_cast<char>(value);
}
void BinaryReader::InternalRead(std::vector<uint8_t>& buffer) { void BinaryReader::InternalRead(std::vector<uint8_t>& buffer) {
if (_disposed) if (_disposed)
throw InvalidOperationException(); throw InvalidOperationException();
@ -143,11 +155,6 @@ namespace csharp {
{ {
auto numBytes = bufferLength; auto numBytes = bufferLength;
if (_2BytesPerChar)
{
numBytes <<= 1;
}
std::vector<uint8_t> byteBuffer; std::vector<uint8_t> byteBuffer;
if (numBytes > MaxCharBytesSize) if (numBytes > MaxCharBytesSize)
@ -170,8 +177,6 @@ namespace csharp {
totalCharsRead += charsRead.length(); totalCharsRead += charsRead.length();
} }
// we may have read fewer than the number of characters requested if end of stream reached
// or if the encoding makes the char count too big for the buffer (e.g. fallback sequence)
return totalCharsRead; return totalCharsRead;
} }