forked from TurboWarp/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumerical-encoding-2.js
More file actions
215 lines (195 loc) · 6.2 KB
/
numerical-encoding-2.js
File metadata and controls
215 lines (195 loc) · 6.2 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
// Name: Numerical Encoding V2
// ID: numericalencoding2
// Description: Encode strings as numbers for cloud variables. Not compatible with V1 due to using much more efficient format.
// License: MPL-2.0
(function (Scratch) {
"use strict";
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
/**
* @param {Uint8Array} bytes
* @returns {string}
*/
const encodeBinary = (bytes) => {
// Pre-allocating buffer seems to be much faster than string concatenation
const buffer = new Uint8Array(Math.ceil((bytes.length * 8) / 3));
let ptr = 0;
for (var i = 0; i <= bytes.length - 3; i += 3) {
// AAAAAAAA BBBBBBBB CCCCCCCC
// 11122233 34445556 66777888
const a = bytes[i];
const b = bytes[i + 1];
const c = bytes[i + 2];
buffer[ptr++] = 49 + (a >> 5);
buffer[ptr++] = 49 + ((a >> 2) & 0b111);
buffer[ptr++] = 49 + (((a & 0b11) << 1) | (b >> 7));
buffer[ptr++] = 49 + ((b >> 4) & 0b111);
buffer[ptr++] = 49 + ((b >> 1) & 0b111);
buffer[ptr++] = 49 + (((b & 0b1) << 2) | (c >> 6));
buffer[ptr++] = 49 + ((c >> 3) & 0b111);
buffer[ptr++] = 49 + (c & 0b111);
}
switch (bytes.length - i) {
case 1: {
// AAAAAAAA
// 11122233 3
const a = bytes[i];
buffer[ptr++] = 49 + (a >> 5);
buffer[ptr++] = 49 + ((a >> 2) & 0b111);
buffer[ptr++] = 49 + ((a & 0b11) << 1);
break;
}
case 2: {
// AAAAAAAA BBBBBBBB
// 11122233 34445556 66
const a = bytes[i];
const b = bytes[i + 1];
buffer[ptr++] = 49 + (a >> 5);
buffer[ptr++] = 49 + ((a >> 2) & 0b111);
buffer[ptr++] = 49 + (((a & 0b11) << 1) | (b >> 7));
buffer[ptr++] = 49 + ((b >> 4) & 0b111);
buffer[ptr++] = 49 + ((b >> 1) & 0b111);
buffer[ptr++] = 49 + ((b & 0b1) << 2);
break;
}
}
return textDecoder.decode(buffer);
};
/**
* @param {string} string
* @returns {Uint8Array}
*/
const decodeBinary = (string) => {
const encodedBytes = Math.floor((string.length * 3) / 8);
const result = new Uint8Array(encodedBytes);
let ptr = 0;
for (var i = 0; i <= string.length - 8; i += 8) {
// AAA BBB CCC DDD EEE FFF GGG HHH
// 111 111 112 222 222 233 333 333
const a = string.charCodeAt(i) - 49;
const b = string.charCodeAt(i + 1) - 49;
const c = string.charCodeAt(i + 2) - 49;
const d = string.charCodeAt(i + 3) - 49;
const e = string.charCodeAt(i + 4) - 49;
const f = string.charCodeAt(i + 5) - 49;
const g = string.charCodeAt(i + 6) - 49;
const h = string.charCodeAt(i + 7) - 49;
result[ptr++] = (a << 5) | (b << 2) | (c >> 1);
result[ptr++] = ((c & 0b1) << 7) | (d << 4) | (e << 1) | (f >> 2);
result[ptr++] = ((f & 0b11) << 6) | (g << 3) | h;
}
switch (encodedBytes - ptr) {
case 1: {
// AAA BBB CCC
// 111 111 11
const a = string.charCodeAt(i) - 49;
const b = string.charCodeAt(i + 1) - 49;
const c = string.charCodeAt(i + 2) - 49;
result[ptr] = (a << 5) | (b << 2) | (c >> 1);
break;
}
case 2: {
// AAA BBB CCC DDD EEE FFF
// 111 111 112 222 222 2
const a = string.charCodeAt(i) - 49;
const b = string.charCodeAt(i + 1) - 49;
const c = string.charCodeAt(i + 2) - 49;
const d = string.charCodeAt(i + 3) - 49;
const e = string.charCodeAt(i + 4) - 49;
const f = string.charCodeAt(i + 5) - 49;
result[ptr++] = (a << 5) | (b << 2) | (c >> 1);
result[ptr] = ((c & 0b1) << 7) | (d << 4) | (e << 1) | (f >> 2);
break;
}
}
return result;
};
/**
* @param {string} text
* @returns {string}
*/
const encodeText = (text) => encodeBinary(textEncoder.encode(text));
/**
* @param {string} text
* @returns {string}
*/
const decodeText = (text) => {
// All characters must be in range [1, 8]
for (let i = 0; i < text.length; i++) {
const ch = text.charCodeAt(i);
if (ch < 49 || ch > 56) {
return "";
}
}
return textDecoder.decode(decodeBinary(text));
};
// Uncomment this to validate that the encoding and decoding is correct.
/*
const stressValidate = () => {
for (let i = 0; i < 100000; i++) {
const randomLength = Math.floor(Math.random() * 1000);
const randomArray = new Uint8Array(randomLength);
for (let j = 0; j < randomLength; j++) {
randomArray[j] = Math.floor(Math.random() * 256);
}
const encoded = encodeBinary(randomArray);
const decoded = decodeBinary(encoded);
if (decoded.length !== randomArray.length) {
debugger;
}
for (let j = 0; j < randomArray.length; j++) {
if (randomArray[j] !== decoded[j]) {
debugger;
}
}
}
};
console.time('validate');
stressValidate();
console.timeEnd('validate');
*/
class NumericalEncodingV2 {
getInfo() {
const example = Scratch.translate({
default: "Hello",
description:
"Used as default input value to show how the encoding works",
});
return {
id: "numericalencoding2",
name: Scratch.translate("Numerical Encoding V2"),
blocks: [
{
blockType: Scratch.BlockType.REPORTER,
opcode: "encode",
text: Scratch.translate("encode [TEXT] as numbers"),
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: example,
},
},
},
{
blockType: Scratch.BlockType.REPORTER,
opcode: "decode",
text: Scratch.translate("decode [TEXT] as text"),
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: encodeText(example),
},
},
},
],
};
}
encode({ TEXT }) {
return encodeText(Scratch.Cast.toString(TEXT));
}
decode({ TEXT }) {
return decodeText(Scratch.Cast.toString(TEXT));
}
}
Scratch.extensions.register(new NumericalEncodingV2());
})(Scratch);