forked from inexorabletash/text-encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples-no-indexes.html
58 lines (50 loc) · 1.65 KB
/
examples-no-indexes.html
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
<!DOCTYPE html>
<script src="lib/encoding.js"></script>
<script>
function encodeArrayOfStrings(strings, encoding) {
var encoder, encoded, len, i, bytes, view, offset;
encoder = new TextEncoder(encoding);
encoded = [];
len = Uint32Array.BYTES_PER_ELEMENT;
for (i = 0; i < strings.length; i += 1) {
len += Uint32Array.BYTES_PER_ELEMENT;
encoded[i] = new TextEncoder(encoding).encode(strings[i]);
len += encoded[i].byteLength;
}
bytes = new Uint8Array(len);
view = new DataView(bytes.buffer);
offset = 0;
view.setUint32(offset, strings.length);
offset += Uint32Array.BYTES_PER_ELEMENT;
for (i = 0; i < encoded.length; i += 1) {
len = encoded[i].byteLength;
view.setUint32(offset, len);
offset += Uint32Array.BYTES_PER_ELEMENT;
bytes.set(encoded[i], offset);
offset += len;
}
return bytes.buffer;
}
function decodeArrayOfStrings(buffer, encoding) {
var decoder, view, offset, num_strings, strings, i, len;
decoder = new TextDecoder(encoding);
view = new DataView(buffer);
offset = 0;
strings = [];
num_strings = view.getUint32(offset);
offset += Uint32Array.BYTES_PER_ELEMENT;
for (i = 0; i < num_strings; i += 1) {
len = view.getUint32(offset);
offset += Uint32Array.BYTES_PER_ELEMENT;
strings[i] = decoder.decode(
new DataView(view.buffer, offset, len));
offset += len;
}
return strings;
}
var strings = ["Hello", "string", "encoding!"];
var buffer = encodeArrayOfStrings(strings, "utf-8");
var results = decodeArrayOfStrings(buffer, "utf-8");
document.write("Encoded " + JSON.stringify(strings) + "<br>");
document.write("Decoded " + JSON.stringify(results) + "<br>");
</script>