From a4020cebd499c725d75fdd9844938bd1b00a3fde Mon Sep 17 00:00:00 2001 From: Xavier Michelon Date: Thu, 18 Apr 2024 14:53:03 +0200 Subject: [PATCH] chore: do not use C++ 20 std::ranges. --- .../bridge-gui/bridge-gui/CommandLine.cpp | 19 ------------------- .../bridge-gui/bridgepp/Test/TestCLI.cpp | 10 ++++++++++ .../bridgepp/bridgepp/CLI/CLIUtils.cpp | 18 +++++++++++++++++- .../bridgepp/bridgepp/CLI/CLIUtils.h | 1 + .../bridgepp/bridgepp/SessionID/SessionID.cpp | 4 ++++ .../bridgepp/bridgepp/SessionID/SessionID.h | 4 ++++ 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp b/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp index 51f9d649..0bd694c5 100644 --- a/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp +++ b/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp @@ -30,8 +30,6 @@ QString const hyphenatedWindowFlag = "--no-window"; ///< The no-window command-l QString const hyphenatedSoftwareRendererFlag = "--software-renderer"; ///< The 'software-renderer' command-line flag. enable software rendering for a single execution QString const hyphenatedSetSoftwareRendererFlag = "--set-software-renderer"; ///< The 'set-software-renderer' command-line flag. Software rendering will be used for all subsequent executions of the application. QString const hyphenatedSetHardwareRendererFlag = "--set-hardware-renderer"; ///< The 'set-hardware-renderer' command-line flag. Hardware rendering will be used for all subsequent executions of the application. -QString const sessionIDFlag = "session-id"; -QString const hyphenatedSessionIDFlag = "--" + sessionIDFlag; //**************************************************************************************************************************************************** /// \brief Parse the log level from the command-line arguments. @@ -50,23 +48,6 @@ Log::Level parseLogLevel(QStringList const &args) { return level; } -//**************************************************************************************************************************************************** -/// \brief Return the most recent sessionID parsed in command-line arguments -/// -/// \param[in] args The command-line arguments. -/// \return The most recent sessionID in the list. If the list is empty, a new sessionID is created. -//**************************************************************************************************************************************************** -QString mostRecentSessionID(QStringList const& args) { - QStringList const sessionIDs = parseGoCLIStringArgument(args, {sessionIDFlag}); - if (sessionIDs.isEmpty()) { - return newSessionID(); - } - - return std::ranges::max(sessionIDs, [](QString const &lhs, QString const &rhs) -> bool { - return sessionIDToDateTime(lhs) < sessionIDToDateTime(rhs); - }); -} - } // anonymous namespace //**************************************************************************************************************************************************** diff --git a/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp b/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp index d3f064a8..8d122c3f 100644 --- a/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp +++ b/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp @@ -17,6 +17,7 @@ #include +#include #include @@ -76,3 +77,12 @@ TEST(CLI, cliArgsToStringList) { EXPECT_EQ(cliArgsToStringList(argc,argv), strList); EXPECT_EQ(cliArgsToStringList(0, nullptr), QStringList {}); } + +TEST(CLI, mostRecentSessionID) { + QStringList const sessionIDs { "20220411_155931148", "20230411_155931148", "20240411_155931148" }; + EXPECT_EQ(mostRecentSessionID({ hyphenatedSessionIDFlag, sessionIDs[0] }), sessionIDs[0]); + EXPECT_EQ(mostRecentSessionID({ hyphenatedSessionIDFlag, sessionIDs[1], hyphenatedSessionIDFlag, sessionIDs[2] }), sessionIDs[2]); + EXPECT_EQ(mostRecentSessionID({ hyphenatedSessionIDFlag, sessionIDs[2], hyphenatedSessionIDFlag, sessionIDs[1] }), sessionIDs[2]); + EXPECT_EQ(mostRecentSessionID({ hyphenatedSessionIDFlag, sessionIDs[1], hyphenatedSessionIDFlag, sessionIDs[2], hyphenatedSessionIDFlag, + sessionIDs[0] }), sessionIDs[2]); +} diff --git a/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp index 31b53ff2..192324b0 100644 --- a/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp +++ b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp @@ -16,7 +16,7 @@ // along with Proton Mail Bridge. If not, see . #include "CLIUtils.h" - +#include "../SessionID/SessionID.h" namespace bridgepp { @@ -89,4 +89,20 @@ QStringList cliArgsToStringList(int argc, char **argv) { return result; } +//**************************************************************************************************************************************************** +/// \param[in] args The command-line arguments. +/// \return The most recent sessionID in the list. If the list is empty, a new sessionID is created. +//**************************************************************************************************************************************************** + QString mostRecentSessionID(QStringList const& args) { + QStringList const sessionIDs = parseGoCLIStringArgument(args, {sessionIDFlag}); + if (sessionIDs.isEmpty()) { + return newSessionID(); + } + + return *std::max_element(sessionIDs.constBegin(), sessionIDs.constEnd(), [](QString const &lhs, QString const &rhs) -> bool { + return sessionIDToDateTime(lhs) < sessionIDToDateTime(rhs); + }); +} + + } // namespace bridgepp diff --git a/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h index fd7600ca..09ed00a3 100644 --- a/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h +++ b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h @@ -23,6 +23,7 @@ namespace bridgepp { QStringList stripStringParameterFromCommandLine(QString const ¶mName, QStringList const &commandLineParams); ///< Remove a string parameter from a list of command-line parameters. QStringList parseGoCLIStringArgument(QStringList const &args, QStringList const ¶mNames); ///< Parse a command-line string argument as expected by go's CLI package. QStringList cliArgsToStringList(int argc, char **argv); ///< Converts C-style command-line arguments to a string list. +QString mostRecentSessionID(QStringList const& args); ///< Returns the most recent sessionID parsed in command-line arguments. } diff --git a/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.cpp b/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.cpp index cb708d9e..f716e0c9 100644 --- a/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.cpp +++ b/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.cpp @@ -32,6 +32,10 @@ QString const dateTimeFormat = "yyyyMMdd_hhmmsszzz"; ///< The format string for namespace bridgepp { +QString const sessionIDFlag = "session-id"; +QString const hyphenatedSessionIDFlag = "--" + sessionIDFlag; + + //**************************************************************************************************************************************************** /// \return a new session ID based on the current local date/time //**************************************************************************************************************************************************** diff --git a/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.h b/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.h index 4c134a9b..a8ac6efb 100644 --- a/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.h +++ b/internal/frontend/bridge-gui/bridgepp/bridgepp/SessionID/SessionID.h @@ -23,6 +23,10 @@ namespace bridgepp { +extern QString const sessionIDFlag; ///< The sessionID command-line flag (without hyphens) +extern QString const hyphenatedSessionIDFlag; ///< The sessionID command-line flag (with two hyphens) + + QString newSessionID(); ///< Create a new sessions QDateTime sessionIDToDateTime(QString const &sessionID); ///< Parse the date/time from a sessionID.