]>
Dogcows Code - chaz/openbox/blob - ustring.cc
8c48cb4d6e64e0fd7eca8dfe315d93fda51273d4
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
)];
122 ustring::ustring(const ustring
& other
)
123 : _string(other
._string
), _utf8(other
._utf8
)
127 ustring
& ustring::operator=(const ustring
& other
)
129 _string
= other
._string
;
134 ustring::ustring(const std::string
& src
)
135 : _string(src
), _utf8(true)
139 ustring::ustring(const char* src
)
140 : _string(src
), _utf8(true)
144 ustring
& ustring::operator+=(const ustring
& src
)
146 assert(_utf8
== src
._utf8
);
147 _string
+= src
._string
;
151 ustring
& ustring::operator+=(const char* src
)
157 ustring
& ustring::operator+=(char c
)
163 ustring::size_type
ustring::size() const
166 const char *const pdata
= _string
.data();
167 return utf8_ptr_to_offset(pdata
, pdata
+ _string
.size());
169 return _string
.size();
172 ustring::size_type
ustring::bytes() const
174 return _string
.size();
177 ustring::size_type
ustring::capacity() const
179 return _string
.capacity();
182 ustring::size_type
ustring::max_size() const
184 return _string
.max_size();
187 bool ustring::empty() const
189 return _string
.empty();
192 void ustring::clear()
197 ustring
& ustring::erase(ustring::size_type i
, ustring::size_type n
)
200 // find a proper offset
201 size_type utf_i
= utf8_byte_offset(_string
.c_str(), i
);
203 // if the offset is not npos, find a proper length for 'n'
204 size_type utf_n
= utf8_byte_offset(_string
.data() + utf_i
, n
,
205 _string
.size() - utf_i
);
206 _string
.erase(utf_i
, utf_n
);
214 void ustring::resize(ustring::size_type n
, char c
)
217 const size_type size_now
= size();
220 else if(n
> size_now
)
221 _string
.append(n
- size_now
, c
);
223 _string
.resize(n
, c
);
226 ustring::value_type
ustring::operator[](ustring::size_type i
) const
228 return utf8_get_char(utf8_offset_to_ptr(_string
.data(), i
));
231 const char* ustring::data() const
233 return _string
.data();
236 const char* ustring::c_str() const
238 return _string
.c_str();
241 bool ustring::utf8() const
246 void ustring::setUtf8(bool utf8
)
This page took 0.046417 seconds and 3 git commands to generate.