1
|
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
|
2
|
var B = require('../').Buffer
|
3
|
var test = require('tape')
|
4
|
|
5
|
test('detect utf16 surrogate pairs', function (t) {
|
6
|
var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
|
7
|
var buf = new B(text)
|
8
|
t.equal(text, buf.toString())
|
9
|
t.end()
|
10
|
})
|
11
|
|
12
|
test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) {
|
13
|
var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF'
|
14
|
var buf = new B(text)
|
15
|
t.equal(text, buf.toString())
|
16
|
t.end()
|
17
|
})
|
18
|
|
19
|
test('replace orphaned utf16 surrogate lead code point', function (t) {
|
20
|
var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
|
21
|
var buf = new B(text)
|
22
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
|
23
|
t.end()
|
24
|
})
|
25
|
|
26
|
test('replace orphaned utf16 surrogate trail code point', function (t) {
|
27
|
var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
|
28
|
var buf = new B(text)
|
29
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
|
30
|
t.end()
|
31
|
})
|
32
|
|
33
|
test('do not write partial utf16 code units', function (t) {
|
34
|
var f = new B([0, 0, 0, 0, 0])
|
35
|
t.equal(f.length, 5)
|
36
|
var size = f.write('あいうえお', 'utf16le')
|
37
|
t.equal(size, 4)
|
38
|
t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00]))
|
39
|
t.end()
|
40
|
})
|
41
|
|
42
|
test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
|
43
|
var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
|
44
|
|
45
|
var buf = new B(8)
|
46
|
buf.fill(0)
|
47
|
buf.write(text)
|
48
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d ]))
|
49
|
|
50
|
buf = new B(7)
|
51
|
buf.fill(0)
|
52
|
buf.write(text)
|
53
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00 ]))
|
54
|
|
55
|
buf = new B(6)
|
56
|
buf.fill(0)
|
57
|
buf.write(text)
|
58
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ]))
|
59
|
|
60
|
buf = new B(5)
|
61
|
buf.fill(0)
|
62
|
buf.write(text)
|
63
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ]))
|
64
|
|
65
|
buf = new B(4)
|
66
|
buf.fill(0)
|
67
|
buf.write(text)
|
68
|
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ]))
|
69
|
|
70
|
buf = new B(3)
|
71
|
buf.fill(0)
|
72
|
buf.write(text)
|
73
|
t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ]))
|
74
|
|
75
|
buf = new B(2)
|
76
|
buf.fill(0)
|
77
|
buf.write(text)
|
78
|
t.deepEqual(buf, new B([ 0x00, 0x00 ]))
|
79
|
|
80
|
buf = new B(1)
|
81
|
buf.fill(0)
|
82
|
buf.write(text)
|
83
|
t.deepEqual(buf, new B([ 0x00 ]))
|
84
|
|
85
|
t.end()
|
86
|
})
|
87
|
|
88
|
test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
|
89
|
var text = 'a' + '\uDE38\uD83D' + 'b'
|
90
|
|
91
|
var buf = new B(8)
|
92
|
buf.fill(0)
|
93
|
buf.write(text)
|
94
|
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62 ]))
|
95
|
|
96
|
buf = new B(7)
|
97
|
buf.fill(0)
|
98
|
buf.write(text)
|
99
|
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd ]))
|
100
|
|
101
|
buf = new B(6)
|
102
|
buf.fill(0)
|
103
|
buf.write(text)
|
104
|
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00 ]))
|
105
|
|
106
|
buf = new B(5)
|
107
|
buf.fill(0)
|
108
|
buf.write(text)
|
109
|
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00 ]))
|
110
|
|
111
|
buf = new B(4)
|
112
|
buf.fill(0)
|
113
|
buf.write(text)
|
114
|
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd ]))
|
115
|
|
116
|
buf = new B(3)
|
117
|
buf.fill(0)
|
118
|
buf.write(text)
|
119
|
t.deepEqual(buf, new B([ 0x61, 0x00, 0x00 ]))
|
120
|
|
121
|
buf = new B(2)
|
122
|
buf.fill(0)
|
123
|
buf.write(text)
|
124
|
t.deepEqual(buf, new B([ 0x61, 0x00 ]))
|
125
|
|
126
|
buf = new B(1)
|
127
|
buf.fill(0)
|
128
|
buf.write(text)
|
129
|
t.deepEqual(buf, new B([ 0x61 ]))
|
130
|
|
131
|
t.end()
|
132
|
})
|