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

pht: Fix wrong output from Prefix::toString #183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions include/opendht/indexation/pht.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ ee *
}

std::string toString() const;
std::string flagsToString() const;

size_t size_ {0};

Expand Down Expand Up @@ -239,7 +240,7 @@ ee *
* @throw out_of_range if bit is superior to blob size * 8
*/
bool isActiveBit(const Blob &b, size_t pos) const {
if ( pos >= content_.size() * 8 )
if (pos >= content_.size() * 8)
throw std::out_of_range("Can't detect active bit at pos, pos larger than prefix size or empty prefix");

return ((b[pos / 8] >> (7 - (pos % 8)) ) & 1) == 1;
Expand All @@ -256,12 +257,14 @@ ee *
* @throw out_of_range if bit is superior to blob size * 8
*/
void swapBit(Blob &b, size_t bit) {
if ( bit >= b.size() * 8 )
if (bit >= b.size() * 8)
throw std::out_of_range("bit larger than prefix size.");

size_t offset_bit = (8 - bit) % 8;
b[bit / 8] ^= (1 << offset_bit);
}

std::string firstBitsToString(const Blob&, size_t) const;
};

using Value = std::pair<InfoHash, dht::Value::Id>;
Expand Down
37 changes: 14 additions & 23 deletions src/indexation/pht.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,30 @@ namespace dht {
namespace indexation {

/**
* Output the blob into string and readable way
* Get bit string representation of a blob for a specified length.
*
* @param bl : Blob to print
* @param bl A Blob
* @param len The number of bits to consider.
*
* @return string that represent the blob into a readable way
* @return string that represent the blob
*/
static std::string blobToString(const Blob &bl) {
std::string Prefix::firstBitsToString(const Blob &bl, size_t len) const {
Copy link
Member

@aberaud aberaud Apr 4, 2017

Choose a reason for hiding this comment

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

If you choose to make it a non-static method, you should remove the second argument and checks, and use size_ directly (otherwise shall remain static). Also compute len/8 out of loops like in previous implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aberaud: In the case of toString, the ouput is the bit string up to size_ bits since this is the most expected behavior. However, one could like to get the whole bit string of the prefix for debugging purposes. To be honest, this function is mostly being used in debugging purposes. That's why I left it this way.

if (len >= bl.size() * 8)
throw std::out_of_range("specified length larger than blob size");
std::stringstream ss;
auto bn = bl.size() % 8;
auto n = bl.size() / 8;
auto bn = len % 8;

for (size_t i = 0; i < bl.size(); i++)
ss << std::bitset<8>(bl[i]) << " ";
for (size_t i = 0; i < len/8; i++)
ss << std::bitset<8>(bl[i]);
if (bn)
for (unsigned b=0; b < bn; b++)
ss << (char)((bl[n] & (1 << (7 - b))) ? '1':'0');
for (unsigned b = 0; b < bn; b++)
ss << (char)((bl[len/8] & (1 << (7 - b))) ? '1':'0');

return ss.str();
}

std::string Prefix::toString() const {
std::stringstream ss;

ss << "Prefix : " << std::endl << "\tContent_ : \"";
ss << blobToString(content_);
ss << "\"" << std::endl;

ss << "\tFlags_ : \"";
ss << blobToString(flags_);
ss << "\"" << std::endl;

return ss.str();
}
std::string Prefix::toString() const { return firstBitsToString(content_, size_); }
std::string Prefix::flagsToString() const { return firstBitsToString(flags_, size_); }

void Pht::Cache::insert(const Prefix& p) {
size_t i = 0;
Expand Down