/* * 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" #include "fud_config.hpp" #include "fud_assert.hpp" #include namespace fud { class String; struct StringView { template consteval StringView(const utf8 (&input)[N]) : m_length{N - 1}, m_data{input} { static_assert(N > 0); } 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} { } template StringView(const char (&input)[N]) : m_length{N - 1}, m_data{reinterpret_cast(input)} { static_assert(N > 0); } StringView(size_t strLen, const char* strData) : m_length(strLen), // line break m_data{reinterpret_cast(strData)} // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) { } StringView(std::string_view rhs) noexcept : StringView(rhs.length(), rhs.data()) {} explicit StringView(const String& fudString) noexcept; template constexpr static StringView cStringView(const char (&input)[N]) { static_assert(N > 0); return StringView{N, reinterpret_cast(input)}; } [[nodiscard]] constexpr size_t length() const { return m_length; } [[nodiscard]] constexpr const utf8* data() const { return m_data; } constexpr const utf8& operator[](size_t index) const { if constexpr (fudBoundsChecking) { fudAssert(m_data != nullptr); fudAssert(index < m_length); } return m_data[index]; } constexpr const utf8* begin() const noexcept { return m_data; } constexpr const utf8* end() const noexcept { return m_data + m_length; } [[nodiscard]] bool nullTerminated() const; [[nodiscard]] bool utf8Valid() const; Result skipWhitespace(); Result trimWhitespace(); [[nodiscard]] bool advance(); void advanceUnsafe(); [[nodiscard]] bool advance(size_t size); void advanceUnsafe(size_t size); size_t m_length{0}; const utf8* m_data{nullptr}; auto operator<=>(const StringView& rhs) const noexcept = default; }; } // namespace fud #endif