summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/strutl.cc
diff options
context:
space:
mode:
authorMichael Vogt <mvo@debian.org>2013-09-07 13:12:50 +0200
committerMichael Vogt <mvo@debian.org>2013-09-07 13:12:50 +0200
commit85bf001994fa59ca979293af3abb89d3486e0afb (patch)
tree9ede8d92d8b143506eecb2287f7891876ca2bd77 /apt-pkg/contrib/strutl.cc
parent00f4d9ffa3468e899abf8fbda8db71fc3143b8e5 (diff)
add maxsplit parameter to StringSplit
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;
}