From ca490df91553f21ff3f258ad5d55f4b91f0cc1ba Mon Sep 17 00:00:00 2001 From: tobid7 Date: Thu, 5 Mar 2026 20:24:47 +0100 Subject: [PATCH] Add U8Iterator and Pool base class --- include/pd/core/core.hpp | 1 + include/pd/core/pool.hpp | 51 +++++++++++++++++++++++++++++++++++++ include/pd/core/strings.hpp | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 include/pd/core/pool.hpp diff --git a/include/pd/core/core.hpp b/include/pd/core/core.hpp index f7be474..efd4996 100755 --- a/include/pd/core/core.hpp +++ b/include/pd/core/core.hpp @@ -28,6 +28,7 @@ SOFTWARE. #include #include #include +#include #include #include #include diff --git a/include/pd/core/pool.hpp b/include/pd/core/pool.hpp new file mode 100644 index 0000000..8301e5c --- /dev/null +++ b/include/pd/core/pool.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include + +namespace PD { +template > +class Pool { + public: + Pool() = default; + ~Pool() = default; + + static Pool* Create(size_t size) { return new Pool(size); } + + void Init(size_t size) { + pPos = 0; + pCap = size; + pPool.resize(size); + } + + Pool(const Pool&) = delete; + Pool(Pool&&) = delete; + Pool& operator=(const Pool&) = delete; + Pool& operator=(Pool&&) = delete; + + T* Allocate(size_t num = 1) { + if (CheckLimits(num)) return nullptr; + T* ret = &pPool[pPos]; + pPos += num; + return ret; + } + + bool CheckLimits(size_t num) { + if ((pPos + num) >= pCap) { + throw std::runtime_error( + std::format("[PD::Pool]: Trying to allocate {} elements but this is " + "going out of range ({}/{})", + num, pPos + num, pCap)); + return true; + } + return false; + } + + void Reset() { pPos = 0; } + + private: + Pool(size_t size) : pCap(size), pPos(0) { pPool.resize(size); } + size_t pCap = 0; + size_t pPos = 0; + std::vector pPool; +}; +} // namespace PD \ No newline at end of file diff --git a/include/pd/core/strings.hpp b/include/pd/core/strings.hpp index 226233e..1a90941 100644 --- a/include/pd/core/strings.hpp +++ b/include/pd/core/strings.hpp @@ -127,4 +127,48 @@ inline const std::string GetCompilerVersion() { return res.str(); } } // namespace Strings +class U8Iterator { + public: + explicit U8Iterator(const char* s) : ptr(reinterpret_cast(s)) {} + ~U8Iterator() = default; + + bool Decode32(u32& ret) { + if (ptr == nullptr || *ptr == 0) return false; + u8 c = *ptr; + if (c < 0x80) { + ret = c; + ptr += 1; + } else if ((c >> 5) == 0x6) { + ret = ((c & 0x1F) << 6) | (ptr[1] & 0x3F); + ptr += 2; + } else if ((c >> 4) == 0xE) { + ret = ((c & 0x0F) << 12) | ((ptr[1] & 0x3F) << 6) | (ptr[2] & 0x3F); + ptr += 3; + } else { + ret = ((c & 0x07) << 18) | ((ptr[1] & 0x3F) << 12) | + ((ptr[2] & 0x3F) << 6) | (ptr[3] & 0x3F); + ptr += 4; + } + return true; + } + + bool PeekNext32(u32& ret) { + if (ptr + 1 == nullptr || *ptr + 1 == 0) return false; + u8 c = *ptr; + if (c < 0x80) { + ret = c; + } else if ((c >> 5) == 0x6) { + ret = ((c & 0x1F) << 6) | (ptr[1] & 0x3F); + } else if ((c >> 4) == 0xE) { + ret = ((c & 0x0F) << 12) | ((ptr[1] & 0x3F) << 6) | (ptr[2] & 0x3F); + } else { + ret = ((c & 0x07) << 18) | ((ptr[1] & 0x3F) << 12) | + ((ptr[2] & 0x3F) << 6) | (ptr[3] & 0x3F); + } + return true; + } + + private: + const u8* ptr = nullptr; +}; } // namespace PD \ No newline at end of file