readMappings.js 2.9 KB

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