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 unichar
utf8_get_char(const char *p
);
37 #endif // DOXYGEN_IGNORE
39 //! The iterator type for ustring
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.
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
52 class ustring_Iterator
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;
61 inline ustring_Iterator() {}
62 inline ustring_Iterator(const ustring_Iterator
<std::string::iterator
>&
63 other
) : _pos(other
.base()) {}
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
));
73 inline ustring_Iterator
<T
> & operator++() {
74 pos_
+= g_utf8_skip
[static_cast<unsigned char>(*pos_
)];
77 inline ustring_Iterator
<T
> & operator--() {
78 do { --_pos
; } while((*_pos
& '\xC0') == '\x80');
82 explicit inline ustring_Iterator(T pos
) : _pos(pos
) {}
83 inline T
base() const { return _pos
; }
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.
94 This class does <b>not</b> handle extended 8-bit ASCII charsets like
97 More info on Unicode and UTF-8 can be found here:
98 http://www.cl.cam.ac.uk/~mgk25/unicode.html
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.
108 typedef std::string::size_type size_type
;
109 typedef std::string::difference_type difference_type
;
111 typedef unichar value_type
;
112 //typedef unichar & reference;
113 //typedef const unichar & const_reference;
115 //typedef ustring_Iterator<std::string::iterator> iterator;
116 //typedef ustring_Iterator<std::string::const_iterator> const_iterator;
118 static const size_type npos
= std::string::npos
;
125 ustring(const ustring
& other
);
126 ustring
& operator=(const ustring
& other
);
127 ustring(const std::string
& src
);
128 ustring(const char* src
);
130 // append to the string
132 ustring
& operator+=(const ustring
& src
);
133 ustring
& operator+=(const char* src
);
134 ustring
& operator+=(char c
);
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;
147 ustring
& erase(size_type i
, size_type n
=npos
);
149 // change the string's size
151 void resize(size_type n
, char c
='\0');
153 // extract characters
155 // No reference return; use replace() to write characters.
156 value_type
operator[](size_type i
) const;
160 const char* data() const;
161 const char* c_str() const;
166 void setUtf8(bool utf8
);
171 #endif // __ustring_hh