-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: Add skip_space_iterator struct
This restores the option to filter-out whitespace before parsing hex. This is useful e.g. when loading hex from a file.
- Loading branch information
Showing
5 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// EVMC: Ethereum Client-VM Connector API. | ||
// Copyright 2022 The EVMC Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
#include <tools/evmc/skip_space_iterator.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
using evmc::skip_space_iterator; | ||
|
||
namespace | ||
{ | ||
std::string remove_space(std::string_view in) | ||
{ | ||
// Copy input to additional buffer. This helps with out-of-buffer reads detection by sanitizers. | ||
const auto in_buffer = std::make_unique<char[]>(in.size()); | ||
const auto begin = in_buffer.get(); | ||
const auto end = begin + in.size(); | ||
std::copy(in.begin(), in.end(), begin); | ||
|
||
// Filter the input. | ||
std::string out; | ||
std::copy(skip_space_iterator{begin, end}, skip_space_iterator{end, end}, | ||
std::back_inserter(out)); | ||
return out; | ||
} | ||
} // namespace | ||
|
||
TEST(skip_space_iterator, empty) | ||
{ | ||
EXPECT_EQ(remove_space(""), ""); | ||
EXPECT_EQ(remove_space(" "), ""); | ||
EXPECT_EQ(remove_space(" "), ""); | ||
} | ||
|
||
TEST(skip_space_iterator, filter_middle) | ||
{ | ||
EXPECT_EQ(remove_space("x y"), "xy"); | ||
EXPECT_EQ(remove_space("x y"), "xy"); | ||
} | ||
|
||
TEST(skip_space_iterator, filter_front) | ||
{ | ||
EXPECT_EQ(remove_space(" x"), "x"); | ||
EXPECT_EQ(remove_space(" x"), "x"); | ||
} | ||
|
||
TEST(skip_space_iterator, filter_back) | ||
{ | ||
EXPECT_EQ(remove_space("x "), "x"); | ||
EXPECT_EQ(remove_space("x "), "x"); | ||
} | ||
|
||
TEST(skip_space_iterator, filter_mixed) | ||
{ | ||
EXPECT_EQ(remove_space(" x y z "), "xyz"); | ||
EXPECT_EQ(remove_space(" x y z "), "xyz"); | ||
} | ||
|
||
TEST(skip_space_iterator, isspace) | ||
{ | ||
// Test internal isspace() compliance with std::isspace(). | ||
// The https://en.cppreference.com/w/cpp/string/byte/isspace has the list of "space" characters. | ||
|
||
for (int i = int{std::numeric_limits<char>::min()}; i <= std::numeric_limits<char>::max(); ++i) | ||
{ | ||
const auto c = static_cast<char>(i); | ||
EXPECT_EQ(evmc::isspace(c), (std::isspace(c) != 0)); | ||
switch (c) | ||
{ | ||
case ' ': | ||
case '\f': | ||
case '\n': | ||
case '\r': | ||
case '\t': | ||
case '\v': | ||
EXPECT_TRUE(evmc::isspace(c)); | ||
break; | ||
default: | ||
EXPECT_FALSE(evmc::isspace(c)); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// EVMC: Ethereum Client-VM Connector API. | ||
// Copyright 2022 The EVMC Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
#pragma once | ||
|
||
#include <iterator> | ||
|
||
namespace evmc | ||
{ | ||
/// The constexpr variant of std::isspace(). | ||
inline constexpr bool isspace(char ch) noexcept | ||
{ | ||
// Implementation taken from LLVM's libc. | ||
return ch == ' ' || (static_cast<unsigned>(ch) - '\t') < 5; | ||
} | ||
|
||
/// The input filter iterator which skips whitespace characters from the base input iterator. | ||
template <typename BaseIterator> | ||
struct skip_space_iterator | ||
{ | ||
using difference_type = typename std::iterator_traits<BaseIterator>::difference_type; | ||
using value_type = typename std::iterator_traits<BaseIterator>::value_type; | ||
using pointer = typename std::iterator_traits<BaseIterator>::pointer; | ||
using reference = typename std::iterator_traits<BaseIterator>::reference; | ||
using iterator_category = std::input_iterator_tag; | ||
|
||
private: | ||
BaseIterator base; | ||
BaseIterator base_end; | ||
value_type value; | ||
|
||
constexpr void forward_to_next_value() | ||
{ | ||
for (; base != base_end; ++base) | ||
{ | ||
value = *base; | ||
if (isspace(value) == 0) | ||
break; | ||
} | ||
} | ||
|
||
public: | ||
constexpr skip_space_iterator(BaseIterator it, BaseIterator end) noexcept | ||
: base{it}, base_end{end} | ||
{ | ||
forward_to_next_value(); | ||
} | ||
|
||
constexpr auto operator*() { return value; } | ||
|
||
constexpr void operator++() | ||
{ | ||
++base; | ||
forward_to_next_value(); | ||
} | ||
|
||
constexpr bool operator!=(const skip_space_iterator& o) { return base != o.base; } | ||
constexpr bool operator==(const skip_space_iterator& o) { return base == o.base; } | ||
}; | ||
} // namespace evmc |