string_manip.h
Go to the documentation of this file.00001
00012 #ifndef STRING_MANIP_H
00013 #define STRING_MANIP_H
00014
00015 #include <string>
00016 #include <vector>
00017 #include <sstream>
00018 #include <stdexcept>
00019
00027 std::string erase_to_last_of(std::string const & str, char ch);
00028
00031 std::string split(std::string & s, char c);
00032
00035 bool is_prefix(std::string const & s, std::string const & prefix);
00036
00046 std::vector<std::string> separate_token(std::string const & str, char sep);
00047
00049 std::string ltrim(std::string const & str, std::string const & totrim = "\t ");
00051 std::string rtrim(std::string const & str, std::string const & totrim = "\t ");
00053 std::string trim(std::string const & str, std::string const & totrim = "\t ");
00054
00068 std::string const
00069 format_percent(double value, size_t int_width,
00070 size_t frac_width, bool showpos = false);
00071
00073 static unsigned int const percent_int_width = 2;
00074 static unsigned int const percent_fract_width = 4;
00075 static unsigned int const percent_width = percent_int_width + percent_fract_width + 1;
00076
00077
00086 template <typename To, typename From>
00087 To op_lexical_cast(From const & src)
00088 {
00089 std::ostringstream in;
00090 if (!(in << src))
00091 throw std::invalid_argument("op_lexical_cast<T>()");
00092 std::istringstream out(in.str());
00093 To value;
00094 if (!(out >> value)) {
00095 throw std::invalid_argument("op_lexical_cast<T>(\"" +
00096 in.str() +"\")");
00097 }
00098 return value;
00099 }
00100
00101
00102
00103
00104 template <>
00105 unsigned int op_lexical_cast<unsigned int>(std::string const & str);
00106
00107 #endif