stringtools.hh
Go to the documentation of this file.
00001 #ifndef UTILMM_STRINGTOOLS_HH
00002 #define UTILMM_STRINGTOOLS_HH
00003 
00004 #include <string>
00005 #include <list>
00006 #include <ctype.h>
00007 
00008 #include <boost/filesystem/path.hpp>
00009 #include <boost/lexical_cast.hpp>
00010 
00011 namespace utilmm {
00012     typedef std::list<std::string> stringlist;
00013 
00014     inline stringlist split(std::string const& s, std::string const& sep = " ", bool ignore_empty = true)
00015     {
00016         using std::string;
00017 
00018         stringlist l;
00019         string::size_type sep_length = sep.length();
00020 
00021         string::size_type from = 0;
00022         for(;;)
00023         {
00024             string::size_type to = s.find(sep, from);
00025             if (to == string::npos)
00026             {
00027                 if (from < s.length() || !ignore_empty)
00028                     l.push_back(string(s, from));
00029                 return l;
00030             }
00031 
00032             if (to > from || !ignore_empty)
00033                 l.push_back(string(s, from, to - from));
00034             from = to + sep_length;
00035         }
00036     }
00037 
00038     inline std::string join(stringlist const& l, std::string const& sep = " ")
00039     {
00040         using std::string;
00041 
00042         if (l.empty())
00043             return "";
00044 
00045         string s = l.front();
00046 
00047         stringlist::const_iterator const end = l.end();
00048         stringlist::const_iterator it = l.begin();
00049         for (++it; it != l.end(); ++it)
00050             s += sep + *it;
00051         return s;
00052     }
00053 
00054     template<typename T>
00055     inline std::string join(std::list<T> const& l, std::string const& sep = " ")
00056     {
00057         stringlist strl;
00058         for (typename std::template list<T>::const_iterator it = l.begin(); it != l.end(); ++it)
00059             strl.push_back(boost::lexical_cast<std::string>(*it));
00060         return join(strl, sep);
00061     }
00062 
00063     inline std::string upcase(std::string const& s)
00064     {
00065         using std::string;
00066         string ret(s);
00067         string::iterator it, end = ret.end();
00068         for (it = ret.begin(); it != ret.end(); ++it)
00069             *it = toupper(*it);
00070 
00071         return ret;
00072     }
00073 
00074     inline bool starts_with(std::string const& str, std::string const& start)
00075     { return std::string(str, 0, start.length()) == start; }
00076 
00077     inline boost::filesystem::path clean_path(std::string str)
00078     {
00079         using std::string;
00080         string::size_type i = str.find("//");
00081         while (i < str.length())
00082         {
00083             str = str.erase(i, 1);
00084             i   = str.find("//");
00085         }
00086 
00087         boost::filesystem::path p(str);
00088         return p.normalize();
00089     }
00090 }
00091 #endif
00092 


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Wed Sep 16 2015 07:05:43