boundary.js
2.71 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
var test = require('tape');
var Parser = require('../');
test('2 byte utf8 \'De\' character: д', function (t) {
t.plan(1);
var p = new Parser();
p.onValue = function (value) {
t.equal(value, 'д');
};
var de_buffer = new Buffer([0xd0, 0xb4]);
p.write('"');
p.write(de_buffer);
p.write('"');
});
test('3 byte utf8 \'Han\' character: 我', function (t) {
t.plan(1);
var p = new Parser();
p.onValue = function (value) {
t.equal(value, '我');
};
var han_buffer = new Buffer([0xe6, 0x88, 0x91]);
p.write('"');
p.write(han_buffer);
p.write('"');
});
test('4 byte utf8 character (unicode scalar U+2070E): 𠜎', function (t) {
t.plan(1);
var p = new Parser();
p.onValue = function (value) {
t.equal(value, '𠜎');
};
var Ux2070E_buffer = new Buffer([0xf0, 0xa0, 0x9c, 0x8e]);
p.write('"');
p.write(Ux2070E_buffer);
p.write('"');
});
test('3 byte utf8 \'Han\' character chunked inbetween 2nd and 3rd byte: 我', function (t) {
t.plan(1);
var p = new Parser();
p.onValue = function (value) {
t.equal(value, '我');
};
var han_buffer_first = new Buffer([0xe6, 0x88]);
var han_buffer_second = new Buffer([0x91]);
p.write('"');
p.write(han_buffer_first);
p.write(han_buffer_second);
p.write('"');
});
test('4 byte utf8 character (unicode scalar U+2070E) chunked inbetween 2nd and 3rd byte: 𠜎', function (t) {
t.plan(1);
var p = new Parser();
p.onValue = function (value) {
t.equal(value, '𠜎');
};
var Ux2070E_buffer_first = new Buffer([0xf0, 0xa0]);
var Ux2070E_buffer_second = new Buffer([0x9c, 0x8e]);
p.write('"');
p.write(Ux2070E_buffer_first);
p.write(Ux2070E_buffer_second);
p.write('"');
});
test('1-4 byte utf8 character string chunked inbetween random bytes: Aж文𠜱B', function (t) {
t.plan(1);
var p = new Parser();
p.onValue = function (value) {
t.equal(value, 'Aж文𠜱B');
};
var eclectic_buffer = new Buffer([0x41, // A
0xd0, 0xb6, // ж
0xe6, 0x96, 0x87, // 文
0xf0, 0xa0, 0x9c, 0xb1, // 𠜱
0x42]); // B
var rand_chunk = Math.floor(Math.random() * (eclectic_buffer.length));
var first_buffer = eclectic_buffer.slice(0, rand_chunk);
var second_buffer = eclectic_buffer.slice(rand_chunk);
//console.log('eclectic_buffer: ' + eclectic_buffer)
//console.log('sliced from 0 to ' + rand_chunk);
//console.log(first_buffer);
//console.log('then sliced from ' + rand_chunk + ' to the end');
//console.log(second_buffer);
console.log('chunked after offset ' + rand_chunk);
p.write('"');
p.write(first_buffer);
p.write(second_buffer);
p.write('"');
});