Template
54 lines
959 B
C++
54 lines
959 B
C++
#pragma once
|
|
#include "core-library/views/view-closure.h"
|
|
|
|
#include <ranges>
|
|
|
|
namespace core {
|
|
|
|
template<std::ranges::forward_range Range>
|
|
requires requires(Range&& range) {
|
|
*std::begin(range) == ' '; // Comparable with char literals.
|
|
}
|
|
class trim_view : public std::ranges::view_interface<trim_view<Range>>
|
|
{
|
|
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_view> trim{};
|
|
|
|
}
|