]> icculus.org git repositories - mikachu/openbox.git/blob - otk/ustring.hh
add otk::MessageDialog
[mikachu/openbox.git] / otk / ustring.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef   __ustring_hh
3 #define   __ustring_hh
4
5 /*! @file ustring.hh
6   @brief Provides a simple UTF-8 encoded string
7 */
8
9 extern "C" {
10
11 #ifdef HAVE_STDINT_H
12 #  include <stdint.h>
13 #else
14 #  ifdef HAVE_SYS_TYPES_H
15 #    include <sys/types.h>
16 #  endif
17 #endif
18
19 }
20
21 #include <string>
22
23 namespace otk {
24
25
26 #ifdef HAVE_STDINT_H
27 typedef uint32_t unichar;
28 #else
29 typedef u_int32_t unichar;
30 #endif
31
32
33 #ifndef DOXYGEN_IGNORE
34
35 unichar utf8_get_char(const char *p);
36
37 #endif // DOXYGEN_IGNORE
38
39 //! The iterator type for ustring
40 /*!
41   Note this is not a random access iterator but a bidirectional one, since all
42   index operations need to iterate over the UTF-8 data.  Use std::advance() to
43   move to a certain position.
44   <p>
45   A writeable iterator isn't provided because:  The number of bytes of the old
46   UTF-8 character and the new one to write could be different. Therefore, any
47   write operation would invalidate all other iterators pointing into the same
48   string.
49 */
50
51 template <class T>
52 class ustring_Iterator
53 {
54 public:
55   typedef std::bidirectional_iterator_tag   iterator_category;
56   typedef unichar                           value_type;
57   typedef std::string::difference_type      difference_type;
58   //typedef value_type                        reference;
59   typedef void                              pointer;
60   
61   inline ustring_Iterator() {}
62   inline ustring_Iterator(const ustring_Iterator<std::string::iterator>&
63                           other) : _pos(other.base()) {}
64
65
66   inline value_type operator*() const {
67     // get an iterator to the internal string
68     std::string::const_iterator pos = _pos;
69     return utf8_get_char(&(*pos));
70   }
71
72   
73   inline ustring_Iterator<T> &     operator++() {
74     pos_ += g_utf8_skip[static_cast<unsigned char>(*pos_)];
75     return *this;
76   }
77   inline ustring_Iterator<T> &     operator--() {
78     do { --_pos; } while((*_pos & '\xC0') == '\x80');
79     return *this;
80   }
81
82   explicit inline ustring_Iterator(T pos) : _pos(pos) {}
83   inline T base() const { return _pos; }
84
85 private:
86   T _pos;
87 };
88
89
90 //! This class provides a simple wrapper to a std::string that can be encoded
91 //! as UTF-8. The ustring::utf() member specifies if the given string is UTF-8
92 //! encoded. ustrings default to specifying UTF-8 encoding.
93 /*!
94   This class does <b>not</b> handle extended 8-bit ASCII charsets like
95   ISO-8859-1.
96   <p>
97   More info on Unicode and UTF-8 can be found here:
98   http://www.cl.cam.ac.uk/~mgk25/unicode.html
99   <p>
100   This does not subclass std::string, because std::string was intended to be a
101   final class.  For instance, it does not have a virtual destructor.
102 */
103 class ustring {
104   std::string _string;
105   bool _utf8;
106   
107 public:
108   typedef std::string::size_type                        size_type;
109   typedef std::string::difference_type                  difference_type;
110
111   typedef unichar                                       value_type;
112   //typedef unichar &                                     reference;
113   //typedef const unichar &                               const_reference;
114
115   //typedef ustring_Iterator<std::string::iterator>       iterator;
116   //typedef ustring_Iterator<std::string::const_iterator> const_iterator;
117
118   static const size_type npos = std::string::npos;
119
120   ustring(bool utf8 = true);
121   ~ustring();
122
123   // make new strings
124   
125   ustring(const ustring& other);
126   ustring& operator=(const ustring& other);
127   ustring(const std::string& src, bool utf8 = true);
128   ustring(const char* src, bool utf8 = true);
129
130   // append to the string
131
132   ustring& operator+=(const ustring& src);
133   ustring& operator+=(const char* src);
134   ustring& operator+=(char c);
135
136   // sizes
137   
138   ustring::size_type size() const;
139   ustring::size_type bytes() const;
140   ustring::size_type capacity() const;
141   ustring::size_type max_size() const;
142   bool empty() const;
143
144   // erase substrings
145
146   void clear();
147   ustring& erase(size_type i, size_type n=npos);
148
149   // change the string's size
150
151   void resize(size_type n, char c='\0');
152
153   // extract characters
154   
155   // No reference return; use replace() to write characters.
156   value_type operator[](size_type i) const;
157
158   // compare strings
159
160   bool operator==(const ustring &other) const;
161   bool operator==(const std::string &other) const;
162   bool operator==(const char *other) const;
163
164   // internal data
165
166   const char* data()  const;
167   const char* c_str() const;
168
169   // encoding
170   
171   bool utf8() const;
172   void setUtf8(bool utf8);
173 };
174
175 }
176
177 #endif // __ustring_hh