/
decode.d
228 lines (192 loc) ยท 5.45 KB
/
decode.d
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
/**
* Provides decoding functionality
*/
module ecoji.d.decode;
import ecoji.d.mapping;
import std.array : array;
import std.range : isInputRange, ElementType, walkLength, popFrontN, takeExactly, front, empty;
import std.string : indexOf, assumeUTF;
import std.format : format;
version(unittest) import fluent.asserts;
/**
* This exception is thrown by `decode`
*/
class DecodingException : Exception {
@safe pure @nogc nothrow this(string msg, string file = __FILE__, size_t line = __LINE__) {
super(msg, file, line);
}
}
/**
* Decodes data
* Throws: `DecodingException` if end of data is reached or if it contains invalid runes
* Parameters:
* r = Input range of 'dchar's
* Returns: input range of `ubyte`s
*/
template decode(Range)
if(isInputRange!Range && is(ElementType!Range : dchar)) {
auto decode(Range r) {
return decodeImpl(r);
}
private struct decodeImpl {
private {
Range m_range;
bool m_empty;
ubyte[5] m_buffer;
size_t m_index = 0;
size_t m_length = 0;
}
this(Range r) {
m_range = r;
this.popFront;
}
@property auto front() { return m_buffer[m_index]; }
@property auto empty() { return m_empty; }
void popFront() {
if(++m_index < m_length)
return;
if(m_range.empty) {
m_empty = true;
return;
}
if(m_range.walkLength(4) < 4)
throw new DecodingException("Unexpected end of data");
m_length = 0;
m_index = 0;
dchar[4] runes = m_range.takeExactly(4).array;
m_range.popFrontN(4);
if(EMOJIS.indexOf(runes[0]) == -1)
throw new DecodingException(format!"Invalid rune `%s`"(runes[0]));
int bits1 = runes[0].runeOf;
int bits2 = runes[1].runeOf;
int bits3 = runes[2].runeOf;
int bits4;
switch(runes[3]) {
case PADDING40: bits4 = 0; break;
case PADDING41: bits4 = 1 << 8; break;
case PADDING42: bits4 = 2 << 8; break;
case PADDING43: bits4 = 3 << 8; break;
default: bits4 = runes[3].runeOf; break;
}
m_buffer[0] = cast(ubyte) (bits1 >> 2);
m_buffer[1] = cast(ubyte) (((bits1 & 0x3) << 6) | (bits2 >> 4));
m_buffer[2] = cast(ubyte) (((bits2 & 0xF) << 4) | (bits3 >> 6));
m_buffer[3] = cast(ubyte) (((bits3 & 0x3F) << 2) | (bits4 >> 8));
m_buffer[4] = cast(ubyte) (bits4 & 0xFF);
foreach(i; 1..4) {
++m_length;
if(runes[i] == PADDING)
return;
}
++m_length;
if(runes[3] == PADDING40 || runes[3] == PADDING41 || runes[3] == PADDING42 || runes[3] == PADDING43)
return;
++m_length;
}
}
private @safe pure nothrow @nogc int runeOf(dchar d) {
auto t = cast(int) EMOJIS.indexOf(d);
return t == -1 ? 0 : t;
}
}
@("decode() returns valid input range")
unittest {
static assert(isInputRange!(typeof([EMOJIS['o' << 2], PADDING, PADDING, PADDING].decode)));
}
@("decode() is @safe")
@safe unittest {
[EMOJIS['o' << 2], PADDING, PADDING, PADDING].decode;
}
@("decode() is pure")
pure unittest {
[EMOJIS['o' << 2], PADDING, PADDING, PADDING].decode;
}
@("decode() works for encoded 1-byte values")
unittest {
[EMOJIS['o' << 2], PADDING, PADDING, PADDING].decode.array.should.be.equal([ubyte('o')]);
[EMOJIS['k' << 2], PADDING, PADDING, PADDING].decode.array.should.be.equal([ubyte('k')]);
}
@("decode() works for encoded 2-byte values")
unittest {
[
EMOJIS[0],
EMOJIS[16],
PADDING,
PADDING,
].decode.array.should.be.equal([ubyte(0), ubyte(1)]);
}
@("decode() works for encoded 3-byte values")
unittest {
[
EMOJIS[0],
EMOJIS[16],
EMOJIS[128],
PADDING,
].decode.array.should.be.equal([ubyte(0), ubyte(1), ubyte(2)]);
}
@("decode() works for encoded 4-byte values")
unittest {
[
EMOJIS[0],
EMOJIS[16],
EMOJIS[128],
PADDING40,
].decode.array.should.be.equal([ubyte(0), ubyte(1), ubyte(2), ubyte(0)]);
[
EMOJIS[0],
EMOJIS[16],
EMOJIS[128],
PADDING41,
].decode.array.should.be.equal([ubyte(0), ubyte(1), ubyte(2), ubyte(1)]);
[
EMOJIS[0],
EMOJIS[16],
EMOJIS[128],
PADDING42,
].decode.array.should.be.equal([ubyte(0), ubyte(1), ubyte(2), ubyte(2)]);
[
EMOJIS[0],
EMOJIS[16],
EMOJIS[128],
PADDING43,
].decode.array.should.be.equal([ubyte(0), ubyte(1), ubyte(2), ubyte(3)]);
}
@("decode() works for encoded 5-byte values")
unittest {
[
EMOJIS[687],
EMOJIS[222],
EMOJIS[960],
EMOJIS[291],
].decode.array.should.be.equal([ubyte(0xAB), ubyte(0xCD), ubyte(0xEF), ubyte(0x01), ubyte(0x23)]);
}
@("decode() can decode encoded strings")
unittest {
"๐๐ธ๐โ".decode.array.assumeUTF.should.be.equal("abc");
"๐ฅ๐ค ๐ ๐".decode.array.assumeUTF.should.be.equal("6789");
"๐ฒ๐ก๐โ".decode.array.assumeUTF.should.be.equal("XY\n");
"๐๐ฉ๐ฆ๐๐๐๐ฏ๐๐๐ฝ๐๐๐ฑ๐ฅ๐๐ฑ๐๐ญ๐ฎ๐ต๐ข๐ฅ๐ญ๐ธ๐๐ฒ๐ฆ๐ถ๐ข๐ฅ๐ฎ๐บ๐๐ธ๐ฎ๐ผ๐ฆ๐๐ฅด๐"
.decode.array.assumeUTF.should.be.equal("Base64 is so 1999, isn't there something better?\n");
}
@("decode() throws an exception if end of data is reached unexpectedly")
unittest {
({
[EMOJIS[1], EMOJIS[2], EMOJIS[3]].decode;
}).should.throwException!DecodingException
.msg.should.be.equal("Unexpected end of data");
({
[EMOJIS[1], EMOJIS[2]].decode;
}).should.throwException!DecodingException
.msg.should.be.equal("Unexpected end of data");
({
[EMOJIS[1]].decode;
}).should.throwException!DecodingException
.msg.should.be.equal("Unexpected end of data");
}
@("decode() throws an exception if rune is invalid")
unittest {
({
['N', 'O', 'T', 'V', 'A', 'L', 'I', 'D'].decode;
}).should.throwException!DecodingException
.msg.should.be.equal("Invalid rune `N`");
}