1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#ifndef REPLXX_UNICODESTRING_HXX_INCLUDED
#define REPLXX_UNICODESTRING_HXX_INCLUDED
#include <vector>
#include <cstring>
#include "conversion.hxx"
namespace replxx {
class UnicodeString {
public:
typedef std::vector<char32_t> data_buffer_t;
typedef data_buffer_t::const_iterator const_iterator;
typedef data_buffer_t::iterator iterator;
private:
data_buffer_t _data;
public:
UnicodeString()
: _data() {
}
explicit UnicodeString( std::string const& src )
: _data() {
assign( src );
}
explicit UnicodeString( char const* src )
: _data() {
assign( src );
}
explicit UnicodeString( char8_t const* src )
: UnicodeString( reinterpret_cast<const char*>( src ) ) {
}
explicit UnicodeString( char32_t const* src )
: _data() {
int len( 0 );
while ( src[len] != 0 ) {
++ len;
}
_data.assign( src, src + len );
}
explicit UnicodeString( char32_t const* src, int len )
: _data() {
_data.assign( src, src + len );
}
explicit UnicodeString( int len )
: _data() {
_data.resize( len );
}
UnicodeString& assign( std::string const& str_ ) {
_data.resize( str_.length() );
int len( 0 );
copyString8to32( _data.data(), str_.length(), len, str_.c_str() );
_data.resize( len );
return *this;
}
UnicodeString& assign( char const* str_ ) {
size_t byteCount( strlen( str_ ) );
_data.resize( byteCount );
int len( 0 );
copyString8to32( _data.data(), byteCount, len, str_ );
_data.resize( len );
return *this;
}
UnicodeString& assign( UnicodeString const& other_ ) {
_data = other_._data;
return *this;
}
explicit UnicodeString( UnicodeString const& ) = default;
UnicodeString& operator = ( UnicodeString const& ) = default;
UnicodeString( UnicodeString&& ) = default;
UnicodeString& operator = ( UnicodeString&& ) = default;
bool operator == ( UnicodeString const& other_ ) const {
return ( _data == other_._data );
}
bool operator != ( UnicodeString const& other_ ) const {
return ( _data != other_._data );
}
UnicodeString& append( UnicodeString const& other ) {
_data.insert( _data.end(), other._data.begin(), other._data.end() );
return *this;
}
UnicodeString& append( char32_t const* src, int len ) {
_data.insert( _data.end(), src, src + len );
return *this;
}
UnicodeString& insert( int pos_, UnicodeString const& str_, int offset_, int len_ ) {
_data.insert( _data.begin() + pos_, str_._data.begin() + offset_, str_._data.begin() + offset_ + len_ );
return *this;
}
UnicodeString& insert( int pos_, char32_t c_ ) {
_data.insert( _data.begin() + pos_, c_ );
return *this;
}
UnicodeString& erase( int pos_ ) {
_data.erase( _data.begin() + pos_ );
return *this;
}
UnicodeString& erase( int pos_, int len_ ) {
_data.erase( _data.begin() + pos_, _data.begin() + pos_ + len_ );
return *this;
}
char32_t const* get() const {
return _data.data();
}
char32_t* get() {
return _data.data();
}
int length() const {
return static_cast<int>( _data.size() );
}
void clear( void ) {
_data.clear();
}
const char32_t& operator[]( size_t pos ) const {
return _data[pos];
}
char32_t& operator[]( size_t pos ) {
return _data[pos];
}
bool starts_with( data_buffer_t::const_iterator first_, data_buffer_t::const_iterator last_ ) const {
return (
( std::distance( first_, last_ ) <= length() )
&& ( std::equal( first_, last_, _data.begin() ) )
);
}
bool is_empty( void ) const {
return ( _data.size() == 0 );
}
void swap( UnicodeString& other_ ) {
_data.swap( other_._data );
}
const_iterator begin( void ) const {
return ( _data.begin() );
}
const_iterator end( void ) const {
return ( _data.end() );
}
iterator begin( void ) {
return ( _data.begin() );
}
iterator end( void ) {
return ( _data.end() );
}
};
}
#endif
|