-
Notifications
You must be signed in to change notification settings - Fork 11
/
Bytes.js
executable file
·185 lines (154 loc) · 5.46 KB
/
Bytes.js
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
/*
import Bytes.Encode as Encode exposing (getWidth, write)
import Elm.Kernel.Scheduler exposing (binding, succeed)
import Elm.Kernel.Utils exposing (Tuple2, chr)
import Maybe exposing (Just, Nothing)
*/
// BYTES
function _Bytes_width(bytes)
{
return bytes.byteLength;
}
var _Bytes_getHostEndianness = F2(function(le, be)
{
return __Scheduler_binding(function(callback)
{
callback(__Scheduler_succeed(new Uint8Array(new Uint32Array([1]))[0] === 1 ? le : be));
});
});
// ENCODERS
function _Bytes_encode(encoder)
{
var mutableBytes = new DataView(new ArrayBuffer(__Encode_getWidth(encoder)));
__Encode_write(encoder)(mutableBytes)(0);
return mutableBytes;
}
// SIGNED INTEGERS
var _Bytes_write_i8 = F3(function(mb, i, n) { mb.setInt8(i, n); return i + 1; });
var _Bytes_write_i16 = F4(function(mb, i, n, isLE) { mb.setInt16(i, n, isLE); return i + 2; });
var _Bytes_write_i32 = F4(function(mb, i, n, isLE) { mb.setInt32(i, n, isLE); return i + 4; });
// UNSIGNED INTEGERS
var _Bytes_write_u8 = F3(function(mb, i, n) { mb.setUint8(i, n); return i + 1 ;});
var _Bytes_write_u16 = F4(function(mb, i, n, isLE) { mb.setUint16(i, n, isLE); return i + 2; });
var _Bytes_write_u32 = F4(function(mb, i, n, isLE) { mb.setUint32(i, n, isLE); return i + 4; });
// FLOATS
var _Bytes_write_f32 = F4(function(mb, i, n, isLE) { mb.setFloat32(i, n, isLE); return i + 4; });
var _Bytes_write_f64 = F4(function(mb, i, n, isLE) { mb.setFloat64(i, n, isLE); return i + 8; });
// BYTES
var _Bytes_write_bytes = F3(function(mb, offset, bytes)
{
for (var i = 0, len = bytes.byteLength, limit = len - 4; i <= limit; i += 4)
{
mb.setUint32(offset + i, bytes.getUint32(i));
}
for (; i < len; i++)
{
mb.setUint8(offset + i, bytes.getUint8(i));
}
return offset + len;
});
// STRINGS
function _Bytes_getStringWidth(string)
{
for (var width = 0, i = 0; i < string.length; i++)
{
var code = string.charCodeAt(i);
width +=
(code < 0x80) ? 1 :
(code < 0x800) ? 2 :
(code < 0xD800 || 0xDBFF < code) ? 3 : (i++, 4);
}
return width;
}
var _Bytes_write_string = F3(function(mb, offset, string)
{
for (var i = 0; i < string.length; i++)
{
var code = string.charCodeAt(i);
offset +=
(code < 0x80)
? (mb.setUint8(offset, code)
, 1
)
:
(code < 0x800)
? (mb.setUint16(offset, 0xC080 /* 0b1100000010000000 */
| (code >>> 6 & 0x1F /* 0b00011111 */) << 8
| code & 0x3F /* 0b00111111 */)
, 2
)
:
(code < 0xD800 || 0xDBFF < code)
? (mb.setUint16(offset, 0xE080 /* 0b1110000010000000 */
| (code >>> 12 & 0xF /* 0b00001111 */) << 8
| code >>> 6 & 0x3F /* 0b00111111 */)
, mb.setUint8(offset + 2, 0x80 /* 0b10000000 */
| code & 0x3F /* 0b00111111 */)
, 3
)
:
(code = (code - 0xD800) * 0x400 + string.charCodeAt(++i) - 0xDC00 + 0x10000
, mb.setUint32(offset, 0xF0808080 /* 0b11110000100000001000000010000000 */
| (code >>> 18 & 0x7 /* 0b00000111 */) << 24
| (code >>> 12 & 0x3F /* 0b00111111 */) << 16
| (code >>> 6 & 0x3F /* 0b00111111 */) << 8
| code & 0x3F /* 0b00111111 */)
, 4
);
}
return offset;
});
// DECODER
var _Bytes_decode = F2(function(decoder, bytes)
{
try {
return __Maybe_Just(A2(decoder, bytes, 0).b);
} catch(e) {
return __Maybe_Nothing;
}
});
var _Bytes_read_i8 = F2(function( bytes, offset) { return __Utils_Tuple2(offset + 1, bytes.getInt8(offset)); });
var _Bytes_read_i16 = F3(function(isLE, bytes, offset) { return __Utils_Tuple2(offset + 2, bytes.getInt16(offset, isLE)); });
var _Bytes_read_i32 = F3(function(isLE, bytes, offset) { return __Utils_Tuple2(offset + 4, bytes.getInt32(offset, isLE)); });
var _Bytes_read_u8 = F2(function( bytes, offset) { return __Utils_Tuple2(offset + 1, bytes.getUint8(offset)); });
var _Bytes_read_u16 = F3(function(isLE, bytes, offset) { return __Utils_Tuple2(offset + 2, bytes.getUint16(offset, isLE)); });
var _Bytes_read_u32 = F3(function(isLE, bytes, offset) { return __Utils_Tuple2(offset + 4, bytes.getUint32(offset, isLE)); });
var _Bytes_read_f32 = F3(function(isLE, bytes, offset) { return __Utils_Tuple2(offset + 4, bytes.getFloat32(offset, isLE)); });
var _Bytes_read_f64 = F3(function(isLE, bytes, offset) { return __Utils_Tuple2(offset + 8, bytes.getFloat64(offset, isLE)); });
var _Bytes_read_bytes = F3(function(len, bytes, offset)
{
return __Utils_Tuple2(offset + len, new DataView(bytes.buffer, bytes.byteOffset + offset, len));
});
var _Bytes_read_string = F3(function(len, bytes, offset)
{
var string = '';
var end = offset + len;
for (; offset < end;)
{
var byte = bytes.getUint8(offset++);
string +=
(byte < 128)
? String.fromCharCode(byte)
:
((byte & 0xE0 /* 0b11100000 */) === 0xC0 /* 0b11000000 */)
? String.fromCharCode((byte & 0x1F /* 0b00011111 */) << 6 | bytes.getUint8(offset++) & 0x3F /* 0b00111111 */)
:
((byte & 0xF0 /* 0b11110000 */) === 0xE0 /* 0b11100000 */)
? String.fromCharCode(
(byte & 0xF /* 0b00001111 */) << 12
| (bytes.getUint8(offset++) & 0x3F /* 0b00111111 */) << 6
| bytes.getUint8(offset++) & 0x3F /* 0b00111111 */
)
:
(byte =
((byte & 0x7 /* 0b00000111 */) << 18
| (bytes.getUint8(offset++) & 0x3F /* 0b00111111 */) << 12
| (bytes.getUint8(offset++) & 0x3F /* 0b00111111 */) << 6
| bytes.getUint8(offset++) & 0x3F /* 0b00111111 */
) - 0x10000
, String.fromCharCode(Math.floor(byte / 0x400) + 0xD800, byte % 0x400 + 0xDC00)
);
}
return __Utils_Tuple2(offset, string);
});
var _Bytes_decodeFailure = F2(function() { throw 0; });