-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseLengthPrefixed2.js
More file actions
43 lines (40 loc) · 1.29 KB
/
parseLengthPrefixed2.js
File metadata and controls
43 lines (40 loc) · 1.29 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
import { Buffer } from 'buffer';
const digits = [...'0123456789'].map(c => c.charCodeAt(0))
const colon = ':'.charCodeAt(0)
export const parseLengthPrefixed2 = (str1, {
open = '['.charCodeAt(0),
close = ']'.charCodeAt(0),
} = {}) => {
const parents = []
let parent = {subvalues: []}, buffer = [], affix = []
const str = Buffer.from(str1, 'utf8')
for (let i = 0; i < str.length; ++i) {
const c = str[i]
if (digits.includes(c)) buffer.push(c)
else if (c === colon) {
const length = +String.fromCharCode(...buffer)
buffer = []
++i
affix = str.slice(i, i + length)
console.log(affix.toString('utf8'))
i += length - 1
} else {
if (buffer.length > 0) throw Error('expected :')
if (c === open) {
const value = {subvalues: []}
parent.subvalues.push({prefix: affix, value})
parents.push(parent)
parent = value
} else if (c === close) {
parent.suffix = affix
if (parents.length < 1) throw Error('Unexpected close!')
parent = parents.pop()
} else throw Error(`unexpected character ${String.fromCharCode(c)}`)
}
}
if (buffer.length > 0 || parents.length > 0) throw Error('Unexpected end!')
parent.suffix = affix
parent.open = open
parent.close = close
return parent
}