-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathu16string_bytes.cpp
More file actions
301 lines (250 loc) · 5.69 KB
/
u16string_bytes.cpp
File metadata and controls
301 lines (250 loc) · 5.69 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <string.h>
#include "../include/encoding/hresult.h"
#include "../include/encoding/u16string_bytes.h"
#include "../include/encoding/encoding_std.h"
u16string_bytes_t::u16string_bytes_t()
{
//assert sizeof(char16_type) >= sizeof(u16string_bytes_type::value_type);
}
u16string_bytes_t::u16string_bytes_t(const u16string_bytes_t &rhs)
{
assign(rhs.c_str(), rhs.size());
}
u16string_bytes_t::u16string_bytes_t(const char16_type *p, size_t l)
{
assign(p, l);
}
u16string_bytes_t::~u16string_bytes_t()
{
;
}
const void * u16string_bytes_t::ptr() const
{
if (empty())
return NULL;
return &buf_[0];
}
size_t u16string_bytes_t::size_of_bytes() const
{
return buf_.size() * sizeof(u16string_bytes_type::value_type);
}
void * u16string_bytes_t::ptr()
{
if (empty())
return NULL;
return &buf_[0];
}
const u16string_bytes_t::char16_type * u16string_bytes_t::char16_ptr() const
{
return (const char16_type *)ptr();
}
size_t u16string_bytes_t::char16_size() const
{
return size_of_bytes() / sizeof(char16_type);
}
bool u16string_bytes_t::empty() const
{
return buf_.empty();
}
size_t u16string_bytes_t::size() const
{
return char16_size();
}
const u16string_bytes_t::char16_type * u16string_bytes_t::c_str() const
{
return char16_ptr();
}
HRESULT u16string_bytes_t::assign(const u16string_bytes_t &rhs)
{
return assign(rhs.c_str(), rhs.size());
}
size_t u16string_bytes_t::find(const u16string_bytes_t &rhs, size_t off) const
{
return find(rhs.c_str(), off, rhs.size());
}
bool u16string_bytes_t::endswith(const u16string_bytes_t &rhs) const
{
if (rhs.size() <= size())
{
return find(rhs, size() - rhs.size()) < size();
}
return false;
}
bool u16string_bytes_t::startswith(const u16string_bytes_t &rhs) const
{
if (rhs.size() <= size())
{
return 0 == memcmp(c_str(), rhs.c_str(), rhs.size_of_bytes());
}
return false;
}
bool u16string_bytes_t::startswith(char16_type c) const
{
u16string_bytes_t v(&c, 1);
return startswith(v);
}
bool u16string_bytes_t::endswith(char16_type c) const
{
u16string_bytes_t v(&c, 1);
return endswith(v);
}
bool u16string_bytes_t::is(const u16string_bytes_t &rhs) const
{
return rhs.size() == size() && startswith(rhs);
}
HRESULT u16string_bytes_t::assign(const char16_type *p, size_t l)
{
if (!(p && l))
return E_INVALIDARG;
const char16_type *e = p + l;
buf_.assign(
(u16string_bytes_type::const_pointer)p, (u16string_bytes_type::const_pointer)e);
return S_OK;
}
HRESULT u16string_bytes_t::assign(const std::string &s)
{
// string_convert string_2_u16string
my_u16string ss;
HRESULT hr;
hr = string_2_u16string(s, ss);
if (FAILED(hr)){
return hr;
}
return assign((const char16_type *)ss.c_str(), ss.size());
}
HRESULT u16string_bytes_t::to_string(std::string &s) const
{
// string_convert u16string_2_string
my_u16string ss;
ss.assign((const my_u16string::value_type *)c_str(), size());
return u16string_2_string(ss, s);
}
size_t u16string_bytes_t::find(const char16_type *substr, size_t off, size_t subsize) const
{
const char16_type *b = c_str();
const char16_type *e = b + size();
size_t default_ = std::string::npos;
if (!(!empty() && subsize && substr))
{
return default_;
}
if (!(off < size() && subsize <= (size() - off)))
{
return default_;
}
b += off;
e = e - subsize + 1;
for (; b < e; ++b)
{
if (*b == *substr && 0 == memcmp(b, substr, subsize * sizeof(char16_type)))
{
return b - c_str();
}
}
return default_;
}
void u16string_bytes_t::tolower()
{
if (empty())
return;
char16_type *b = (char16_type *)ptr();
char16_type *e = b + size();
for (; b < e; ++b)
{
*b = (char16_type)::tolower(*b);
}
}
size_t u16string_bytes_t::find_first_not_of(const u16string_bytes_t &rhs) const
{
size_t default_ = std::string::npos;
if (empty())
return default_;
const char16_type *end = c_str() + size();
for (const char16_type *p = c_str(); p < end; ++p)
{
if (!(rhs.find(p, 0, 1) < rhs.size()))
{
return p - c_str();
}
}
return default_;
}
size_t u16string_bytes_t::find_last_not_of(const u16string_bytes_t &rhs) const
{
size_t default_ = std::string::npos;
if (empty())
return default_;
const char16_type *end = c_str() + size();
for (const char16_type *p = end - 1;; --p)
{
if (!(rhs.find(p, 0, 1) < rhs.size()))
{
return p - c_str();
}
else if (p == c_str())
break;
}
return default_;
}
void u16string_bytes_t::lstrip(const u16string_bytes_t &s)
{
if (empty())
return;
size_t off = find_first_not_of(s);
if (off < size())
{
// erase [0, off)
off *= sizeof(char16_type) / sizeof(u16string_bytes_type::value_type);
if (off < buf_.size())
{
buf_.erase(buf_.begin(), buf_.begin() + off);
}
}
}
void
u16string_bytes_t::rstrip(const u16string_bytes_t &s)
{
if (empty())
return;
size_t off = find_last_not_of(s);
if (off < size())
{
off += 1;
// erase [off, end)
off *= sizeof(char16_type) / sizeof(u16string_bytes_type::value_type);
if (off < buf_.size())
{
buf_.erase(buf_.begin() + off, buf_.end());
}
}
}
void
u16string_bytes_t::trim_head(const u16string_bytes_t &s)
{
lstrip(s);
}
void
u16string_bytes_t::trim_tail(const u16string_bytes_t &s)
{
return rstrip(s);
}
void
u16string_bytes_t::clear()
{
buf_.clear();
}
void
u16string_bytes_t::resize(size_t s)
{
buf_.resize(s * sizeof(value_type) / sizeof(u16string_bytes_type::value_type));
}
u16string_bytes_t::value_type &
u16string_bytes_t::operator[](const size_t off)
{
return *((value_type *)ptr() + off);
}
const u16string_bytes_t::value_type &
u16string_bytes_t::operator[](const size_t off) const
{
return *(char16_ptr() + off);
}