update to paranthetical_split

This commit is contained in:
John W. C. McNabb 2006-11-17 00:16:56 +00:00
parent 8e63280b42
commit 1668e073d0
2 changed files with 22 additions and 8 deletions

View file

@ -199,14 +199,15 @@ std::vector< std::string > split(std::string const &val, char c, int flags)
return res;
}
//splits a string into an odd number of parts. The part before the first '(', the part between the
//splits a function based either on a separator where text within paranthesis is protected from splitting,
//or it splits a string into an odd number of parts. The part before the first '(', the part between the
//first '(' and the matching right ')', etc ... and the remainder of the string. Note that this
//will find the first matching char in the left string and match against the corresponding
//char in the right string. A correctly processed string should return with an odd number of elements to the vector.
//Empty elements are never removed as they are placeholders.
//parenthetical_split("a(b)c{d}e(f{g})h","({",")}") should return a vector of
// <"a","b","c","d","e","f{g}","h">
std::vector< std::string > paranthetical_split(std::string const &val, std::string const &left, std::string const &right,int flags)
// <"a","b","c","d","e","f{g}","h"> REMOVE EMPTY only works for the separator split
std::vector< std::string > paranthetical_split(std::string const &val, const char separator, std::string const &left, std::string const &right,int flags)
{
std::vector< std::string > res;
std::vector<char> part;
@ -223,9 +224,19 @@ std::vector< std::string > paranthetical_split(std::string const &val, std::stri
}
while (i2 != val.end()) {
if(separator && *i2 == separator){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);
++i2;
i1=i2;
break;
}
if(part.size() && *i2 == part.back()){
part.pop_back();
if(part.size() == 0){
if(!separator && part.size() == 0){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
@ -237,9 +248,10 @@ std::vector< std::string > paranthetical_split(std::string const &val, std::stri
}
break;
}
bool found=false;
for(size_t i=0; i < lp.size(); i++){
if (*i2 == lp[i]){
if (part.size()==0){
if (!separator && part.size()==0){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
strip(new_val);
@ -250,11 +262,13 @@ std::vector< std::string > paranthetical_split(std::string const &val, std::stri
++i2;
}
part.push_back(rp[i]);
found=true;
break;
}else{
++i2;
}
}
if(!found){
++i2;
}
}
std::string new_val(i1, i2);

View file

@ -49,7 +49,7 @@ bool notspace(char c);
// STRIP_SPACES : strips leading and trailing blank spaces
enum { REMOVE_EMPTY = 0x01, STRIP_SPACES = 0x02 };
std::vector< std::string > split(std::string const &val, char c = ',', int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector< std::string > paranthetical_split(std::string const &val, std::string const &left="(", std::string const &right=")",int flags = STRIP_SPACES);
std::vector< std::string > paranthetical_split(std::string const &val, const char separator = 0 , std::string const &left="(", std::string const &right=")",int flags = STRIP_SPACES);
std::string join(std::vector< std::string > const &v, char c = ',');
std::vector< std::string > quoted_split(std::string const &val, char c= ',',
int flags = REMOVE_EMPTY | STRIP_SPACES, char quote = '\\');