make url_random take a span

This commit is contained in:
arvidn 2018-01-14 20:27:23 +01:00 committed by Arvid Norberg
parent a01274ce2c
commit 785fffd976
4 changed files with 14 additions and 9 deletions

View File

@ -78,6 +78,11 @@ namespace aux {
span(std::array<U, N>& arr) noexcept // NOLINT
: m_ptr(arr.data()), m_len(arr.size()) {}
// this is necessary until C++17, where data() returns a non-const pointer
template <typename U>
span(std::basic_string<U>& str) noexcept // NOLINT
: m_ptr(&str[0]), m_len(str.size()) {}
template <typename U, std::size_t N>
span(U (&arr)[N]) noexcept // NOLINT
: m_ptr(&arr[0]), m_len(N) {}

View File

@ -68,7 +68,7 @@ namespace libtorrent {
TORRENT_EXTRA_EXPORT bool string_begins_no_case(char const* s1, char const* s2);
TORRENT_EXTRA_EXPORT bool string_equal_no_case(string_view s1, string_view s2);
TORRENT_EXTRA_EXPORT void url_random(char* begin, char* end);
TORRENT_EXTRA_EXPORT void url_random(span<char> dest);
TORRENT_EXTRA_EXPORT bool string_ends_with(string_view s1, string_view s2);

View File

@ -1072,7 +1072,7 @@ namespace {
ret.upload_limit = 0xf0f0f0f;
ret.download_limit = 0xf0f0f0f;
ret.label.resize(20);
url_random(&ret.label[0], &ret.label[0] + 20);
url_random(span<char>(ret.label));
ret.ignore_unchoke_slots = false;
ret.connection_limit_factor = 0xf0f0f0f;
ret.upload_priority = 0xf0f0f0f;
@ -5349,13 +5349,13 @@ namespace {
{
// ---- generate a peer id ----
std::string print = m_settings.get_str(settings_pack::peer_fingerprint);
if (print.size() > 20) print.resize(20);
if (print.size() > m_peer_id.size()) print.resize(m_peer_id.size());
// the client's fingerprint
std::copy(print.begin(), print.begin() + int(print.length()), m_peer_id.begin());
if (print.length() < 20)
if (print.size() < m_peer_id.size())
{
url_random(m_peer_id.data() + print.length(), m_peer_id.data() + 20);
url_random(span<char>(m_peer_id).subspan(print.length()));
}
}
@ -6395,7 +6395,7 @@ namespace {
}
if (m_upnp) m_upnp->set_user_agent("");
url_random(m_peer_id.data(), m_peer_id.data() + 20);
url_random(m_peer_id);
}
void session_impl::update_force_proxy()

View File

@ -129,7 +129,7 @@ namespace libtorrent {
}
// generate a url-safe random string
void url_random(char* begin, char* end)
void url_random(span<char> dest)
{
// http-accepted characters:
// excluding ', since some buggy trackers don't support that
@ -137,8 +137,8 @@ namespace libtorrent {
"abcdefghijklmnopqrstuvwxyz-_.!~*()";
// the random number
while (begin != end)
*begin++ = printable[random(sizeof(printable) - 2)];
for (char& c : dest)
c = printable[random(sizeof(printable) - 2)];
}
bool string_ends_with(string_view s1, string_view s2)