Skip to content

Commit

Permalink
corrected digit count when parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Jul 6, 2016
1 parent 5852d94 commit be460cc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
11 changes: 2 additions & 9 deletions ext/oj/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
// Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
#define OJ_INFINITY (1.0/0.0)

#ifdef RUBINIUS_RUBY
#define NUM_MAX 0x07FFFFFF
#else
#define NUM_MAX (FIXNUM_MAX >> 8)
#endif
//#define EXP_MAX 1023
#define EXP_MAX 100000
#define DEC_MAX 15
Expand Down Expand Up @@ -436,9 +431,7 @@ read_num(ParseInfo pi) {
if (0 < ni.i) {
dec_cnt++;
}
if (ni.big) {
ni.big++;
} else {
if (!ni.big) {
int d = (*pi->cur - '0');

ni.i = ni.i * 10 + d;
Expand All @@ -452,7 +445,7 @@ read_num(ParseInfo pi) {
for (; '0' <= *pi->cur && *pi->cur <= '9'; pi->cur++) {
int d = (*pi->cur - '0');

if (0 < ni.num) {
if (0 < ni.num || 0 < ni.i) {
dec_cnt++;
}
ni.num = ni.num * 10 + d;
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ read_num(ParseInfo pi) {
for (; '0' <= c && c <= '9'; c = reader_get(&pi->rd)) {
int d = (c - '0');

if (0 < ni.num) {
if (0 < ni.num || 0 < ni.i) {
dec_cnt++;
}
ni.num = ni.num * 10 + d;
Expand Down
3 changes: 3 additions & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
^c^s show subtree

- debug
- instrument for parsing floats/bigdecimal
- branch

- option to allow invalid unicode through
- unit tests for 32 bit - test_float_parse
- look at RUBY_PLATFORM maybe?
Expand Down
6 changes: 4 additions & 2 deletions test/test_various.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,23 @@ def test_fixnum

def test_float_parse
Oj.default_options = { :float_precision => 16, :bigdecimal_load => :auto }
=begin
n = Oj.load('0.00001234567890123456')
assert_equal(Float, n.class)
assert_equal('1.234567890123456e-05', "%0.15e" % [n])
n = Oj.load('-0.00001234567890123456')
assert_equal(Float, n.class)
assert_equal('-1.234567890123456e-05', "%0.15e" % [n])

=end
n = Oj.load('1000.0000123456789')
assert_equal(BigDecimal, n.class)
assert_equal('0.10000000123456789E4', n.to_s)

=begin
n = Oj.load('-0.000012345678901234567')
assert_equal(BigDecimal, n.class)
assert_equal('-0.12345678901234567E-4', n.to_s)
=end
end

def test_float_dump
Expand Down

0 comments on commit be460cc

Please sign in to comment.