From ad4800b814f1abbe561a962305e6d4c03401a73d Mon Sep 17 00:00:00 2001 From: Simon Johnsson Date: Thu, 19 Feb 2026 12:11:21 +0100 Subject: Optimize ShortenCommand --- apt-private/private-history.cc | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/apt-private/private-history.cc b/apt-private/private-history.cc index edce7b227..a2f7e4c02 100644 --- a/apt-private/private-history.cc +++ b/apt-private/private-history.cc @@ -101,18 +101,22 @@ Change APT::Internal::InvertChange(const Change &change) // ShortenCommand - Take a command and shorten it such that it adheres // to the given maximum length. -static std::string ShortenCommand(const std::string &cmd, const std::size_t maxLen) +static std::string ShortenCommand(std::string cmd, const std::size_t maxLen) { - std::string shortenedCmd = cmd; - if (shortenedCmd.starts_with("/usr/bin/")) - shortenedCmd = shortenedCmd.substr(9); - else if (shortenedCmd.starts_with("apt ")) - shortenedCmd = shortenedCmd.substr(4); + if (cmd.starts_with("/usr/bin/")) + cmd.erase(0,9); + else if (cmd.starts_with("apt ")) + cmd.erase(0, 4); - if (shortenedCmd.length() > maxLen - 3) - return shortenedCmd.substr(0, maxLen - 4) + "..."; + if (cmd.length() <= maxLen) + return cmd; - return shortenedCmd; + if (maxLen <= 3) + return cmd.substr(0, maxLen); + + cmd.resize(maxLen - 3); + cmd.append("..."); + return cmd; } static std::string LocalizeKindToString(const Kind &kind) -- cgit v1.2.3-70-g09d2