
---------- Forwarded Message ---------- Subject: Proposal for String Algorithms Library Date: Thursday 22 July 2004 09:43 From: zaufi@sendmail.ru To: boost@lists.boost.org hi boosters I would like to propose one more algorithm to SAL -- squeeze. It removes leading/trailing spaces and duplicate spaces between words. So sample string " This is \n\n a test \n" become "This is\na test". I use this function with XML/HTML related code to 'normalize' content... Hope this will help to smbd else ;) Have fun! ------------------------------------------------------- Oops. Forget to paste code... ;) <squeeze.cc> std::string squeeze(const std::string& str) { std::string out; // bool is_start_of_line = true; char last_space = 0; for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) { if (std::isspace(*i)) last_space = int(!is_start_of_line) * (int(*i == '\n') * '\n' + int((*i != '\n') & (last_space != '\n')) * ' ' + int(last_space == '\n') * last_space); else { if (last_space) { out.push_back(last_space); last_space = 0; } out.push_back(*i); is_start_of_line = false; } } return out; } </squeeze.cc>