From 9a1726e1e40a4f6fe7d8980c89ab1c290140876d Mon Sep 17 00:00:00 2001 From: David Mohr Date: Mon, 28 Oct 2019 07:59:06 +1100 Subject: [PATCH] fix: remove support for underscore (`_`) in number parsing (fixes #627) --- lib/type/float.js | 6 +++--- lib/type/int.js | 24 +++++------------------- test/issues/0027.js | 7 +++++++ test/issues/0614.js | 6 +++--- test/samples-common/construct-float.yml | 4 ++-- test/samples-common/construct-int.yml | 6 +++--- 6 files changed, 23 insertions(+), 30 deletions(-) diff --git a/lib/type/float.js b/lib/type/float.js index 74d77ec2..960c0b64 100644 --- a/lib/type/float.js +++ b/lib/type/float.js @@ -5,10 +5,10 @@ var Type = require('../type'); var YAML_FLOAT_PATTERN = new RegExp( // 2.5e4, 2.5 and integers - '^(?:[-+]?(?:[0-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?' + + '^(?:[-+]?(?:[0-9][0-9]*)(?:\\.[0-9]*)?(?:[eE][-+]?[0-9]+)?' + // .2e4, .2 // special case, seems not from spec - '|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?' + + '|\\.[0-9]+(?:[eE][-+]?[0-9]+)?' + // .inf '|[-+]?\\.(?:inf|Inf|INF)' + // .nan @@ -30,7 +30,7 @@ function resolveYamlFloat(data) { function constructYamlFloat(data) { var value, sign; - value = data.replace(/_/g, '').toLowerCase(); + value = data.toLowerCase(); sign = value[0] === '-' ? -1 : 1; if ('+-'.indexOf(value[0]) >= 0) { diff --git a/lib/type/int.js b/lib/type/int.js index 3fe3a443..72493723 100644 --- a/lib/type/int.js +++ b/lib/type/int.js @@ -47,11 +47,10 @@ function resolveYamlInteger(data) { for (; index < max; index++) { ch = data[index]; - if (ch === '_') continue; if (ch !== '0' && ch !== '1') return false; hasDigits = true; } - return hasDigits && ch !== '_'; + return hasDigits; } @@ -61,55 +60,42 @@ function resolveYamlInteger(data) { for (; index < max; index++) { ch = data[index]; - if (ch === '_') continue; if (!isHexCode(data.charCodeAt(index))) return false; hasDigits = true; } - return hasDigits && ch !== '_'; + return hasDigits; } - if (ch === 'o') { // base 8 index++; for (; index < max; index++) { ch = data[index]; - if (ch === '_') continue; if (!isOctCode(data.charCodeAt(index))) return false; hasDigits = true; } - return hasDigits && ch !== '_'; + return hasDigits; } } // base 10 (except 0) - // 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; - - return true; + // Should have digits + return hasDigits; } function constructYamlInteger(data) { var value = data, sign = 1, ch; - if (value.indexOf('_') !== -1) { - value = value.replace(/_/g, ''); - } - ch = value[0]; if (ch === '-' || ch === '+') { diff --git a/test/issues/0027.js b/test/issues/0027.js index acef8267..8c775fed 100644 --- a/test/issues/0027.js +++ b/test/issues/0027.js @@ -28,6 +28,13 @@ describe('Should load numbers in YAML 1.2 format', function () { // not valid octal assert.strictEqual(yaml.load('0o1289'), '0o1289'); }); + + it('should not allow underscore', function () { + // previously parsed as int + assert.strictEqual(yaml.load('1_23'), '1_23'); + // previously parsed as float + assert.strictEqual(yaml.load('1_23.45'), '1_23.45'); + }); }); diff --git a/test/issues/0614.js b/test/issues/0614.js index 18d9e119..5f4df377 100644 --- a/test/issues/0614.js +++ b/test/issues/0614.js @@ -34,9 +34,9 @@ it('Should allow int override', function () { const SCHEMA = yaml.DEFAULT_SCHEMA.extend({ implicit: [ BigIntType ] }); const data = ` -int: -123_456_789 -bigint: -12_345_678_901_234_567_890 -float: -12_345_678_901_234_567_890.1234 +int: -123456789 +bigint: -12345678901234567890 +float: -12345678901234567890.1234 `; assert.deepStrictEqual(yaml.load(data, { schema: SCHEMA }), { diff --git a/test/samples-common/construct-float.yml b/test/samples-common/construct-float.yml index 3bcf9750..a568fa87 100644 --- a/test/samples-common/construct-float.yml +++ b/test/samples-common/construct-float.yml @@ -1,5 +1,5 @@ canonical: 6.8523015e+5 -exponential: 685.230_15e+03 -fixed: 685_230.15 +exponential: 685.23015e+03 +fixed: 685230.15 negative infinity: -.inf not a number: .NaN diff --git a/test/samples-common/construct-int.yml b/test/samples-common/construct-int.yml index 93d092e6..7a866914 100644 --- a/test/samples-common/construct-int.yml +++ b/test/samples-common/construct-int.yml @@ -1,5 +1,5 @@ canonical: 685230 -decimal: +685_230 +decimal: +685230 octal: 0o2472256 -hexadecimal: 0x_0A_74_AE -binary: 0b1010_0111_0100_1010_1110 +hexadecimal: 0x0A74AE +binary: 0b10100111010010101110