From 574fd8ed4aba42da3908e6ae9446c512a6e5d9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Thu, 30 Mar 2017 17:05:08 -0400 Subject: [PATCH 1/3] pht: Pht::toString remove confusing output toString function should only output the string representation of the prefix in bits (0s and 1s). More information is confusing and not useful as it requires to parse the string. --- src/indexation/pht.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index dbda88f26..719cba0d0 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -44,20 +44,7 @@ static std::string blobToString(const Blob &bl) { 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 blobToString(content_); } void Pht::Cache::insert(const Prefix& p) { size_t i = 0; From 625b09be8107c93311b360f8a3358d22ca04e1b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Sun, 2 Apr 2017 23:55:13 -0400 Subject: [PATCH 2/3] 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. --- include/opendht/indexation/pht.h | 6 ++++-- src/indexation/pht.cpp | 25 ++++++++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index e8363074f..deaea4524 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -239,7 +239,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; @@ -256,12 +256,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; diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index 719cba0d0..dfd93cdaa 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -25,26 +25,29 @@ 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 { + 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 { return blobToString(content_); } + +std::string Prefix::toString() const { return firstBitsToString(content_, size_); } void Pht::Cache::insert(const Prefix& p) { size_t i = 0; From c0e39b50ba0303dd393280b40ca6c87b318972c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Mon, 3 Apr 2017 00:24:59 -0400 Subject: [PATCH 3/3] pht: add Prefix::flagsToString method for debug --- include/opendht/indexation/pht.h | 1 + src/indexation/pht.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index deaea4524..ee66ca519 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -200,6 +200,7 @@ ee * } std::string toString() const; + std::string flagsToString() const; size_t size_ {0}; diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index dfd93cdaa..0e80cd731 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -48,6 +48,7 @@ std::string Prefix::firstBitsToString(const Blob &bl, size_t len) const { } 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;