diff --git a/src/orm/tiny/utils/string.cpp b/src/orm/tiny/utils/string.cpp index 9abf606e9..2ae54dc26 100644 --- a/src/orm/tiny/utils/string.cpp +++ b/src/orm/tiny/utils/string.cpp @@ -1,5 +1,9 @@ #include "orm/tiny/utils/string.hpp" +#include + +#include + #include "orm/constants.hpp" using Orm::Constants::DASH; @@ -28,7 +32,7 @@ namespace using SnakeCache = std::unordered_map; /*! Snake cache for already computed strings. */ - Q_GLOBAL_STATIC(SnakeCache, snakeCache); + Q_GLOBAL_STATIC(SnakeCache, snakeCache); // NOLINT(readability-redundant-member-init) } // namespace QString String::snake(QString string, const QChar delimiter) @@ -70,7 +74,7 @@ namespace using StudlyCache = std::unordered_map; /*! Studly cache for already computed strings. */ - Q_GLOBAL_STATIC(StudlyCache, studlyCache); + Q_GLOBAL_STATIC(StudlyCache, studlyCache); // NOLINT(readability-redundant-member-init) } // namespace QString String::studly(QString string) @@ -147,35 +151,27 @@ bool String::isNumber(const QString &string, const bool allowFloating) } #ifndef TINYORM_DISABLE_TOM -/*! Split a string by the given width (not in the middle of a word). */ -std::vector String::splitStringByWidth(const QString &string, const int width) +namespace { - // Nothing to split - if (string.size() <= width) - return {string}; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + using StringViewType = QStringView; +#else + using StringViewType = QStringRef; +#endif - std::vector lines; - QString line; + /*! Split the token to multiple lines by the given width. */ + bool splitLongToken(StringViewType token, const int width, QString &line, + std::vector &lines) + { + auto shouldContinue = false; - for (auto token : string.tokenize(SPACE)) { - /* If there is still a space on the line then append the token */ - if (line.size() + token.size() + 1 <= width) { - // Don't prepend the space at beginning of an empty line - if (!line.isEmpty()) - line.append(' '); - - line.append(token); - continue; - } - - /* If a token is longer than the width or an empty space on the current line */ const auto spaceSize = line.isEmpty() ? 0 : 1; if (const auto emptySpace = width - line.size() + spaceSize; token.size() > emptySpace ) { // If on the line is still more than 30% of an empty space, use/fill it - if (emptySpace > llround(static_cast(width) * 0.3)) { + if (emptySpace > std::llround(static_cast(width) * 0.3F)) { // Position where to split the token auto pos = width - line.size() - spaceSize; @@ -208,12 +204,45 @@ std::vector String::splitStringByWidth(const QString &string, const int // Push to lines lines.emplace_back(std::move(line)); // Start a new line - line.clear(); + line.clear(); // NOLINT(bugprone-use-after-move) } + shouldContinue = true; + } + + return shouldContinue; + } +} // namespace + +/*! Split a string by the given width (not in the middle of a word). */ +std::vector String::splitStringByWidth(const QString &string, const int width) +{ + // Nothing to split + if (string.size() <= width) + return {string}; + + std::vector lines; + QString line; + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + for (auto token : string.tokenize(SPACE)) { +#else + for (auto token : string.splitRef(SPACE)) { // NOLINT(performance-for-range-copy) clazy:exclude=range-loop +#endif + /* If there is still a space on the line then append the token */ + if (line.size() + token.size() + 1 <= width) { + // Don't prepend the space at beginning of an empty line + if (!line.isEmpty()) + line.append(SPACE); + + line.append(token); continue; } + /* If a token is longer than the width or an empty space on the current line */ + if (splitLongToken(token, width, line, lines)) + continue; + // No space on the line, push to lines and start a new line lines.emplace_back(std::move(line)); diff --git a/tom/include/tom/concerns/callscommands.hpp b/tom/include/tom/concerns/callscommands.hpp index ad218cc46..1acfd67b5 100644 --- a/tom/include/tom/concerns/callscommands.hpp +++ b/tom/include/tom/concerns/callscommands.hpp @@ -29,8 +29,8 @@ namespace Concerns public: /*! Default constructor. */ inline CallsCommands() = default; - /*! Default destructor. */ - inline ~CallsCommands() = default; + /*! Virtual destructor. */ + inline virtual ~CallsCommands() = default; /*! Call another console command. */ inline int call(const QString &command, QStringList &&arguments = {}) const; diff --git a/tom/include/tom/concerns/confirmable.hpp b/tom/include/tom/concerns/confirmable.hpp index a7a9464f9..f11255ec8 100644 --- a/tom/include/tom/concerns/confirmable.hpp +++ b/tom/include/tom/concerns/confirmable.hpp @@ -34,8 +34,8 @@ namespace Concerns public: /*! Constructor (int param. to avoid interpret it as copy ctor). */ Confirmable(Command &command, int); - /*! Default destructor. */ - inline ~Confirmable() = default; + /*! Virtual destructor. */ + inline virtual ~Confirmable() = default; /*! Confirm before proceeding with the action (only in production environment). */ bool confirmToProceed( diff --git a/tom/include/tom/concerns/interactswithio.hpp b/tom/include/tom/concerns/interactswithio.hpp index 4da140bd8..6107ba6b6 100644 --- a/tom/include/tom/concerns/interactswithio.hpp +++ b/tom/include/tom/concerns/interactswithio.hpp @@ -40,8 +40,8 @@ namespace Concerns /*! Constructor. */ explicit InteractsWithIO(const QCommandLineParser &parser); - /*! Default destructor. */ - ~InteractsWithIO(); + /*! Virtual destructor. */ + virtual ~InteractsWithIO(); /*! Base enum for the verbosity levels. */ enum struct Verbosity { diff --git a/tom/include/tom/concerns/printsoptions.hpp b/tom/include/tom/concerns/printsoptions.hpp index 849487348..f70e89bfe 100644 --- a/tom/include/tom/concerns/printsoptions.hpp +++ b/tom/include/tom/concerns/printsoptions.hpp @@ -29,8 +29,8 @@ namespace Concerns public: /*! Constructor (int param. to avoid interpret it as copy ctor). */ PrintsOptions(const Commands::Command &command, int); - /*! Default destructor. */ - inline ~PrintsOptions() = default; + /*! Virtual destructor. */ + inline virtual ~PrintsOptions() = default; /*! Print options section. */ int printOptionsSection(bool commonOptions) const; diff --git a/tom/include/tom/migrator.hpp b/tom/include/tom/migrator.hpp index a9244d26b..c0e15db4b 100644 --- a/tom/include/tom/migrator.hpp +++ b/tom/include/tom/migrator.hpp @@ -139,7 +139,7 @@ namespace Tom /*! The database connection resolver instance. */ std::shared_ptr m_resolver; /*! The name of the database connection to use. */ - QString m_connection; + QString m_connection {}; /*! Reference to the migrations vector to process. */ const std::vector> &m_migrations; diff --git a/tom/include/tom/terminal.hpp b/tom/include/tom/terminal.hpp index 600dea5cf..795c4b6eb 100644 --- a/tom/include/tom/terminal.hpp +++ b/tom/include/tom/terminal.hpp @@ -76,9 +76,9 @@ namespace Tom #endif /*! Cache for detected ansi output. */ - mutable std::unordered_map m_isAnsiOutput; + mutable std::unordered_map m_isAnsiOutput {}; /*! Cache for detected ansi output, wide version. */ - mutable std::unordered_map m_isAnsiWOutput; + mutable std::unordered_map m_isAnsiWOutput {}; /*! Current terminal width. */ int m_lastWidth = 80;