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 */
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
+/* See header for details.
*/
-vector<string> StringSplit(string const &s, std::string const &sep)
+vector<string> StringSplit(std::string const &s, std::string const &sep,
+ unsigned int maxsplit)
{
vector<string> split;
size_t start, pos;
- start = pos = 0;
+
+ // no seperator given, this is bogus
if(sep.size() == 0)
return split;
-
- do {
+
+ start = pos = 0;
+ while (pos != string::npos)
+ {
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);
+
+ // if maxsplit is reached, the remaining string is the last item
+ if(split.size() >= maxsplit)
+ {
+ split[split.size()-1] = s.substr(start);
+ break;
+ }
+ start = pos+sep.size();
+ }
return split;
}
/*}}}*/