From 7f4c339d6fe19ff7e26144e373a6e5c73344539f Mon Sep 17 00:00:00 2001 From: наб Date: Thu, 23 Jan 2025 20:21:16 +0100 Subject: APT::String::* should take string_views --- apt-pkg/contrib/strutl.cc | 31 +++++++++---------------------- apt-pkg/contrib/strutl.h | 8 ++++---- 2 files changed, 13 insertions(+), 26 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 5d91e5f11..a4df85710 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -56,43 +56,30 @@ using namespace std; // --------------------------------------------------------------------- namespace APT { namespace String { -std::string Strip(const std::string &str) +std::string Strip(std::string_view str) { - // ensure we have at least one character - if (str.empty() == true) - return str; - - char const * const s = str.c_str(); - size_t start = 0; - for (; isspace(s[start]) != 0; ++start) - ; // find the first not-space - - // string contains only whitespaces - if (s[start] == '\0') - return ""; - - size_t end = str.length() - 1; - for (; isspace(s[end]) != 0; --end) - ; // find the last not-space - - return str.substr(start, end - start + 1); + while (!str.empty() && isspace(str[0])) + str.remove_prefix(1); + while (!str.empty() && isspace(str.back())) + str.remove_suffix(1); + return std::string{str}; } -bool Endswith(const std::string &s, const std::string &end) +bool Endswith(const std::string_view &s, const std::string_view &end) { if (end.size() > s.size()) return false; return (s.compare(s.size() - end.size(), end.size(), end) == 0); } -bool Startswith(const std::string &s, const std::string &start) +bool Startswith(const std::string_view &s, const std::string_view &start) { if (start.size() > s.size()) return false; return (s.compare(0, start.size(), start) == 0); } -std::string Join(std::vector list, const std::string &sep) +std::string Join(std::vector list, const std::string_view &sep) { std::ostringstream oss; for (auto it = list.begin(); it != list.end(); it++) diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 9db908c7f..7e6aba272 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -42,10 +42,10 @@ namespace { namespace APT { namespace String { - APT_PUBLIC std::string Strip(const std::string &s); - APT_PUBLIC bool Endswith(const std::string &s, const std::string &ending); - APT_PUBLIC bool Startswith(const std::string &s, const std::string &starting); - APT_PUBLIC std::string Join(std::vector list, const std::string &sep); + APT_PUBLIC std::string Strip(std::string_view s); + APT_PUBLIC bool Endswith(const std::string_view &s, const std::string_view &ending); + APT_PUBLIC bool Startswith(const std::string_view &s, const std::string_view &starting); + APT_PUBLIC std::string Join(std::vector list, const std::string_view &sep); // Returns string display length honoring multi-byte characters APT_PUBLIC size_t DisplayLength(std::string_view str); } -- cgit v1.2.3-70-g09d2