54 lines
1016 B
Rust
54 lines
1016 B
Rust
use core::fmt;
|
|
|
|
// Should be greater than MAX_SIG_DIGITS = 17
|
|
const DEC_BUF_SIZE: usize = 32;
|
|
|
|
pub struct DecBuf {
|
|
buf: [u8; DEC_BUF_SIZE],
|
|
len: usize
|
|
}
|
|
|
|
impl DecBuf {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
buf: unsafe { core::mem::uninitialized() },
|
|
len: 0
|
|
}
|
|
}
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
&self.buf[0..self.len]
|
|
}
|
|
|
|
fn extend_mut_slice(&mut self, add_len: usize) -> Result<&mut [u8], fmt::Error> {
|
|
let cur_len = self.len;
|
|
let new_len = cur_len + add_len;
|
|
if new_len > self.buf.len() {
|
|
Err(fmt::Error)
|
|
} else {
|
|
self.len = new_len;
|
|
Ok(&mut self.buf[cur_len..new_len])
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Write for DecBuf {
|
|
#[inline]
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
if s.is_empty() {
|
|
Ok(())
|
|
} else {
|
|
let new_bytes = s.as_bytes();
|
|
self.extend_mut_slice(new_bytes.len()).map(|b| {
|
|
b.copy_from_slice(new_bytes);
|
|
})
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn write_char(&mut self, c: char) -> fmt::Result {
|
|
self.extend_mut_slice(c.len_utf8()).map(|b| {
|
|
c.encode_utf8(b);
|
|
})
|
|
}
|
|
} |