From d45ad8fcf5e79d95c55ed2185351d4e20d940cb2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 27 Jan 2016 17:18:20 +0100 Subject: [PATCH] Make hashLength32() a method of Hash --- src/libutil/hash.cc | 12 +++--------- src/libutil/hash.hh | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 2d97c5e6b..647393003 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -96,19 +96,13 @@ Hash parseHash(HashType ht, const string & s) } -unsigned int hashLength32(const Hash & hash) -{ - return (hash.hashSize * 8 - 1) / 5 + 1; -} - - // omitted: E O U T const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz"; string printHash32(const Hash & hash) { - unsigned int len = hashLength32(hash); + size_t len = hash.base32Len(); string s; s.reserve(len); @@ -136,7 +130,7 @@ string printHash16or32(const Hash & hash) Hash parseHash32(HashType ht, const string & s) { Hash hash(ht); - unsigned int len = hashLength32(ht); + size_t len = hash.base32Len(); assert(s.size() == len); for (unsigned int n = 0; n < len; ++n) { @@ -163,7 +157,7 @@ Hash parseHash16or32(HashType ht, const string & s) if (s.size() == hash.hashSize * 2) /* hexadecimal representation */ hash = parseHash(ht, s); - else if (s.size() == hashLength32(hash)) + else if (s.size() == hash.base32Len()) /* base-32 representation */ hash = parseHash32(ht, s); else diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index 841b4cb29..8bd7e9f17 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -40,6 +40,18 @@ struct Hash /* For sorting. */ bool operator < (const Hash & h) const; + + /* Returns the length of a base-16 representation of this hash. */ + size_t base16Len() const + { + return hashSize * 2; + } + + /* Returns the length of a base-32 representation of this hash. */ + size_t base32Len() const + { + return (hashSize * 8 - 1) / 5 + 1; + } }; @@ -49,9 +61,6 @@ string printHash(const Hash & hash); /* Parse a hexadecimal representation of a hash code. */ Hash parseHash(HashType ht, const string & s); -/* Returns the length of a base-32 hash representation. */ -unsigned int hashLength32(const Hash & hash); - /* Convert a hash to a base-32 representation. */ string printHash32(const Hash & hash);