0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-01 19:22:53 +01:00

ircd: Add allocated string convenience for u2a().

This commit is contained in:
Jason Volk 2018-01-28 11:41:46 -08:00
parent 513082b656
commit 68b408a065
2 changed files with 26 additions and 12 deletions

View file

@ -45,8 +45,9 @@ namespace ircd
template<class T> string_view lex_cast(const T &t);
// Binary <-> Hex conversion suite
string_view u2a(const mutable_buffer &out, const const_raw_buffer &in);
const_raw_buffer a2u(const mutable_raw_buffer &out, const const_buffer &in);
string_view u2a(const mutable_buffer &out, const const_raw_buffer &in);
std::string u2a(const const_raw_buffer &in);
}
namespace ircd

View file

@ -662,6 +662,30 @@ ircd::replace(const string_view &s,
return ret;
}
std::string
ircd::u2a(const const_raw_buffer &in)
{
std::string ret(size(in) * 2, char{});
const mutable_buffer out
{
const_cast<char *>(ret.data()), ret.size()
};
ret.resize(size(u2a(out, in)));
return ret;
}
ircd::string_view
ircd::u2a(const mutable_buffer &out,
const const_raw_buffer &in)
{
char *p(data(out));
for(size_t i(0); i < size(in); ++i)
p += snprintf(p, size(out) - (p - data(out)), "%02x", in[i]);
return { data(out), size_t(p - data(out)) };
}
ircd::const_raw_buffer
ircd::a2u(const mutable_raw_buffer &out,
const const_buffer &in)
@ -681,14 +705,3 @@ ircd::a2u(const mutable_raw_buffer &out,
return { data(out), len };
}
ircd::string_view
ircd::u2a(const mutable_buffer &out,
const const_raw_buffer &in)
{
char *p(data(out));
for(size_t i(0); i < size(in); ++i)
p += snprintf(p, size(out) - (p - data(out)), "%02x", in[i]);
return { data(out), size_t(p - data(out)) };
}