From 70e0c1683e7021a0682b0808b329a3cced3920ac Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 12 Aug 2013 17:24:15 +0200 Subject: some more coverity fixes --- apt-pkg/contrib/strutl.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index df02c3499..b70a62a47 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -943,6 +943,8 @@ bool StrToTime(const string &Val,time_t &Result) Tm.tm_isdst = 0; if (Month[0] != 0) Tm.tm_mon = MonthConv(Month); + else + Tm.tm_mon = 0; // we don't have a month, so pick something Tm.tm_year -= 1900; // Convert to local time and then to GMT -- cgit v1.2.3 From 00f4d9ffa3468e899abf8fbda8db71fc3143b8e5 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 7 Sep 2013 12:19:51 +0200 Subject: implement StringSplit() as we need this to fix the dpkg status-fd output parsing --- apt-pkg/contrib/strutl.cc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 0955b69f7..819d50de0 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1118,6 +1118,28 @@ vector VectorizeString(string const &haystack, char const &split) return exploded; } /*}}}*/ +// StringSplit - like python string.split /*{{{*/ +// --------------------------------------------------------------------- +/* This can be used to split a given string up into a vector of strings + * The seperator is a string + */ +vector StringSplit(string const &s, std::string const &sep) +{ + vector split; + size_t start, pos; + start = pos = 0; + if(sep.size() == 0) + return split; + + do { + pos = s.find(sep, start); + split.push_back(s.substr(start, pos-start)); + if(pos != string::npos) + start = pos+sep.size(); + } while (pos != string::npos); + return split; +} + /*}}}*/ // RegexChoice - Simple regex list/list matcher /*{{{*/ // --------------------------------------------------------------------- /* */ -- cgit v1.2.3 From 85bf001994fa59ca979293af3abb89d3486e0afb Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 7 Sep 2013 13:12:50 +0200 Subject: add maxsplit parameter to StringSplit --- apt-pkg/contrib/strutl.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 819d50de0..508af8922 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1123,19 +1123,27 @@ vector VectorizeString(string const &haystack, char const &split) /* This can be used to split a given string up into a vector of strings * The seperator is a string */ -vector StringSplit(string const &s, std::string const &sep) +vector StringSplit(string const &s, std::string const &sep, + unsigned int maxsplit) { vector split; size_t start, pos; - start = pos = 0; + if(sep.size() == 0) return split; - + + start = pos = 0; do { pos = s.find(sep, start); split.push_back(s.substr(start, pos-start)); - if(pos != string::npos) - start = pos+sep.size(); + + // deal with the max-split + if(maxsplit > 0 && split.size() >= maxsplit) + { + split[split.size()-1] = s.substr(start); + break; + } + start = pos+sep.size(); } while (pos != string::npos); return split; } -- cgit v1.2.3 From 9572a54bbc96eb653b3e13260abb183ba7a316f3 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 7 Sep 2013 16:16:49 +0200 Subject: doc update --- apt-pkg/contrib/strutl.cc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 508af8922..fd768f183 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1118,10 +1118,11 @@ vector VectorizeString(string const &haystack, char const &split) return exploded; } /*}}}*/ -// StringSplit - like python string.split /*{{{*/ +// StringSplit - split a string into a string vector by token /*{{{*/ // --------------------------------------------------------------------- -/* This can be used to split a given string up into a vector of strings - * The seperator is a string +/* This can be used to split a given string up from a given string token + * into a vector of strings. A optional "maxsplit" argument can be used + * to limit the splitting, in this case the */ vector StringSplit(string const &s, std::string const &sep, unsigned int maxsplit) @@ -1129,22 +1130,24 @@ vector StringSplit(string const &s, std::string const &sep, vector split; size_t start, pos; + // no seperator given, this is bogus if(sep.size() == 0) return split; start = pos = 0; - do { + while (pos != string::npos) + { pos = s.find(sep, start); split.push_back(s.substr(start, pos-start)); - // deal with the max-split + // if maxsplit is reached, the remaining string is the last item if(maxsplit > 0 && split.size() >= maxsplit) { split[split.size()-1] = s.substr(start); break; } start = pos+sep.size(); - } while (pos != string::npos); + } return split; } /*}}}*/ -- cgit v1.2.3 From 41053d721ce7f81652d7e873067376b94f9a060d Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 1 Oct 2013 12:03:37 +0200 Subject: improve documentation for StringSplit() --- apt-pkg/contrib/strutl.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index fd768f183..96c6d2f35 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1120,11 +1120,9 @@ vector VectorizeString(string const &haystack, char const &split) /*}}}*/ // StringSplit - split a string into a string vector by token /*{{{*/ // --------------------------------------------------------------------- -/* This can be used to split a given string up from a given string token - * into a vector of strings. A optional "maxsplit" argument can be used - * to limit the splitting, in this case the +/* See header for details. */ -vector StringSplit(string const &s, std::string const &sep, +vector StringSplit(std::string const &s, std::string const &sep, unsigned int maxsplit) { vector split; -- cgit v1.2.3 From 2ddab3fb958518acbd26685eeeb7755106b721a3 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 1 Oct 2013 12:38:03 +0200 Subject: change maxsplit default from "0" to maxint --- apt-pkg/contrib/strutl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 96c6d2f35..77e48962c 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1139,7 +1139,7 @@ vector StringSplit(std::string const &s, std::string const &sep, split.push_back(s.substr(start, pos-start)); // if maxsplit is reached, the remaining string is the last item - if(maxsplit > 0 && split.size() >= maxsplit) + if(split.size() >= maxsplit) { split[split.size()-1] = s.substr(start); break; -- cgit v1.2.3 From 65dbd5a1a32f63dae81c8b5814cd246f1154c38d Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 18 Oct 2013 18:26:56 +0200 Subject: re-add missing APT::String::Strip --- apt-pkg/contrib/strutl.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 77e48962c..9f794927d 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -36,7 +36,22 @@ using namespace std; /*}}}*/ - +// Strip - Remove white space from the front and back of a string /*{{{*/ +// --------------------------------------------------------------------- +namespace APT { + namespace String { +std::string Strip(const std::string &s) +{ + size_t start = s.find_first_not_of(" \t\n"); + // only whitespace + if (start == string::npos) + return ""; + size_t end = s.find_last_not_of(" \t\n"); + return s.substr(start, end-start+1); +} +} +} + /*}}}*/ // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/ // --------------------------------------------------------------------- /* This is handy to use before display some information for enduser */ -- cgit v1.2.3 From cf993341c2067ee091cfd51e5da0e237babce171 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 29 Nov 2013 08:35:05 +0100 Subject: add "APT::String::Endswith" and automatic adding of ".list" in apt edit-source --- apt-pkg/contrib/strutl.cc | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 9f794927d..962112854 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -49,6 +49,14 @@ std::string Strip(const std::string &s) size_t end = s.find_last_not_of(" \t\n"); return s.substr(start, end-start+1); } + +bool Endswith(const std::string &s, const std::string &end) +{ + if (end.size() > s.size()) + return false; + return (s.substr(s.size() - end.size(), s.size()) == end); +} + } } /*}}}*/ -- cgit v1.2.3 From 1e3f4083db29bba600b9725e9456b0e140975c99 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 22 Feb 2014 18:34:33 +0100 Subject: Fix typos in documentation (codespell) --- apt-pkg/contrib/strutl.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 962112854..d4f53ea3a 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -426,7 +426,7 @@ string TimeToStr(unsigned long Sec) /*}}}*/ // SubstVar - Substitute a string for another string /*{{{*/ // --------------------------------------------------------------------- -/* This replaces all occurances of Subst with Contents in Str. */ +/* This replaces all occurrences of Subst with Contents in Str. */ string SubstVar(const string &Str,const string &Subst,const string &Contents) { string::size_type Pos = 0; @@ -926,7 +926,7 @@ bool FTPMDTMStrToTime(const char* const str,time_t &time) /*}}}*/ // StrToTime - Converts a string into a time_t /*{{{*/ // --------------------------------------------------------------------- -/* This handles all 3 populare time formats including RFC 1123, RFC 1036 +/* This handles all 3 popular time formats including RFC 1123, RFC 1036 and the C library asctime format. It requires the GNU library function 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar reason the C library does not provide any such function :< This also @@ -1313,7 +1313,7 @@ string StripEpoch(const string &VerStr) // tolower_ascii - tolower() function that ignores the locale /*{{{*/ // --------------------------------------------------------------------- /* This little function is the most called method we have and tries - therefore to do the absolut minimum - and is noteable faster than + therefore to do the absolut minimum - and is notable faster than standard tolower/toupper and as a bonus avoids problems with different locales - we only operate on ascii chars anyway. */ int tolower_ascii(int const c) @@ -1324,9 +1324,9 @@ int tolower_ascii(int const c) } /*}}}*/ -// CheckDomainList - See if Host is in a , seperate list /*{{{*/ +// CheckDomainList - See if Host is in a , separate list /*{{{*/ // --------------------------------------------------------------------- -/* The domain list is a comma seperate list of domains that are suffix +/* The domain list is a comma separate list of domains that are suffix matched against the argument */ bool CheckDomainList(const string &Host,const string &List) { -- cgit v1.2.3