-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.h
More file actions
67 lines (57 loc) · 985 Bytes
/
encode.h
File metadata and controls
67 lines (57 loc) · 985 Bytes
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
#ifndef _UTF8_ENCODE_H
#define _UTF8_ENCODE_H
#include "types.h"
#include "vars.h"
#include "helper.h"
namespace utf8
{
/// 函数声明
bytes *encode(codepoints &);
bytes *encode_char(u32 &);
/**
* 编码,将Unicode codepoint转换成字节流
*/
bytes *encode(codepoints &cplist)
{
auto list = new bytes();
u32 p = 0u;
u8 b = 0u;
int i = 0, // 字节数
s = 0,
j = 0; // 临时变量
for (auto const &cp : cplist)
{
i = detect_char(cp);
if (!i)
continue;
if (i == 1)
{
list->push_back((u8)cp);
}
else
{
p = cp;
s = i - 1;
while (i-- > 0)
{
j = i * 6;
b = p >> j;
p &= ~(b << j);
b |= (i == s) ? ldrop(i + 1) : B10X;
list->push_back(b);
}
}
}
return list;
};
/**
* 将单个 codepoint 编码
*/
bytes *encode_char(u32 &c)
{
auto list = codepoints();
list.push_back(c);
return encode(list);
};
} // namespace utf8
#endif