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
271 lines (226 loc) · 6.92 KB
/
Copy pathbit_string.js
File metadata and controls
271 lines (226 loc) · 6.92 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
if (!Object.assign) {
Object.defineProperty(Object, 'assign', {
enumerable: false,
configurable: true,
writable: true,
value: function(target, firstSource) {
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}
var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
}
nextSource = Object(nextSource);
var keysArray = Object.keys(Object(nextSource));
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}
});
}
if (!String.prototype.codePointAt) {
(function() {
var codePointAt = function(position) {
if (this == null) {
throw TypeError();
}
var string = String(this);
var size = string.length;
// `ToInteger`
var index = position ? Number(position) : 0;
if (index !== index) { // better `isNaN`
index = 0;
}
// Account for out-of-bounds indices:
if (index < 0 || index >= size) {
return undefined;
}
// Get the first code unit
var first = string.charCodeAt(index);
var second;
if ( // check if it’s the start of a surrogate pair
first >= 0xD800 && first <= 0xDBFF && // high surrogate
size > index + 1 // there is a next code unit
) {
second = string.charCodeAt(index + 1);
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
}
}
return first;
};
if (Object.defineProperty) {
Object.defineProperty(String.prototype, 'codePointAt', {
'value': codePointAt,
'configurable': true,
'writable': true
});
} else {
String.prototype.codePointAt = codePointAt;
}
}());
}
let BitString = {};
BitString.__MODULE__ = Symbol.for("BitString");
BitString.integer = function(value){
return BitString.wrap(value, { 'type': 'integer', 'unit': 1, 'size': 8 });
};
BitString.float = function(value){
return BitString.wrap(value, { 'type': 'float', 'unit': 1, 'size': 64 });
};
BitString.bitstring = function(value){
return BitString.wrap(value, { 'type': 'bitstring', 'unit': 1, 'size': value.length });
};
BitString.bits = function(value){
return BitString.bitstring(value);
};
BitString.binary = function(value){
return BitString.wrap(value, { 'type': 'binary', 'unit': 8, 'size': value.length});
};
BitString.bytes = function(value){
return BitString.binary(value);
};
BitString.utf8 = function(value){
return BitString.wrap(value, { 'type': 'utf8' });
};
BitString.utf16 = function(value){
return BitString.wrap(value, { 'type': 'utf16' });
};
BitString.utf32 = function(value){
return BitString.wrap(value, { 'type': 'utf32' });
};
BitString.signed = function(value){
return BitString.wrap(value, {}, 'signed');
};
BitString.unsigned = function(value){
return BitString.wrap(value, {}, 'unsigned');
};
BitString.native = function(value){
return BitString.wrap(value, {}, 'native');
};
BitString.big = function(value){
return BitString.wrap(value, {}, 'big');
};
BitString.little = function(value){
return BitString.wrap(value, {}, 'little');
};
BitString.size = function(value, count){
return BitString.wrap(value, {'size': count});
};
BitString.unit = function(value, count){
return BitString.wrap(value, {'unit': count});
};
BitString.wrap = function(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;
};
BitString.toUTF8Array = function(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;
};
BitString.toUTF16Array = function(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;
};
BitString.toUTF32Array = function(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
BitString.float32ToBytes = function(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;
};
BitString.float64ToBytes = function(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;