EVMC
filter_iterator.hpp
1// EVMC: Ethereum Client-VM Connector API.
2// Copyright 2022 The EVMC Authors.
3// Licensed under the Apache License, Version 2.0.
4#pragma once
5
6#include <iterator>
7
8namespace evmc
9{
11inline constexpr bool isspace(char ch) noexcept
12{
13 // Implementation taken from LLVM's libc.
14 return ch == ' ' || (static_cast<unsigned>(ch) - '\t') < 5;
15}
16
18inline constexpr bool is_not_space(char ch) noexcept
19{
20 return !isspace(ch);
21}
22
32template <typename BaseIterator,
33 bool predicate(typename std::iterator_traits<BaseIterator>::value_type) noexcept>
35{
37 using difference_type = typename std::iterator_traits<BaseIterator>::difference_type;
38
40 using value_type = typename std::iterator_traits<BaseIterator>::value_type;
41
43 using pointer = typename std::iterator_traits<BaseIterator>::pointer;
44
46 using reference = typename std::iterator_traits<BaseIterator>::reference;
47
49 using iterator_category = std::input_iterator_tag;
50
51private:
52 BaseIterator base;
53 BaseIterator base_end;
54 value_type value;
55
56 constexpr void forward_to_next_value() noexcept
57 {
58 for (; base != base_end; ++base)
59 {
60 value = *base;
61 if (predicate(value))
62 break;
63 }
64 }
65
66public:
68 constexpr filter_iterator(BaseIterator it, BaseIterator end) noexcept : base{it}, base_end{end}
69 {
70 forward_to_next_value();
71 }
72
74 constexpr auto operator*() noexcept
75 {
76 // We should not read from an input base iterator twice. So the only read is in
77 // forward_to_next_value() and here we return the cached value.
78 return value;
79 }
80
82 constexpr void operator++() noexcept
83 {
84 ++base;
85 forward_to_next_value();
86 }
87
89 constexpr bool operator==(const filter_iterator& o) const noexcept { return base == o.base; }
90
92 constexpr bool operator!=(const filter_iterator& o) const noexcept { return base != o.base; }
93};
94
96template <typename BaseIterator>
97struct skip_space_iterator : filter_iterator<BaseIterator, is_not_space>
98{
99 using filter_iterator<BaseIterator, is_not_space>::filter_iterator;
100};
101
103template <typename BaseIterator>
105} // namespace evmc
EVMC C++ API - wrappers and bindings for C++.
Definition: bytes.hpp:12
constexpr bool is_not_space(char ch) noexcept
Checks if a character is not a white space.
constexpr bool isspace(char ch) noexcept
The constexpr variant of std::isspace().
The filter iterator adaptor creates a view of an iterator range in which some elements of the range a...
constexpr void operator++() noexcept
The increment operator.
constexpr bool operator!=(const filter_iterator &o) const noexcept
The inequality operator.
typename std::iterator_traits< BaseIterator >::reference reference
The iterator reference type.
typename std::iterator_traits< BaseIterator >::difference_type difference_type
The iterator difference type.
typename std::iterator_traits< BaseIterator >::value_type value_type
The iterator value type.
typename std::iterator_traits< BaseIterator >::pointer pointer
The iterator pointer type.
constexpr auto operator*() noexcept
The dereference operator.
constexpr filter_iterator(BaseIterator it, BaseIterator end) noexcept
The constructor of the base iterator pair.
std::input_iterator_tag iterator_category
The iterator category.
constexpr bool operator==(const filter_iterator &o) const noexcept
The equality operator.
The input filter iterator which skips whitespace characters from the base input iterator.