From f9b910ae138f26ee052469091b90fd94081608b5 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Tue, 11 Aug 2026 16:15:48 +0200 Subject: [PATCH] feat(furlang): add view Add a basic view (span from C++20). --- furlang/include/furlang/view.hpp | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 furlang/include/furlang/view.hpp diff --git a/furlang/include/furlang/view.hpp b/furlang/include/furlang/view.hpp new file mode 100644 index 0000000..fad33c3 --- /dev/null +++ b/furlang/include/furlang/view.hpp @@ -0,0 +1,38 @@ +#ifndef FURLANG_VIEW_HPP +#define FURLANG_VIEW_HPP + +#include +#include +#include +#include + +namespace furlang { + +template +class view { +public: + constexpr view() noexcept = default; + + constexpr view(const T* data, std::size_t size) noexcept + : m_data(data), m_size(size) {} +public: + constexpr view subview(std::size_t offset, std::size_t count = std::numeric_limits::max()) { + if (count > 0 && offset >= m_size) throw std::runtime_error("offset too large"); + return { m_data + offset, std::min(m_size - offset, count) }; + } + + const T& operator[](std::size_t offset) const { + if (offset >= m_size) throw std::runtime_error("out of bounds"); + return m_data[offset]; + } + + constexpr const T* data() const { return m_data; } + constexpr std::size_t size() const { return m_size; } +private: + const T* m_data = nullptr; + std::size_t m_size = 0; +}; + +} // namespace furlang + +#endif // FURLANG_VIEW_HPP