-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathencode-ranges.js
64 lines (59 loc) · 1.61 KB
/
encode-ranges.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
/**
* See static/decode-ranges.js for decode utilities
*/
const base64enc =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
/**
* Base64 encode variable-length deltas (5/10/15/21-bit).
*/
function encodeDeltas(input) {
const output = [];
for (let i = 0; i < input.length; ++i) {
const x = input[i];
if ((x >> 5) === 0) {
output.push(x);
} else if ((x >> 10) === 0) {
output.push(32 + (x >> 6), x);
} else if ((x >> 15) === 0) {
output.push(48 + (x >> 12), x >> 6, x);
} else {
console.assert((x >> 21) === 0, `delta ${x} out of range`);
output.push(56 + (x >> 18), x >> 12, x >> 6, x);
}
}
return output.map(x => base64enc[x & 63]).join('');
}
/**
* RLE + base64 encode code point ranges.
*/
function encodeRanges(values) {
const deltas = [];
for (let end = -1, i = 0; i < values.length; ) {
const begin = values[i];
console.assert(begin > end, `code point ${begin} out of order`);
deltas.push(begin - end - 1);
end = begin + 1;
while (++i < values.length && values[i] === end) {
++end;
}
deltas.push(end - begin - 1);
}
debugger;
return encodeDeltas(deltas);
}
function encodeRegenerate(regenerateSet) {
const deltas = [];
const regenerateData = regenerateSet.data;
for (let end = - 1, i = 0; i < regenerateData.length; i += 2) {
const begin = regenerateData[i];
console.assert(begin > end, `code point ${begin} out of order`);
deltas.push(begin - end - 1);
end = regenerateData[i + 1];
deltas.push(end - begin - 1);
}
return encodeDeltas(deltas);
}
module.exports = {
encodeRanges: encodeRanges,
encodeRegenerate: encodeRegenerate,
}