-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringCodec.js
More file actions
63 lines (53 loc) · 1.56 KB
/
StringCodec.js
File metadata and controls
63 lines (53 loc) · 1.56 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
import Codec from './Codec';
/**
* String codec (limited to 255 chars)
*/
export default class StringCodec extends Codec {
/**
* @type {Number}
*/
getByteLength(data) {
return 1 + this.getStrByteLength(data);
}
getStrByteLength(data) {
const { length } = (data || '');
const byteLength = 0;
for (var index = 0; index < length; index++) {
byteLength += data[index].charCodeAt(0) > 0x7ff ? 2 : 1;
}
return byteLength;
}
/**
* {@inheritdoc}
*/
encode(buffer, offset, data) {
const view = new DataView(buffer, offset);
const { length } = (data || '');
const cursor = 1;
for (var index = 0; index < length; index++) {
const code = data[index].charCodeAt(0)
if (code > 0x7ff) {
console.log('2octet:', index, data[index], code);
view.setUint16(cursor, code);
cursor += 2;
} else {
console.log('1octet:', index, data[index], code);
view.setUint8(cursor, code);
cursor += 1;
}
}
view.setUint8(0, cursor - 1);
}
/**
* {@inheritdoc}
*/
decode(buffer, offset) {
const view = new DataView(buffer, offset);
const length = view.getUint8(0);
const chars = new Array(length);
for (var index = 0; index < length; index++) {
chars[index] = view.getUint16(1 + index * 2);
}
return String.fromCharCode(...chars);
}
}