Debian Codegraph @

lezer-lr/src/decode.ts

raw
1 // See lezer-generator/src/encode.ts for comments about the encoding
2 // used here
3
4 import {Encode} from "./constants"
5
6 export function decodeArray<T extends {[i: number]: number} = Uint16Array>(
7 input: string | T,
8 Type: {new (n: number): T} = Uint16Array as any
9 ): T {
10 if (typeof input != "string") return input
11 let array: T | null = null
12 for (let pos = 0, out = 0; pos < input.length;) {
13 let value = 0
14 for (;;) {
15 let next = input.charCodeAt(pos++), stop = false
16 if (next == Encode.BigValCode) { value = Encode.BigVal; break }
17 if (next >= Encode.Gap2) next--
18 if (next >= Encode.Gap1) next--
19 let digit = next - Encode.Start
20 if (digit >= Encode.Base) { digit -= Encode.Base; stop = true }
21 value += digit
22 if (stop) break
23 value *= Encode.Base
24 }
25 if (array) array[out++] = value
26 else array = new Type(value)
27 }
28 return array!
29 }