string_utils.hpp

Go to the documentation of this file.
00001 /* $Id: string_utils.hpp 23842 2008-02-16 08:47:16Z mordante $ */
00002 /*
00003    Copyright (C) 2003 by David White <dave@whitevine.net>
00004    Copyright (C) 2005 - 2008 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License version 2
00009    or at your option any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 //! @file serialization/string_utils.hpp
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 //! The type we use to represent Unicode strings.
00038 typedef std::vector<wchar_t> wide_string;
00039 // If we append a 0 to that one,
00040 // we can pass it to SDL_ttf as a const Uint16*
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, //!< REMOVE_EMPTY : remove empty elements
00052       STRIP_SPACES  = 0x02  //!< STRIP_SPACES : strips leading and trailing blank spaces
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 //! Remove whitespace from the front and back of the string 'str'.
00067 std::string &strip(std::string &str);
00068 //! Removes character 'c' from the first and last position of the string 'str'.
00069 std::string& strip_char(std::string &str, const char c);
00070 bool string_bool(const std::string& str,bool def=false);
00071 
00072 //! Try to complete the last word of 'text' with the 'wordlist'.
00073 bool word_completion(std::string& text, std::vector<std::string>& wordlist);
00074 //! Check if a message contains a word.
00075 bool word_match(const std::string& message, const std::string& word);
00076 //! Match using '*' as any number of characters (including none), 
00077 //! and '?' as any one character.
00078 bool wildcard_string_match(const std::string& str, const std::string& match);
00079 //! Check if the username contains only valid characters.
00080 bool isvalid_username(const std::string &login);
00081 
00082 typedef std::map< std::string, t_string > string_map;
00083 //! Function which will interpolate variables, starting with '$' in the string 'str'
00084 //! with the equivalent symbols in the given symbol table.
00085 //! If 'symbols' is NULL, then game event variables will be used instead.
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 //! Functions for converting Unicode wide-char strings
00090 //! to UTF-8 encoded strings, back and forth
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 //! Returns a version of the string with the first letter capitalized
00128 utf8_string capitalize(const utf8_string&);
00129 //! Returns an uppercased version of the string
00130 utf8_string uppercase(const utf8_string&);
00131 //! Returns a lowercased version of the string
00132 utf8_string lowercase(const utf8_string&);
00133 
00134 //! Truncates a string.
00135 void truncate_as_wstring(std::string& str, const size_t size);
00136 
00137 }
00138 
00139 // Handy wrappers around interpolate_variables_into_string and gettext
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

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs