Template
Initial commit
This commit is contained in:
commit
d1890b6e18
16 files changed
+702
No files matched your search
@@ -0,0 +1,53 @@
|
||||
#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{};
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user