Mike Dodd | 8cfa702 | 2010-11-17 11:12:26 -0800 | [diff] [blame] | 1 | /** |
| 2 | * @file string_manip.h |
| 3 | * std::string helpers |
| 4 | * |
| 5 | * @remark Copyright 2002 OProfile authors |
| 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author Philippe Elie |
| 9 | * @author John Levon |
| 10 | */ |
| 11 | |
| 12 | #ifndef STRING_MANIP_H |
| 13 | #define STRING_MANIP_H |
| 14 | |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | #include <sstream> |
| 18 | #include <stdexcept> |
| 19 | |
| 20 | /** |
| 21 | * @param str string |
| 22 | * @param ch the characterto search |
| 23 | * |
| 24 | * erase char from the begin of str to the last |
| 25 | * occurence of ch from and return the string |
| 26 | */ |
| 27 | std::string erase_to_last_of(std::string const & str, char ch); |
| 28 | |
| 29 | /// split string s by first occurence of char c, returning the second part. |
| 30 | /// s is set to the first part. Neither include the split character |
| 31 | std::string split(std::string & s, char c); |
| 32 | |
| 33 | /// return true if "prefix" is a prefix of "s", behavior is undefined |
| 34 | /// if prefix is an empty string |
| 35 | bool is_prefix(std::string const & s, std::string const & prefix); |
| 36 | |
| 37 | /** |
| 38 | * @param str the string to tokenize |
| 39 | * @param sep the separator_char |
| 40 | * |
| 41 | * separate fields in a string in a list of token; field are |
| 42 | * separated by the sep character, sep char can be escaped |
| 43 | * by '\\' to specify a sep char in a token, '\\' not followed |
| 44 | * by a sep is taken as it e.g. "\,\a" --> ",\a" |
| 45 | */ |
| 46 | std::vector<std::string> separate_token(std::string const & str, char sep); |
| 47 | |
| 48 | /// remove trim chars from start of input string return the new string |
| 49 | std::string ltrim(std::string const & str, std::string const & totrim = "\t "); |
| 50 | /// remove trim chars from end of input string return the new string |
| 51 | std::string rtrim(std::string const & str, std::string const & totrim = "\t "); |
| 52 | /// ltrim(rtrim(str)) |
| 53 | std::string trim(std::string const & str, std::string const & totrim = "\t "); |
| 54 | |
| 55 | /** |
| 56 | * format_percent - smart format of double percentage value |
| 57 | * @param value - the value |
| 58 | * @param int_width - the maximum integer integer width default to 2 |
| 59 | * @param frac_width - the fractionnary width default to 4 |
| 60 | * @param showpos - show + sign for positive values |
| 61 | * |
| 62 | * This formats a percentage into exactly the given width and returns |
| 63 | * it. If the integer part is larger than the given int_width, the |
| 64 | * returned string will be wider. The returned string is never |
| 65 | * shorter than (fract_with + int_width + 1) |
| 66 | * |
| 67 | */ |
| 68 | std::string const |
| 69 | format_percent(double value, size_t int_width, |
| 70 | size_t frac_width, bool showpos = false); |
| 71 | |
| 72 | /// prefered width to format percentage |
| 73 | static unsigned int const percent_int_width = 2; |
| 74 | static unsigned int const percent_fract_width = 4; |
| 75 | static unsigned int const percent_width = percent_int_width + percent_fract_width + 1; |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * @param src input parameter |
| 80 | * convert From src to a T through an istringstream. |
| 81 | * |
| 82 | * Throws invalid_argument if conversion fail. |
| 83 | * |
| 84 | * Note that this is not as foolproof as boost's lexical_cast |
| 85 | */ |
| 86 | template <typename To, typename From> |
| 87 | To op_lexical_cast(From const & src) |
| 88 | { |
| 89 | std::ostringstream in; |
| 90 | if (!(in << src)) |
| 91 | throw std::invalid_argument("op_lexical_cast<T>()"); |
| 92 | std::istringstream out(in.str()); |
| 93 | To value; |
| 94 | if (!(out >> value)) { |
| 95 | throw std::invalid_argument("op_lexical_cast<T>(\"" + |
| 96 | in.str() +"\")"); |
| 97 | } |
| 98 | return value; |
| 99 | } |
| 100 | |
| 101 | // specialization accepting hexadecimal and octal number in input. Note that |
| 102 | // op_lexical_cast<unsigned int>("0x23"); will fail because it call the |
| 103 | // non specialized template. |
| 104 | template <> |
| 105 | unsigned int op_lexical_cast<unsigned int>(std::string const & str); |
| 106 | |
| 107 | #endif /* !STRING_MANIP_H */ |