]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
Revamp the split functions to allow a max_split argument and splitting by longer...
[libs/core.git] / source / utils.cpp
index 3ef7b797b5040c5cb8b6f6a545ec83d04b944fcb..244055b232cb81df6347f9200d315683d372aff3 100644 (file)
@@ -1,8 +1,51 @@
+/* $Id$
+
+This file is part of libmspstrings
+Copyright © 2006-2007 Mikko Rasa
+Distributed under the LGPL
+*/
+
 #include <list>
 #include "utils.h"
 
 using namespace std;
 
+namespace {
+
+template<bool long_sep, bool allow_empty>
+vector<string> do_split(const string &str, const string &sep, int max_split)
+{
+       vector<string> result;
+
+       unsigned start=0;
+       while(start<str.size())
+       {
+               unsigned end=long_sep ? str.find(sep, start) : str.find_first_of(sep, start);
+               if(end!=start || allow_empty)
+               {
+                       if(max_split>=0 && result.size()==static_cast<unsigned>(max_split))
+                       {
+                               result.push_back(str.substr(start));
+                               break;
+                       }
+                       else
+                               result.push_back(str.substr(start, end-start));
+               }
+
+               if(end>str.size())
+                       break;
+
+               start=end+(long_sep ? sep.size() : 1);
+
+               if(allow_empty && start==str.size())
+                       result.push_back(string());
+       }
+
+       return result;
+}
+
+}
+
 namespace Msp {
 
 /**
@@ -33,7 +76,7 @@ Returns a lowercase copy of the given string.
 */
 string tolower(const string &str)
 {
-       string  result(str);
+       string result(str);
        transform(result.begin(), result.end(), result.begin(), ::tolower);
        return result;
 }
@@ -43,11 +86,36 @@ Returns an uppercase copy of the given string.
 */
 string toupper(const string &str)
 {
-       string  result(str);
+       string result(str);
        transform(result.begin(), result.end(), result.begin(), ::toupper);
        return result;
 }
 
+vector<string> split_fields(const string &str, const string &sep, int max_split)
+{
+       return do_split<true, true>(str, sep, max_split);
+}
+
+vector<string> split_fields(const string &str, char sep, int max_split)
+{
+       return split_fields(str, string(1, sep), max_split);
+}
+
+vector<string> split_long(const string &str, const string &sep, int max_split)
+{
+       return do_split<true, false>(str, sep, max_split);
+}
+
+vector<string> split(const string &str, const string &sep, int max_split)
+{
+       return do_split<false, false>(str, sep, max_split);
+}
+
+vector<string> split(const string &str, char sep, int max_split)
+{
+       return split(str, string(1, sep), max_split);
+}
+
 /**
 Splits a string to parts.
 
@@ -59,23 +127,35 @@ 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);
+               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;
 }
 
 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);
 }
 
 /**
@@ -108,8 +188,8 @@ removed.
 string strip(const string &s)
 {
        string result=s;
-       if(!result.erase(0, result.find_first_not_of(" \t\n")).empty())
-               result.erase(result.find_last_not_of(" \t\n")+1);
+       if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty())
+               result.erase(result.find_last_not_of(" \t\r\n")+1);
        return result;
 }