Skip to content

Commit

Permalink
Faster float handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Jul 1, 2020
1 parent 14f9770 commit 6d99640
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions ext/oj/parse.c
Expand Up @@ -453,6 +453,7 @@ read_num(ParseInfo pi) {
// except when mimicing the JSON gem or in strict mode.
if (StrictMode == pi->options.mode || CompatMode == pi->options.mode) {
int pos = (int)(pi->cur - ni.str);

if (1 == pos || (2 == pos && ni.neg)) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number");
return;
Expand All @@ -468,11 +469,19 @@ read_num(ParseInfo pi) {
if (0 < ni.num || 0 < ni.i) {
dec_cnt++;
}
ni.num = ni.num * 10 + d;
ni.div *= 10;
ni.di++;
if (INT64_MAX <= ni.div || DEC_MAX < dec_cnt) {
ni.big = 1;
if (INT64_MAX <= ni.div) {
if (!ni.no_big) {
ni.big = true;
}
} else {
ni.num = ni.num * 10 + d;
ni.div *= 10;
ni.di++;
if (INT64_MAX <= ni.div || DEC_MAX < dec_cnt) {
if (!ni.no_big) {
ni.big = true;
}
}
}
}
}
Expand All @@ -490,7 +499,7 @@ read_num(ParseInfo pi) {
for (; '0' <= *pi->cur && *pi->cur <= '9'; pi->cur++) {
ni.exp = ni.exp * 10 + (*pi->cur - '0');
if (EXP_MAX <= ni.exp) {
ni.big = 1;
ni.big = true;
}
}
if (eneg) {
Expand Down
5 changes: 5 additions & 0 deletions test/test_custom.rb
Expand Up @@ -118,6 +118,11 @@ def test_float
dump_and_load(-2.48e100 * 1.0e10, false)
end

def test_float_parse
f = Oj.load("12.123456789012345678", mode: :custom, bigdecimal_load: :float);
assert_equal(Float, f.class)
end

def test_nan_dump
assert_equal('null', Oj.dump(0/0.0, :nan => :null))
assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
Expand Down

0 comments on commit 6d99640

Please sign in to comment.