马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const ALPHABET =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
const CONTINUATION_BIT = 0x20;
const END_SEGMENT_BIT = 0x40;
const NEXT_LINE = END_SEGMENT_BIT | 0x01;
const INVALID = END_SEGMENT_BIT | 0x02;
const DATA_MASK = 0x1f;
 
const ccToValue = new Uint8Array("z".charCodeAt(0) + 1);
{
    ccToValue.fill(INVALID);
    for (let i = 0; i < ALPHABET.length; i++) {
        ccToValue[ALPHABET.charCodeAt(i)] = i;
    }
    ccToValue[",".charCodeAt(0)] = END_SEGMENT_BIT;
    ccToValue[";".charCodeAt(0)] = NEXT_LINE;
}
const ccMax = ccToValue.length - 1;
 
/**
 * @param {string} mappings the mappings string
 * @param {function(number, number, number, number, number, number): void} onMapping called for each mapping
 * @returns {void}
 */
const readMappings = (mappings, onMapping) => {
    // generatedColumn, [sourceIndex, originalLine, orignalColumn, [nameIndex]]
    const currentData = new Uint32Array([0, 0, 1, 0, 0]);
    let currentDataPos = 0;
    // currentValue will include a sign bit at bit 0
    let currentValue = 0;
    let currentValuePos = 0;
    let generatedLine = 1;
    let generatedColumn = -1;
    for (let i = 0; i < mappings.length; i++) {
        const cc = mappings.charCodeAt(i);
        if (cc > ccMax) continue;
        const value = ccToValue[cc];
        if ((value & END_SEGMENT_BIT) !== 0) {
            // End current segment
            if (currentData[0] > generatedColumn) {
                if (currentDataPos === 1) {
                    onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
                } else if (currentDataPos === 4) {
                    onMapping(
                        generatedLine,
                        currentData[0],
                        currentData[1],
                        currentData[2],
                        currentData[3],
                        -1
                    );
                } else if (currentDataPos === 5) {
                    onMapping(
                        generatedLine,
                        currentData[0],
                        currentData[1],
                        currentData[2],
                        currentData[3],
                        currentData[4]
                    );
                }
                generatedColumn = currentData[0];
            }
            currentDataPos = 0;
            if (value === NEXT_LINE) {
                // Start new line
                generatedLine++;
                currentData[0] = 0;
                generatedColumn = -1;
            }
        } else if ((value & CONTINUATION_BIT) === 0) {
            // last sextet
            currentValue |= value << currentValuePos;
            const finalValue =
                currentValue & 1 ? -(currentValue >> 1) : currentValue >> 1;
            currentData[currentDataPos++] += finalValue;
            currentValuePos = 0;
            currentValue = 0;
        } else {
            currentValue |= (value & DATA_MASK) << currentValuePos;
            currentValuePos += 5;
        }
    }
    // End current segment
    if (currentDataPos === 1) {
        onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
    } else if (currentDataPos === 4) {
        onMapping(
            generatedLine,
            currentData[0],
            currentData[1],
            currentData[2],
            currentData[3],
            -1
        );
    } else if (currentDataPos === 5) {
        onMapping(
            generatedLine,
            currentData[0],
            currentData[1],
            currentData[2],
            currentData[3],
            currentData[4]
        );
    }
};
 
module.exports = readMappings;