diff options
| author | Simon Johnsson <simon.johnsson@canonical.com> | 2026-02-19 12:11:21 +0100 |
|---|---|---|
| committer | Julian Andres Klode <jak@debian.org> | 2026-04-07 09:39:14 +0000 |
| commit | ad4800b814f1abbe561a962305e6d4c03401a73d (patch) | |
| tree | 8738ee2b86cd26e27ec39391d040b5d90782af85 /apt-private | |
| parent | e2e4e3ced75bb5e9db28cc35b6ebf371e8e5be94 (diff) | |
Optimize ShortenCommand
Diffstat (limited to 'apt-private')
| -rw-r--r-- | apt-private/private-history.cc | 22 |
1 files 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) |
