From 0feee69ee904c7d4dfd728d364e557ab354e9e3c Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 11 Jan 2026 04:40:56 +0300 Subject: [PATCH] better number support... Signed-off-by: Alex A. Naanou --- serialize.js | 27 +++++++++++++++++++++++---- test.js | 3 +++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/serialize.js b/serialize.js index 07fddc3..116f438 100644 --- a/serialize.js +++ b/serialize.js @@ -408,13 +408,32 @@ module.eJSON = { && str.slice(i, i+'-Infinity'.length) == '-Infinity'){ return [-Infinity, i+'-Infinity'.length, line] } // numbers... - var j = i+1 + // XXX do we need to handle the odd case of 077 vs 088??? + var j = i + var mode = 'dec' + if(str[j] == '0' + && 'xXbBoO'.includes(str[j+1])){ + mode = str[j+1] + j++ } + j++ while(j < str.length && (str[j] == '.' - || str[j] == 'e' + // dec/oct/bin... + // XXX do we need to be pedantic and check the rest + // of the modes explicitly??? || (str[j] >= '0' - && str[j] <= '9'))){ - if(str.slice(j, j+2) == 'e+'){ + && str[j] <= '9') + // hex... + || (mode == 'x' + && ((str[j] >= 'a' + && str[j] <= 'f') + || (str[j] >= 'A' + && str[j] <= 'F'))) + // exponent... + || str[j] == 'e' + || str[j] == 'E')){ + if('eE'.includes(str[j]) + && '+-'.includes(str[j+1])){ j++ } j++ } // BigInt... diff --git a/test.js b/test.js index 0cba81f..22b1d7f 100755 --- a/test.js +++ b/test.js @@ -72,6 +72,8 @@ var setups = test.Setups({ // XXX also test diffrerent quotations... string: function(assert){ return ['"string"', json] }, + 'string-empty': function(assert){ + return ['""', json] }, 'string-multiline': function(assert){ return ['"string\\nstring\\nstring"', json] }, @@ -202,6 +204,7 @@ test.Cases({ ['123.', '123'], ['+123', '123'], ['123e100', '123e+100'], + ['0xff', '255'], // string quotes... ["'abc'", '"abc"'],