Fixed split to allow empty parts in beginning and end of string
authorMikko Rasa <tdb@tdb.fi>
Sun, 18 Mar 2007 20:21:58 +0000 (20:21 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 18 Mar 2007 20:21:58 +0000 (20:21 +0000)
source/utils.cpp

index 55b191f5216dcd2a60f92c3b878f23248e9e5722..0ed3fc11ef1e13c5a38fe180daf5306261b3bc46 100644 (file)
@@ -59,17 +59,29 @@ Splits a string to parts.
 vector<string> split(const string &str, const string &sep, bool allow_empty)
 {
        vector<string> result;
-       unsigned start=str.find_first_not_of(sep);
+       
+       unsigned start=0;
+       if(!allow_empty)
+               start=str.find_first_not_of(sep);
+       
        while(start<str.size())
        {
                unsigned end=str.find_first_of(sep, start);
                result.push_back(str.substr(start, end-start));
-               if(end==string::npos) break;
+               
+               if(end==string::npos)
+                       break;
+               
                if(allow_empty)
+               {
                        start=end+1;
+                       if(start==str.size())
+                               result.push_back(string());
+               }
                else
                        start=str.find_first_not_of(sep, end);
        }
+
        return result;
 }