Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(yaml): inline readDoubleQuotedScalar() #5838

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 76 additions & 80 deletions yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,81 @@
"Unexpected end of the stream within a single quoted scalar",
);
}
readDoubleQuotedScalar(nodeIndent: number): boolean {
let ch = this.peek();

if (ch !== DOUBLE_QUOTE) {
return false;
}

this.kind = "scalar";
this.result = "";
this.position++;
let captureEnd = this.position;
let captureStart = this.position;
let tmp: number;
while ((ch = this.peek()) !== 0) {
if (ch === DOUBLE_QUOTE) {
this.captureSegment(captureStart, this.position, true);
this.position++;
return true;
}
if (ch === BACKSLASH) {
this.captureSegment(captureStart, this.position, true);
ch = this.next();

if (isEOL(ch)) {
this.skipSeparationSpace(false, nodeIndent);
} else if (ch < 256 && SIMPLE_ESCAPE_SEQUENCES.has(ch)) {
this.result += SIMPLE_ESCAPE_SEQUENCES.get(ch);
this.position++;
} else if ((tmp = ESCAPED_HEX_LENGTHS.get(ch) ?? 0) > 0) {
let hexLength = tmp;
let hexResult = 0;

for (; hexLength > 0; hexLength--) {
ch = this.next();

if ((tmp = hexCharCodeToNumber(ch)) >= 0) {
hexResult = (hexResult << 4) + tmp;
} else {
return this.throwError(
"Cannot read double quoted scalar: expected hexadecimal character",
);
}
}

this.result += codepointToChar(hexResult);

this.position++;
} else {
return this.throwError(
"Cannot read double quoted scalar: unknown escape sequence",
);
}

captureStart = captureEnd = this.position;
} else if (isEOL(ch)) {
this.captureSegment(captureStart, captureEnd, true);
this.writeFoldedLines(this.skipSeparationSpace(false, nodeIndent));
captureStart = captureEnd = this.position;
} else if (
this.position === this.lineStart &&
this.testDocumentSeparator()

Check warning on line 813 in yaml/_loader_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader_state.ts#L813

Added line #L813 was not covered by tests
) {
return this.throwError(
"Unexpected end of the document within a double quoted scalar",

Check warning on line 816 in yaml/_loader_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader_state.ts#L815-L816

Added lines #L815 - L816 were not covered by tests
);
} else {

Check warning on line 818 in yaml/_loader_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader_state.ts#L818

Added line #L818 was not covered by tests
this.position++;
captureEnd = this.position;
}
}

return this.throwError(
"Unexpected end of the stream within a double quoted scalar",
);
}

readDocument() {
const documentStart = this.position;
Expand Down Expand Up @@ -879,85 +954,6 @@
}
}

function readDoubleQuotedScalar(
state: LoaderState,
nodeIndent: number,
): boolean {
let ch = state.peek();

if (ch !== DOUBLE_QUOTE) {
return false;
}

state.kind = "scalar";
state.result = "";
state.position++;
let captureEnd = state.position;
let captureStart = state.position;
let tmp: number;
while ((ch = state.peek()) !== 0) {
if (ch === DOUBLE_QUOTE) {
state.captureSegment(captureStart, state.position, true);
state.position++;
return true;
}
if (ch === BACKSLASH) {
state.captureSegment(captureStart, state.position, true);
ch = state.next();

if (isEOL(ch)) {
state.skipSeparationSpace(false, nodeIndent);
} else if (ch < 256 && SIMPLE_ESCAPE_SEQUENCES.has(ch)) {
state.result += SIMPLE_ESCAPE_SEQUENCES.get(ch);
state.position++;
} else if ((tmp = ESCAPED_HEX_LENGTHS.get(ch) ?? 0) > 0) {
let hexLength = tmp;
let hexResult = 0;

for (; hexLength > 0; hexLength--) {
ch = state.next();

if ((tmp = hexCharCodeToNumber(ch)) >= 0) {
hexResult = (hexResult << 4) + tmp;
} else {
return state.throwError(
"Cannot read double quoted scalar: expected hexadecimal character",
);
}
}

state.result += codepointToChar(hexResult);

state.position++;
} else {
return state.throwError(
"Cannot read double quoted scalar: unknown escape sequence",
);
}

captureStart = captureEnd = state.position;
} else if (isEOL(ch)) {
state.captureSegment(captureStart, captureEnd, true);
state.writeFoldedLines(state.skipSeparationSpace(false, nodeIndent));
captureStart = captureEnd = state.position;
} else if (
state.position === state.lineStart &&
state.testDocumentSeparator()
) {
return state.throwError(
"Unexpected end of the document within a double quoted scalar",
);
} else {
state.position++;
captureEnd = state.position;
}
}

return state.throwError(
"Unexpected end of the stream within a double quoted scalar",
);
}

function readFlowCollection(state: LoaderState, nodeIndent: number): boolean {
let ch = state.peek();
let terminator: number;
Expand Down Expand Up @@ -1656,7 +1652,7 @@
if (
(allowBlockScalars && readBlockScalar(state, flowIndent)) ||
state.readSingleQuotedScalar(flowIndent) ||
readDoubleQuotedScalar(state, flowIndent)
state.readDoubleQuotedScalar(flowIndent)
) {
hasContent = true;
} else if (readAlias(state)) {
Expand Down