from-string.js
3.25 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
var B = require('../').Buffer
var test = require('tape')
test('detect utf16 surrogate pairs', function (t) {
var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
var buf = new B(text)
t.equal(text, buf.toString())
t.end()
})
test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) {
var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF'
var buf = new B(text)
t.equal(text, buf.toString())
t.end()
})
test('replace orphaned utf16 surrogate lead code point', function (t) {
var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
var buf = new B(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
t.end()
})
test('replace orphaned utf16 surrogate trail code point', function (t) {
var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
var buf = new B(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ]))
t.end()
})
test('do not write partial utf16 code units', function (t) {
var f = new B([0, 0, 0, 0, 0])
t.equal(f.length, 5)
var size = f.write('あいうえお', 'utf16le')
t.equal(size, 4)
t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00]))
t.end()
})
test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
var buf = new B(8)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d ]))
buf = new B(7)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00 ]))
buf = new B(6)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ]))
buf = new B(5)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ]))
buf = new B(4)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ]))
buf = new B(3)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ]))
buf = new B(2)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x00, 0x00 ]))
buf = new B(1)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x00 ]))
t.end()
})
test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
var text = 'a' + '\uDE38\uD83D' + 'b'
var buf = new B(8)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62 ]))
buf = new B(7)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd ]))
buf = new B(6)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00 ]))
buf = new B(5)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00 ]))
buf = new B(4)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd ]))
buf = new B(3)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0x00, 0x00 ]))
buf = new B(2)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61, 0x00 ]))
buf = new B(1)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x61 ]))
t.end()
})