From c3fe2de828576900021d27a52114ebdb0a4cb6f0 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 3 Nov 2024 09:28:13 -0600 Subject: Factor out growth of String and make it scale by 1.5x. --- include/fud_string.hpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'include/fud_string.hpp') diff --git a/include/fud_string.hpp b/include/fud_string.hpp index 59c434a..86261c4 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -251,6 +251,17 @@ class String { return SsoBufSize - 1U; } + /** \brief Returns the remaining capacity for characters excluding the null + * terminating byte. */ + [[nodiscard]] size_t remainingCapacity() const + { + if (length() > capacity()) { + return 0; + } + + return capacity() - length(); + } + /** \brief The underlying data, guaranteed to have c string representation. */ [[nodiscard]] const utf8* data() const { @@ -263,6 +274,11 @@ class String { return reinterpret_cast(data()); } + [[nodiscard]] inline StringView asView() const + { + return StringView(*this); + } + /** \brief Indicates if the contents of the string form a valid sequence of * UTF8 code points. */ [[nodiscard]] bool utf8Valid() const; @@ -274,22 +290,6 @@ class String { * greater than zero. */ [[nodiscard]] Option back(); - /** \brief Returns the remaining capacity for characters excluding the null - * terminating byte. */ - [[nodiscard]] size_t remainingLength() const - { - if (length() > capacity()) { - return 0; - } - - return capacity() - length(); - } - - [[nodiscard]] inline StringView asView() const - { - return StringView(*this); - } - FudStatus pushBack(char letter); FudStatus pushBack(utf8 letter); @@ -358,6 +358,8 @@ class String { FudStatus resize(size_t newCapacity); + FudStatus grow(); + /** \brief The allocator used to get storage for characters when the string * is large. */ uintptr_t m_allocator{reinterpret_cast(&globalFudAllocator)}; -- cgit v1.2.3