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 ++++++++++++++++++++++ apt-pkg/contrib/strutl.h | 2 ++ 2 files changed, 24 insertions(+) (limited to 'apt-pkg/contrib') 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 /*{{{*/ // --------------------------------------------------------------------- /* */ diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 530896141..c97246c90 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -65,6 +65,8 @@ bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length); bool TokSplitString(char Tok,char *Input,char **List, unsigned long ListMax); std::vector VectorizeString(std::string const &haystack, char const &split) __attrib_const; +// like python string.split +std::vector StringSplit(std::string const &haystack, std::string const &sep) __attrib_const; void ioprintf(std::ostream &out,const char *format,...) __like_printf(2); void strprintf(std::string &out,const char *format,...) __like_printf(2); char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3); -- 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 +++++++++++++----- apt-pkg/contrib/strutl.h | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'apt-pkg/contrib') 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; } diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index c97246c90..944f91403 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -66,7 +66,7 @@ bool TokSplitString(char Tok,char *Input,char **List, unsigned long ListMax); std::vector VectorizeString(std::string const &haystack, char const &split) __attrib_const; // like python string.split -std::vector StringSplit(std::string const &haystack, std::string const &sep) __attrib_const; +std::vector StringSplit(std::string const &haystack, std::string const &sep, unsigned int maxsplit=0) __attrib_const; void ioprintf(std::ostream &out,const char *format,...) __like_printf(2); void strprintf(std::string &out,const char *format,...) __like_printf(2); char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3); -- 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 +++++++++------ apt-pkg/contrib/strutl.h | 10 ++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'apt-pkg/contrib') 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; } /*}}}*/ diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 944f91403..080f9f82e 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -62,11 +62,17 @@ bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0) bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0); bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len); bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length); + +// input changing string split bool TokSplitString(char Tok,char *Input,char **List, unsigned long ListMax); + +// split a given string by a char std::vector VectorizeString(std::string const &haystack, char const &split) __attrib_const; -// like python string.split -std::vector StringSplit(std::string const &haystack, std::string const &sep, unsigned int maxsplit=0) __attrib_const; + +// split a given string by a string token +std::vector StringSplit(std::string const &input, std::string const &sep, unsigned int maxsplit=0) __attrib_const; + void ioprintf(std::ostream &out,const char *format,...) __like_printf(2); void strprintf(std::string &out,const char *format,...) __like_printf(2); char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3); -- 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 ++---- apt-pkg/contrib/strutl.h | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'apt-pkg/contrib') 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; diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 080f9f82e..eb47287a4 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -70,8 +70,23 @@ bool TokSplitString(char Tok,char *Input,char **List, // split a given string by a char std::vector VectorizeString(std::string const &haystack, char const &split) __attrib_const; -// split a given string by a string token -std::vector StringSplit(std::string const &input, std::string const &sep, unsigned int maxsplit=0) __attrib_const; +/* \brief Return a vector of strings from string "input" where "sep" + * is used as the delimiter string. + * + * \param input The input string. + * + * \param sep The seperator to use. + * + * \param maxsplit (optional) The maximum amount of splitting that + * should be done . + * + * The optional "maxsplit" argument can be used to limit the splitting, + * if used the string is only split on maxsplit places and the last + * item in the vector contains the remainder string. + */ +std::vector StringSplit(std::string const &input, + std::string const &sep, + unsigned int maxsplit=0) __attrib_const; void ioprintf(std::ostream &out,const char *format,...) __like_printf(2); void strprintf(std::string &out,const char *format,...) __like_printf(2); -- 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 +- apt-pkg/contrib/strutl.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib') 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; diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index eb47287a4..b42e06491 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -17,7 +17,7 @@ #define STRUTL_H - +#include #include #include #include @@ -86,7 +86,7 @@ std::vector VectorizeString(std::string const &haystack, char const */ std::vector StringSplit(std::string const &input, std::string const &sep, - unsigned int maxsplit=0) __attrib_const; + unsigned int maxsplit=std::numeric_limits::max()) __attrib_const; void ioprintf(std::ostream &out,const char *format,...) __like_printf(2); void strprintf(std::string &out,const char *format,...) __like_printf(2); -- cgit v1.2.3