From 552ff0489de3194c1e7b818574194e8c48c7744d Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 3 Jan 2021 16:53:24 +0300 Subject: [PATCH] Use nested namespaces definition syntax --- src/base/utils/bytearray.h | 15 ++--- src/base/utils/foreignapps.h | 21 +++---- src/base/utils/fs.h | 69 +++++++++++----------- src/base/utils/gzip.h | 9 +-- src/base/utils/io.h | 67 ++++++++++----------- src/base/utils/misc.h | 109 +++++++++++++++++------------------ src/base/utils/net.h | 31 +++++----- src/base/utils/password.h | 23 ++++---- src/base/utils/random.h | 7 +-- src/base/utils/string.h | 79 ++++++++++++------------- 10 files changed, 200 insertions(+), 230 deletions(-) diff --git a/src/base/utils/bytearray.h b/src/base/utils/bytearray.h index 1c1dd2dbb..c61afb931 100644 --- a/src/base/utils/bytearray.h +++ b/src/base/utils/bytearray.h @@ -33,15 +33,12 @@ class QByteArray; -namespace Utils +namespace Utils::ByteArray { - namespace ByteArray - { - // Mimic QString::splitRef(sep, behavior) - QVector splitToViews(const QByteArray &in, const QByteArray &sep, const QString::SplitBehavior behavior = QString::KeepEmptyParts); + // Mimic QString::splitRef(sep, behavior) + QVector splitToViews(const QByteArray &in, const QByteArray &sep, const QString::SplitBehavior behavior = QString::KeepEmptyParts); - // Mimic QByteArray::mid(pos, len) but instead of returning a full-copy, - // we only return a partial view - const QByteArray midView(const QByteArray &in, int pos, int len = -1); - } + // Mimic QByteArray::mid(pos, len) but instead of returning a full-copy, + // we only return a partial view + const QByteArray midView(const QByteArray &in, int pos, int len = -1); } diff --git a/src/base/utils/foreignapps.h b/src/base/utils/foreignapps.h index 2ec5d1378..a99db5c08 100644 --- a/src/base/utils/foreignapps.h +++ b/src/base/utils/foreignapps.h @@ -33,21 +33,18 @@ #include "base/utils/version.h" -namespace Utils +namespace Utils::ForeignApps { - namespace ForeignApps + struct PythonInfo { - struct PythonInfo - { - using Version = Utils::Version; + using Version = Utils::Version; - bool isValid() const; - bool isSupportedVersion() const; + bool isValid() const; + bool isSupportedVersion() const; - QString executableName; - Version version; - }; + QString executableName; + Version version; + }; - PythonInfo pythonInfo(); - } + PythonInfo pythonInfo(); } diff --git a/src/base/utils/fs.h b/src/base/utils/fs.h index 3f7e5006f..253bca628 100644 --- a/src/base/utils/fs.h +++ b/src/base/utils/fs.h @@ -34,47 +34,44 @@ #include -namespace Utils +namespace Utils::Fs { - namespace Fs - { - /** - * Converts a path to a string suitable for display. - * This function makes sure the directory separator used is consistent - * with the OS being run. - */ - QString toNativePath(const QString &path); - /** - * Converts a path to a string suitable for processing. - * This function makes sure the directory separator used is independent - * from the OS being run so it is the same on all supported platforms. - * Slash ('/') is used as "uniform" directory separator. - */ - QString toUniformPath(const QString &path); + /** + * Converts a path to a string suitable for display. + * This function makes sure the directory separator used is consistent + * with the OS being run. + */ + QString toNativePath(const QString &path); + /** + * Converts a path to a string suitable for processing. + * This function makes sure the directory separator used is independent + * from the OS being run so it is the same on all supported platforms. + * Slash ('/') is used as "uniform" directory separator. + */ + QString toUniformPath(const QString &path); - QString fileExtension(const QString &filename); - QString fileName(const QString &filePath); - QString folderName(const QString &filePath); - qint64 computePathSize(const QString &path); - bool sameFiles(const QString &path1, const QString &path2); - QString toValidFileSystemName(const QString &name, bool allowSeparators = false - , const QString &pad = QLatin1String(" ")); - bool isValidFileSystemName(const QString &name, bool allowSeparators = false); - qint64 freeDiskSpaceOnPath(const QString &path); - QString branchPath(const QString &filePath, QString *removed = nullptr); - bool sameFileNames(const QString &first, const QString &second); - QString expandPath(const QString &path); - QString expandPathAbs(const QString &path); - bool isRegularFile(const QString &path); + QString fileExtension(const QString &filename); + QString fileName(const QString &filePath); + QString folderName(const QString &filePath); + qint64 computePathSize(const QString &path); + bool sameFiles(const QString &path1, const QString &path2); + QString toValidFileSystemName(const QString &name, bool allowSeparators = false + , const QString &pad = QLatin1String(" ")); + bool isValidFileSystemName(const QString &name, bool allowSeparators = false); + qint64 freeDiskSpaceOnPath(const QString &path); + QString branchPath(const QString &filePath, QString *removed = nullptr); + bool sameFileNames(const QString &first, const QString &second); + QString expandPath(const QString &path); + QString expandPathAbs(const QString &path); + bool isRegularFile(const QString &path); - bool smartRemoveEmptyFolderTree(const QString &path); - bool forceRemove(const QString &filePath); - void removeDirRecursive(const QString &path); + bool smartRemoveEmptyFolderTree(const QString &path); + bool forceRemove(const QString &filePath); + void removeDirRecursive(const QString &path); - QString tempPath(); + QString tempPath(); #if !defined Q_OS_HAIKU - bool isNetworkFileSystem(const QString &path); + bool isNetworkFileSystem(const QString &path); #endif - } } diff --git a/src/base/utils/gzip.h b/src/base/utils/gzip.h index 90a1e85bc..9ca8b568a 100644 --- a/src/base/utils/gzip.h +++ b/src/base/utils/gzip.h @@ -31,11 +31,8 @@ class QByteArray; -namespace Utils +namespace Utils::Gzip { - namespace Gzip - { - QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr); - QByteArray decompress(const QByteArray &data, bool *ok = nullptr); - } + QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr); + QByteArray decompress(const QByteArray &data, bool *ok = nullptr); } diff --git a/src/base/utils/io.h b/src/base/utils/io.h index 2cd3c9573..9d69efa59 100644 --- a/src/base/utils/io.h +++ b/src/base/utils/io.h @@ -34,47 +34,44 @@ class QByteArray; class QFileDevice; -namespace Utils +namespace Utils::IO { - namespace IO + // A wrapper class that satisfy LegacyOutputIterator requirement + class FileDeviceOutputIterator { - // A wrapper class that satisfy LegacyOutputIterator requirement - class FileDeviceOutputIterator - { - public: - // std::iterator_traits - using iterator_category = std::output_iterator_tag; - using difference_type = void; - using value_type = void; - using pointer = void; - using reference = void; + public: + // std::iterator_traits + using iterator_category = std::output_iterator_tag; + using difference_type = void; + using value_type = void; + using pointer = void; + using reference = void; - explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024)); - FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default; - ~FileDeviceOutputIterator(); + explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024)); + FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default; + ~FileDeviceOutputIterator(); - // mimic std::ostream_iterator behavior - FileDeviceOutputIterator &operator=(char c); + // mimic std::ostream_iterator behavior + FileDeviceOutputIterator &operator=(char c); - constexpr FileDeviceOutputIterator &operator*() - { - return *this; - } + constexpr FileDeviceOutputIterator &operator*() + { + return *this; + } - constexpr FileDeviceOutputIterator &operator++() - { - return *this; - } + constexpr FileDeviceOutputIterator &operator++() + { + return *this; + } - constexpr FileDeviceOutputIterator &operator++(int) - { - return *this; - } + constexpr FileDeviceOutputIterator &operator++(int) + { + return *this; + } - private: - QFileDevice *m_device; - std::shared_ptr m_buffer; - int m_bufferSize; - }; - } + private: + QFileDevice *m_device; + std::shared_ptr m_buffer; + int m_bufferSize; + }; } diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index 4b9863039..7f7c3e7e2 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -41,69 +41,66 @@ enum class ShutdownDialogAction; /* Miscellaneous functions that can be useful */ -namespace Utils +namespace Utils::Misc { - namespace Misc + // use binary prefix standards from IEC 60027-2 + // see http://en.wikipedia.org/wiki/Kilobyte + enum class SizeUnit { - // use binary prefix standards from IEC 60027-2 - // see http://en.wikipedia.org/wiki/Kilobyte - enum class SizeUnit - { - Byte, // 1024^0, - KibiByte, // 1024^1, - MebiByte, // 1024^2, - GibiByte, // 1024^3, - TebiByte, // 1024^4, - PebiByte, // 1024^5, - ExbiByte // 1024^6, - // int64 is used for sizes and thus the next units can not be handled - // ZebiByte, // 1024^7, - // YobiByte, // 1024^8 - }; - - QString parseHtmlLinks(const QString &rawText); - - void shutdownComputer(const ShutdownDialogAction &action); - - QString osName(); - QString boostVersionString(); - QString libtorrentVersionString(); - QString opensslVersionString(); - QString zlibVersionString(); - - QString unitString(SizeUnit unit, bool isSpeed = false); - - // return the best user friendly storage unit (B, KiB, MiB, GiB, TiB) - // value must be given in bytes - QString friendlyUnit(qint64 bytesValue, bool isSpeed = false); - int friendlyUnitPrecision(SizeUnit unit); - qint64 sizeInBytes(qreal size, SizeUnit unit); - - bool isPreviewable(const QString &extension); - - // Take a number of seconds and return a user-friendly - // time duration like "1d 2h 10m". - QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1); - QString getUserIDString(); + Byte, // 1024^0, + KibiByte, // 1024^1, + MebiByte, // 1024^2, + GibiByte, // 1024^3, + TebiByte, // 1024^4, + PebiByte, // 1024^5, + ExbiByte // 1024^6, + // int64 is used for sizes and thus the next units can not be handled + // ZebiByte, // 1024^7, + // YobiByte, // 1024^8 + }; + + QString parseHtmlLinks(const QString &rawText); + + void shutdownComputer(const ShutdownDialogAction &action); + + QString osName(); + QString boostVersionString(); + QString libtorrentVersionString(); + QString opensslVersionString(); + QString zlibVersionString(); + + QString unitString(SizeUnit unit, bool isSpeed = false); + + // return the best user friendly storage unit (B, KiB, MiB, GiB, TiB) + // value must be given in bytes + QString friendlyUnit(qint64 bytesValue, bool isSpeed = false); + int friendlyUnitPrecision(SizeUnit unit); + qint64 sizeInBytes(qreal size, SizeUnit unit); + + bool isPreviewable(const QString &extension); + + // Take a number of seconds and return a user-friendly + // time duration like "1d 2h 10m". + QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1); + QString getUserIDString(); #ifdef Q_OS_WIN - QString windowsSystemPath(); + QString windowsSystemPath(); - template - T loadWinAPI(const QString &source, const char *funcName) - { - QString path = windowsSystemPath(); - if (!path.endsWith('\\')) - path += '\\'; + template + T loadWinAPI(const QString &source, const char *funcName) + { + QString path = windowsSystemPath(); + if (!path.endsWith('\\')) + path += '\\'; - path += source; + path += source; - auto pathWchar = std::make_unique(path.length() + 1); - path.toWCharArray(pathWchar.get()); + auto pathWchar = std::make_unique(path.length() + 1); + path.toWCharArray(pathWchar.get()); - return reinterpret_cast( - ::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName)); - } -#endif // Q_OS_WIN + return reinterpret_cast( + ::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName)); } +#endif // Q_OS_WIN } diff --git a/src/base/utils/net.h b/src/base/utils/net.h index 82a4a0286..f9712d59c 100644 --- a/src/base/utils/net.h +++ b/src/base/utils/net.h @@ -35,24 +35,21 @@ class QSslCertificate; class QSslKey; class QString; -namespace Utils +namespace Utils::Net { - namespace Net - { - using Subnet = QPair; + using Subnet = QPair; - bool isValidIP(const QString &ip); - Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr); - bool canParseSubnet(const QString &subnetStr); - bool isLoopbackAddress(const QHostAddress &addr); - bool isIPInRange(const QHostAddress &addr, const QVector &subnets); - QString subnetToString(const Subnet &subnet); - QHostAddress canonicalIPv6Addr(const QHostAddress &addr); + bool isValidIP(const QString &ip); + Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr); + bool canParseSubnet(const QString &subnetStr); + bool isLoopbackAddress(const QHostAddress &addr); + bool isIPInRange(const QHostAddress &addr, const QVector &subnets); + QString subnetToString(const Subnet &subnet); + QHostAddress canonicalIPv6Addr(const QHostAddress &addr); - const int MAX_SSL_FILE_SIZE = 1024 * 1024; - QList loadSSLCertificate(const QByteArray &data); - bool isSSLCertificatesValid(const QByteArray &data); - QSslKey loadSSLKey(const QByteArray &data); - bool isSSLKeyValid(const QByteArray &data); - } + const int MAX_SSL_FILE_SIZE = 1024 * 1024; + QList loadSSLCertificate(const QByteArray &data); + bool isSSLCertificatesValid(const QByteArray &data); + QSslKey loadSSLKey(const QByteArray &data); + bool isSSLKeyValid(const QByteArray &data); } diff --git a/src/base/utils/password.h b/src/base/utils/password.h index b7b2320f2..3ad6d8578 100644 --- a/src/base/utils/password.h +++ b/src/base/utils/password.h @@ -31,21 +31,18 @@ class QByteArray; class QString; -namespace Utils +namespace Utils::Password { - namespace Password - { - // Implements constant-time comparison to protect against timing attacks - // Taken from https://crackstation.net/hashing-security.htm - bool slowEquals(const QByteArray &a, const QByteArray &b); + // Implements constant-time comparison to protect against timing attacks + // Taken from https://crackstation.net/hashing-security.htm + bool slowEquals(const QByteArray &a, const QByteArray &b); - namespace PBKDF2 - { - QByteArray generate(const QString &password); - QByteArray generate(const QByteArray &password); + namespace PBKDF2 + { + QByteArray generate(const QString &password); + QByteArray generate(const QByteArray &password); - bool verify(const QByteArray &secret, const QString &password); - bool verify(const QByteArray &secret, const QByteArray &password); - } + bool verify(const QByteArray &secret, const QString &password); + bool verify(const QByteArray &secret, const QByteArray &password); } } diff --git a/src/base/utils/random.h b/src/base/utils/random.h index 93f1f278c..bf004660d 100644 --- a/src/base/utils/random.h +++ b/src/base/utils/random.h @@ -31,10 +31,7 @@ #include #include -namespace Utils +namespace Utils::Random { - namespace Random - { - uint32_t rand(uint32_t min = 0, uint32_t max = std::numeric_limits::max()); - } + uint32_t rand(uint32_t min = 0, uint32_t max = std::numeric_limits::max()); } diff --git a/src/base/utils/string.h b/src/base/utils/string.h index b2ec83599..3d3c3aa72 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -37,59 +37,56 @@ class QStringRef; -namespace Utils +namespace Utils::String { - namespace String + QString fromDouble(double n, int precision); + + int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity); + template + bool naturalLessThan(const QString &left, const QString &right) { - QString fromDouble(double n, int precision); + return (naturalCompare(left, right, caseSensitivity) < 0); + } - int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity); - template - bool naturalLessThan(const QString &left, const QString &right) - { - return (naturalCompare(left, right, caseSensitivity) < 0); - } + QString wildcardToRegex(const QString &pattern); - QString wildcardToRegex(const QString &pattern); + template + T unquote(const T &str, const QString "es = QChar('"')) + { + if (str.length() < 2) return str; - template - T unquote(const T &str, const QString "es = QChar('"')) + for (const QChar quote : quotes) { - if (str.length() < 2) return str; - - for (const QChar quote : quotes) - { - if (str.startsWith(quote) && str.endsWith(quote)) - return str.mid(1, (str.length() - 2)); - } - - return str; + if (str.startsWith(quote) && str.endsWith(quote)) + return str.mid(1, (str.length() - 2)); } - bool parseBool(const QString &string, bool defaultValue); + return str; + } + + bool parseBool(const QString &string, bool defaultValue); - QString join(const QVector &strings, const QString &separator); + QString join(const QVector &strings, const QString &separator); - template , int> = 0> - QString fromEnum(const T &value) - { - static_assert(std::is_same_v>, - "Enumeration underlying type has to be int."); + template , int> = 0> + QString fromEnum(const T &value) + { + static_assert(std::is_same_v>, + "Enumeration underlying type has to be int."); - const auto metaEnum = QMetaEnum::fromType(); - return QString::fromLatin1(metaEnum.valueToKey(static_cast(value))); - } + const auto metaEnum = QMetaEnum::fromType(); + return QString::fromLatin1(metaEnum.valueToKey(static_cast(value))); + } - template , int> = 0> - T toEnum(const QString &serializedValue, const T &defaultValue) - { - static_assert(std::is_same_v>, - "Enumeration underlying type has to be int."); + template , int> = 0> + T toEnum(const QString &serializedValue, const T &defaultValue) + { + static_assert(std::is_same_v>, + "Enumeration underlying type has to be int."); - const auto metaEnum = QMetaEnum::fromType(); - bool ok = false; - const T value = static_cast(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok)); - return (ok ? value : defaultValue); - } + const auto metaEnum = QMetaEnum::fromType(); + bool ok = false; + const T value = static_cast(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok)); + return (ok ? value : defaultValue); } }