stringtools.hh

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

Generated on Tue Feb 19 10:51:01 2008 for Util-- by doxygen 1.5.3
SourceForge.net Project Page