Skip to content

Commit 4a78242

Browse files
authored
Merge pull request #115 from lpbak/issue_108
Issue 108
2 parents 7497c5a + d38a183 commit 4a78242

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/util/print_dec.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
117117
}
118118

119119
// Not easily printable, write down fraction, then full number, then exponent
120+
121+
// Since we move the decimal point right after the first digit, we have to adjust the
122+
// exponent part. If the number is long enough, this may result in the exponent switching
123+
// sign from negative to positive - we have to handle this case separately.
124+
let mut exponent_positive = false;
120125
if n < 10 {
121126
// Single digit, no fraction
122127
curr -= 1;
@@ -154,16 +159,24 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
154159
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
155160
}
156161

157-
// Substract the amount of digits printed in the fraction
158-
// from the exponent that we still need to print using
159-
// the `e` notation
160-
e -= buf.len() as u16 - curr as u16;
162+
let printed_so_far = buf.len() as u16 - curr as u16;
163+
164+
165+
if printed_so_far <= e {
166+
// Subtract the amount of digits printed in the fraction
167+
// from the exponent that we still need to print using
168+
// the `e` notation
169+
e -= printed_so_far;
170+
} else {
171+
// Same as e = |e - printed_so_far|.
172+
e = printed_so_far - e;
173+
exponent_positive = true;
174+
}
161175

162176
curr -= 1;
163177
*buf_ptr.offset(curr) = b'.';
164178

165179
write_num(&mut n, &mut curr, buf_ptr, lut_ptr);
166-
167180
}
168181

169182
// Write out the number with a fraction
@@ -174,8 +187,16 @@ pub unsafe fn write<W: io::Write>(wr: &mut W, positive: bool, mut n: u64, expone
174187
)
175188
));
176189

177-
// Write the remaining `e` notation
178-
try!(wr.write_all(b"e-"));
190+
// Omit the 'e' notation for e == 0
191+
if e == 0 {
192+
return Ok(());
193+
}
194+
// Write the remaining `e` notation, with proper sign
195+
if exponent_positive {
196+
wr.write_all(b"e+")?;
197+
} else {
198+
wr.write_all(b"e-")?;
199+
}
179200
return write(wr, true, e as u64, 0);
180201

181202
}

tests/number.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,3 @@ fn as_fixed_point_i64() {
126126
fn convert_f64_precision() {
127127
assert_eq!(Number::from_parts(true, 4750000000000001, -18), 0.004750000000000001);
128128
}
129-
130-
#[test]
131-
fn issue_107() {
132-
let n = Number::from_parts(true, 1, -32768);
133-
assert_eq!(format!("{}", n), "1e-32768");
134-
}

tests/print_dec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern crate json;
2+
3+
use json::number::Number;
4+
5+
#[test]
6+
fn issue_107() {
7+
let n = Number::from_parts(true, 1, -32768);
8+
assert_eq!(format!("{}", n), "1e-32768");
9+
}
10+
11+
#[test]
12+
fn issue_108_exponent_positive() {
13+
let n = Number::from_parts(true, 10_000_000_000_000_000_001, -18);
14+
assert_eq!(format!("{}", n), "1.0000000000000000001e+1");
15+
}
16+
17+
#[test]
18+
fn issue_108_exponent_0() {
19+
let n = Number::from_parts(true, 10_000_000_000_000_000_001, -19);
20+
assert_eq!(format!("{}", n), "1.0000000000000000001");
21+
}

0 commit comments

Comments
 (0)