[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const ALPHABET =
|
---|
| 9 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
---|
| 10 |
|
---|
| 11 | const CONTINUATION_BIT = 0x20;
|
---|
| 12 | const END_SEGMENT_BIT = 0x40;
|
---|
| 13 | const NEXT_LINE = END_SEGMENT_BIT | 0x01;
|
---|
| 14 | const INVALID = END_SEGMENT_BIT | 0x02;
|
---|
| 15 | const DATA_MASK = 0x1f;
|
---|
| 16 |
|
---|
| 17 | const ccToValue = new Uint8Array("z".charCodeAt(0) + 1);
|
---|
| 18 | {
|
---|
| 19 | ccToValue.fill(INVALID);
|
---|
| 20 | for (let i = 0; i < ALPHABET.length; i++) {
|
---|
| 21 | ccToValue[ALPHABET.charCodeAt(i)] = i;
|
---|
| 22 | }
|
---|
| 23 | ccToValue[",".charCodeAt(0)] = END_SEGMENT_BIT;
|
---|
| 24 | ccToValue[";".charCodeAt(0)] = NEXT_LINE;
|
---|
| 25 | }
|
---|
| 26 | const ccMax = ccToValue.length - 1;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * @param {string} mappings the mappings string
|
---|
| 30 | * @param {function(number, number, number, number, number, number): void} onMapping called for each mapping
|
---|
| 31 | * @returns {void}
|
---|
| 32 | */
|
---|
| 33 | const readMappings = (mappings, onMapping) => {
|
---|
| 34 | // generatedColumn, [sourceIndex, originalLine, orignalColumn, [nameIndex]]
|
---|
| 35 | const currentData = new Uint32Array([0, 0, 1, 0, 0]);
|
---|
| 36 | let currentDataPos = 0;
|
---|
| 37 | // currentValue will include a sign bit at bit 0
|
---|
| 38 | let currentValue = 0;
|
---|
| 39 | let currentValuePos = 0;
|
---|
| 40 | let generatedLine = 1;
|
---|
| 41 | let generatedColumn = -1;
|
---|
| 42 | for (let i = 0; i < mappings.length; i++) {
|
---|
| 43 | const cc = mappings.charCodeAt(i);
|
---|
| 44 | if (cc > ccMax) continue;
|
---|
| 45 | const value = ccToValue[cc];
|
---|
| 46 | if ((value & END_SEGMENT_BIT) !== 0) {
|
---|
| 47 | // End current segment
|
---|
| 48 | if (currentData[0] > generatedColumn) {
|
---|
| 49 | if (currentDataPos === 1) {
|
---|
| 50 | onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
|
---|
| 51 | } else if (currentDataPos === 4) {
|
---|
| 52 | onMapping(
|
---|
| 53 | generatedLine,
|
---|
| 54 | currentData[0],
|
---|
| 55 | currentData[1],
|
---|
| 56 | currentData[2],
|
---|
| 57 | currentData[3],
|
---|
| 58 | -1
|
---|
| 59 | );
|
---|
| 60 | } else if (currentDataPos === 5) {
|
---|
| 61 | onMapping(
|
---|
| 62 | generatedLine,
|
---|
| 63 | currentData[0],
|
---|
| 64 | currentData[1],
|
---|
| 65 | currentData[2],
|
---|
| 66 | currentData[3],
|
---|
| 67 | currentData[4]
|
---|
| 68 | );
|
---|
| 69 | }
|
---|
| 70 | generatedColumn = currentData[0];
|
---|
| 71 | }
|
---|
| 72 | currentDataPos = 0;
|
---|
| 73 | if (value === NEXT_LINE) {
|
---|
| 74 | // Start new line
|
---|
| 75 | generatedLine++;
|
---|
| 76 | currentData[0] = 0;
|
---|
| 77 | generatedColumn = -1;
|
---|
| 78 | }
|
---|
| 79 | } else if ((value & CONTINUATION_BIT) === 0) {
|
---|
| 80 | // last sextet
|
---|
| 81 | currentValue |= value << currentValuePos;
|
---|
| 82 | const finalValue =
|
---|
| 83 | currentValue & 1 ? -(currentValue >> 1) : currentValue >> 1;
|
---|
| 84 | currentData[currentDataPos++] += finalValue;
|
---|
| 85 | currentValuePos = 0;
|
---|
| 86 | currentValue = 0;
|
---|
| 87 | } else {
|
---|
| 88 | currentValue |= (value & DATA_MASK) << currentValuePos;
|
---|
| 89 | currentValuePos += 5;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | // End current segment
|
---|
| 93 | if (currentDataPos === 1) {
|
---|
| 94 | onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
|
---|
| 95 | } else if (currentDataPos === 4) {
|
---|
| 96 | onMapping(
|
---|
| 97 | generatedLine,
|
---|
| 98 | currentData[0],
|
---|
| 99 | currentData[1],
|
---|
| 100 | currentData[2],
|
---|
| 101 | currentData[3],
|
---|
| 102 | -1
|
---|
| 103 | );
|
---|
| 104 | } else if (currentDataPos === 5) {
|
---|
| 105 | onMapping(
|
---|
| 106 | generatedLine,
|
---|
| 107 | currentData[0],
|
---|
| 108 | currentData[1],
|
---|
| 109 | currentData[2],
|
---|
| 110 | currentData[3],
|
---|
| 111 | currentData[4]
|
---|
| 112 | );
|
---|
| 113 | }
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | module.exports = readMappings;
|
---|