This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Refactor hex decoding utilities #648
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
| // EVMC: Ethereum Client-VM Connector API. | ||
| // Copyright 2022 The EVMC Authors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| #include <tools/evmc/filter_iterator.hpp> | ||
| #include <gtest/gtest.h> | ||
| #include <cctype> | ||
|
|
||
| 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 std::vector<char> in_buffer(in.begin(), in.end()); | ||
|
|
||
| // Filter the input. | ||
| std::string out; | ||
| std::copy(skip_space_iterator{in_buffer.begin(), in_buffer.end()}, | ||
| skip_space_iterator{in_buffer.end(), in_buffer.end()}, std::back_inserter(out)); | ||
| return out; | ||
| } | ||
|
|
||
| bool is_positive(int x) noexcept | ||
| { | ||
| return x > 0; | ||
| } | ||
| } // namespace | ||
|
|
||
|
|
||
| TEST(filter_iterator, filter_positive_integers) | ||
| { | ||
| std::vector<int> in{1, 0, 0, 2, -3, 3, 4, 5, 0, 6, 7, -1, -2, 0, 8, 9, -10}; | ||
| std::vector<int> out; | ||
|
|
||
| using iter = evmc::filter_iterator<std::vector<int>::const_iterator, is_positive>; | ||
| std::copy(iter{in.begin(), in.end()}, iter{in.end(), in.end()}, std::back_inserter(out)); | ||
| ASSERT_EQ(out.size(), 9u); | ||
| EXPECT_EQ(out[0], 1); | ||
| EXPECT_EQ(out[1], 2); | ||
| EXPECT_EQ(out[2], 3); | ||
| EXPECT_EQ(out[3], 4); | ||
| EXPECT_EQ(out[4], 5); | ||
| EXPECT_EQ(out[5], 6); | ||
| EXPECT_EQ(out[6], 7); | ||
| EXPECT_EQ(out[7], 8); | ||
| EXPECT_EQ(out[8], 9); | ||
| } | ||
|
|
||
|
|
||
| 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -133,9 +133,8 @@ TEST(tool_commands, create_preserve_storage) | |
| std::ostringstream out; | ||
|
|
||
| const auto exit_code = | ||
| run(vm, EVMC_BERLIN, 200, | ||
| *from_hex("60bb 6000 55 6a6000546000526001601ff3 6000 52 600b 6015 f3"), {}, true, | ||
| false, out); | ||
| run(vm, EVMC_BERLIN, 200, *from_hex("60bb6000556a6000546000526001601ff3600052600b6015f3"), | ||
| {}, true, false, out); | ||
| EXPECT_EQ(exit_code, 0); | ||
| EXPECT_EQ(out.str(), out_pattern("Berlin", 200, "success", 7, "bb", true)); | ||
| } | ||
|
|
@@ -145,7 +144,7 @@ TEST(tool_commands, bench_add) | |
| auto vm = evmc::VM{evmc_create_example_vm()}; | ||
| std::ostringstream out; | ||
|
|
||
| const auto exit_code = run(vm, EVMC_LONDON, 200, *from_hex("6002 80 01"), {}, false, true, out); | ||
| const auto exit_code = run(vm, EVMC_LONDON, 200, *from_hex("60028001"), {}, false, true, out); | ||
| EXPECT_EQ(exit_code, 0); | ||
|
|
||
| const auto o = out.str(); | ||
|
|
@@ -160,7 +159,7 @@ TEST(tool_commands, bench_inconsistent_output) | |
| auto vm = evmc::VM{evmc_create_example_vm()}; | ||
| std::ostringstream out; | ||
|
|
||
| const auto code = *from_hex("6000 54 6001 6000 55 6000 52 6001 601f f3"); | ||
| const auto code = *from_hex("60005460016000556000526001601ff3"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So these tests technically can still have spaces, correct?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. We apply filter only when loading from a file. |
||
| const auto exit_code = run(vm, EVMC_BYZANTIUM, 200, code, {}, false, true, out); | ||
| EXPECT_EQ(exit_code, 0); | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of
end,end?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For filter iterator you need iterator pair so for the end filter iterator double end is provided. I considered some other options (default argument value, sentinel) but
boost::filter_iteratordoes the same so I left it as is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But couldn't you just pass
end()as the iterator?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How exactly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
endan iterator itself?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is also "workable" but the iterators would have different types.
filter_iteratorwithBaseIterator).std::copy()and many algorithms cannot be used because they expectInputIterators to be of the same type. https://en.cppreference.com/w/cpp/algorithm/copy.I expect much better user experience with
std::range, but I also have 0 experience with these so far.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I was wondering as it looked weird. Fine this way to avoid more code.