0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-16 15:00:51 +01:00

ircd::util: Improve string() suite; add flags for shrink_to_fit().

This commit is contained in:
Jason Volk 2019-02-07 17:52:24 -08:00
parent 4c43b42f62
commit cc636d375f
2 changed files with 37 additions and 5 deletions

View file

@ -17,10 +17,27 @@
namespace ircd::util namespace ircd::util
{ {
using string_closure_size = std::function<size_t (const mutable_buffer &)>;
using string_closure_view = std::function<string_view (const mutable_buffer &)>;
// OR this with a string size to trigger a shrink_to_fit() after closure.
constexpr const size_t SHRINK_TO_FIT
{
1UL << (sizeof(size_t) * 8 - 1)
};
// Simple string generation by copy from existing buffer.
std::string string(const char *const &buf, const size_t &size); std::string string(const char *const &buf, const size_t &size);
std::string string(const uint8_t *const &buf, const size_t &size); std::string string(const uint8_t *const &buf, const size_t &size);
std::string string(const size_t &size, const std::function<size_t (const mutable_buffer &)> &closure); std::string string(const const_buffer &);
std::string string(const size_t &size, const std::function<string_view (const mutable_buffer &)> &closure);
// String generation from closure. The closure is presented a buffer of
// size for writing into. Closure returns how much it wrote via size or
// view of written portion.
std::string string(const size_t &size, const string_closure_size &);
std::string string(const size_t &size, const string_closure_view &);
// toString()'ish template suite (see defs)
template<class T> std::string string(const mutable_buffer &buf, const T &s); template<class T> std::string string(const mutable_buffer &buf, const T &s);
template<class T> std::string string(const T &s); template<class T> std::string string(const T &s);
} }

View file

@ -299,7 +299,7 @@ ircd::util::si(const uint64_t &value)
/// a view of the data actually written to the buffer. /// a view of the data actually written to the buffer.
std::string std::string
ircd::util::string(const size_t &size, ircd::util::string(const size_t &size,
const std::function<string_view (const mutable_buffer &)> &closure) const string_closure_view &closure)
{ {
return string(size, [&closure] return string(size, [&closure]
(const mutable_buffer &buffer) (const mutable_buffer &buffer)
@ -313,9 +313,14 @@ ircd::util::string(const size_t &size,
/// the final size of the data written into the buffer. /// the final size of the data written into the buffer.
std::string std::string
ircd::util::string(const size_t &size, ircd::util::string(const size_t &size,
const std::function<size_t (const mutable_buffer &)> &closure) const string_closure_size &closure)
{ {
std::string ret(size, char{}); const size_t alloc_size
{
size & ~SHRINK_TO_FIT
};
std::string ret(alloc_size, char{});
const mutable_buffer buf const mutable_buffer buf
{ {
const_cast<char *>(ret.data()), ret.size() const_cast<char *>(ret.data()), ret.size()
@ -329,9 +334,19 @@ ircd::util::string(const size_t &size,
assert(consumed <= buffer::size(buf)); assert(consumed <= buffer::size(buf));
data(buf)[consumed] = '\0'; data(buf)[consumed] = '\0';
ret.resize(consumed); ret.resize(consumed);
if(size & SHRINK_TO_FIT)
ret.shrink_to_fit();
return ret; return ret;
} }
std::string
ircd::util::string(const const_buffer &buf)
{
return string(data(buf), size(buf));
}
std::string std::string
ircd::util::string(const char *const &buf, ircd::util::string(const char *const &buf,
const size_t &size) const size_t &size)