Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SERIALIZATION_STRING_UTILS_HPP_INCLUDED
00018 #define SERIALIZATION_STRING_UTILS_HPP_INCLUDED
00019
00020 #include <algorithm>
00021 #include <map>
00022 #include <sstream>
00023 #include <string>
00024 #include <vector>
00025 #include <boost/next_prior.hpp>
00026
00027 #include "SDL_types.h"
00028
00029
00030 typedef std::vector<wchar_t> wide_string;
00031
00032
00033 typedef std::vector<Uint16> ucs2_string;
00034 typedef std::vector<Uint32> ucs4_string;
00035 typedef std::string utf8_string;
00036
00037 class t_string;
00038
00039 namespace utils {
00040
00041 extern const std::string unicode_minus;
00042 extern const std::string unicode_en_dash;
00043 extern const std::string unicode_em_dash;
00044 extern const std::string unicode_figure_dash;
00045 extern const std::string unicode_multiplication_sign;
00046 extern const std::string unicode_bullet;
00047
00048 bool isnewline(const char c);
00049 bool portable_isspace(const char c);
00050 bool notspace(char c);
00051
00052 enum { REMOVE_EMPTY = 0x01,
00053 STRIP_SPACES = 0x02
00054 };
00055
00056 std::vector< std::string > split(std::string const &val, char c = ',', int flags = REMOVE_EMPTY | STRIP_SPACES);
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 std::vector< std::string > parenthetical_split(std::string const &val,
00079 const char separator = 0 , std::string const &left="(",
00080 std::string const &right=")",int flags = REMOVE_EMPTY | STRIP_SPACES);
00081
00082
00083
00084
00085
00086
00087
00088 template <typename T>
00089 std::string join(T const &v, const std::string& s = ",")
00090 {
00091 std::stringstream str;
00092 for(typename T::const_iterator i = v.begin(); i != v.end(); ++i) {
00093 str << *i;
00094 if (boost::next(i) != v.end())
00095 str << s;
00096 }
00097
00098 return str.str();
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 template<typename T>
00112 std::string bullet_list(const T& v, size_t indent = 4, const std::string& bullet = unicode_bullet)
00113 {
00114 std::ostringstream str;
00115
00116 for(typename T::const_iterator i = v.begin(); i != v.end(); ++i) {
00117 if(i != v.begin()) {
00118 str << '\n';
00119 }
00120
00121 str << std::string(indent, ' ') << bullet << ' ' << *i;
00122 }
00123
00124 return str.str();
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 std::vector< std::string > quoted_split(std::string const &val, char c= ',',
00137 int flags = REMOVE_EMPTY | STRIP_SPACES, char quote = '\\');
00138 std::pair< int, int > parse_range(std::string const &str);
00139 std::vector< std::pair< int, int > > parse_ranges(std::string const &str);
00140 int apply_modifier( const int number, const std::string &amount, const int minimum = 0);
00141
00142
00143 inline std::string print_modifier(const std::string &mod)
00144 { return mod[0] == '-' ?
00145 (unicode_minus + std::string(mod.begin()+1, mod.end())) : ("+" + mod);}
00146
00147
00148 std::string escape(const std::string &str, const char *special_chars);
00149
00150
00151
00152
00153
00154
00155
00156 inline std::string escape(const std::string &str)
00157 { return escape(str, "#@{}+-,\\*="); }
00158
00159
00160 std::string unescape(const std::string &str);
00161
00162
00163 std::string &strip(std::string &str);
00164
00165
00166 bool string_bool(const std::string& str,bool def=false);
00167
00168
00169 std::string signed_value(int val);
00170
00171
00172 std::string half_signed_value(int val);
00173
00174
00175 inline std::string signed_percent(int val) {return signed_value(val) + "%";}
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 std::string si_string(double input, bool base2, std::string unit);
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 bool word_completion(std::string& text, std::vector<std::string>& wordlist);
00199
00200
00201 bool word_match(const std::string& message, const std::string& word);
00202
00203
00204
00205
00206
00207 bool wildcard_string_match(const std::string& str, const std::string& match);
00208
00209
00210
00211
00212
00213
00214 bool isvalid_username(const std::string &login);
00215
00216
00217
00218
00219
00220
00221
00222 bool isvalid_wildcard(const std::string &login);
00223
00224 typedef std::map< std::string, t_string > string_map;
00225
00226
00227
00228
00229
00230 class invalid_utf8_exception : public std::exception {
00231 };
00232
00233 class utf8_iterator
00234 {
00235 public:
00236 typedef std::input_iterator_tag iterator_category;
00237 typedef wchar_t value_type;
00238 typedef ptrdiff_t difference_type;
00239 typedef wchar_t* pointer;
00240 typedef wchar_t& reference;
00241
00242 utf8_iterator(const std::string& str);
00243 utf8_iterator(std::string::const_iterator const &begin, std::string::const_iterator const &end);
00244
00245 static utf8_iterator begin(const std::string& str);
00246 static utf8_iterator end(const std::string& str);
00247
00248 bool operator==(const utf8_iterator& a) const;
00249 bool operator!=(const utf8_iterator& a) const { return ! (*this == a); }
00250 utf8_iterator& operator++();
00251 wchar_t operator*() const;
00252 bool next_is_end();
00253 const std::pair<std::string::const_iterator, std::string::const_iterator>& substr() const;
00254 private:
00255 void update();
00256
00257 wchar_t current_char;
00258 std::string::const_iterator string_end;
00259 std::pair<std::string::const_iterator, std::string::const_iterator> current_substr;
00260 };
00261
00262 std::string wstring_to_string(const wide_string &);
00263 wide_string string_to_wstring(const std::string &);
00264 std::string wchar_to_string(const wchar_t);
00265
00266
00267 utf8_string lowercase(const utf8_string&);
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 void truncate_as_wstring(std::string& str, const size_t size);
00281
00282
00283
00284
00285 void ellipsis_truncate(std::string& str, const size_t size);
00286
00287 }
00288
00289 #endif