/* * libfud * Copyright 2024 Dominick Allen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FUD_STRING_HPP #define FUD_STRING_HPP #include "fud_allocator.hpp" #include "fud_assert.hpp" #include "fud_result.hpp" #include "fud_status.hpp" #include "fud_string_view.hpp" #include "fud_c_string.hpp" #include "fud_utf8.hpp" #include #include static_assert(CHAR_BIT == 8); namespace fud { constexpr size_t SSO_BUF_LENGTH = 15; constexpr size_t SSO_BUF_SIZE = SSO_BUF_LENGTH + 1; using StringResult = Result; struct DrainResult { size_t bytesWritten; FudStatus status; }; class String { public: static StringResult makeFromCString(const char* cString); static StringResult makeFromCString(const char* cString, Allocator* allocator); template static StringResult makeFromCStrings(CStrings... cStrings) { return makeFromCStringsAlloc(&globalFudAllocator, cStrings...); } template static StringResult makeFromCStringsAlloc(Allocator* allocator, CStrings... cStrings) { if (allocator == nullptr) { return StringResult::error(FudStatus::NullPointer); } size_t totalLength = 0; Array lengths{}; Array strPointers{}; size_t index = 0; for (const auto* cStringItem: {cStrings...}) { const char* cString = nullptr; if constexpr (std::is_same_v) { cString = cStringItem; } else if constexpr (std::is_same_v) { cString = reinterpret_cast(cStringItem); } else { static_assert(!std::is_same_v); } strPointers[index] = cString; auto lengthResult = cStringLength(cString); if (lengthResult < 0 || lengthResult >= SSIZE_MAX) { return StringResult::error(FudStatus::ArgumentInvalid); } auto stringLength = static_cast(lengthResult); if (SIZE_MAX - totalLength < stringLength) { return StringResult::error(FudStatus::Failure); } totalLength += stringLength; lengths[index] = stringLength; index++; } String output{}; output.m_length = totalLength; output.m_allocator = allocator; if (output.m_length >= output.m_capacity) { output.m_capacity = output.m_length + 1; /* Avoid using compiler expansions in headers */ auto dataResult = output.m_allocator->allocate(output.m_capacity); if (dataResult.isError()) { return StringResult::error(dataResult.getError()); } output.m_data = static_cast(dataResult.getOkay()); } auto* data = output.data(); size_t cumulativeLength = 0; for (size_t idx = 0; idx < strPointers.size(); ++idx) { const auto* cString = strPointers[idx]; auto copyStatus = copyMem(data + cumulativeLength, output.m_capacity - cumulativeLength, cString, lengths[idx]); fudAssert(copyStatus == FudStatus::Success); cumulativeLength += lengths[idx]; } auto terminateStatus = output.nullTerminate(); fudAssert(terminateStatus == FudStatus::Success); return StringResult::okay(std::move(output)); } String() noexcept = default; String(const String& rhs) = delete; String(String&& rhs) noexcept; ~String(); String& operator=(const String& rhs) = delete; String& operator=(String&& rhs) noexcept; static StringResult from(const String& rhs); static StringResult from(StringView view, Allocator* allocator = &globalFudAllocator); FudStatus copy(const String& rhs); [[nodiscard]] constexpr size_t length() const { return m_length; } [[nodiscard]] constexpr bool empty() const { return m_length == 0; } [[nodiscard]] constexpr size_t size() const { return m_length + 1; } [[nodiscard]] constexpr size_t capacity() const { return m_capacity - 1; } /** \brier The underlying data, guaranteed to have c string representation. */ [[nodiscard]] constexpr const utf8* data() const { return isLarge() ? m_data : m_buffer.data(); } /** \brier The underlying data as an explicit c string. */ [[nodiscard]] inline const char* c_str() const { return isLarge() ? reinterpret_cast(m_data) : reinterpret_cast(m_buffer.data()); } /** \brier The underlying data, guaranteed to have c string representation. */ [[nodiscard]] constexpr utf8* data() { return isLarge() ? m_data : m_buffer.data(); } /** \brier The underlying data as an explicit c string. */ [[nodiscard]] inline char* c_str() { return isLarge() ? reinterpret_cast(m_data) : reinterpret_cast(m_buffer.data()); } [[nodiscard]] bool nullTerminated() const; [[nodiscard]] bool valid() const; [[nodiscard]] bool utf8Valid() const; FudStatus nullTerminate(); FudStatus reserve(size_t newCapacity); [[nodiscard]] std::optional back(); [[nodiscard]] constexpr size_t remainingLength() const { if (m_length >= m_capacity) { return 0; } return m_capacity - 1U - m_length; } [[nodiscard]] inline StringView asView() const { return StringView(*this); } FudStatus pushBack(char letter); FudStatus pushBack(utf8 letter); FudStatus pushBack(const FudUtf8& letter); std::optional pop(); FudStatus append(const char* source); FudStatus append(const String& source); FudStatus append(StringView source); DrainResult drain(const char* source); DrainResult drain(const String& source); DrainResult drain(StringView source); [[nodiscard]] StringResult catenate(const String& rhs) const; [[nodiscard]] StringResult catenate(const char* rhs) const; [[nodiscard]] bool compare(const String& rhs) const; FudStatus clear(); const utf8* begin() const; const utf8* end() const; private: void cleanup(); FudStatus resize(size_t newCapacity); using BufType = Array; union { BufType m_buffer{BufType::constFill(0)}; utf8* m_data; }; size_t m_length{0}; size_t m_capacity{SSO_BUF_SIZE}; [[nodiscard]] constexpr bool isLarge() const { return m_capacity > SSO_BUF_SIZE; } Allocator* m_allocator{&globalFudAllocator}; }; } // namespace fud #endif