Skip to content
Open
Show file tree
Hide file tree
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
64 changes: 32 additions & 32 deletions yaml/_chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

export const BOM = 0xfeff; /* BOM */
export const TAB = 0x09; /* Tab */
export const LINE_FEED = 0x0a; /* LF */
export const CARRIAGE_RETURN = 0x0d; /* CR */
export const SPACE = 0x20; /* Space */
export const EXCLAMATION = 0x21; /* ! */
export const DOUBLE_QUOTE = 0x22; /* " */
export const SHARP = 0x23; /* # */
export const PERCENT = 0x25; /* % */
export const AMPERSAND = 0x26; /* & */
export const SINGLE_QUOTE = 0x27; /* ' */
export const ASTERISK = 0x2a; /* * */
export const PLUS = 0x2b; /* + */
export const COMMA = 0x2c; /* , */
export const MINUS = 0x2d; /* - */
export const DOT = 0x2e; /* . */
export const COLON = 0x3a; /* : */
export const SMALLER_THAN = 0x3c; /* < */
export const GREATER_THAN = 0x3e; /* > */
export const QUESTION = 0x3f; /* ? */
export const COMMERCIAL_AT = 0x40; /* @ */
export const LEFT_SQUARE_BRACKET = 0x5b; /* [ */
export const BACKSLASH = 0x5c; /* \ */
export const RIGHT_SQUARE_BRACKET = 0x5d; /* ] */
export const GRAVE_ACCENT = 0x60; /* ` */
export const LEFT_CURLY_BRACKET = 0x7b; /* { */
export const VERTICAL_LINE = 0x7c; /* | */
export const RIGHT_CURLY_BRACKET = 0x7d; /* } */
export const BOM = "\uFEFF";
export const TAB = "\t";
export const LINE_FEED = "\n";
export const CARRIAGE_RETURN = "\r";
export const SPACE = " ";
export const EXCLAMATION = "!";
export const DOUBLE_QUOTE = '"';
export const SHARP = "#";
export const PERCENT = "%";
export const AMPERSAND = "&";
export const SINGLE_QUOTE = "'";
export const ASTERISK = "*";
export const PLUS = "+";
export const COMMA = ",";
export const MINUS = "-";
export const DOT = ".";
export const COLON = ":";
export const SMALLER_THAN = "<";
export const GREATER_THAN = ">";
export const QUESTION = "?";
export const COMMERCIAL_AT = "@";
export const LEFT_SQUARE_BRACKET = "[";
export const BACKSLASH = "\\";
export const RIGHT_SQUARE_BRACKET = "]";
export const GRAVE_ACCENT = "`";
export const LEFT_CURLY_BRACKET = "{";
export const VERTICAL_LINE = "|";
export const RIGHT_CURLY_BRACKET = "}";

export function isEOL(c: number): boolean {
export function isEOL(c: string): boolean {
return c === LINE_FEED || c === CARRIAGE_RETURN;
}

export function isWhiteSpace(c: number): boolean {
export function isWhiteSpace(c: string): boolean {
return c === TAB || c === SPACE;
}

export function isWhiteSpaceOrEOL(c: number): boolean {
export function isWhiteSpaceOrEOL(c: string): boolean {
return isWhiteSpace(c) || isEOL(c);
}

export function isFlowIndicator(c: number): boolean {
export function isFlowIndicator(c: string): boolean {
return (
c === COMMA ||
c === LEFT_SQUARE_BRACKET ||
Expand Down
27 changes: 14 additions & 13 deletions yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,20 @@ function generateNextLine(indent: number, level: number): string {
* @link https://yaml.org/spec/1.2.2/ 5.1. Character Set
* @return `true` if the character is printable without escaping, `false` otherwise.
*/
function isPrintable(c: number): boolean {
function isPrintable(s: string): boolean {
const c = s.charCodeAt(0);
return (
(0x00020 <= c && c <= 0x00007e) ||
(0x000a1 <= c && c <= 0x00d7ff && c !== 0x2028 && c !== 0x2029) ||
(0x0e000 <= c && c <= 0x00fffd && c !== BOM) ||
(0x0e000 <= c && c <= 0x00fffd && s !== BOM) ||
(0x10000 <= c && c <= 0x10ffff)
);
}

/**
* @return `true` if value is allowed after the first character in plain style, `false` otherwise.
*/
function isPlainSafe(c: number): boolean {
function isPlainSafe(c: string): boolean {
return (
isPrintable(c) &&
c !== BOM &&
Expand All @@ -149,7 +150,7 @@ function isPlainSafe(c: number): boolean {
/**
* @return `true` if value is allowed as the first character in plain style, `false` otherwise.
*/
function isPlainSafeFirst(c: number): boolean {
function isPlainSafeFirst(c: string): boolean {
return (
isPlainSafe(c) &&
!isWhiteSpace(c) &&
Expand Down Expand Up @@ -191,16 +192,16 @@ function chooseScalarStyle(
let hasLineBreak = false;
let hasFoldableLine = false; // only checked if shouldTrackWidth
let previousLineBreak = -1; // count the first line correctly
let plain = isPlainSafeFirst(string.charCodeAt(0)) &&
!isWhiteSpace(string.charCodeAt(string.length - 1));
let plain = isPlainSafeFirst(string[0]!) &&
!isWhiteSpace(string.at(-1)!);

let char: number;
let char: string;
let i: number;
if (singleLineOnly) {
// Case: no block styles.
// Check for disallowed characters to rule out plain and single.
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
char = string.at(i)!;
if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
Expand All @@ -209,7 +210,7 @@ function chooseScalarStyle(
} else {
// Case: block styles permitted.
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
char = string.at(i)!;
if (char === LINE_FEED) {
hasLineBreak = true;
// Check if any line can be folded.
Expand Down Expand Up @@ -358,7 +359,7 @@ function escapeString(string: string): string {
}
}
escapeSeq = ESCAPE_SEQUENCES.get(char);
result += !escapeSeq && isPrintable(char)
result += !escapeSeq && isPrintable(String.fromCharCode(char))
? string[i]
: escapeSeq || charCodeToHexString(char);
}
Expand Down Expand Up @@ -591,7 +592,7 @@ export class DumperState {
isKey: false,
});
if (string === null) continue;
const linePrefix = LINE_FEED === string.charCodeAt(0) ? "-" : "- ";
const linePrefix = LINE_FEED === string[0] ? "-" : "- ";
results.push(`${linePrefix}${string}`);
}
return results.length ? prefix + results.join(whitespace) : "[]";
Expand Down Expand Up @@ -684,11 +685,11 @@ export class DumperState {

let pairBuffer = "";
if (explicitPair) {
pairBuffer += keyString.charCodeAt(0) === LINE_FEED ? "?" : "? ";
pairBuffer += keyString[0] === LINE_FEED ? "?" : "? ";
}
pairBuffer += keyString;
if (explicitPair) pairBuffer += separator;
pairBuffer += valueString.charCodeAt(0) === LINE_FEED ? ":" : ": ";
pairBuffer += valueString[0] === LINE_FEED ? ":" : ": ";
pairBuffer += valueString;
results.push(pairBuffer);
}
Expand Down
Loading
Loading