Created
June 21, 2011 08:59
-
-
Save mattn/1037493 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <strtk.hpp> | |
class extended_predicate | |
{ | |
public: | |
extended_predicate(const std::string& delimiters) | |
: escape_(false), | |
in_bracket_range_(false), | |
mdp_(delimiters) | |
{} | |
inline bool operator()(const unsigned char c) const | |
{ | |
if (escape_) | |
{ | |
escape_ = false; | |
return false; | |
} | |
else if ('\\' == c) | |
{ | |
escape_ = true; | |
return false; | |
} | |
else if ('"' == c) | |
{ | |
in_bracket_range_ = !in_bracket_range_; | |
return true; | |
} | |
else if (in_bracket_range_) | |
return false; | |
else | |
return mdp_(c); | |
} | |
inline void reset() | |
{ | |
escape_ = false; | |
in_bracket_range_ = false; | |
} | |
private: | |
mutable bool escape_; | |
mutable bool in_bracket_range_; | |
mutable strtk::multiple_char_delimiter_predicate mdp_; | |
}; | |
int main() | |
{ | |
std::string str = "abc;\"123, mno xyz\",i\\,jk"; | |
strtk::std_string::token_list_type token_list; | |
strtk::split(extended_predicate(".,; "), | |
str, | |
std::back_inserter(token_list), | |
strtk::split_options::compress_delimiters); | |
strtk::std_string::token_list_type::iterator itr = token_list.begin(); | |
while (token_list.end() != itr) { | |
std::cout << "[" << *itr << "]\t"; | |
++itr; | |
} | |
// [abc] [123, mno xyz] [i\,jk] | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment