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

IF: Use string for variant format of fc::dynamic_bitset #67

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions libraries/libfc/include/fc/variant_dynamic_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ namespace fc
if ( num_blocks > MAX_NUM_ARRAY_ELEMENTS )
throw std::range_error( "number of blocks of dynamic_bitset cannot be greather than MAX_NUM_ARRAY_ELEMENTS" );

std::vector<fc::dynamic_bitset::block_type> blocks(num_blocks);
boost::to_block_range(bs, blocks.begin());
v = fc::mutable_variant_object("size", bs.size())
("bits", blocks);
std::string s;
to_string(bs, s);
// From boost::dynamic_bitset docs:
// A character in the string is '1' if the corresponding bit is set, and '0' if it is not. Character
// position i in the string corresponds to bit position b.size() - 1 - i.
// reverse so the ith string character corresponds to the ith dynamic_bitset entry
std::ranges::reverse(s);
v = std::move(s);
}

inline void from_variant( const fc::variant& v, fc::dynamic_bitset& bs ) {
fc::dynamic_bitset::size_type size;
std::vector<fc::dynamic_bitset::block_type> blocks;

from_variant(v["size"], size);
from_variant(v["bits"], blocks);
bs = { blocks.cbegin(), blocks.cend() };
bs.resize(size);
std::string s = v.get_string();
// see comment above
std::ranges::reverse(s);
bs = fc::dynamic_bitset(s);
}
} // namespace fc
Loading