0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-14 00:34:18 +01:00

ircd: Simplify various string generations.

This commit is contained in:
Jason Volk 2018-01-29 08:37:20 -08:00
parent bce1206c70
commit f7e928e852
4 changed files with 38 additions and 74 deletions

View file

@ -59,14 +59,11 @@ template<class... args>
std::string
ircd::timestr(args&&... a)
{
std::string ret(128, char{});
const mutable_buffer buf
static const size_t max{128};
return string(max, [&](const mutable_buffer &buf)
{
const_cast<char *>(ret.data()), ret.size()
};
ret.resize(timef(buf, std::forward<args>(a)...).size());
return ret;
return timef(buf, std::forward<args>(a)...);
});
}
inline ircd::string_view

View file

@ -334,29 +334,16 @@ ircd::fs::read__aio(const string_view &path,
struct stat stat;
syscall(::fstat, fd, &stat);
const auto &size
return string(stat.st_size, [&fd, &opts]
(const mutable_buffer &buf)
{
stat.st_size
};
aio::request::read request
{
int(fd), buf, opts
};
std::string ret(size, char{});
const mutable_buffer buf
{
const_cast<char *>(ret.data()), ret.size()
};
aio::request::read request
{
int(fd), buf, opts
};
const size_t bytes
{
request()
};
ret.resize(bytes);
return ret;
return request();
});
}
ircd::string_view

View file

@ -55,20 +55,11 @@ ircd::_b64encode(const const_raw_buffer &in,
ceil(size(in) * (4.0 / 3.0)) + 4
};
std::string ret(max, char{});
const mutable_buffer buf
return string(max, [&in, &encoder]
(const mutable_buffer &buf)
{
const_cast<char *>(ret.data()), ret.size()
};
const string_view encoded
{
encoder(buf, in)
};
assert(size(encoded) <= ret.size());
ret.resize(size(encoded));
return ret;
return encoder(buf, in);
});
}
/// Encoding in to base64 at out. Out must be 1.33+ larger than in
@ -249,20 +240,11 @@ ircd::b58decode(const mutable_raw_buffer &buf,
std::string
ircd::b58encode(const const_raw_buffer &in)
{
std::string ret(b58encode_size(in), char{});
const mutable_buffer buf
return string(b58encode_size(in), [&in]
(const mutable_buffer &buf)
{
const_cast<char *>(ret.data()), ret.size()
};
const auto encoded
{
b58encode(buf, in)
};
assert(size(encoded) <= ret.size());
ret.resize(size(encoded));
return ret;
return b58encode(buf, in);
});
}
ircd::string_view

View file

@ -644,35 +644,33 @@ ircd::replace(const string_view &s,
s.size() - occurs
};
std::string ret(size, char{});
auto *p{const_cast<char *>(ret.data())};
std::for_each(begin(s), end(s), [&before, &after, &p]
(const char &c)
return string(size, [&s, &before, &after]
(const mutable_buffer &buf)
{
if(c == before)
char *p{begin(buf)};
std::for_each(begin(s), end(s), [&before, &after, &p]
(const char &c)
{
memcpy(p, after.data(), after.size());
p += after.size();
}
else *p++ = c;
});
if(c == before)
{
memcpy(p, after.data(), after.size());
p += after.size();
}
else *p++ = c;
});
//assert(ret.size() == size_t(std::distance(ret.data(), const_cast<const char *>(p))));
ret.resize(std::distance(ret.data(), const_cast<const char *>(p)));
return ret;
return std::distance(begin(buf), p);
});
}
std::string
ircd::u2a(const const_raw_buffer &in)
{
std::string ret(size(in) * 2, char{});
const mutable_buffer out
return string(size(in) * 2, [&in]
(const mutable_buffer &out)
{
const_cast<char *>(ret.data()), ret.size()
};
ret.resize(size(u2a(out, in)));
return ret;
return u2a(out, in);
});
}
ircd::string_view