improve the paranthetical_split() function to actually perform as advertised

This commit is contained in:
Gunter Labes 2007-10-29 14:39:08 +00:00
parent 9312c56be8
commit 94e0c29dcc
2 changed files with 38 additions and 9 deletions

View file

@ -198,9 +198,10 @@ std::vector< std::string > split(std::string const &val, char c, int flags)
return res; return res;
} }
// Splits a function based either on a separator // Splits a string based either on a separator where text within paranthesis
// where text within paranthesis is protected from splitting, // is protected from splitting (the paranthesis get removed; note that one can
// (note that one can use the same character for both the left and right paranthesis) // use the same character for both the left and right paranthesis and in this
// mode it usually makes only sense to have )
// or if the separator == 0 it splits a string into an odd number of parts: // or if the separator == 0 it splits a string into an odd number of parts:
// - The part before the first '(', // - The part before the first '(',
// - the part between the first '(' // - the part between the first '('
@ -220,6 +221,7 @@ std::vector< std::string > paranthetical_split(std::string const &val, const cha
{ {
std::vector< std::string > res; std::vector< std::string > res;
std::vector<char> part; std::vector<char> part;
bool in_paranthesis = false;
std::string::const_iterator i1 = val.begin(); std::string::const_iterator i1 = val.begin();
std::string::const_iterator i2 = val.begin(); std::string::const_iterator i2 = val.begin();
@ -233,8 +235,16 @@ std::vector< std::string > paranthetical_split(std::string const &val, const cha
} }
while (i2 != val.end()) { while (i2 != val.end()) {
if(separator && *i2 == separator){ if(!in_paranthesis && separator && *i2 == separator){
std::string new_val(i1, i2); std::string::const_iterator end = i2;
// Remove the right paranthesis at the end of the new string.
for(size_t i=0; i < rp.size(); i++){
if (*(i2 - 1) == rp[i]) {
end = i2 - 1;
break;
}
}
std::string new_val(i1, end);
if (flags & STRIP_SPACES) if (flags & STRIP_SPACES)
strip(new_val); strip(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty()) if (!(flags & REMOVE_EMPTY) || !new_val.empty())
@ -244,6 +254,15 @@ std::vector< std::string > paranthetical_split(std::string const &val, const cha
while (i2 != val.end() && *i2 == ' ') while (i2 != val.end() && *i2 == ' ')
++i2; ++i2;
} }
// Remove the left paranthesis at the start of the next string.
for(size_t i=0; i < lp.size(); i++){
if (*i2 == lp[i]) {
++i2;
in_paranthesis = true;
part.push_back(rp[i]);
break;
}
}
i1=i2; i1=i2;
continue; continue;
} }
@ -257,6 +276,8 @@ std::vector< std::string > paranthetical_split(std::string const &val, const cha
++i2; ++i2;
i1=i2; i1=i2;
}else{ }else{
if (part.size() == 0)
in_paranthesis = false;
++i2; ++i2;
} }
continue; continue;
@ -281,10 +302,19 @@ std::vector< std::string > paranthetical_split(std::string const &val, const cha
} }
if(!found){ if(!found){
++i2; ++i2;
} else
in_paranthesis = true;
}
std::string::const_iterator end = i2;
for(size_t i=0; i < rp.size(); i++){
if (*(i2 - 1) == rp[i]) {
end = i2 - 1;
break;
} }
} }
std::string new_val(i1, i2); std::string new_val(i1, end);
if (flags & STRIP_SPACES) if (flags & STRIP_SPACES)
strip(new_val); strip(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty()) if (!(flags & REMOVE_EMPTY) || !new_val.empty())

View file

@ -67,8 +67,7 @@ std::string &strip(std::string &str);
bool has_value(std::string const &values, std::string const &val); bool has_value(std::string const &values, std::string const &val);
bool string_bool(const std::string& str,bool def=false); bool string_bool(const std::string& str,bool def=false);
// Checks the username is valid (all alpha-numeric or space (but no space at ends)). //! Check if the username contains only valid characters.
// Does not check length, disallow-list, or already in use */
bool isvalid_username(const std::string &login); bool isvalid_username(const std::string &login);
typedef std::map< std::string, t_string > string_map; typedef std::map< std::string, t_string > string_map;