Optimize win32 filesystem access via static buffer or unlimited path len

This commit is contained in:
Wojtek Figat
2024-05-08 17:28:46 +02:00
parent 40652a0ebc
commit 571ba6773d
2 changed files with 35 additions and 6 deletions

View File

@@ -132,3 +132,36 @@ public:
this->_static = text;
}
};
template<typename CharType = Char>
class StringAsTerminated
{
protected:
const CharType* _static = nullptr;
CharType* _dynamic = nullptr;
public:
StringAsTerminated(const CharType* str, int32 length)
{
if (length != 0 && str[length] == 0) // Unsafe to access out of bounds...
{
_static = str;
}
else
{
_dynamic = (CharType*)Allocator::Allocate((length + 1) * sizeof(CharType));
Platform::MemoryCopy(_dynamic, str, length * sizeof(CharType));
_dynamic[length] = 0;
}
}
~StringAsTerminated()
{
Allocator::Free(_dynamic);
}
operator const CharType*() const
{
return _static ? _static : _dynamic;
}
};