diff options
| author | наб <nabijaczleweli@nabijaczleweli.xyz> | 2025-01-23 20:37:35 +0100 |
|---|---|---|
| committer | Julian Andres Klode <julian.klode@canonical.com> | 2025-02-14 19:45:12 +0100 |
| commit | 384cfa1fe9dfd30b35d6f8b0ae87be643ab75d06 (patch) | |
| tree | cbda3ac7c585141a4900d41a7162be78f9a61150 /apt-pkg/contrib | |
| parent | 7f4c339d6fe19ff7e26144e373a6e5c73344539f (diff) | |
Return string_view from Apt::String::Strip(). Take string_view in VectorizeString(), StringSplit(), SubstVar()
Diffstat (limited to 'apt-pkg/contrib')
| -rw-r--r-- | apt-pkg/contrib/gpgv.cc | 2 | ||||
| -rw-r--r-- | apt-pkg/contrib/strutl.cc | 22 | ||||
| -rw-r--r-- | apt-pkg/contrib/strutl.h | 10 |
3 files changed, 17 insertions, 17 deletions
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 { |
