summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/strutl.cc
diff options
context:
space:
mode:
Diffstat (limited to 'apt-pkg/contrib/strutl.cc')
-rw-r--r--apt-pkg/contrib/strutl.cc18
1 files changed, 13 insertions, 5 deletions
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<string> 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<string> StringSplit(string const &s, std::string const &sep)
+vector<string> StringSplit(string const &s, std::string const &sep,
+ unsigned int maxsplit)
{
vector<string> 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;
}