00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef SERIALIZATION_STRING_UTILS_HPP_INCLUDED
00020 #define SERIALIZATION_STRING_UTILS_HPP_INCLUDED
00021
00022 #include <algorithm>
00023 #include <map>
00024 #include <string>
00025 #include <vector>
00026 #include "../tstring.hpp"
00027 #include "../util.hpp"
00028
00029 class variable_set
00030 {
00031 public:
00032 virtual ~variable_set();
00033
00034 virtual const t_string& get_variable_const(const std::string& id) const = 0;
00035 };
00036
00037
00038 typedef std::vector<wchar_t> wide_string;
00039
00040
00041 typedef std::vector<Uint16> ucs2_string;
00042 typedef std::vector<Uint32> ucs4_string;
00043 typedef std::string utf8_string;
00044
00045 namespace utils {
00046
00047 bool isnewline(const char c);
00048 bool portable_isspace(const char c);
00049 bool notspace(char c);
00050
00051 enum { REMOVE_EMPTY = 0x01,
00052 STRIP_SPACES = 0x02
00053 };
00054
00055 std::vector< std::string > split(std::string const &val, char c = ',', int flags = REMOVE_EMPTY | STRIP_SPACES);
00056 std::vector< std::string > paranthetical_split(std::string const &val, const char separator = 0 , std::string const &left="(", std::string const &right=")",int flags = REMOVE_EMPTY | STRIP_SPACES);
00057 std::string join(std::vector< std::string > const &v, char c = ',');
00058 std::vector< std::string > quoted_split(std::string const &val, char c= ',',
00059 int flags = REMOVE_EMPTY | STRIP_SPACES, char quote = '\\');
00060 std::pair< int, int > parse_range(std::string const &str);
00061 std::vector< std::pair< int, int > > parse_ranges(std::string const &str);
00062 int apply_modifier( const int number, const std::string &amount, const int minimum = 0);
00063 std::string &escape(std::string &str, const std::string& special_chars);
00064 std::string &escape(std::string &str);
00065 std::string &unescape(std::string &str);
00066
00067 std::string &strip(std::string &str);
00068
00069 std::string& strip_char(std::string &str, const char c);
00070 bool string_bool(const std::string& str,bool def=false);
00071
00072
00073 bool word_completion(std::string& text, std::vector<std::string>& wordlist);
00074
00075 bool word_match(const std::string& message, const std::string& word);
00076
00077
00078 bool wildcard_string_match(const std::string& str, const std::string& match);
00079
00080 bool isvalid_username(const std::string &login);
00081
00082 typedef std::map< std::string, t_string > string_map;
00083
00084
00085
00086 std::string interpolate_variables_into_string(const std::string &str, const string_map * const symbols);
00087 std::string interpolate_variables_into_string(const std::string &str, const variable_set& variables);
00088
00089
00090
00091 class invalid_utf8_exception : public std::exception {
00092 };
00093
00094 class utf8_iterator
00095 {
00096 public:
00097 typedef std::input_iterator_tag iterator_category;
00098 typedef wchar_t value_type;
00099 typedef ptrdiff_t difference_type;
00100 typedef wchar_t* pointer;
00101 typedef wchar_t& reference;
00102
00103 utf8_iterator(const std::string& str);
00104 utf8_iterator(std::string::const_iterator const &begin, std::string::const_iterator const &end);
00105
00106 static utf8_iterator begin(const std::string& str);
00107 static utf8_iterator end(const std::string& str);
00108
00109 bool operator==(const utf8_iterator& a) const;
00110 bool operator!=(const utf8_iterator& a) const { return ! (*this == a); }
00111 utf8_iterator& operator++();
00112 wchar_t operator*() const;
00113 bool next_is_end();
00114 const std::pair<std::string::const_iterator, std::string::const_iterator>& substr() const;
00115 private:
00116 void update();
00117
00118 wchar_t current_char;
00119 std::string::const_iterator string_end;
00120 std::pair<std::string::const_iterator, std::string::const_iterator> current_substr;
00121 };
00122
00123 std::string wstring_to_string(const wide_string &);
00124 wide_string string_to_wstring(const std::string &);
00125 std::string wchar_to_string(const wchar_t);
00126
00127
00128 utf8_string capitalize(const utf8_string&);
00129
00130 utf8_string uppercase(const utf8_string&);
00131
00132 utf8_string lowercase(const utf8_string&);
00133
00134
00135 void truncate_as_wstring(std::string& str, const size_t size);
00136
00137 }
00138
00139
00140 std::string vgettext(const char*, const utils::string_map&);
00141 std::string vngettext(const char*, const char*, int, const utils::string_map&);
00142
00143 #endif