From: Mikko Rasa Date: Fri, 3 Aug 2007 09:47:32 +0000 (+0000) Subject: Revamp the split functions to allow a max_split argument and splitting by longer... X-Git-Tag: strings-1.0~14 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=43a991406be78b2905ef1f13f4e7589f6f6c1ba3;p=libs%2Fcore.git Revamp the split functions to allow a max_split argument and splitting by longer separators Add \r to strip --- diff --git a/source/utils.cpp b/source/utils.cpp index 1feb086..244055b 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -10,6 +10,42 @@ Distributed under the LGPL using namespace std; +namespace { + +template +vector do_split(const string &str, const string &sep, int max_split) +{ + vector result; + + unsigned start=0; + while(start=0 && result.size()==static_cast(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 { /** @@ -55,6 +91,31 @@ string toupper(const string &str) return result; } +vector split_fields(const string &str, const string &sep, int max_split) +{ + return do_split(str, sep, max_split); +} + +vector split_fields(const string &str, char sep, int max_split) +{ + return split_fields(str, string(1, sep), max_split); +} + +vector split_long(const string &str, const string &sep, int max_split) +{ + return do_split(str, sep, max_split); +} + +vector split(const string &str, const string &sep, int max_split) +{ + return do_split(str, sep, max_split); +} + +vector split(const string &str, char sep, int max_split) +{ + return split(str, string(1, sep), max_split); +} + /** Splits a string to parts. @@ -127,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; } diff --git a/source/utils.h b/source/utils.h index 39b330b..d0d8a17 100644 --- a/source/utils.h +++ b/source/utils.h @@ -16,8 +16,13 @@ namespace Msp { int strcasecmp(const std::string &, const std::string &); std::string tolower(const std::string &); std::string toupper(const std::string &); -std::vector split(const std::string &, const std::string & =" \t\r\n", bool =false); -std::vector split(const std::string &, char, bool =false); + +std::vector split_fields(const std::string &, const std::string &, int =-1); +std::vector split_fields(const std::string &, char, int =-1); +std::vector split_long(const std::string &, const std::string &, int =-1); +std::vector split(const std::string &, const std::string & =" \t\r\n", int =-1); +std::vector split(const std::string &, char, int =-1); + template std::string join(const T &, const std::string & =" "); std::string strip(const std::string &);