Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor hex decoding utilities #648

Merged
merged 2 commits into from
Jun 2, 2022
Merged

Refactor hex decoding utilities #648

merged 2 commits into from
Jun 2, 2022

Conversation

chfast
Copy link
Member

@chfast chfast commented May 26, 2022

Generalize the core from_hex() to take the input as pair in input iterators. The optional 0x prefix is still omitted but the "skip space" feature has been dropped.

Add skip_space_iterator. This restores the option to filter-out whitespace before parsing hex. This is useful e.g. when loading hex from a file.

@chfast chfast force-pushed the hex_refactor branch 2 times, most recently from 6806151 to 3b05ee3 Compare May 26, 2022 07:04
@codecov
Copy link

codecov bot commented May 26, 2022

Codecov Report

Merging #648 (5cec7bd) into master (58913e0) will increase coverage by 0.14%.
The diff coverage is 99.22%.

❗ Current head 5cec7bd differs from pull request most recent head ebd8622. Consider uploading reports for the commit ebd8622 to get more accurate results

@@            Coverage Diff             @@
##           master     #648      +/-   ##
==========================================
+ Coverage   92.84%   92.99%   +0.14%     
==========================================
  Files          23       25       +2     
  Lines        3552     3626      +74     
  Branches      376      377       +1     
==========================================
+ Hits         3298     3372      +74     
  Misses        144      144              
  Partials      110      110              

@chfast chfast requested review from axic, gumb0 and yperbasis May 26, 2022 07:25
@chfast chfast force-pushed the cli_file branch 2 times, most recently from f317de3 to f5cf3ab Compare June 1, 2022 11:49
Base automatically changed from cli_file to master June 1, 2022 11:57
@@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these tests technically can still have spaces, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We apply filter only when loading from a file.


/// The input filter iterator which skips whitespace characters from the base input iterator.
template <typename BaseIterator>
struct skip_space_iterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't fully reviewed this file, but intuitively looks ok.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We extract the filter function (isspace) to get generic filter iterator (like boost::filter_iterator). This can be done later to filter out literal separator (').

Copy link
Member

@axic axic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay to me (at least the from_hex part). Would be nice to get a check from @gumb0.

@gumb0
Copy link
Member

gumb0 commented Jun 1, 2022

Would be nice to have a test that checks how both from_hex and skip_space_iterator work together.

Generalize the core from_hex() to take the input as pair in input
iterators. The optional 0x prefix is still omitted but the "skip space"
feature has been dropped.
@chfast chfast force-pushed the hex_refactor branch 4 times, most recently from c0337b9 to 5cec7bd Compare June 1, 2022 18:07
// 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));
Copy link
Member

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?

Copy link
Member Author

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_iterator does the same so I left it as is.

Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How exactly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::copy(skip_space_iterator{in_buffer.begin(), in_buffer.end()}, in_buffer.end(), std::back_inserter(out));

Isn't end an iterator itself?

Copy link
Member Author

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.

  1. Additional overloads for comparison operators are needed (so we can compare filter_iterator with BaseIterator).
  2. The std::copy() and many algorithms cannot be used because they expect InputIterators 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.

Copy link
Member

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.

This adds the filter_iterator input iterator adapter to filter input
values. With the additional is_not_space predicate these restores
the option to filter-out whitespace before parsing hex.
This is useful e.g. when loading hex from a file.
@chfast chfast merged commit 568f9df into master Jun 2, 2022
@chfast chfast deleted the hex_refactor branch June 2, 2022 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants