From 7eea7cd5e5b451de9db5bd289f8b5d152d5803f5 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 2 Oct 2024 11:42:00 -0500 Subject: Separate string view, implement string appending semantics. --- include/fud_memory.hpp | 2 +- include/fud_string.hpp | 94 ++++++++------------------------------- include/fud_string_view.hpp | 101 ++++++++++++++++++++++++++++++++++++++++++ include/fud_utf8_iterator.hpp | 2 +- 4 files changed, 121 insertions(+), 78 deletions(-) create mode 100644 include/fud_string_view.hpp (limited to 'include') diff --git a/include/fud_memory.hpp b/include/fud_memory.hpp index d6708cd..41393bd 100644 --- a/include/fud_memory.hpp +++ b/include/fud_memory.hpp @@ -28,7 +28,7 @@ namespace fud { extern void* fudAlloc(size_t size); -extern void* fudRealloc(size_t size); +extern void* fudRealloc(void* ptr, size_t size); extern void fudFree(void* ptr); // An allocating function which returns null on failure. diff --git a/include/fud_string.hpp b/include/fud_string.hpp index cd8e8f1..1a38d43 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -18,6 +18,8 @@ #ifndef FUD_STRING_HPP #define FUD_STRING_HPP +#include "fud_status.hpp" +#include "fud_string_view.hpp" #include "fud_utf8.hpp" #include @@ -92,7 +94,9 @@ class String { [[nodiscard]] bool utf8Valid() const; - [[nodiscard]] FudStatus nullTerminate(); + FudStatus nullTerminate(); + + FudStatus reserve(size_t newCapacity); [[nodiscard]] std::optional back(); @@ -105,15 +109,23 @@ class String { return m_capacity - 1U - m_length; } - [[nodiscard]] FudStatus pushBack(char letter); + [[nodiscard]] inline StringView asView() const { + return StringView(*this); + } + + FudStatus pushBack(char letter); - [[nodiscard]] FudStatus pushBack(utf8 letter); + FudStatus pushBack(utf8 letter); - [[nodiscard]] FudStatus pushBack(const FudUtf8& letter); + FudStatus pushBack(const FudUtf8& letter); std::optional pop(); - [[nodiscard]] FudStatus append(StringView source); + FudStatus append(const char* source); + + FudStatus append(const String& source); + + FudStatus append(StringView source); [[nodiscard]] String catenate(const String& rhs) const; @@ -127,6 +139,7 @@ class String { private: void cleanup(); + FudStatus resize(size_t newCapacity); using BufType = Array; union { @@ -142,77 +155,6 @@ class String { } }; -class StringView { - public: - constexpr StringView() noexcept : m_length(0), m_data{nullptr} - { - } - - constexpr StringView(size_t strLen, const utf8* strData) : m_length(strLen), m_data{strData} - { - } - - StringView(size_t strLen, const char* strData) : - m_length(strLen), // line break - m_data{reinterpret_cast(strData)} // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) - { - } - - explicit constexpr StringView(const StringView& view) noexcept = default; - - explicit constexpr StringView(const String& fudString) noexcept : StringView(fudString.length(), fudString.data()) - { - } - - [[nodiscard]] constexpr size_t length() const - { - return m_length; - } - - [[nodiscard]] constexpr const utf8* data() const - { - return m_data; - } - - [[nodiscard]] bool nullTerminated() const; - - [[nodiscard]] bool utf8Valid() const; - - Result skipWhitespace(); - - Result trimWhitespace(); - - FudStatus toUint8(uint8_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toUint16(uint16_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toUint32(uint32_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toUint64(uint64_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toInt8(int8_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toInt16(int16_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toInt32(int32_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toInt64(int64_t& number, uint8_t specifiedRadix, size_t& strLen) const; - - FudStatus toFloat(float& number, size_t& strLen) const; - - FudStatus toDouble(double& number, size_t& strLen) const; - - private: - size_t m_length; - const utf8* m_data; -}; - -FudStatus skipWhitespace(StringView& view, size_t& skipIndex); - -ssize_t cStringLength(const char* str); - -ssize_t cStringLength(const char* str, size_t maxLength); - } // namespace fud #endif diff --git a/include/fud_string_view.hpp b/include/fud_string_view.hpp new file mode 100644 index 0000000..4796003 --- /dev/null +++ b/include/fud_string_view.hpp @@ -0,0 +1,101 @@ +/* + * 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_VIEW_HPP +#define FUD_STRING_VIEW_HPP + +#include "fud_status.hpp" +#include "fud_utf8.hpp" + +namespace fud { + +class String; + +class StringView { + public: + constexpr StringView() noexcept = default; + constexpr StringView(const StringView& rhs) noexcept = default; + constexpr StringView(StringView&& rhs) noexcept = default; + constexpr ~StringView() noexcept = default; + constexpr StringView& operator=(const StringView& rhs) = default; + constexpr StringView& operator=(StringView&& rhs) = default; + + constexpr StringView(size_t strLen, const utf8* strData) : m_length(strLen), m_data{strData} + { + } + + StringView(size_t strLen, const char* strData) : + m_length(strLen), // line break + m_data{reinterpret_cast(strData)} // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + { + } + + explicit StringView(const String& fudString) noexcept; + + [[nodiscard]] constexpr size_t length() const + { + return m_length; + } + + [[nodiscard]] constexpr const utf8* data() const + { + return m_data; + } + + [[nodiscard]] bool nullTerminated() const; + + [[nodiscard]] bool utf8Valid() const; + + Result skipWhitespace(); + + Result trimWhitespace(); + + FudStatus toUint8(uint8_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toUint16(uint16_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toUint32(uint32_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toUint64(uint64_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toInt8(int8_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toInt16(int16_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toInt32(int32_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toInt64(int64_t& number, uint8_t specifiedRadix, size_t& strLen) const; + + FudStatus toFloat(float& number, size_t& strLen) const; + + FudStatus toDouble(double& number, size_t& strLen) const; + + private: + size_t m_length{0}; + const utf8* m_data{nullptr}; +}; + +FudStatus skipWhitespace(StringView& view, size_t& skipIndex); + +ssize_t cStringLength(const char* str); + +ssize_t cStringLength(const char* str, size_t maxLength); + +} // namespace fud + +#endif diff --git a/include/fud_utf8_iterator.hpp b/include/fud_utf8_iterator.hpp index 3721b00..ec35927 100644 --- a/include/fud_utf8_iterator.hpp +++ b/include/fud_utf8_iterator.hpp @@ -34,7 +34,7 @@ class Utf8Iterator { // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members) public: - explicit constexpr Utf8Iterator(const String& fudString) : m_view{fudString} + explicit Utf8Iterator(const String& fudString) : m_view{fudString} { } -- cgit v1.2.3