]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
Fixed split to allow empty parts in beginning and end of string
[libs/core.git] / source / utils.cpp
index 3ef7b797b5040c5cb8b6f6a545ec83d04b944fcb..0ed3fc11ef1e13c5a38fe180daf5306261b3bc46 100644 (file)
@@ -33,7 +33,7 @@ Returns a lowercase copy of the given string.
 */
 string tolower(const string &str)
 {
 */
 string tolower(const string &str)
 {
-       string  result(str);
+       string result(str);
        transform(result.begin(), result.end(), result.begin(), ::tolower);
        return result;
 }
        transform(result.begin(), result.end(), result.begin(), ::tolower);
        return result;
 }
@@ -43,7 +43,7 @@ Returns an uppercase copy of the given string.
 */
 string toupper(const string &str)
 {
 */
 string toupper(const string &str)
 {
-       string  result(str);
+       string result(str);
        transform(result.begin(), result.end(), result.begin(), ::toupper);
        return result;
 }
        transform(result.begin(), result.end(), result.begin(), ::toupper);
        return result;
 }
@@ -59,23 +59,35 @@ Splits a string to parts.
 vector<string> split(const string &str, const string &sep, bool allow_empty)
 {
        vector<string> result;
 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())
        {
        while(start<str.size())
        {
-               unsigned        end=str.find_first_of(sep, start);
+               unsigned end=str.find_first_of(sep, start);
                result.push_back(str.substr(start, end-start));
                result.push_back(str.substr(start, end-start));
-               if(end==string::npos) break;
+               
+               if(end==string::npos)
+                       break;
+               
                if(allow_empty)
                if(allow_empty)
+               {
                        start=end+1;
                        start=end+1;
+                       if(start==str.size())
+                               result.push_back(string());
+               }
                else
                        start=str.find_first_not_of(sep, end);
        }
                else
                        start=str.find_first_not_of(sep, end);
        }
+
        return result;
 }
 
 vector<string> split(const string &str, char sep, bool allow_empty)
 {
        return result;
 }
 
 vector<string> split(const string &str, char sep, bool allow_empty)
 {
-       return split(str, string(1,sep), allow_empty);
+       return split(str, string(1, sep), allow_empty);
 }
 
 /**
 }
 
 /**