Skip to content

Commit 9a1726e

Browse files
committed
fix: remove support for underscore (_) in number parsing (fixes #627)
1 parent 49baadd commit 9a1726e

File tree

6 files changed

+23
-30
lines changed

6 files changed

+23
-30
lines changed

lib/type/float.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ var Type = require('../type');
55

66
var YAML_FLOAT_PATTERN = new RegExp(
77
// 2.5e4, 2.5 and integers
8-
'^(?:[-+]?(?:[0-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?' +
8+
'^(?:[-+]?(?:[0-9][0-9]*)(?:\\.[0-9]*)?(?:[eE][-+]?[0-9]+)?' +
99
// .2e4, .2
1010
// special case, seems not from spec
11-
'|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?' +
11+
'|\\.[0-9]+(?:[eE][-+]?[0-9]+)?' +
1212
// .inf
1313
'|[-+]?\\.(?:inf|Inf|INF)' +
1414
// .nan
@@ -30,7 +30,7 @@ function resolveYamlFloat(data) {
3030
function constructYamlFloat(data) {
3131
var value, sign;
3232

33-
value = data.replace(/_/g, '').toLowerCase();
33+
value = data.toLowerCase();
3434
sign = value[0] === '-' ? -1 : 1;
3535

3636
if ('+-'.indexOf(value[0]) >= 0) {

lib/type/int.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ function resolveYamlInteger(data) {
4747

4848
for (; index < max; index++) {
4949
ch = data[index];
50-
if (ch === '_') continue;
5150
if (ch !== '0' && ch !== '1') return false;
5251
hasDigits = true;
5352
}
54-
return hasDigits && ch !== '_';
53+
return hasDigits;
5554
}
5655

5756

@@ -61,55 +60,42 @@ function resolveYamlInteger(data) {
6160

6261
for (; index < max; index++) {
6362
ch = data[index];
64-
if (ch === '_') continue;
6563
if (!isHexCode(data.charCodeAt(index))) return false;
6664
hasDigits = true;
6765
}
68-
return hasDigits && ch !== '_';
66+
return hasDigits;
6967
}
7068

71-
7269
if (ch === 'o') {
7370
// base 8
7471
index++;
7572

7673
for (; index < max; index++) {
7774
ch = data[index];
78-
if (ch === '_') continue;
7975
if (!isOctCode(data.charCodeAt(index))) return false;
8076
hasDigits = true;
8177
}
82-
return hasDigits && ch !== '_';
78+
return hasDigits;
8379
}
8480
}
8581

8682
// base 10 (except 0)
8783

88-
// value should not start with `_`;
89-
if (ch === '_') return false;
90-
9184
for (; index < max; index++) {
9285
ch = data[index];
93-
if (ch === '_') continue;
9486
if (!isDecCode(data.charCodeAt(index))) {
9587
return false;
9688
}
9789
hasDigits = true;
9890
}
9991

100-
// Should have digits and should not end with `_`
101-
if (!hasDigits || ch === '_') return false;
102-
103-
return true;
92+
// Should have digits
93+
return hasDigits;
10494
}
10595

10696
function constructYamlInteger(data) {
10797
var value = data, sign = 1, ch;
10898

109-
if (value.indexOf('_') !== -1) {
110-
value = value.replace(/_/g, '');
111-
}
112-
11399
ch = value[0];
114100

115101
if (ch === '-' || ch === '+') {

test/issues/0027.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ describe('Should load numbers in YAML 1.2 format', function () {
2828
// not valid octal
2929
assert.strictEqual(yaml.load('0o1289'), '0o1289');
3030
});
31+
32+
it('should not allow underscore', function () {
33+
// previously parsed as int
34+
assert.strictEqual(yaml.load('1_23'), '1_23');
35+
// previously parsed as float
36+
assert.strictEqual(yaml.load('1_23.45'), '1_23.45');
37+
});
3138
});
3239

3340

test/issues/0614.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ it('Should allow int override', function () {
3434
const SCHEMA = yaml.DEFAULT_SCHEMA.extend({ implicit: [ BigIntType ] });
3535

3636
const data = `
37-
int: -123_456_789
38-
bigint: -12_345_678_901_234_567_890
39-
float: -12_345_678_901_234_567_890.1234
37+
int: -123456789
38+
bigint: -12345678901234567890
39+
float: -12345678901234567890.1234
4040
`;
4141

4242
assert.deepStrictEqual(yaml.load(data, { schema: SCHEMA }), {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
canonical: 6.8523015e+5
2-
exponential: 685.230_15e+03
3-
fixed: 685_230.15
2+
exponential: 685.23015e+03
3+
fixed: 685230.15
44
negative infinity: -.inf
55
not a number: .NaN
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
canonical: 685230
2-
decimal: +685_230
2+
decimal: +685230
33
octal: 0o2472256
4-
hexadecimal: 0x_0A_74_AE
5-
binary: 0b1010_0111_0100_1010_1110
4+
hexadecimal: 0x0A74AE
5+
binary: 0b10100111010010101110

0 commit comments

Comments
 (0)