#pragma once #ifndef __PL_UNSAFEPASCALSTR_H__ #define __PL_UNSAFEPASCALSTR_H__ #include "DataTypes.h" #include "SmallestInt.h" class PLPasStr; namespace PortabilityLayer { template class UnsafePascalStr { public: UnsafePascalStr(); UnsafePascalStr(size_t size, const char *str); char &operator[](size_t index); const char &operator[](size_t index) const; void Set(size_t size, const char *str); void SetLength(size_t size); size_t Length() const; const char *UnsafeCharPtr() const; PLPasStr ToShortStr() const; private: char m_chars[TSize + (TCStr ? 1 : 0)]; typename SmallestUInt::ValueType_t m_size; }; } #include #include #include "PLPasStr.h" namespace PortabilityLayer { template inline UnsafePascalStr::UnsafePascalStr(size_t size, const char *str) : m_size(static_cast::ValueType_t>(size)) { assert(size <= TSize); if (size) memcpy(m_chars, str, size); if (TCStr) m_chars[size] = '\0'; } template inline char &UnsafePascalStr::operator[](size_t index) { assert(index < m_size); return m_chars[index]; } template inline const char &UnsafePascalStr::operator[](size_t index) const { assert(index < m_size); return m_chars[index]; } template inline void UnsafePascalStr::Set(size_t size, const char *str) { assert(size <= TSize); memcpy(m_chars, str, size); m_size = static_cast::ValueType_t>(size); if (TCStr) m_chars[size] = '\0'; } template inline void UnsafePascalStr::SetLength(size_t size) { assert(size <= TSize); if (TCStr) m_chars[size] = '\0'; } template inline size_t UnsafePascalStr::Length() const { return m_size; } template inline const char *UnsafePascalStr::UnsafeCharPtr() const { return m_chars; } template PLPasStr UnsafePascalStr::ToShortStr() const { if (m_size > 255) return PLPasStr(255, m_chars); else return PLPasStr(static_cast(m_size), m_chars); } } #endif