diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-02 11:42:00 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-02 11:42:00 -0500 |
commit | 7eea7cd5e5b451de9db5bd289f8b5d152d5803f5 (patch) | |
tree | d4c948fc00f7c3c63fd9c6c8d85ac1c57c8241b5 /include/fud_string_view.hpp | |
parent | 132ddf65fe6e253ebd113036d61c51cd0eb5e827 (diff) |
Separate string view, implement string appending semantics.
Diffstat (limited to 'include/fud_string_view.hpp')
-rw-r--r-- | include/fud_string_view.hpp | 101 |
1 files changed, 101 insertions, 0 deletions
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<const utf8*>(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<size_t, FudStatus> skipWhitespace(); + + Result<size_t, FudStatus> 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 |