Add strHex(Slice)

This commit is contained in:
Vinnie Falco
2015-05-31 11:16:47 -07:00
parent 4b91a18532
commit 4825cefbf8
3 changed files with 20 additions and 12 deletions

View File

@@ -25,6 +25,7 @@
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include <beast/cxx14/type_traits.h> // <type_traits>
@@ -142,6 +143,9 @@ make_Slice (std::basic_string<char, Traits, Alloc> const& s)
return Slice(s.data(), s.size());
}
std::string
strHex (Slice const& slice);
} // ripple
#endif

View File

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

View File

@@ -64,22 +64,18 @@ charUnHex (char c)
// NIKB TODO cleanup this function and reduce the need for the many overloads
// it has in various places.
template<class Iterator>
std::string strHex (Iterator first, int iSize)
template<class FwdIt>
std::string strHex (FwdIt first, int size)
{
std::string strDst;
strDst.resize (iSize * 2);
for (int i = 0; i < iSize; i++)
std::string s;
s.resize (size * 2);
for (int i = 0; i < size; i++)
{
unsigned char c = *first++;
strDst[i * 2] = charHex (c >> 4);
strDst[i * 2 + 1] = charHex (c & 15);
s[i * 2] = charHex (c >> 4);
s[i * 2 + 1] = charHex (c & 15);
}
return strDst;
return s;
}
}