Skip to content

Commit fcc1491

Browse files
authored
refactor(toml): add Scanner startsWith() method (#6532)
1 parent a7ebdb9 commit fcc1491

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

toml/_parser.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ export class Scanner {
115115
}
116116

117117
isCurrentCharEOL() {
118-
return this.char() === "\n" || this.slice(0, 2) === "\r\n";
118+
return this.char() === "\n" || this.startsWith("\r\n");
119+
}
120+
121+
startsWith(searchString: string) {
122+
return this.#source.startsWith(searchString, this.#position);
119123
}
120124
}
121125

@@ -304,7 +308,7 @@ function surround<T>(
304308
function character(str: string) {
305309
return (scanner: Scanner): ParseResult<void> => {
306310
scanner.nextUntilChar({ inline: true });
307-
if (scanner.slice(0, str.length) !== str) return failure();
311+
if (!scanner.startsWith(str)) return failure();
308312
scanner.next(str.length);
309313
scanner.nextUntilChar({ inline: true });
310314
return success(undefined);
@@ -429,23 +433,23 @@ export function multilineBasicString(
429433
scanner: Scanner,
430434
): ParseResult<string> {
431435
scanner.nextUntilChar({ inline: true });
432-
if (scanner.slice(0, 3) !== '"""') return failure();
436+
if (!scanner.startsWith('"""')) return failure();
433437
scanner.next(3);
434438
if (scanner.char() === "\n") {
435439
// The first newline (LF) is trimmed
436440
scanner.next();
437-
} else if (scanner.slice(0, 2) === "\r\n") {
441+
} else if (scanner.startsWith("\r\n")) {
438442
// The first newline (CRLF) is trimmed
439443
scanner.next(2);
440444
}
441445
const acc: string[] = [];
442-
while (scanner.slice(0, 3) !== '"""' && !scanner.eof()) {
446+
while (!scanner.startsWith('"""') && !scanner.eof()) {
443447
// line ending backslash
444-
if (scanner.slice(0, 2) === "\\\n") {
448+
if (scanner.startsWith("\\\n")) {
445449
scanner.next();
446450
scanner.nextUntilChar({ comment: false });
447451
continue;
448-
} else if (scanner.slice(0, 3) === "\\\r\n") {
452+
} else if (scanner.startsWith("\\\r\n")) {
449453
scanner.next();
450454
scanner.nextUntilChar({ comment: false });
451455
continue;
@@ -477,17 +481,17 @@ export function multilineLiteralString(
477481
scanner: Scanner,
478482
): ParseResult<string> {
479483
scanner.nextUntilChar({ inline: true });
480-
if (scanner.slice(0, 3) !== "'''") return failure();
484+
if (!scanner.startsWith("'''")) return failure();
481485
scanner.next(3);
482486
if (scanner.char() === "\n") {
483487
// The first newline (LF) is trimmed
484488
scanner.next();
485-
} else if (scanner.slice(0, 2) === "\r\n") {
489+
} else if (scanner.startsWith("\r\n")) {
486490
// The first newline (CRLF) is trimmed
487491
scanner.next(2);
488492
}
489493
const acc: string[] = [];
490-
while (scanner.slice(0, 3) !== "'''" && !scanner.eof()) {
494+
while (!scanner.startsWith("'''") && !scanner.eof()) {
491495
acc.push(scanner.char());
492496
scanner.next();
493497
}
@@ -517,9 +521,7 @@ const symbolPairs: [string, unknown][] = [
517521
];
518522
export function symbols(scanner: Scanner): ParseResult<unknown> {
519523
scanner.nextUntilChar({ inline: true });
520-
const found = symbolPairs.find(([str]) =>
521-
scanner.slice(0, str.length) === str
522-
);
524+
const found = symbolPairs.find(([str]) => scanner.startsWith(str));
523525
if (!found) return failure();
524526
const [str, value] = found;
525527
scanner.next(str.length);

0 commit comments

Comments
 (0)