forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_string.js
More file actions
304 lines (238 loc) · 6.5 KB
/
Copy pathbit_string.js
File metadata and controls
304 lines (238 loc) · 6.5 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
class BitString {
constructor(...args){
this.raw_value = function(){
return Object.freeze(args);
};
this.value = Object.freeze(this.process(args));
}
get(index){
return this.value[index];
}
count(){
return this.value.length;
}
[Symbol.iterator]() {
return this.value[Symbol.iterator]();
}
toString(){
var i, s = "";
for (i = 0; i < this.count(); i++) {
if (s !== "") {
s += ", ";
}
s += this[i].toString();
}
return "<<" + s + ">>";
}
process(){
let processed_values = [];
var i;
for (i = 0; i < this.raw_value().length; i++) {
let processed_value = this['process_' + this.raw_value()[i].type](this.raw_value()[i]);
for(let attr of this.raw_value()[i].attributes){
processed_value = this['process_' + attr](processed_value);
}
processed_values = processed_values.concat(processed_value);
}
return processed_values;
}
process_integer(value){
return value.value;
}
process_float(value){
if(value.size === 64){
return BitString.float64ToBytes(value.value);
}else if(value.size === 32){
return BitString.float32ToBytes(value.value);
}
throw new Error('Invalid size for float');
}
process_bitstring(value){
return value.value.value;
}
process_binary(value){
return BitString.toUTF8Array(value.value);
}
process_utf8(value){
return BitString.toUTF8Array(value.value);
}
process_utf16(value){
return BitString.toUTF16Array(value.value);
}
process_utf32(value){
return BitString.toUTF32Array(value.value);
}
process_signed(value){
return (new Uint8Array([value]))[0];
}
process_unsigned(value){
return value;
}
process_native(value){
return value;
}
process_big(value){
return value;
}
process_little(value){
return value.reverse();
}
process_size(value){
return value;
}
process_unit(value){
return value;
}
static integer(value){
return BitString.wrap(value, { 'type': 'integer', 'unit': 1, 'size': 8 });
}
static float(value){
return BitString.wrap(value, { 'type': 'float', 'unit': 1, 'size': 64 });
}
static bitstring(value){
return BitString.wrap(value, { 'type': 'bitstring', 'unit': 1, 'size': value.length });
}
static bits(value){
return BitString.bitstring(value);
}
static binary(value){
return BitString.wrap(value, { 'type': 'binary', 'unit': 8, 'size': value.length});
}
static bytes(value){
return BitString.binary(value);
}
static utf8(value){
return BitString.wrap(value, { 'type': 'utf8' });
}
static utf16(value){
return BitString.wrap(value, { 'type': 'utf16' });
}
static utf32(value){
return BitString.wrap(value, { 'type': 'utf32' });
}
static signed(value){
return BitString.wrap(value, {}, 'signed');
}
static unsigned(value){
return BitString.wrap(value, {}, 'unsigned');
}
static native(value){
return BitString.wrap(value, {}, 'native');
}
static big(value){
return BitString.wrap(value, {}, 'big');
}
static little(value){
return BitString.wrap(value, {}, 'little');
}
static size(value, count){
return BitString.wrap(value, {'size': count});
}
static unit(value, count){
return BitString.wrap(value, {'unit': count});
}
static wrap(value, opt, new_attribute = null){
let the_value = value;
if(!(value instanceof Object)){
the_value = {'value': value, 'attributes': []};
}
the_value = Object.assign(the_value, opt);
if(new_attribute){
the_value.attributes.push(new_attribute);
}
return the_value;
}
static toUTF8Array(str) {
var utf8 = [];
for (var i = 0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80){
utf8.push(charcode);
}
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f));
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff) << 10)
| (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (charcode >> 18),
0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
return utf8;
}
static toUTF16Array(str) {
var utf16 = [];
for (var i = 0; i < str.length; i++) {
var codePoint = str.codePointAt(i);
if(codePoint <= 255){
utf16.push(0);
utf16.push(codePoint);
}else{
utf16.push(((codePoint >> 8) & 0xFF));
utf16.push((codePoint & 0xFF));
}
}
return utf16;
}
static toUTF32Array(str) {
var utf32 = [];
for (var i = 0; i < str.length; i++) {
var codePoint = str.codePointAt(i);
if(codePoint <= 255){
utf32.push(0);
utf32.push(0);
utf32.push(0);
utf32.push(codePoint);
}else{
utf32.push(0);
utf32.push(0);
utf32.push(((codePoint >> 8) & 0xFF));
utf32.push((codePoint & 0xFF));
}
}
return utf32;
}
//http://stackoverflow.com/questions/2003493/javascript-float-from-to-bits
static float32ToBytes(f) {
var bytes = [];
var buf = new ArrayBuffer(4);
(new Float32Array(buf))[0] = f;
let intVersion = (new Uint32Array(buf))[0];
bytes.push(((intVersion >> 24) & 0xFF));
bytes.push(((intVersion >> 16) & 0xFF));
bytes.push(((intVersion >> 8) & 0xFF));
bytes.push((intVersion & 0xFF));
return bytes;
}
static float64ToBytes(f) {
var bytes = [];
var buf = new ArrayBuffer(8);
(new Float64Array(buf))[0] = f;
var intVersion1 = (new Uint32Array(buf))[0];
var intVersion2 = (new Uint32Array(buf))[1];
bytes.push(((intVersion2 >> 24) & 0xFF));
bytes.push(((intVersion2 >> 16) & 0xFF));
bytes.push(((intVersion2 >> 8) & 0xFF));
bytes.push((intVersion2 & 0xFF));
bytes.push(((intVersion1 >> 24) & 0xFF));
bytes.push(((intVersion1 >> 16) & 0xFF));
bytes.push(((intVersion1 >> 8) & 0xFF));
bytes.push((intVersion1 & 0xFF));
return bytes;
}
}
export default BitString;