mirror of
https://gitlab.com/artofrev/smallint.git
synced 2024-12-21 22:12:43 -05:00
Compare commits
2 Commits
f9eb2a1afa
...
2dc3f2472f
Author | SHA1 | Date | |
---|---|---|---|
2dc3f2472f | |||
ef013a5b16 |
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "smallint"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
edition = "2021"
|
||||
authors = ["artofrev"]
|
||||
description = "A library for optimized arbitrary precision integers."
|
||||
|
|
114
src/pretty.rs
114
src/pretty.rs
|
@ -1,6 +1,8 @@
|
|||
use crate::SmallInt;
|
||||
use crate::SmallUint;
|
||||
|
||||
use crate::smallint::{SmallIntType, SmallUintType};
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
use num_bigint::{BigInt, BigUint};
|
||||
|
||||
|
@ -17,3 +19,115 @@ impl core::fmt::Debug for SmallUint {
|
|||
write!(f, "{}", BigUint::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl core::fmt::Display for SmallInt {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", BigInt::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl core::fmt::Display for SmallUint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", BigUint::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::UpperHex for SmallUint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self.0 {
|
||||
SmallUintType::Inline(i) => write!(f, "0x{:X}", i),
|
||||
SmallUintType::Heap((r, s)) => {
|
||||
let slice = unsafe { core::slice::from_raw_parts(r, s) };
|
||||
let mut iter = slice.iter().rev();
|
||||
if let Some(i) = iter.next() {
|
||||
write!(f, "0x{:X}", i)?;
|
||||
}
|
||||
for i in iter {
|
||||
write!(f, "{:08X}", i)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::LowerHex for SmallUint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self.0 {
|
||||
SmallUintType::Inline(i) => write!(f, "0x{:x}", i),
|
||||
SmallUintType::Heap((r, s)) => {
|
||||
let slice = unsafe { core::slice::from_raw_parts(r, s) };
|
||||
let mut iter = slice.iter().rev();
|
||||
if let Some(i) = iter.next() {
|
||||
write!(f, "0x{:x}", i)?;
|
||||
}
|
||||
for i in iter {
|
||||
write!(f, "{:08x}", i)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::UpperHex for SmallInt {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self.0 {
|
||||
SmallIntType::Inline(i) => match i {
|
||||
x if x >= 0 => write!(f, "0x{:X}", i),
|
||||
x if x < 0 => write!(f, "-0x{:X}", -i),
|
||||
_ => panic!("This should not happen."),
|
||||
},
|
||||
SmallIntType::Heap((r, s)) => {
|
||||
let sign = match s.signum() {
|
||||
x if x >= 0 => "",
|
||||
x if x < 0 => "-",
|
||||
_ => panic!("This should not happen."),
|
||||
};
|
||||
|
||||
let slice =
|
||||
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
|
||||
let mut iter = slice.iter().rev();
|
||||
if let Some(i) = iter.next() {
|
||||
write!(f, "{}0x{:X}", sign, i)?;
|
||||
}
|
||||
for i in iter {
|
||||
write!(f, "{:08X}", i)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::LowerHex for SmallInt {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self.0 {
|
||||
SmallIntType::Inline(i) => match i {
|
||||
x if x >= 0 => write!(f, "0x{:x}", i),
|
||||
x if x < 0 => write!(f, "-0x{:x}", -i),
|
||||
_ => panic!("This should not happen."),
|
||||
},
|
||||
SmallIntType::Heap((r, s)) => {
|
||||
let sign = match s.signum() {
|
||||
x if x >= 0 => "",
|
||||
x if x < 0 => "-",
|
||||
_ => panic!("This should not happen."),
|
||||
};
|
||||
|
||||
let slice =
|
||||
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
|
||||
let mut iter = slice.iter().rev();
|
||||
if let Some(i) = iter.next() {
|
||||
write!(f, "{}0x{:x}", sign, i)?;
|
||||
}
|
||||
for i in iter {
|
||||
write!(f, "{:08x}", i)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user