wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
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
'use strict'
 
const tap = require('tap')
const readEscapeSequence = require('./read-escape-sequence')
 
tap.test('throws for bad sequence', async t => {
  const input = Buffer.from('foo\\ø')
  t.throws(
    () => readEscapeSequence({ searchBuffer: input, startPos: 3 }),
    Error('invalid hex code in escape sequence')
  )
})
 
tap.test('reads a single ascii sequence', async t => {
  const input = Buffer.from('foo\\#bar', 'utf8')
  const { parsed, endPos } = readEscapeSequence({
    searchBuffer: input,
    startPos: 3
  })
  t.equal(parsed.toString(), '#')
  t.equal(endPos, 5)
})
 
tap.test('reads a sequence of ascii sequences', async t => {
  const input = Buffer.from('foo\\#\\!bar', 'utf8')
  const { parsed, endPos } = readEscapeSequence({
    searchBuffer: input,
    startPos: 3
  })
  t.equal(parsed.toString(), '#!')
  t.equal(endPos, 7)
})
 
tap.test('reads a single hex sequence', async t => {
  const input = Buffer.from('foo\\2abar', 'utf8')
  const { parsed, endPos } = readEscapeSequence({
    searchBuffer: input,
    startPos: 3
  })
  t.equal(parsed.toString(), '*')
  t.equal(endPos, 6)
})
 
tap.test('reads 2-byte utf-8 sequence', async t => {
  const input = Buffer.from('fo\\c5\\8f bar')
  const { parsed, endPos } = readEscapeSequence({
    searchBuffer: input,
    startPos: 2
  })
  t.equal(parsed.toString(), 'ŏ')
  t.equal(endPos, 8)
})
 
tap.test('reads 3-byte utf-8 sequence', async t => {
  const input = Buffer.from('fo\\e0\\b0\\b0 bar')
  const { parsed, endPos } = readEscapeSequence({
    searchBuffer: input,
    startPos: 2
  })
  t.equal(parsed.toString(), 'ర')
  t.equal(endPos, 11)
})
 
tap.test('reads 4-byte utf-8 sequence', async t => {
  const input = Buffer.from('fo\\f0\\92\\84\\ad bar')
  const { parsed, endPos } = readEscapeSequence({
    searchBuffer: input,
    startPos: 2
  })
  t.equal(parsed.toString(), '𒄭')
  t.equal(endPos, 14)
})