diff --git a/src/base/bittorrent/infohash.cpp b/src/base/bittorrent/infohash.cpp index c29c8cd85..d900a4776 100644 --- a/src/base/bittorrent/infohash.cpp +++ b/src/base/bittorrent/infohash.cpp @@ -28,6 +28,7 @@ #include "infohash.h" +#include #include using namespace BitTorrent; @@ -41,29 +42,25 @@ InfoHash::InfoHash(const libtorrent::sha1_hash &nativeHash) : m_valid(true) , m_nativeHash(nativeHash) { - char out[(libtorrent::sha1_hash::size * 2) + 1]; - libtorrent::to_hex(reinterpret_cast(&m_nativeHash[0]), libtorrent::sha1_hash::size, out); - m_hashString = QString(out); + const QByteArray raw = QByteArray::fromRawData(nativeHash.data(), libtorrent::sha1_hash::size); + m_hashString = QString::fromLatin1(raw.toHex()); } InfoHash::InfoHash(const QString &hashString) : m_valid(false) - , m_hashString(hashString) { - QByteArray raw = m_hashString.toLatin1(); - if (raw.size() == 40) - m_valid = libtorrent::from_hex(raw.constData(), 40, reinterpret_cast(&m_nativeHash[0])); + if (hashString.size() != (libtorrent::sha1_hash::size * 2)) + return; + + const QByteArray raw = QByteArray::fromHex(hashString.toLatin1()); + if (raw.size() != libtorrent::sha1_hash::size) // QByteArray::fromHex() will skip over invalid characters + return; + + m_valid = true; + m_hashString = hashString; + m_nativeHash.assign(raw.constData()); } - -InfoHash::InfoHash(const InfoHash &other) - : m_valid(other.m_valid) - , m_nativeHash(other.m_nativeHash) - , m_hashString(other.m_hashString) -{ -} - - bool InfoHash::isValid() const { return m_valid; @@ -74,25 +71,23 @@ InfoHash::operator libtorrent::sha1_hash() const return m_nativeHash; } - InfoHash::operator QString() const { return m_hashString; } - -bool InfoHash::operator==(const InfoHash &other) const +bool BitTorrent::operator==(const InfoHash &left, const InfoHash &right) { - return (m_nativeHash == other.m_nativeHash); + return (static_cast(left) + == static_cast(right)); } - -bool InfoHash::operator!=(const InfoHash &other) const +bool BitTorrent::operator!=(const InfoHash &left, const InfoHash &right) { - return (m_nativeHash != other.m_nativeHash); + return !(left == right); } -uint BitTorrent::qHash(const InfoHash &key, uint seed) +uint BitTorrent::qHash(const InfoHash &key, const uint seed) { - return qHash(static_cast(key), seed); + return ::qHash(static_cast(key), seed); } diff --git a/src/base/bittorrent/infohash.h b/src/base/bittorrent/infohash.h index dfa766ea0..7841e8bfc 100644 --- a/src/base/bittorrent/infohash.h +++ b/src/base/bittorrent/infohash.h @@ -40,14 +40,12 @@ namespace BitTorrent InfoHash(); InfoHash(const libtorrent::sha1_hash &nativeHash); InfoHash(const QString &hashString); - InfoHash(const InfoHash &other); + InfoHash(const InfoHash &other) = default; bool isValid() const; operator libtorrent::sha1_hash() const; operator QString() const; - bool operator==(const InfoHash &other) const; - bool operator!=(const InfoHash &other) const; private: bool m_valid; @@ -55,6 +53,8 @@ namespace BitTorrent QString m_hashString; }; + bool operator==(const InfoHash &left, const InfoHash &right); + bool operator!=(const InfoHash &left, const InfoHash &right); uint qHash(const InfoHash &key, uint seed); } diff --git a/src/base/bittorrent/trackerentry.cpp b/src/base/bittorrent/trackerentry.cpp index a757709bb..7cbaa64eb 100644 --- a/src/base/bittorrent/trackerentry.cpp +++ b/src/base/bittorrent/trackerentry.cpp @@ -29,14 +29,12 @@ #include "trackerentry.h" #include - -#include "base/utils/misc.h" -#include "base/utils/string.h" +#include using namespace BitTorrent; TrackerEntry::TrackerEntry(const QString &url) - : m_nativeEntry(libtorrent::announce_entry(url.toStdString())) + : m_nativeEntry(url.toStdString()) { } @@ -45,11 +43,6 @@ TrackerEntry::TrackerEntry(const libtorrent::announce_entry &nativeEntry) { } -TrackerEntry::TrackerEntry(const TrackerEntry &other) - : m_nativeEntry(other.m_nativeEntry) -{ -} - QString TrackerEntry::url() const { return QString::fromStdString(m_nativeEntry.url); @@ -74,23 +67,17 @@ TrackerEntry::Status TrackerEntry::status() const return NotWorking; } -void TrackerEntry::setTier(int value) +void TrackerEntry::setTier(const int value) { m_nativeEntry.tier = value; } -TrackerEntry &TrackerEntry::operator=(const TrackerEntry &other) -{ - this->m_nativeEntry = other.m_nativeEntry; - return *this; -} - -bool TrackerEntry::operator==(const TrackerEntry &other) const -{ - return (QUrl(url()) == QUrl(other.url())); -} - libtorrent::announce_entry TrackerEntry::nativeEntry() const { return m_nativeEntry; } + +bool BitTorrent::operator==(const TrackerEntry &left, const TrackerEntry &right) +{ + return (QUrl(left.url()) == QUrl(right.url())); +} diff --git a/src/base/bittorrent/trackerentry.h b/src/base/bittorrent/trackerentry.h index e2930e0a9..8a4afd0be 100644 --- a/src/base/bittorrent/trackerentry.h +++ b/src/base/bittorrent/trackerentry.h @@ -52,21 +52,22 @@ namespace BitTorrent TrackerEntry(const QString &url); TrackerEntry(const libtorrent::announce_entry &nativeEntry); - TrackerEntry(const TrackerEntry &other); + TrackerEntry(const TrackerEntry &other) = default; + TrackerEntry &operator=(const TrackerEntry &other) = default; QString url() const; - int tier() const; Status status() const; + int tier() const; void setTier(int value); - TrackerEntry &operator=(const TrackerEntry &other); - bool operator==(const TrackerEntry &other) const; libtorrent::announce_entry nativeEntry() const; private: libtorrent::announce_entry m_nativeEntry; }; + + bool operator==(const TrackerEntry &left, const TrackerEntry &right); } #endif // BITTORRENT_TRACKERENTRY_H