AuroraRuntime/Source/IO/Character/StringToProvider.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

96 lines
2.4 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: StringToProvider.cpp
Date: 2022-1-31
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "StringToProvider.hpp"
namespace Aurora::IO::Character
{
struct CharacterProvider : ICharacterProviderEx
{
CharacterProvider(const AuROString &in, AuUInt offset = 0);
CharacterProvider(const AuSPtr<AuString> &in, AuUInt offset = 0);
bool GetByte(AuUInt8 &val) override;
AuUInt GetPosition() override;
bool SetPosition(AuUInt offset) override;
bool HasString();
protected:
AuUInt uOffset_ {};
AuSPtr<AuString> pString_;
};
CharacterProvider::CharacterProvider(const AuROString &in, AuUInt offset) :
pString_(AuMakeShared<AuString>(in)),
uOffset_(offset)
{}
CharacterProvider::CharacterProvider(const AuSPtr<AuString> &in, AuUInt offset) :
pString_(in),
uOffset_(offset)
{}
bool CharacterProvider::GetByte(AuUInt8 &val)
{
auto offset = this->uOffset_++;
if (offset >= this->pString_->size())
{
offset = this->pString_->size();
return false;
}
val = this->pString_->at(offset);
return true;
}
AuUInt CharacterProvider::GetPosition()
{
return this->uOffset_;
}
bool CharacterProvider::SetPosition(AuUInt offset)
{
this->uOffset_ = offset;
return this->uOffset_ < this->pString_->size();
}
bool CharacterProvider::HasString()
{
return this->pString_;
}
AUKN_SYM ICharacterProviderEx *ProviderFromSharedStringNew(const AuSPtr<AuString> &str, AuUInt index)
{
SysCheckArgNotNull(str, {});
return _new CharacterProvider(str, index);
}
AUKN_SYM void ProviderFromSharedStringRelease(ICharacterProviderEx *me)
{
AuSafeDelete<CharacterProvider *>(me);
}
AUKN_SYM ICharacterProviderEx *ProviderFromStringNew(const AuROString &str, AuUInt index)
{
if (auto pRet = _new CharacterProvider(str, index))
{
if (pRet->HasString())
{
return pRet;
}
}
return nullptr;
}
AUKN_SYM void ProviderFromStringRelease(ICharacterProviderEx *me)
{
AuSafeDelete<CharacterProvider *>(me);
}
}