00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "common/util.hpp"
00025
00026 #include <boost/algorithm/string.hpp>
00027 #include <boost/function.hpp>
00028 #include <boost/bind.hpp>
00029
00030 using boost::algorithm::trim_right_copy_if;
00031 using boost::algorithm::is_any_of;
00032 using boost::algorithm::is_alnum;
00033 using boost::algorithm::is_space;
00034
00035
00036 namespace util
00037 {
00038
00039 typedef boost::function<bool(string::value_type)> ChPredicate;
00040 ChPredicate operator! (ChPredicate p) { return ! boost::bind(p,_1); }
00041
00042
00043 ChPredicate isValid (is_alnum() || is_any_of("-_.:+$'()@"));
00044 ChPredicate isPunct (is_space() || is_any_of(",;#*~ยด`?\\=/&%![]{}"));
00045
00046
00047 string
00048 sanitize (const string& org)
00049 {
00050 string res (trim_right_copy_if(org, !isValid ));
00051 string::iterator j = res.begin();
00052 string::const_iterator i = org.begin();
00053 string::const_iterator e = i + (res.length());
00054 while ( i != e )
00055 {
00056 while ( i != e && !isValid (*i) ) ++i;
00057 while ( i != e && isValid (*i) ) *(j++) = *(i++);
00058 if ( i != e && isPunct (*i) )
00059 {
00060 *j++ = '_';
00061 do ++i;
00062 while ( i != e && isPunct (*i));
00063 }
00064 }
00065 res.erase(j,res.end());
00066 return res;
00067 }
00068
00069
00070
00071 }
00072