trivial code refactor in entry and string_util related code

This commit is contained in:
Alden Torres 2017-11-28 19:22:50 -05:00 committed by Arvid Norberg
parent e8b42e95c4
commit 7fe75dd1eb
3 changed files with 30 additions and 35 deletions

View File

@ -137,14 +137,14 @@ namespace {
ec = bdecode_errors::expected_digit;
return start;
}
if (val > (std::numeric_limits<std::int64_t>::max)() / 10)
if (val > std::numeric_limits<std::int64_t>::max() / 10)
{
ec = bdecode_errors::overflow;
return start;
}
val *= 10;
int digit = *start - '0';
if (val > (std::numeric_limits<std::int64_t>::max)() - digit)
if (val > std::numeric_limits<std::int64_t>::max() - digit)
{
ec = bdecode_errors::overflow;
return start;

View File

@ -31,11 +31,6 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/config.hpp"
#if TORRENT_USE_IOSTREAM
#include <iostream>
#endif
#ifndef TORRENT_NO_DEPRECATE
#include "libtorrent/lazy_entry.hpp"
#endif
@ -97,9 +92,9 @@ namespace {
entry& entry::operator[](string_view key)
{
dictionary_type::iterator i = dict().find(key);
auto const i = dict().find(key);
if (i != dict().end()) return i->second;
dictionary_type::iterator ret = dict().emplace(
auto const ret = dict().emplace(
std::piecewise_construct,
std::forward_as_tuple(key),
std::forward_as_tuple()).first;
@ -108,21 +103,21 @@ namespace {
const entry& entry::operator[](string_view key) const
{
dictionary_type::const_iterator i = dict().find(key);
auto const i = dict().find(key);
if (i == dict().end()) throw_error();
return i->second;
}
entry* entry::find_key(string_view key)
{
dictionary_type::iterator i = dict().find(key);
auto const i = dict().find(key);
if (i == dict().end()) return nullptr;
return &i->second;
}
entry const* entry::find_key(string_view key) const
{
dictionary_type::const_iterator i = dict().find(key);
auto const i = dict().find(key);
if (i == dict().end()) return nullptr;
return &i->second;
}
@ -371,7 +366,7 @@ namespace {
list_type& l = this->list();
for (int i = 0; i < e.list_size(); ++i)
{
l.push_back(entry());
l.emplace_back();
l.back() = e.list_at(i);
}
break;
@ -410,7 +405,7 @@ namespace {
list_type& l = this->list();
for (int i = 0; i < e.list_size(); ++i)
{
l.push_back(entry());
l.emplace_back();
l.back() = *e.list_at(i);
}
break;
@ -656,7 +651,7 @@ namespace {
return ret;
}
void entry::to_string_impl(std::string& out, int indent) const
void entry::to_string_impl(std::string& out, int const indent) const
{
TORRENT_ASSERT(indent >= 0);
for (int i = 0; i < indent; ++i) out += ' ';
@ -669,9 +664,9 @@ namespace {
case string_t:
{
bool binary_string = false;
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
for (auto const i : string())
{
if (!is_print(*i))
if (!is_print(i))
{
binary_string = true;
break;
@ -691,20 +686,20 @@ namespace {
case list_t:
{
out += "list\n";
for (list_type::const_iterator i = list().begin(); i != list().end(); ++i)
for (auto const& i : list())
{
i->to_string_impl(out, indent + 1);
i.to_string_impl(out, indent + 1);
}
} break;
case dictionary_t:
{
out += "dictionary\n";
for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i)
for (auto const& i : dict())
{
bool binary_string = false;
for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k)
for (auto const k : i.first)
{
if (!is_print(*k))
if (!is_print(k))
{
binary_string = true;
break;
@ -712,15 +707,15 @@ namespace {
}
for (int j = 0; j < indent + 1; ++j) out += ' ';
out += '[';
if (binary_string) out += aux::to_hex(i->first);
else out += i->first;
if (binary_string) out += aux::to_hex(i.first);
else out += i.first;
out += ']';
if (i->second.type() != entry::string_t
&& i->second.type() != entry::int_t)
if (i.second.type() != entry::string_t
&& i.second.type() != entry::int_t)
out += '\n';
else out += ' ';
i->second.to_string_impl(out, indent + 2);
i.second.to_string_impl(out, indent + 2);
}
} break;
case preformatted_t:

View File

@ -154,7 +154,7 @@ namespace libtorrent {
TORRENT_ASSERT(target.size() >= src.size());
TORRENT_ASSERT(target.size() < std::size_t(std::numeric_limits<int>::max()));
auto it = std::search(target.begin(), target.end(), src.begin(), src.end());
auto const it = std::search(target.begin(), target.end(), src.begin(), src.end());
// no complete sync
if (it == target.end()) return -1;
@ -329,7 +329,7 @@ namespace libtorrent {
if (colon != std::string::npos && colon > start)
{
int port = atoi(in.substr(colon + 1, end - colon - 1).c_str());
int port = std::atoi(in.substr(colon + 1, end - colon - 1).c_str());
// skip trailing spaces
std::string::size_type soft_end = colon;
@ -342,7 +342,7 @@ namespace libtorrent {
if (in[start] == '[') ++start;
if (soft_end > start && in[soft_end-1] == ']') --soft_end;
out.push_back(std::make_pair(in.substr(start, soft_end - start), port));
out.emplace_back(in.substr(start, soft_end - start), port);
}
start = end + 1;
@ -420,8 +420,8 @@ namespace libtorrent {
std::size_t string_hash_no_case::operator()(std::string const& s) const
{
std::size_t ret = 5381;
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
ret = (ret * 33) ^ static_cast<std::size_t>(to_lower(*i));
for (auto const c : s)
ret = (ret * 33) ^ static_cast<std::size_t>(to_lower(c));
return ret;
}
@ -429,8 +429,8 @@ namespace libtorrent {
{
if (lhs.size() != rhs.size()) return false;
std::string::const_iterator s1 = lhs.begin();
std::string::const_iterator s2 = rhs.begin();
auto s1 = lhs.cbegin();
auto s2 = rhs.cbegin();
while (s1 != lhs.end() && s2 != rhs.end())
{