Skip to content

Commit 5e438af

Browse files
lib: replace StringCharIterator with simpler functions for character access
1 parent 137fbf3 commit 5e438af

File tree

1 file changed

+26
-57
lines changed

1 file changed

+26
-57
lines changed

lib/internal/source_map/source_map.js

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const {
7272
ArrayPrototypeSlice,
7373
ArrayPrototypeSort,
7474
ObjectPrototypeHasOwnProperty,
75-
StringPrototypeCharAt,
7675
Symbol,
7776
} = primordials;
7877

@@ -86,38 +85,6 @@ const VLQ_CONTINUATION_MASK = 1 << 5;
8685

8786
const kMappings = Symbol('kMappings');
8887

89-
class StringCharIterator {
90-
/**
91-
* @constructor
92-
* @param {string} string
93-
*/
94-
constructor(string) {
95-
this._string = string;
96-
this._position = 0;
97-
}
98-
99-
/**
100-
* @return {string}
101-
*/
102-
next() {
103-
return StringPrototypeCharAt(this._string, this._position++);
104-
}
105-
106-
/**
107-
* @return {string}
108-
*/
109-
peek() {
110-
return StringPrototypeCharAt(this._string, this._position);
111-
}
112-
113-
/**
114-
* @return {boolean}
115-
*/
116-
hasNext() {
117-
return this._position < this._string.length;
118-
}
119-
}
120-
12188
/**
12289
* Implements Source Map V3 model.
12390
* See https://github.com/google/closure-compiler/wiki/Source-Maps
@@ -279,46 +246,48 @@ class SourceMap {
279246
this.#sourceContentByURL[url] = mapSourcesContent[i];
280247
}
281248

282-
const stringCharIterator = new StringCharIterator(mapMappings);
249+
let pos = 0;
250+
const peek = () => mapMappings.charAt(pos);
251+
const next = () => mapMappings.charAt(pos++);
252+
const hasNext = () => pos < mapMappings.length;
253+
283254
let sourceURL = sources[sourceIndex];
284255
while (true) {
285-
if (stringCharIterator.peek() === ',')
286-
stringCharIterator.next();
287-
else {
288-
while (stringCharIterator.peek() === ';') {
256+
if (peek() === ',') {
257+
next();
258+
} else {
259+
while (peek() === ';') {
289260
lineNumber += 1;
290261
columnNumber = 0;
291-
stringCharIterator.next();
262+
next();
292263
}
293-
if (!stringCharIterator.hasNext())
294-
break;
264+
if (!hasNext()) break;
295265
}
296266

297-
columnNumber += decodeVLQ(stringCharIterator);
298-
if (isSeparator(stringCharIterator.peek())) {
299-
ArrayPrototypePush(this.#mappings, [lineNumber, columnNumber]);
267+
columnNumber += decodeVLQ(next);
268+
if (isSeparator(peek())) {
269+
this.#mappings.push([lineNumber, columnNumber]);
300270
continue;
301271
}
302272

303-
const sourceIndexDelta = decodeVLQ(stringCharIterator);
273+
const sourceIndexDelta = decodeVLQ(next);
304274
if (sourceIndexDelta) {
305275
sourceIndex += sourceIndexDelta;
306276
sourceURL = sources[sourceIndex];
307277
}
308-
sourceLineNumber += decodeVLQ(stringCharIterator);
309-
sourceColumnNumber += decodeVLQ(stringCharIterator);
278+
sourceLineNumber += decodeVLQ(next);
279+
sourceColumnNumber += decodeVLQ(next);
310280

311281
let name;
312-
if (!isSeparator(stringCharIterator.peek())) {
313-
nameIndex += decodeVLQ(stringCharIterator);
282+
if (!isSeparator(peek())) {
283+
nameIndex += decodeVLQ(next);
314284
name = mapNames?.[nameIndex];
315285
}
316286

317-
ArrayPrototypePush(
318-
this.#mappings,
319-
[lineNumber, columnNumber, sourceURL, sourceLineNumber,
320-
sourceColumnNumber, name],
321-
);
287+
this.#mappings.push([
288+
lineNumber, columnNumber, sourceURL,
289+
sourceLineNumber, sourceColumnNumber, name,
290+
]);
322291
}
323292
}
324293
}
@@ -332,16 +301,16 @@ function isSeparator(char) {
332301
}
333302

334303
/**
335-
* @param {SourceMap.StringCharIterator} stringCharIterator
304+
* @param {() => string} next
336305
* @return {number}
337306
*/
338-
function decodeVLQ(stringCharIterator) {
307+
function decodeVLQ(next) {
339308
// Read unsigned value.
340309
let result = 0;
341310
let shift = 0;
342311
let digit;
343312
do {
344-
digit = base64Map[stringCharIterator.next()];
313+
digit = base64Map[next()];
345314
result += (digit & VLQ_BASE_MASK) << shift;
346315
shift += VLQ_BASE_SHIFT;
347316
} while (digit & VLQ_CONTINUATION_MASK);

0 commit comments

Comments
 (0)