Skip to content

Commit

Permalink
Include the minus sign in the same buffer, to only make a single writ…
Browse files Browse the repository at this point in the history
…e_all call.
  • Loading branch information
SimonSapin authored and dtolnay committed Aug 29, 2017
1 parent 678a4b4 commit 220a868
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ macro_rules! impl_Integer {
let mut n = if is_nonnegative {
self as $conv_fn
} else {
try!(wr.write_all(b"-"));
// convert the negative num to positive by summing 1 to it's 2 complement
(!(self as $conv_fn)).wrapping_add(1)
};
Expand Down Expand Up @@ -81,13 +80,15 @@ macro_rules! impl_Integer {
curr -= 2;
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
}

if !is_nonnegative {
curr -= 1;
*buf_ptr.offset(curr) = b'-';
}
}

let mut len = buf.len() - curr as usize;
let len = buf.len() - curr as usize;
try!(wr.write_all(unsafe { slice::from_raw_parts(buf_ptr.offset(curr), len) }));
if !is_nonnegative {
len += 1;
}
Ok(len)
}
})*);
Expand Down
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test!(
test_0u64(0u64, "0"),
test_HALFu64(<u32>::max_value() as u64, "4294967295"),
test_MAXu64(<u64>::max_value(), "18446744073709551615"),
test_MINi64(<i64>::min_value(), "-9223372036854775808"),

test_0i16(0i16, "0"),
test_MINi16(<i16>::min_value(), "-32768"),
Expand Down

0 comments on commit 220a868

Please sign in to comment.