-
Notifications
You must be signed in to change notification settings - Fork 1
/
utf8_xxx.h
79 lines (66 loc) · 2.75 KB
/
utf8_xxx.h
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
#ifndef _UTF8_XXX_H_
#define _UTF8_XXX_H_
#include <string>
inline size_t utf8_len(const std::string& _Str)
{
size_t _idx = 0, _len = 0;
while (_Str[_idx])
_len += ((_Str[_idx++] & 0xc0) != 0x80);
return _len;
}
std::string utf8_sub(const std::string& _Str, const size_t _Off, const size_t _Count = std::string::npos);
size_t utf8_find(const std::string& _Str, const std::string& _Dst, const size_t _Off = 0);
size_t utf8_rfind(const std::string& _Str, const std::string& _Dst, const size_t _Off = std::string::npos);
std::string& utf8_insert(std::string& _Str, const size_t _Off, const std::string& _Ins);
#ifdef _UTF8_XXX_IMPLEMENTATION
std::string utf8_sub(const std::string& _Str, const size_t _Off, size_t _Count)
{
size_t _idx_utf8 = 0, _idx_begin_raw = 0;
while (_idx_utf8 < _Off && _Str[_idx_begin_raw])
_idx_utf8 += ((_Str[_idx_begin_raw++] & 0xc0) != 0x80);
while ((_Str[_idx_begin_raw] & 0xc0) == 0x80 && _Str[_idx_begin_raw])
_idx_begin_raw++;
if (_Count == std::string::npos)
return _Str.substr(_idx_begin_raw);
size_t _idx_end_raw = _idx_begin_raw;
while (_idx_utf8 < _Off + _Count && _Str[_idx_end_raw])
_idx_utf8 += ((_Str[_idx_end_raw++] & 0xc0) != 0x80);
while ((_Str[_idx_end_raw] & 0xc0) == 0x80 && _Str[_idx_end_raw])
_idx_end_raw++;
return _Str.substr(_idx_begin_raw, _idx_end_raw - _idx_begin_raw);
}
size_t utf8_find(const std::string& _Str, const std::string& _Dst, const size_t _Off)
{
size_t _idx_utf8 = 0, _idx_raw = 0;
while (_idx_utf8 < _Off && _Str[_idx_raw])
_idx_utf8 += ((_Str[_idx_raw++] & 0xc0) != 0x80);
while ((_Str[_idx_raw] & 0xc0) == 0x80 && _Str[_idx_raw]) _idx_raw++;
size_t _idx_dst_raw = _Str.find(_Dst, _idx_raw);
if (_idx_dst_raw == std::string::npos) return std::string::npos;
while (_idx_raw < _idx_dst_raw)
_idx_utf8 += ((_Str[_idx_raw++] & 0xc0) != 0x80);
return _idx_utf8;
}
size_t utf8_rfind(const std::string& _Str, const std::string& _Dst, const size_t _Off)
{
size_t _idx_utf8 = 0, _idx_raw = 0;
while (_idx_utf8 < _Off && _Str[_idx_raw])
_idx_utf8 += ((_Str[_idx_raw++] & 0xc0) != 0x80);
while ((_Str[_idx_raw] & 0xc0) == 0x80 && _Str[_idx_raw]) _idx_raw++;
size_t _idx_dst_raw = _Str.rfind(_Dst, _idx_raw);
if (_idx_dst_raw == std::string::npos) return std::string::npos;
_idx_utf8 = 0, _idx_raw = 0;
while (_idx_raw < _idx_dst_raw)
_idx_utf8 += ((_Str[_idx_raw++] & 0xc0) != 0x80);
return _idx_utf8;
}
std::string& utf8_insert(std::string& _Str, const size_t _Off, const std::string& _Ins)
{
size_t _idx_utf8 = 0, _idx_raw = 0;
while (_idx_utf8 < _Off && _Str[_idx_raw])
_idx_utf8 += ((_Str[_idx_raw++] & 0xc0) != 0x80);
while ((_Str[_idx_raw] & 0xc0) == 0x80 && _Str[_idx_raw]) _idx_raw++;
return _Str.insert(_idx_raw, _Ins);
}
#endif // _UTF8_XXX_IMPLEMENTATION
#endif // !_UTF8_XXX_H_