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