This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8.cpp
More file actions
118 lines (99 loc) · 2.95 KB
/
Copy pathutf8.cpp
File metadata and controls
118 lines (99 loc) · 2.95 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
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "utf8.hpp"
#include <iostream>
#include <sstream>
#include "logger.hpp"
namespace TextEncoding {
std::vector<Unicode::CodePoint>
UTF8::ASCIIDecode(const char *data, std::size_t size) {
std::vector<Unicode::CodePoint> result(size);
for (std::size_t i = 0; i < size; i++)
result[i] = static_cast<unsigned char>(static_cast<Unicode::CodePoint>(data[i]));
return result;
}
bool
UTF8::Decode(const char *data, std::size_t size) {
std::size_t bytePosition = 0;
Output.clear();
CodePoint = 0;
BytesSeen = 0;
BytesNeeded = 0;
LowerBoundary = 0x80;
UpperBoundary = 0xBF;
while (true) {
if (bytePosition == size) {
if (BytesNeeded != 0) {
BytesNeeded = 0;
Logger::Warning("TextEncoding::UTF8::Decode", "BytesLeft = 0 but BytesNeeded > 0");
return false;
}
// finished:
return true;
}
uint8_t currentByte = data[bytePosition++];
if (BytesNeeded == 0) {
// TODO Is the ""to"" of (`0xE0 ""to"" 0xEF`) inclusive or not?
if (currentByte <= 0x7F) {
Output.push_back(currentByte);
continue;
}
if (currentByte >= 0xC2 && currentByte <= 0xDF) {
BytesNeeded = 1;
CodePoint = currentByte & 0x1F;
continue;
}
if (currentByte >= 0xE0 && currentByte <= 0xEF) {
if (currentByte == 0xE0)
LowerBoundary = 0xA0;
else if (currentByte == 0xED)
UpperBoundary = 0x9F;
BytesNeeded = 2;
CodePoint = currentByte & 0xF;
continue;
}
if (currentByte >= 0xF0 && currentByte <= 0xF4) {
if (currentByte == 0xF0)
LowerBoundary = 0x90;
else if (currentByte == 0xF4)
UpperBoundary = 0x8F;
BytesNeeded = 3;
CodePoint = currentByte & 0x7;
continue;
}
std::stringstream info;
info << "Octet out of scope: 0x" << std::hex << static_cast<uint16_t>(currentByte) << std::dec;
Logger::Error("TextEncoding::UTF8::Decode", info.str());
return false;
}
// 'in range ... to ... `inclusive`, then'??
if (currentByte < LowerBoundary || currentByte > UpperBoundary) {
CodePoint = 0;
BytesNeeded = 0;
BytesSeen = 0;
LowerBoundary = 0x80;
UpperBoundary = 0xBF;
// TODO 'Prepend byte to stream'
std::stringstream info;
info << "Character out of boundaries: [0x" << std::hex << static_cast<uint16_t>(LowerBoundary) << ", 0x"
<< static_cast<uint16_t>(UpperBoundary) << "], value: 0x" << static_cast<uint16_t>(currentByte)
<< std::dec;
Logger::Error("TextEncoding::UTF8::Decode", info.str());
return false;
}
LowerBoundary = 0x80;
UpperBoundary = 0xBF;
CodePoint = (CodePoint << 6) | (currentByte & 0x3F);
BytesSeen += 1;
if (BytesSeen != BytesNeeded)
continue;
Output.push_back(CodePoint);
CodePoint = 0;
BytesNeeded = 0;
BytesSeen = 0;
}
}
} // namespace TextEncoding