1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 |
 
 
 
 
 
 
x15
 
x529
x529
x529
 
x23
x23
x23
x23
x23
 
x23
 
x28
x28
x28
 
x503
x503
x503
 
x478
x478
x478
x478
 
x478
 
x940
 
 
x478
x485
x485
 
x478
 
x499
x532
 
 
 
x517
 
x520
 
x520
x536
x536
x536
x550
x550
x520
x520
 
x517
 
x520
 
x520
x529
x529
x529
x536
x536
x520
x520
 
 
x517
x531
x531
x531
x537
x537
x499
x499
 
 
 
 
x478
 
x478
x968
x968
x968
x1367
x1367
x1057
x1057
 
 
x478
 
 
x759
x478
 
x27
x27
 
x27
x30
x30
 
x27
x27
x27
x29
x29
x29
x29
 
x27
 
x42
x42
x42
x44
x44
 
 
x27
 
x358
x358
x358
x358
x358
 
x15
x15
x15
x15
x15
x15
x15
 
x15
x18
x18
x18
x18
x15
 
x15
x18
x18
x18
x18
x15
 
x15
x41
x41
x15
 
x15
x18
x18
x18
x18
x18
x15
x15
x15 |
I
|
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.
import type { Type } from "../_type.ts";
import { isNegativeZero } from "../_utils.ts";
function isCharCodeInRange(c: number, lower: number, upper: number): boolean {
return lower <= c && c <= upper;
}
function isHexCode(c: number): boolean {
return (
isCharCodeInRange(c, 0x30, 0x39) || // 0-9
isCharCodeInRange(c, 0x41, 0x46) || // A-F
isCharCodeInRange(c, 0x61, 0x66) // a-f
);
}
function isOctCode(c: number): boolean {
return isCharCodeInRange(c, 0x30, 0x37); // 0-7
}
function isDecCode(c: number): boolean {
return isCharCodeInRange(c, 0x30, 0x39); // 0-9
}
function resolveYamlInteger(data: string): boolean {
const max = data.length;
let index = 0;
let hasDigits = false;
if (!max) return false;
let ch = data[index];
// sign
if (ch === "-" || ch === "+") {
ch = data[++index];
}
if (ch === "0") {
// 0
if (index + 1 === max) return true;
ch = data[++index];
// base 2, base 8, base 16
if (ch === "b") {
// base 2
index++;
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (ch !== "0" && ch !== "1") return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}
if (ch === "x") {
// base 16
index++;
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (!isHexCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}
// base 8
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (!isOctCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}
// base 10 (except 0) or base 60
// value should not start with `_`;
if (ch === "_") return false;
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (!isDecCode(data.charCodeAt(index))) {
return false;
}
hasDigits = true;
}
// Should have digits and should not end with `_`
if (!hasDigits || ch === "_") return false;
// base60 almost not used, no needs to optimize
return /^(:[0-5]?[0-9])+$/.test(data.slice(index));
}
function constructYamlInteger(data: string): number {
let value = data;
if (value.includes("_")) {
value = value.replace(/_/g, "");
}
let sign = 1;
let ch = value[0];
if (ch === "-" || ch === "+") {
if (ch === "-") sign = -1;
value = value.slice(1);
ch = value[0];
}
if (value === "0") return 0;
if (ch === "0") {
if (value[1] === "b") return sign * parseInt(value.slice(2), 2);
if (value[1] === "x") return sign * parseInt(value, 16);
return sign * parseInt(value, 8);
}
return sign * parseInt(value, 10);
}
function isInteger(object: unknown): object is number {
if (object instanceof Number) object = object.valueOf();
return typeof object === "number" && object % 1 === 0 &&
!isNegativeZero(object);
}
export const int: Type<"scalar", number> = {
tag: "tag:yaml.org,2002:int",
construct: constructYamlInteger,
defaultStyle: "decimal",
kind: "scalar",
predicate: isInteger,
represent: {
// deno-lint-ignore ban-types
binary(object: number | Number): string {
const value = object instanceof Number ? object.valueOf() : object;
return value >= 0
? `0b${value.toString(2)}`
: `-0b${value.toString(2).slice(1)}`;
},
// deno-lint-ignore ban-types
octal(object: number | Number): string {
const value = object instanceof Number ? object.valueOf() : object;
return value >= 0
? `0${value.toString(8)}`
: `-0${value.toString(8).slice(1)}`;
},
// deno-lint-ignore ban-types
decimal(object: number | Number): string {
const value = object instanceof Number ? object.valueOf() : object;
return value.toString(10);
},
// deno-lint-ignore ban-types
hexadecimal(object: number | Number): string {
const value = object instanceof Number ? object.valueOf() : object;
return value >= 0
? `0x${value.toString(16).toUpperCase()}`
: `-0x${value.toString(16).toUpperCase().slice(1)}`;
},
},
resolve: resolveYamlInteger,
};
|