Skip to content

Commit 625b09b

Browse files
committed
pht: rename and fix Pht::blobToString
A refactoring error led to blobToString outputing garbage. This patch fixes the issue and enables output of a number of bits other than multiple of 8.
1 parent 574fd8e commit 625b09b

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

include/opendht/indexation/pht.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ ee *
239239
* @throw out_of_range if bit is superior to blob size * 8
240240
*/
241241
bool isActiveBit(const Blob &b, size_t pos) const {
242-
if ( pos >= content_.size() * 8 )
242+
if (pos >= content_.size() * 8)
243243
throw std::out_of_range("Can't detect active bit at pos, pos larger than prefix size or empty prefix");
244244

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

262262
size_t offset_bit = (8 - bit) % 8;
263263
b[bit / 8] ^= (1 << offset_bit);
264264
}
265+
266+
std::string firstBitsToString(const Blob&, size_t) const;
265267
};
266268

267269
using Value = std::pair<InfoHash, dht::Value::Id>;

src/indexation/pht.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,29 @@ namespace dht {
2525
namespace indexation {
2626

2727
/**
28-
* Output the blob into string and readable way
28+
* Get bit string representation of a blob for a specified length.
2929
*
30-
* @param bl : Blob to print
30+
* @param bl A Blob
31+
* @param len The number of bits to consider.
3132
*
32-
* @return string that represent the blob into a readable way
33+
* @return string that represent the blob
3334
*/
34-
static std::string blobToString(const Blob &bl) {
35+
std::string Prefix::firstBitsToString(const Blob &bl, size_t len) const {
36+
if (len >= bl.size() * 8)
37+
throw std::out_of_range("specified length larger than blob size");
3538
std::stringstream ss;
36-
auto bn = bl.size() % 8;
37-
auto n = bl.size() / 8;
39+
auto bn = len % 8;
3840

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

4547
return ss.str();
4648
}
47-
std::string Prefix::toString() const { return blobToString(content_); }
49+
50+
std::string Prefix::toString() const { return firstBitsToString(content_, size_); }
4851

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

0 commit comments

Comments
 (0)