#pragma once #include "core-library/views/view-closure.h" #include namespace core { template requires requires(Range&& range) { *std::begin(range) == ' '; // Comparable with char literals. } class trim_view : public std::ranges::view_interface> { public: constexpr explicit trim_view(Range range) : m_range(std::move(range)) { } static constexpr bool is_space(auto&& c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\0'; } constexpr auto begin() { auto it = std::begin(m_range); while (it != std::end(m_range) && is_space(*it)) ++it; return it; } constexpr auto end() { auto nonspace_end = std::end(m_range); for (auto it = std::begin(m_range); it != std::end(m_range);) { if (!is_space(*it++)) nonspace_end = it; } return nonspace_end; } private: Range m_range{}; }; constexpr view_closure trim{}; }