summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Vogt <mvo@debian.org>2013-09-07 12:19:51 +0200
committerMichael Vogt <mvo@debian.org>2013-09-07 12:19:51 +0200
commit00f4d9ffa3468e899abf8fbda8db71fc3143b8e5 (patch)
tree7a32bb35ba43285f5d24f7c07ad8347b5ca499e5
parent968179cf7b9e1a633c283745da15755d8de4acbd (diff)
implement StringSplit() as we need this to fix the dpkg status-fd output parsing
-rw-r--r--apt-pkg/contrib/strutl.cc22
-rw-r--r--apt-pkg/contrib/strutl.h2
-rw-r--r--test/libapt/strutil_test.cc17
3 files changed, 41 insertions, 0 deletions
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<string> 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<string> StringSplit(string const &s, std::string const &sep)
+{
+ vector<string> 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<std::string> VectorizeString(std::string const &haystack, char const &split) __attrib_const;
+// like python string.split
+std::vector<std::string> 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);
diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc
index bfe0d7222..bac9dd2f1 100644
--- a/test/libapt/strutil_test.cc
+++ b/test/libapt/strutil_test.cc
@@ -42,5 +42,22 @@ int main(int argc,char *argv[])
output = DeEscapeString(input);
equals(output, expected);
+ // Split
+ input = "status: libnet1:amd64: unpacked";
+ vector<std::string> result = StringSplit(input, ": ");
+ equals(result[0], "status");
+ equals(result[1], "libnet1:amd64");
+ equals(result[2], "unpacked");
+ equals(result.size(), 3);
+
+ input = "status: libnet1:amd64: unpacked";
+ result = StringSplit(input, "xxx");
+ equals(result[0], input);
+ equals(result.size(), 1);
+
+ input = "status: libnet1:amd64: unpacked";
+ result = StringSplit(input, "");
+ equals(result.size(), 0);
+
return 0;
}