1 #ifndef _library__string__hpp__included__
2 #define _library__string__hpp__included__
8 #include <boost/lexical_cast.hpp>
11 * Strip trailing CR if any.
13 std::string
strip_CR(const std::string
& str
);
16 * Strip trailing CR if any.
18 void istrip_CR(std::string
& str
);
21 * Return first character or -1 if empty.
23 int firstchar(const std::string
& str
);
32 std::string
str() { return x
.str(); }
33 template<typename T
> stringfmt
& operator<<(const T
& y
) { x
<< y
; return *this; }
34 void throwex() { throw std::runtime_error(x
.str()); }
40 * Extract token out of string.
42 * Parameter str: The original string and the rest of the string on return.
43 * Parameter tok: The extracted token will be written here.
44 * Parameter sep: The characters to split on (empty always extracts the rest).
45 * Parameter seq: If true, skip whole sequence of token ending characters.
46 * Returns: The character token splitting occured on (-1 if end of string, -2 if string is empty).
48 int extract_token(std::string
& str
, std::string
& tok
, const char* sep
, bool seq
= false) throw(std::bad_alloc
);
54 regex_results(std::vector
<std::string
> res
);
55 operator bool() const;
56 bool operator!() const;
58 const std::string
& operator[](size_t i
) const;
61 std::vector
<std::string
> results
;
65 * Regexp a string and return matches.
67 * Parameter regex: The regexp to apply.
68 * Parameter str: The string to apply the regexp to.
69 * Parameter ex: If non-null and string does not match, throw this as std::runtime_error.
70 * Returns: The captures.
72 regex_results
regex(const std::string
& regex
, const std::string
& str
, const char* ex
= NULL
)
73 throw(std::bad_alloc
, std::runtime_error
);
76 * Regexp a string and return match result.
78 * Parameter regex: The regexp to apply.
79 * Parameter str: The string to apply the regexp to.
80 * Returns: True if matches, false if not.
82 bool regex_match(const std::string
& regex
, const std::string
& str
) throw(std::bad_alloc
, std::runtime_error
);
85 * Cast string to bool.
87 * The following is true: 'on', 'true', 'yes', '1', 'enable', 'enabled'.
88 * The following is false: 'off', 'false', 'no', '0', 'disable', 'disabled'.
89 * Parameter str: The string to cast.
90 * Returns: -1 if string is bad, 0 if false, 1 if true.
92 int string_to_bool(const std::string
& cast_to_bool
);
95 * \brief Typeconvert string.
97 template<typename T
> inline T
parse_value(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
100 //Hack, since lexical_cast lets negative values slip through.
101 if(!std::numeric_limits
<T
>::is_signed
&& value
.length() && value
[0] == '-') {
102 throw std::runtime_error("Unsigned values can't be negative");
104 return boost::lexical_cast
<T
>(value
);
105 } catch(std::exception
& e
) {
106 throw std::runtime_error("Can't parse value '" + value
+ "': " + e
.what());
110 template<> inline std::string
parse_value(const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)