summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorнаб <nabijaczleweli@nabijaczleweli.xyz>2025-01-23 20:37:35 +0100
committerJulian Andres Klode <julian.klode@canonical.com>2025-02-14 19:45:12 +0100
commit384cfa1fe9dfd30b35d6f8b0ae87be643ab75d06 (patch)
treecbda3ac7c585141a4900d41a7162be78f9a61150 /apt-pkg
parent7f4c339d6fe19ff7e26144e373a6e5c73344539f (diff)
Return string_view from Apt::String::Strip(). Take string_view in VectorizeString(), StringSplit(), SubstVar()
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/cdrom.cc2
-rw-r--r--apt-pkg/contrib/gpgv.cc2
-rw-r--r--apt-pkg/contrib/strutl.cc22
-rw-r--r--apt-pkg/contrib/strutl.h10
-rw-r--r--apt-pkg/deb/dpkgpm.cc2
-rw-r--r--apt-pkg/init.cc8
6 files changed, 23 insertions, 23 deletions
diff --git a/apt-pkg/cdrom.cc b/apt-pkg/cdrom.cc
index e2823d900..4da2b738d 100644
--- a/apt-pkg/cdrom.cc
+++ b/apt-pkg/cdrom.cc
@@ -490,7 +490,7 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
while (F.ReadLine(Buffer))
{
++CurLine;
- auto const Cleaned = APT::String::Strip(SubstVar(Buffer, "\t", " "));
+ std::string const Cleaned{APT::String::Strip(SubstVar(Buffer, "\t", " "))};
// Comment or blank
if (Cleaned.empty() || Cleaned[0] == '#')
diff --git a/apt-pkg/contrib/gpgv.cc b/apt-pkg/contrib/gpgv.cc
index c6eabc690..b555331d6 100644
--- a/apt-pkg/contrib/gpgv.cc
+++ b/apt-pkg/contrib/gpgv.cc
@@ -157,7 +157,7 @@ static bool CheckGPGV(std::unordered_map<std::string, std::forward_list<std::str
{
if (unlikely(Debug))
std::clog << "Read line: " << line << std::endl;
- checkedCommands[gpgv].push_front(APT::String::Strip(line));
+ checkedCommands[gpgv].emplace_front(APT::String::Strip(line));
}
dumpOptions.Close();
waitpid(child, NULL, 0);
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index a4df85710..ea70ae581 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/apt-pkg/contrib/strutl.cc
@@ -56,13 +56,13 @@ using namespace std;
// ---------------------------------------------------------------------
namespace APT {
namespace String {
-std::string Strip(std::string_view str)
+std::string_view Strip(std::string_view str)
{
while (!str.empty() && isspace(str[0]))
str.remove_prefix(1);
while (!str.empty() && isspace(str.back()))
str.remove_suffix(1);
- return std::string{str};
+ return str;
}
bool Endswith(const std::string_view &s, const std::string_view &end)
@@ -490,10 +490,10 @@ string TimeToStr(unsigned long Sec)
// SubstVar - Substitute a string for another string /*{{{*/
// ---------------------------------------------------------------------
/* This replaces all occurrences of Subst with Contents in Str. */
-string SubstVar(const string &Str,const string &Subst,const string &Contents)
+string SubstVar(const string_view &Str,const string_view &Subst,const string_view &Contents)
{
if (Subst.empty() == true)
- return Str;
+ return std::string{Str};
string::size_type Pos = 0;
string::size_type OldPos = 0;
@@ -510,7 +510,7 @@ string SubstVar(const string &Str,const string &Subst,const string &Contents)
}
if (OldPos == 0)
- return Str;
+ return std::string{Str};
if (OldPos >= Str.length())
return Temp;
@@ -1363,13 +1363,13 @@ bool TokSplitString(char Tok,char *Input,char **List,
/* This can be used to split a given string up into a vector, so the
propose is the same as in the method above and this one is a bit slower
also, but the advantage is that we have an iteratable vector */
-vector<string> VectorizeString(string const &haystack, char const &split)
+vector<string> VectorizeString(string_view const &haystack, char const &split)
{
vector<string> exploded;
if (haystack.empty() == true)
return exploded;
- string::const_iterator start = haystack.begin();
- string::const_iterator end = start;
+ auto start = haystack.begin();
+ auto end = start;
do {
for (; end != haystack.end() && *end != split; ++end);
exploded.push_back(string(start, end));
@@ -1382,7 +1382,7 @@ vector<string> VectorizeString(string const &haystack, char const &split)
// ---------------------------------------------------------------------
/* See header for details.
*/
-vector<string> StringSplit(std::string const &s, std::string const &sep,
+vector<string> StringSplit(std::string_view const &s, std::string_view const &sep,
unsigned int maxsplit)
{
vector<string> split;
@@ -1396,8 +1396,8 @@ vector<string> StringSplit(std::string const &s, std::string const &sep,
while (pos != string::npos)
{
pos = s.find(sep, start);
- split.push_back(s.substr(start, pos-start));
-
+ split.emplace_back(s.substr(start, pos-start));
+
// if maxsplit is reached, the remaining string is the last item
if(split.size() >= maxsplit)
{
diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h
index 7e6aba272..92df7c9d3 100644
--- a/apt-pkg/contrib/strutl.h
+++ b/apt-pkg/contrib/strutl.h
@@ -42,7 +42,7 @@ namespace {
namespace APT {
namespace String {
- APT_PUBLIC std::string Strip(std::string_view s);
+ APT_PUBLIC std::string_view 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);
@@ -111,7 +111,7 @@ APT_PUBLIC bool TokSplitString(char Tok,char *Input,char **List,
unsigned long ListMax);
// split a given string by a char
-APT_PUBLIC std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
+APT_PUBLIC std::vector<std::string> VectorizeString(std::string_view const &haystack, char const &split) APT_PURE;
/* \brief Return a vector of strings from string "input" where "sep"
* is used as the delimiter string.
@@ -127,8 +127,8 @@ APT_PUBLIC std::vector<std::string> VectorizeString(std::string const &haystack,
* if used the string is only split on maxsplit places and the last
* item in the vector contains the remainder string.
*/
-APT_PUBLIC std::vector<std::string> StringSplit(std::string const &input,
- std::string const &sep,
+APT_PUBLIC std::vector<std::string> StringSplit(std::string_view const &input,
+ std::string_view const &sep,
unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_PURE;
@@ -248,7 +248,7 @@ struct SubstVar
const std::string *Contents;
};
APT_PUBLIC std::string SubstVar(std::string Str,const struct SubstVar *Vars);
-APT_PUBLIC std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
+APT_PUBLIC std::string SubstVar(const std::string_view &Str,const std::string_view &Subst,const std::string_view &Contents);
struct RxChoiceList
{
diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc
index a6be563ca..cc695ceb9 100644
--- a/apt-pkg/deb/dpkgpm.cc
+++ b/apt-pkg/deb/dpkgpm.cc
@@ -600,7 +600,7 @@ void pkgDPkgPM::ProcessDpkgStatusLine(char *line)
// build the (prefix, pkgname, action) tuple, position of this
// is different for "processing" or "status" messages
- std::string prefix = APT::String::Strip(list[0]);
+ auto prefix = APT::String::Strip(list[0]);
std::string pkgname;
std::string action;
diff --git a/apt-pkg/init.cc b/apt-pkg/init.cc
index d56973eeb..61d45e37e 100644
--- a/apt-pkg/init.cc
+++ b/apt-pkg/init.cc
@@ -70,7 +70,7 @@ static bool pkgInitArchTupleMap()
auto cpurow = split(cpuline);
auto cpu = APT::String::Strip(cpurow.at(0));
- cpus.push_back(cpu);
+ cpus.emplace_back(cpu);
}
if (!cputable.eof())
return _error->Error("Error reading the CPU table");
@@ -97,14 +97,14 @@ static bool pkgInitArchTupleMap()
if (tuple.find("<cpu>") == tuple.npos && arch.find("<cpu>") == arch.npos)
{
- APT::ArchToTupleMap.insert({arch, VectorizeString(tuple, '-')});
+ APT::ArchToTupleMap.insert({std::string{arch}, VectorizeString(tuple, '-')});
}
else
{
for (auto && cpu : cpus)
{
- auto mytuple = SubstVar(tuple, std::string("<cpu>"), cpu);
- auto myarch = SubstVar(arch, std::string("<cpu>"), cpu);
+ auto mytuple = SubstVar(tuple, "<cpu>", cpu);
+ auto myarch = SubstVar(arch, "<cpu>", cpu);
APT::ArchToTupleMap.insert({myarch, VectorizeString(mytuple, '-')});
}