summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorнаб <nabijaczleweli@nabijaczleweli.xyz>2025-01-23 20:21:16 +0100
committerJulian Andres Klode <julian.klode@canonical.com>2025-02-14 19:45:12 +0100
commit7f4c339d6fe19ff7e26144e373a6e5c73344539f (patch)
treeb2463e55e2e04b4e46cc4771d9f2b53e72f4e75b
parent2499d9ff0c55a445a62643028a9d3bc9faf1195a (diff)
APT::String::* should take string_views
-rw-r--r--apt-pkg/contrib/strutl.cc31
-rw-r--r--apt-pkg/contrib/strutl.h8
2 files changed, 13 insertions, 26 deletions
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<std::string> list, const std::string &sep)
+std::string Join(std::vector<std::string> 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<std::string> 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<std::string> list, const std::string_view &sep);
// Returns string display length honoring multi-byte characters
APT_PUBLIC size_t DisplayLength(std::string_view str);
}