1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
17 static ustring::size_type utf8_find_offset(const char *str, const char *pos)
19 ustring::size_type offset = 0;
22 str += g_utf8_skip[*str];
23 offset += g_utf8_skip[*str];
29 // First overload: stop on '\0' character.
30 ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset)
32 if(offset == ustring::npos)
37 for(; offset != 0; --offset)
48 // Second overload: stop when reaching maxlen.
49 ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset,
50 ustring::size_type maxlen)
52 if(offset == ustring::npos)
55 const char *const pend = str + maxlen;
58 for(; offset != 0; --offset)
80 ustring::ustring(const ustring& other)
81 : _string(other._string), _utf8(other._utf8)
85 ustring& ustring::operator=(const ustring& other)
87 _string = other._string;
92 ustring::ustring(const std::string& src)
93 : _string(src), _utf8(true)
97 ustring::ustring(const char* src)
98 : _string(src), _utf8(true)
102 ustring& ustring::operator+=(const ustring& src)
104 assert(_utf8 == src._utf8);
105 _string += src._string;
109 ustring& ustring::operator+=(const char* src)
115 ustring& ustring::operator+=(char c)
121 ustring::size_type ustring::size() const
124 const char *const pdata = _string.data();
125 return utf8_find_offset(pdata, pdata + _string.size());
127 return _string.size();
130 ustring::size_type ustring::bytes() const
132 return _string.size();
135 ustring::size_type ustring::capacity() const
137 return _string.capacity();
140 ustring::size_type ustring::max_size() const
142 return _string.max_size();
145 void ustring::clear()
150 ustring& ustring::erase(ustring::size_type i, ustring::size_type n)
153 // find a proper offset
154 size_type utf_i = utf8_byte_offset(_string.c_str(), i);
156 // if the offset is not npos, find a proper length for 'n'
157 size_type utf_n = utf8_byte_offset(_string.data() + utf_i, n,
158 _string.size() - utf_i);
159 _string.erase(utf_i, utf_n);
167 void ustring::resize(ustring::size_type n, char c)
170 const size_type size_now = size();
173 else if(n > size_now)
174 _string.append(n - size_now, c);
176 _string.resize(n, c);
179 const char* ustring::data() const
181 return _string.data();
184 const char* ustring::c_str() const
186 return _string.c_str();
189 bool ustring::utf8() const
194 void ustring::setUtf8(bool utf8)