Simplify strHex:

Problem:
- There are several specific overloads with some custom code that can be
  easily replaced using Boost.Hex.

Solution:
- Introduce `strHex(itr, itr)` to return a string given a begin and end
  iterator.
- Remove `strHex(itr, size)` in favor of the `strHex(T)` where T is
  something that has a `begin()` member function. This allows us to
  remove the strHex overloads for `std::string`, Blob, and Slice.
This commit is contained in:
Joe Loser
2018-08-20 22:38:40 -04:00
committed by seelabs
parent 3661dc88fe
commit 1ac9694dbc
17 changed files with 174 additions and 54 deletions

View File

@@ -39,6 +39,8 @@ private:
std::size_t size_ = 0;
public:
using const_iterator = std::uint8_t const*;
Buffer() = default;
/** Create an uninitialized buffer with the given size. */
@@ -191,6 +193,30 @@ public:
{
return alloc(n);
}
const_iterator
begin() const noexcept
{
return p_.get();
}
const_iterator
cbegin() const noexcept
{
return p_.get();
}
const_iterator
end() const noexcept
{
return p_.get() + size_;
}
const_iterator
cend() const noexcept
{
return p_.get() + size_;
}
};
inline bool operator==(Buffer const& lhs, Buffer const& rhs) noexcept

View File

@@ -47,6 +47,8 @@ private:
std::size_t size_ = 0;
public:
using const_iterator = std::uint8_t const*;
/** Default constructed Slice has length 0. */
Slice() noexcept = default;
@@ -114,6 +116,31 @@ public:
return temp += n;
}
/** @} */
const_iterator
begin() const noexcept
{
return data_;
}
const_iterator
cbegin() const noexcept
{
return data_;
}
const_iterator
end() const noexcept
{
return data_ + size_;
}
const_iterator
cend() const noexcept
{
return data_ + size_;
}
};
//------------------------------------------------------------------------------
@@ -159,7 +186,7 @@ operator< (Slice const& lhs, Slice const& rhs) noexcept
template <class Stream>
Stream& operator<<(Stream& s, Slice const& v)
{
s << strHex(v.data(), v.size());
s << strHex(v);
return s;
}
@@ -192,9 +219,6 @@ makeSlice (std::basic_string<char, Traits, Alloc> const& s)
return Slice(s.data(), s.size());
}
std::string
strHex (Slice const& slice);
} // ripple
#endif

View File

@@ -22,32 +22,13 @@
#include <ripple/basics/Blob.h>
#include <ripple/basics/strHex.h>
#include <boost/endian/conversion.hpp>
#include <boost/format.hpp>
#include <boost/optional.hpp>
#include <sstream>
#include <string>
namespace ripple {
// NIKB TODO Remove the need for all these overloads. Move them out of here.
inline const std::string strHex (std::string const& strSrc)
{
return strHex (strSrc.begin (), strSrc.size ());
}
inline std::string strHex (Blob const& vucData)
{
return strHex (vucData.begin (), vucData.size ());
}
inline std::string strHex (const std::uint64_t uiHost)
{
uint64_t uBig = boost::endian::native_to_big (uiHost);
return strHex ((unsigned char*) &uBig, sizeof (uBig));
}
inline static std::string sqlEscape (std::string const& strSrc)
{
static boost::format f ("X'%s'");

View File

@@ -510,7 +510,7 @@ inline const base_uint<Bits, Tag> operator+ (
template <std::size_t Bits, class Tag>
inline std::string to_string (base_uint<Bits, Tag> const& a)
{
return strHex (a.begin (), a.size ());
return strHex (a.cbegin (), a.cend ());
}
// Function templates that return a base_uint given text in hexadecimal.

View File

@@ -17,9 +17,9 @@
*/
//==============================================================================
#include <ripple/basics/Slice.h>
#include <ripple/basics/strHex.h>
#include <algorithm>
#include <string>
namespace ripple {
@@ -51,10 +51,4 @@ int charUnHex (unsigned char c)
return xtab[c];
}
std::string
strHex(Slice const& slice)
{
return strHex(slice.data(), slice.size());
}
}

View File

@@ -28,6 +28,9 @@
#include <cassert>
#include <string>
#include <boost/algorithm/hex.hpp>
#include <boost/endian/conversion.hpp>
namespace ripple {
/** Converts an integer to the corresponding hex digit
@@ -62,22 +65,35 @@ charUnHex (char c)
}
/** @} */
// NIKB TODO cleanup this function and reduce the need for the many overloads
// it has in various places.
template<class FwdIt>
std::string strHex (FwdIt first, int size)
template <class FwdIt>
std::string
strHex(FwdIt begin, FwdIt end)
{
std::string s;
s.resize (size * 2);
for (int i = 0; i < size; i++)
{
unsigned char c = *first++;
s[i * 2] = charHex (c >> 4);
s[i * 2 + 1] = charHex (c & 15);
}
return s;
static_assert(
std::is_convertible<
typename std::iterator_traits<FwdIt>::iterator_category,
std::forward_iterator_tag>::value,
"FwdIt must be a forward iterator");
std::string result;
result.reserve(2 * std::distance(begin, end));
boost::algorithm::hex(begin, end, std::back_inserter(result));
return result;
}
template <class T, class = decltype(std::declval<T>().begin())>
std::string strHex(T const& from)
{
return strHex(from.begin(), from.end());
}
inline std::string strHex (const std::uint64_t uiHost)
{
uint64_t uBig = boost::endian::native_to_big (uiHost);
auto const begin = (unsigned char*) &uBig;
auto const end = begin + sizeof(uBig);
return strHex(begin, end);
}
}
#endif