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