-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathString.js
151 lines (124 loc) Β· 5.61 KB
/
String.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
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
import assert from 'assert';
import {String as StringT, uint16le, uint8, DecodeStream, Struct} from 'restructure';
describe('String', function() {
describe('decode', function() {
it('should decode fixed length', function() {
const string = new StringT(7);
assert.equal(string.fromBuffer(Buffer.from('testing')), 'testing');
});
it('should decode length from parent key', function() {
const stream = new DecodeStream(Buffer.from('testing'));
const string = new StringT('len');
assert.equal(string.decode(stream, {len: 7}), 'testing');
});
it('should decode length as number before string', function() {
const string = new StringT(uint8);
assert.equal(string.fromBuffer(Buffer.from('\x07testing')), 'testing');
});
it('should decode utf8', function() {
const string = new StringT(4, 'utf8');
assert.equal(string.fromBuffer(Buffer.from('π»')), 'π»');
});
it('should decode encoding computed from function', function() {
const string = new StringT(4, function() { return 'utf8'; });
assert.equal(string.fromBuffer(Buffer.from('π»')), 'π»');
});
it('should decode null-terminated string and read past terminator', function() {
const stream = new DecodeStream(Buffer.from('π»\x00'));
const string = new StringT(null, 'utf8');
assert.equal(string.decode(stream), 'π»');
assert.equal(stream.pos, 5);
});
it('should decode remainder of buffer when null-byte missing', function() {
const string = new StringT(null, 'utf8');
assert.equal(string.fromBuffer(Buffer.from('π»')), 'π»');
});
it('should decode two-byte null-terminated string for utf16le', function() {
const stream = new DecodeStream(Buffer.from('π»\x00', 'utf16le'));
const string = new StringT(null, 'utf16le');
assert.equal(string.decode(stream), 'π»');
assert.equal(stream.pos, 6);
});
it('should decode remainder of buffer when null-byte missing, utf16le', function() {
const string = new StringT(null, 'utf16le');
assert.equal(string.fromBuffer(Buffer.from('π»', 'utf16le')), 'π»');
});
it('should decode x-mac-roman', function() {
const string = new StringT(null, 'x-mac-roman');
const buf = new Uint8Array([0x8a, 0x63, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x64, 0x20, 0x63, 0x68, 0x87, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73]);
assert.equal(string.fromBuffer(buf), 'Γ€ccented chΓ‘racters');
})
});
describe('size', function() {
it('should use string length', function() {
const string = new StringT(7);
assert.equal(string.size('testing'), 7);
});
it('should use correct encoding', function() {
const string = new StringT(10, 'utf8');
assert.equal(string.size('π»'), 4);
});
it('should use encoding from function', function() {
const string = new StringT(10, function() { return 'utf8'; });
assert.equal(string.size('π»'), 4);
});
it('should add size of length field before string', function() {
const string = new StringT(uint8, 'utf8');
assert.equal(string.size('π»'), 5);
});
it('should work with utf16be encoding', function() {
const string = new StringT(10, 'utf16be');
assert.equal(string.size('π»'), 4);
});
it('should take null-byte into account', function() {
const string = new StringT(null, 'utf8');
assert.equal(string.size('π»'), 5);
});
it('should take null-byte into account, utf16le', function() {
const string = new StringT(null, 'utf16le');
assert.equal(string.size('π»'), 6);
});
it('should use defined length if no value given', function() {
const array = new StringT(10);
assert.equal(array.size(), 10);
});
});
describe('encode', function() {
it('should encode using string length', function() {
const string = new StringT(7);
assert.deepEqual(string.toBuffer('testing'), Buffer.from('testing'));
});
it('should encode length as number before string', function() {
const string = new StringT(uint8);
assert.deepEqual(string.toBuffer('testing'), Buffer.from('\x07testing'));
});
it('should encode length as number before string utf8', function() {
const string = new StringT(uint8, 'utf8');
assert.deepEqual(string.toBuffer('testing π'), Buffer.from('\x0ctesting π', 'utf8'));
});
it('should encode utf8', function() {
const string = new StringT(4, 'utf8');
assert.deepEqual(string.toBuffer('π»'), Buffer.from('π»'));
});
it('should encode encoding computed from function', function() {
const string = new StringT(4, function() { return 'utf8'; });
assert.deepEqual(string.toBuffer('π»'), Buffer.from('π»'));
});
it('should encode null-terminated string', function() {
const string = new StringT(null, 'utf8');
assert.deepEqual(string.toBuffer('π»'), Buffer.from('π»\x00'));
});
it('should encode using string length, utf16le', function() {
const string = new StringT(16, 'utf16le');
assert.deepEqual(string.toBuffer('testing'), Buffer.from('testing', 'utf16le'));
});
it('should encode length as number before string utf16le', function() {
const string = new StringT(uint16le, 'utf16le');
assert.deepEqual(string.toBuffer('testing π'), Buffer.from('\u0014testing π', 'utf16le'));
});
it('should encode two-byte null-terminated string for UTF-16', function() {
const string = new StringT(null, 'utf16le');
assert.deepEqual(string.toBuffer('π»'), Buffer.from('π»\x00', 'utf16le'));
});
});
});