1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
6 @brief Provides a simple UTF-8 encoded string
14 # ifdef HAVE_SYS_TYPES_H
15 # include <sys/types.h>
27 typedef uint32_t unichar
;
29 typedef u_int32_t unichar
;
33 #ifndef DOXYGEN_IGNORE
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
47 unichar
utf8_get_char(const char *p
);
49 #endif // DOXYGEN_IGNORE
51 //! The iterator type for ustring
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.
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
64 class ustring_Iterator
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;
73 inline ustring_Iterator() {}
74 inline ustring_Iterator(const ustring_Iterator
<std::string::iterator
>&
75 other
) : _pos(other
.base()) {}
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
));
85 inline ustring_Iterator
<T
> & operator++() {
86 pos_
+= g_utf8_skip
[static_cast<unsigned char>(*pos_
)];
89 inline ustring_Iterator
<T
> & operator--() {
90 do { --_pos
; } while((*_pos
& '\xC0') == '\x80');
94 explicit inline ustring_Iterator(T pos
) : _pos(pos
) {}
95 inline T
base() const { return _pos
; }
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.
106 This class does <b>not</b> handle extended 8-bit ASCII charsets like
109 More info on Unicode and UTF-8 can be found here:
110 http://www.cl.cam.ac.uk/~mgk25/unicode.html
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.
120 typedef std::string::size_type size_type
;
121 typedef std::string::difference_type difference_type
;
123 typedef unichar value_type
;
124 //typedef unichar & reference;
125 //typedef const unichar & const_reference;
127 //typedef ustring_Iterator<std::string::iterator> iterator;
128 //typedef ustring_Iterator<std::string::const_iterator> const_iterator;
130 static const size_type npos
= std::string::npos
;
137 ustring(const ustring
& other
);
138 ustring
& operator=(const ustring
& other
);
139 ustring(const std::string
& src
);
140 ustring(const char* src
);
142 // append to the string
144 ustring
& operator+=(const ustring
& src
);
145 ustring
& operator+=(const char* src
);
146 ustring
& operator+=(char c
);
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;
159 ustring
& erase(size_type i
, size_type n
=npos
);
161 // change the string's size
163 void resize(size_type n
, char c
='\0');
165 // extract characters
167 // No reference return; use replace() to write characters.
168 value_type
operator[](size_type i
) const;
172 const char* data() const;
173 const char* c_str() const;
178 void setUtf8(bool utf8
);
183 #endif // __ustring_hh