AuroraRuntime/Source/IO/Character/BufferedLineReader.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

136 lines
3.6 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: BufferedLineReader.cpp
Date: 2022-2-8
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "BufferedLineReader.hpp"
namespace Aurora::IO::Character
{
BufferedLineReader::BufferedLineReader(AuSPtr<IStreamReader> reader) :
pInputStream_(reader)
{
}
void BufferedLineReader::Flush()
{
this->flushLine_ = true;
}
bool BufferedLineReader::ReadBytes(AuUInt32 length)
{
AU_LOCK_GUARD(this->lock_);
if (!this->buffer_.Resize(this->buffer_.GetWriteOffset() + length))
{
SysPushErrorMem();
return false;
}
AuUInt bytesRead;
if (this->pInputStream_->Read(AuMemoryViewStreamWrite(AuMemoryViewWrite(this->buffer_.writePtr, this->buffer_.writePtr + this->buffer_.length), bytesRead)) !=
EStreamError::eErrorNone)
{
SysPushErrorIO();
return false;
}
this->buffer_.writePtr += bytesRead;
return true;
}
AuList<AuString> BufferedLineReader::ReadLines()
{
AU_LOCK_GUARD(this->lock_);
AuList<AuString> ret;
bool writeAll = AuExchange(this->flushLine_, false);
if (!this->buffer_)
{
return ret;
}
while (this->buffer_.readPtr < this->buffer_.writePtr)
{
Memory::ByteBufferPushReadState _(this->buffer_, true);
auto pStartPointer = this->buffer_.readPtr;
if (*this->buffer_.readPtr != '\n' &&
this->buffer_.readPtr < this->buffer_.base + this->buffer_.length)
{
this->buffer_.readPtr++;
continue;
}
auto pEndReadPointer = this->buffer_.readPtr;
auto pEndReadPointer2 = this->buffer_.readPtr;
if (pEndReadPointer != this->buffer_.base)
{
if (pEndReadPointer[-1] == '\r')
{
pEndReadPointer--;
}
}
try
{
AuMemoryViewRead view = this->buffer_;
if (AuTryInsert(ret, AuString((char *)pStartPointer, (char *)pEndReadPointer)))
{
this->buffer_.flagReadError = true;
break;
}
this->buffer_.readPtr = pEndReadPointer2;
}
catch (...)
{
this->buffer_.flagReadError = true;
SysPushErrorCatch();
break;
}
}
if (writeAll && this->buffer_.readPtr && this->buffer_.readPtr != this->buffer_.writePtr)
{
try
{
AuMemoryViewRead view = this->buffer_;
if (AuTryInsert(ret, AuString(view.begin(), view.end())))
{
this->buffer_.readPtr += view.length;
}
}
catch (...)
{
SysPushErrorCatch();
}
}
if (this->buffer_.readPtr == this->buffer_.writePtr)
{
this->buffer_.readPtr = this->buffer_.writePtr = this->buffer_.base;
}
return ret;
}
AUKN_SYM IBufferedLineReader *NewLineReaderNew(const AuSPtr<IStreamReader> &pInputReader)
{
SysCheckArgNotNull(pInputReader, {});
return _new BufferedLineReader(pInputReader);
}
AUKN_SYM void NewLineReaderNew(IBufferedLineReader *reader)
{
AuSafeDelete<BufferedLineReader *>(reader);
}
}