mirror of
https://github.com/Xahau/xahaud.git
synced 2025-11-19 01:55:48 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'");
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -63,6 +63,8 @@ protected:
|
||||
std::uint8_t buf_[33]; // should be large enough
|
||||
|
||||
public:
|
||||
using const_iterator = std::uint8_t const*;
|
||||
|
||||
PublicKey() = default;
|
||||
PublicKey (PublicKey const& other);
|
||||
PublicKey& operator= (PublicKey const& other);
|
||||
@@ -87,6 +89,30 @@ public:
|
||||
return size_;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
begin() const noexcept
|
||||
{
|
||||
return buf_;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
cbegin() const noexcept
|
||||
{
|
||||
return buf_;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
end() const noexcept
|
||||
{
|
||||
return buf_ + size_;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
cend() const noexcept
|
||||
{
|
||||
return buf_ + size_;
|
||||
}
|
||||
|
||||
bool
|
||||
empty() const noexcept
|
||||
{
|
||||
|
||||
@@ -120,7 +120,7 @@ public:
|
||||
STPathElement(STPathElement const&) = default;
|
||||
STPathElement& operator=(STPathElement const&) = default;
|
||||
|
||||
int
|
||||
auto
|
||||
getNodeType () const
|
||||
{
|
||||
return mType;
|
||||
|
||||
@@ -38,6 +38,8 @@ private:
|
||||
std::uint8_t buf_[32];
|
||||
|
||||
public:
|
||||
using const_iterator = std::uint8_t const*;
|
||||
|
||||
SecretKey() = default;
|
||||
SecretKey (SecretKey const&) = default;
|
||||
SecretKey& operator= (SecretKey const&) = default;
|
||||
@@ -66,6 +68,30 @@ public:
|
||||
*/
|
||||
std::string
|
||||
to_string() const;
|
||||
|
||||
const_iterator
|
||||
begin() const noexcept
|
||||
{
|
||||
return buf_;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
cbegin() const noexcept
|
||||
{
|
||||
return buf_;
|
||||
}
|
||||
|
||||
const_iterator
|
||||
end() const noexcept
|
||||
{
|
||||
return buf_ + sizeof(buf_);
|
||||
}
|
||||
|
||||
const_iterator
|
||||
cend() const noexcept
|
||||
{
|
||||
return buf_ + sizeof(buf_);
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
|
||||
@@ -35,6 +35,8 @@ private:
|
||||
std::array<uint8_t, 16> buf_;
|
||||
|
||||
public:
|
||||
using const_iterator = std::array<uint8_t, 16>::const_iterator;
|
||||
|
||||
Seed() = delete;
|
||||
|
||||
Seed (Seed const&) = default;
|
||||
@@ -62,6 +64,30 @@ public:
|
||||
{
|
||||
return buf_.size();
|
||||
}
|
||||
|
||||
const_iterator
|
||||
begin() const noexcept
|
||||
{
|
||||
return buf_.begin();
|
||||
}
|
||||
|
||||
const_iterator
|
||||
cbegin() const noexcept
|
||||
{
|
||||
return buf_.cbegin();
|
||||
}
|
||||
|
||||
const_iterator
|
||||
end() const noexcept
|
||||
{
|
||||
return buf_.end();
|
||||
}
|
||||
|
||||
const_iterator
|
||||
cend() const noexcept
|
||||
{
|
||||
return buf_.cend();
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace ripple {
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, PublicKey const& pk)
|
||||
{
|
||||
os << strHex(pk.data(), pk.size());
|
||||
os << strHex(pk);
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ STBlob::STBlob (SerialIter& st, SField const& name)
|
||||
std::string
|
||||
STBlob::getText () const
|
||||
{
|
||||
return strHex (value_.data (), value_.size ());
|
||||
return strHex (value_);
|
||||
}
|
||||
|
||||
bool
|
||||
|
||||
@@ -156,7 +156,7 @@ STPath::getJson (int) const
|
||||
for (auto it: mPath)
|
||||
{
|
||||
Json::Value elem (Json::objectValue);
|
||||
int iType = it.getNodeType ();
|
||||
auto const iType = it.getNodeType ();
|
||||
|
||||
elem[jss::type] = iType;
|
||||
elem[jss::type_hex] = strHex (iType);
|
||||
|
||||
@@ -51,7 +51,7 @@ SecretKey::SecretKey (Slice const& slice)
|
||||
std::string
|
||||
SecretKey::to_string() const
|
||||
{
|
||||
return strHex(data(), size());
|
||||
return strHex(*this);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -61,7 +61,7 @@ std::string to_string(Currency const& currency)
|
||||
}
|
||||
}
|
||||
|
||||
return strHex (currency.begin (), currency.size ());
|
||||
return strHex (currency);
|
||||
}
|
||||
|
||||
bool to_currency(Currency& currency, std::string const& code)
|
||||
|
||||
@@ -108,7 +108,7 @@ Json::Value walletPropose (Json::Value const& params)
|
||||
Json::Value obj (Json::objectValue);
|
||||
|
||||
auto const seed1751 = seedAs1751 (*seed);
|
||||
auto const seedHex = strHex (seed->data(), seed->size());
|
||||
auto const seedHex = strHex (*seed);
|
||||
auto const seedBase58 = toBase58 (*seed);
|
||||
|
||||
obj[jss::master_seed] = seedBase58;
|
||||
@@ -117,7 +117,7 @@ Json::Value walletPropose (Json::Value const& params)
|
||||
obj[jss::account_id] = toBase58(calcAccountID(publicKey));
|
||||
obj[jss::public_key] = toBase58(TokenType::AccountPublic, publicKey);
|
||||
obj[jss::key_type] = to_string (keyType);
|
||||
obj[jss::public_key_hex] = strHex (publicKey.data(), publicKey.size());
|
||||
obj[jss::public_key_hex] = strHex (publicKey);
|
||||
|
||||
// If a passphrase was specified, and it was hashed and used as a seed
|
||||
// run a quick entropy check and add an appropriate warning, because
|
||||
|
||||
@@ -212,7 +212,8 @@ public:
|
||||
env(jt);
|
||||
BEAST_EXPECT(! env.le(alice)->isFieldPresent(sfMessageKey));
|
||||
|
||||
jt[sfMessageKey.fieldName] = strHex("NOT_REALLY_A_PUBKEY");
|
||||
using namespace std::string_literals;
|
||||
jt[sfMessageKey.fieldName] = strHex("NOT_REALLY_A_PUBKEY"s);
|
||||
env(jt, ter(telBAD_PUBLIC_KEY));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user