Fix integer overflow in ft_rendered_size_line
[ilaris-y4m-tools.git] / parseval.hpp
blobe0a2be0c4db1abbaa012d67f5c6b8fdd1ca2bf81
1 #ifndef _parseval_hpp_included_
2 #define _parseval_hpp_included_
4 #include <stdexcept>
5 #include <boost/lexical_cast.hpp>
6 #include <string>
7 #include <vector>
8 #include <cstdint>
10 template<typename T> inline T parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
12 try {
13 //Hack, since lexical_cast lets negative values slip through.
14 if(!std::numeric_limits<T>::is_signed && value.length() && value[0] == '-') {
15 throw std::runtime_error("Unsigned values can't be negative");
17 return boost::lexical_cast<T>(value);
18 } catch(std::exception& e) {
19 throw std::runtime_error("Can't parse value '" + value + "': " + e.what());
23 template<> inline uint8_t parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
25 try {
26 //Hack, since lexical_cast doesn't handle uint8_t as value.
27 int16_t v = boost::lexical_cast<int16_t>(value);
28 if(v < 0 || v > 255)
29 throw std::runtime_error("8-bit unsigned int out of range");
30 return static_cast<uint8_t>(v);
31 } catch(std::exception& e) {
32 throw std::runtime_error("Can't parse value '" + value + "': " + e.what());
36 template<> inline std::string parse_value(const std::string& value) throw(std::bad_alloc, std::runtime_error)
38 return value;
41 class regex_results
43 public:
44 regex_results();
45 regex_results(std::vector<std::string> res);
46 operator bool() const;
47 bool operator!() const;
48 size_t size() const;
49 const std::string& operator[](size_t i) const;
50 private:
51 bool matched;
52 std::vector<std::string> results;
55 /**
56 * Regexp a string and return matches.
58 * Parameter regex: The regexp to apply.
59 * Parameter str: The string to apply the regexp to.
60 * Parameter ex: If non-null and string does not match, throw this as std::runtime_error.
61 * Returns: The captures.
63 regex_results regex(const std::string& regex, const std::string& str, const char* ex = NULL)
64 throw(std::bad_alloc, std::runtime_error);
66 /**
67 * Regexp a string and return match result.
69 * Parameter regex: The regexp to apply.
70 * Parameter str: The string to apply the regexp to.
71 * Returns: True if matches, false if not.
73 bool regex_match(const std::string& regex, const std::string& str) throw(std::bad_alloc, std::runtime_error);
75 /**
76 * Cast string to bool.
78 * The following is true: 'on', 'true', 'yes', '1', 'enable', 'enabled'.
79 * The following is false: 'off', 'false', 'no', '0', 'disable', 'disabled'.
80 * Parameter str: The string to cast.
81 * Returns: -1 if string is bad, 0 if false, 1 if true.
83 int string_to_bool(const std::string& cast_to_bool);
85 int32_t utf8_parse_byte(int ch, uint16_t& state) throw();
86 std::u32string to_u32string(const std::string& utf8);
87 std::string to_u8string(const std::u32string& utf32);
88 size_t utf8_strlen(const std::string& str) throw();
90 /**
91 * String formatter
93 class stringfmt
95 public:
96 stringfmt() {}
97 std::string str() { return x.str(); }
98 std::u32string str32() { return to_u32string(x.str()); }
99 template<typename T> stringfmt& operator<<(const T& y) { x << y; return *this; }
100 void throwex() { throw std::runtime_error(x.str()); }
101 private:
102 std::ostringstream x;
105 extern const uint16_t utf8_initial_state;
108 * Iterator copy from UTF-8 to UTF-32
110 template<typename srcitr, typename dstitr>
111 inline void copy_from_utf8(srcitr begin, srcitr end, dstitr target)
113 uint16_t state = utf8_initial_state;
114 for(srcitr i = begin; i != end; i++) {
115 int32_t x = utf8_parse_byte((unsigned char)*i, state);
116 if(x >= 0) {
117 *target = x;
118 ++target;
121 int32_t x = utf8_parse_byte(-1, state);
122 if(x >= 0) {
123 *target = x;
124 ++target;
129 #endif