00001 /** 00002 * @file string_manip.h 00003 * std::string helpers 00004 * 00005 * @remark Copyright 2002 OProfile authors 00006 * @remark Read the file COPYING 00007 * 00008 * @author Philippe Elie 00009 * @author John Levon 00010 */ 00011 00012 #ifndef STRING_MANIP_H 00013 #define STRING_MANIP_H 00014 00015 #include <string> 00016 #include <vector> 00017 #include <sstream> 00018 #include <stdexcept> 00019 00020 /** 00021 * @param str string 00022 * @param ch the characterto search 00023 * 00024 * erase char from the begin of str to the last 00025 * occurence of ch from and return the string 00026 */ 00027 std::string erase_to_last_of(std::string const & str, char ch); 00028 00029 /// split string s by first occurence of char c, returning the second part. 00030 /// s is set to the first part. Neither include the split character 00031 std::string split(std::string & s, char c); 00032 00033 /// return true if "prefix" is a prefix of "s", behavior is undefined 00034 /// if prefix is an empty string 00035 bool is_prefix(std::string const & s, std::string const & prefix); 00036 00037 /** 00038 * @param str the string to tokenize 00039 * @param sep the separator_char 00040 * 00041 * separate fields in a string in a list of token; field are 00042 * separated by the sep character, sep char can be escaped 00043 * by '\\' to specify a sep char in a token, '\\' not followed 00044 * by a sep is taken as it e.g. "\,\a" --> ",\a" 00045 */ 00046 std::vector<std::string> separate_token(std::string const & str, char sep); 00047 00048 /// remove trim chars from start of input string return the new string 00049 std::string ltrim(std::string const & str, std::string const & totrim = "\t "); 00050 /// remove trim chars from end of input string return the new string 00051 std::string rtrim(std::string const & str, std::string const & totrim = "\t "); 00052 /// ltrim(rtrim(str)) 00053 std::string trim(std::string const & str, std::string const & totrim = "\t "); 00054 00055 /** 00056 * format_percent - smart format of double percentage value 00057 * @param value - the value 00058 * @param int_width - the maximum integer integer width default to 2 00059 * @param frac_width - the fractionnary width default to 4 00060 * @param showpos - show + sign for positive values 00061 * 00062 * This formats a percentage into exactly the given width and returns 00063 * it. If the integer part is larger than the given int_width, the 00064 * returned string will be wider. The returned string is never 00065 * shorter than (fract_with + int_width + 1) 00066 * 00067 */ 00068 std::string const 00069 format_percent(double value, size_t int_width, 00070 size_t frac_width, bool showpos = false); 00071 00072 /// prefered width to format percentage 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 00078 /** 00079 * @param src input parameter 00080 * convert From src to a T through an istringstream. 00081 * 00082 * Throws invalid_argument if conversion fail. 00083 * 00084 * Note that this is not as foolproof as boost's lexical_cast 00085 */ 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 // specialization accepting hexadecimal and octal number in input. Note that 00102 // op_lexical_cast<unsigned int>("0x23"); will fail because it call the 00103 // non specialized template. 00104 template <> 00105 unsigned int op_lexical_cast<unsigned int>(std::string const & str); 00106 00107 #endif /* !STRING_MANIP_H */
1.6.1