]>
Dogcows Code - chaz/openbox/blob - otk/ustring.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
17 // The number of bytes to skip to find the next character in the string
18 static const char utf8_skip
[256] = {
19 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,
20 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,
21 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,
22 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,
23 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,
24 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,
25 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,
26 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
29 // takes a pointer into a utf8 string and returns a unicode character for the
30 // first character at the pointer
31 unichar
utf8_get_char (const char *p
)
33 unichar result
= static_cast<unsigned char>(*p
);
35 // if its not a 7-bit ascii character
36 if((result
& 0x80) != 0) {
37 // len is the number of bytes this character takes up in the string
38 unsigned char len
= utf8_skip
[result
];
39 result
&= 0x7F >> len
;
43 result
|= static_cast<unsigned char>(*++p
) & 0x3F;
50 // takes a pointer into a string and finds its offset
51 static ustring::size_type
utf8_ptr_to_offset(const char *str
, const char *pos
)
53 ustring::size_type offset
= 0;
56 str
+= utf8_skip
[static_cast<unsigned char>(*str
)];
63 // takes an offset into a string and returns a pointer to it
64 const char *utf8_offset_to_ptr(const char *str
, ustring::size_type offset
)
67 str
+= utf8_skip
[static_cast<unsigned char>(*str
)];
71 // First overload: stop on '\0' character.
72 ustring::size_type
utf8_byte_offset(const char* str
, ustring::size_type offset
)
74 if(offset
== ustring::npos
)
79 for(; offset
!= 0; --offset
)
84 p
+= utf8_skip
[static_cast<unsigned char>(*p
)];
90 // Second overload: stop when reaching maxlen.
91 ustring::size_type
utf8_byte_offset(const char* str
, ustring::size_type offset
,
92 ustring::size_type maxlen
)
94 if(offset
== ustring::npos
)
97 const char *const pend
= str
+ maxlen
;
100 for(; offset
!= 0; --offset
)
103 return ustring::npos
;
105 p
+= utf8_skip
[static_cast<unsigned char>(*p
)];
114 ustring::ustring(bool utf8
)
123 ustring::ustring(const ustring
& other
)
124 : _string(other
._string
), _utf8(other
._utf8
)
128 ustring
& ustring::operator=(const ustring
& other
)
130 _string
= other
._string
;
135 ustring::ustring(const std::string
& src
, bool utf8
)
136 : _string(src
), _utf8(utf8
)
140 ustring::ustring(const char* src
, bool utf8
)
141 : _string(src
), _utf8(utf8
)
145 ustring
& ustring::operator+=(const ustring
& src
)
147 assert(_utf8
== src
._utf8
);
148 _string
+= src
._string
;
152 ustring
& ustring::operator+=(const char* src
)
158 ustring
& ustring::operator+=(char c
)
164 ustring::size_type
ustring::size() const
167 const char *const pdata
= _string
.data();
168 return utf8_ptr_to_offset(pdata
, pdata
+ _string
.size());
170 return _string
.size();
173 ustring::size_type
ustring::bytes() const
175 return _string
.size();
178 ustring::size_type
ustring::capacity() const
180 return _string
.capacity();
183 ustring::size_type
ustring::max_size() const
185 return _string
.max_size();
188 bool ustring::empty() const
190 return _string
.empty();
193 void ustring::clear()
198 ustring
& ustring::erase(ustring::size_type i
, ustring::size_type n
)
201 // find a proper offset
202 size_type utf_i
= utf8_byte_offset(_string
.c_str(), i
);
204 // if the offset is not npos, find a proper length for 'n'
205 size_type utf_n
= utf8_byte_offset(_string
.data() + utf_i
, n
,
206 _string
.size() - utf_i
);
207 _string
.erase(utf_i
, utf_n
);
215 void ustring::resize(ustring::size_type n
, char c
)
218 const size_type size_now
= size();
221 else if(n
> size_now
)
222 _string
.append(n
- size_now
, c
);
224 _string
.resize(n
, c
);
227 ustring::value_type
ustring::operator[](ustring::size_type i
) const
229 return utf8_get_char(utf8_offset_to_ptr(_string
.data(), i
));
232 bool ustring::operator==(const ustring
&other
) const
234 return _string
== other
._string
&& _utf8
== other
._utf8
;
237 bool ustring::operator==(const std::string
&other
) const
239 return _string
== other
;
242 bool ustring::operator==(const char *other
) const
244 return _string
== other
;
247 const char* ustring::data() const
249 return _string
.data();
252 const char* ustring::c_str() const
254 return _string
.c_str();
257 bool ustring::utf8() const
262 void ustring::setUtf8(bool utf8
)
This page took 0.046614 seconds and 4 git commands to generate.