1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// License: see LICENSE file at root directory of `master` branch

//! # Number writer

#![cfg(feature="std")]

use {
    alloc::string::ToString,
    core::fmt::{self, Debug, Formatter},
    std::io::Write,

    crate::IoResult,

    super::{Inner, Number},
};

#[cfg(any(
    target_pointer_width = "8", target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64",
    target_pointer_width = "128",
))]
const BUF_SIZE: usize = 40;

/// This is redundant. But, on this big machine, some large buffer like this is fine.
#[cfg(not(any(
    target_pointer_width = "8", target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64",
    target_pointer_width = "128",
)))]
const BUF_SIZE: usize = core::mem::size_of::<usize>() * 3;

const BUF_LAST_IDX: usize = BUF_SIZE - 1;

/// # Number writer
pub struct NumberWriter {
    buf: [u8; BUF_SIZE],
}

const I8_MIN_VALUE: &[u8] = b"-128";
const I16_MIN_VALUE: &[u8] = b"-32768";
const I32_MIN_VALUE: &[u8] = b"-2147483648";
const I64_MIN_VALUE: &[u8] = b"-9223372036854775808";
const I128_MIN_VALUE: &[u8] = b"-170141183460469231731687303715884105728";

macro_rules! write_unsigned { ($buf: expr, $n: expr, $negative: expr, $stream: ident) => {{
    let mut n = $n;
    let mut i = BUF_LAST_IDX;
    loop {
        $buf[i] = (n % 10) as u8 + b'0';
        n /= 10;
        match n {
            0 => {
                if $negative {
                    i -= 1;
                    $buf[i] = b'-';
                }
                return $stream.write_all(&$buf[i..]);
            },
            _ => i -= 1,
        };
    }
}}}

macro_rules! write_signed { ($buf: expr, $ty: ty, $min_value: ident, $n: ident, $stream: ident) => {{
    if $n == <$ty>::min_value() {
        return $stream.write_all($min_value);
    }

    let negative = $n.is_negative();
    write_unsigned!($buf, $n.abs(), negative, $stream)
}}}

impl NumberWriter {

    /// # Makes new instance
    pub fn new() -> Self {
        Self {
            buf: [0; BUF_SIZE],
        }
    }

    /// # Writes a number to stream
    pub fn write<W>(&mut self, n: &Number, stream: &mut W) -> IoResult<()> where W: Write {
        match n.inner {
            Inner::I8(i) => write_signed!(self.buf, i8, I8_MIN_VALUE, i, stream),
            Inner::I16(i) => write_signed!(self.buf, i16, I16_MIN_VALUE, i, stream),
            Inner::I32(i) => write_signed!(self.buf, i32, I32_MIN_VALUE, i, stream),
            Inner::I64(i) => write_signed!(self.buf, i64, I64_MIN_VALUE, i, stream),
            Inner::I128(i) => write_signed!(self.buf, i128, I128_MIN_VALUE, i, stream),

            #[cfg(target_pointer_width = "8")]
            Inner::ISize(i) => {
                let i = i as i8;
                write_signed!(self.buf, i8, I8_MIN_VALUE, i, stream)
            },
            #[cfg(target_pointer_width = "16")]
            Inner::ISize(i) => {
                let i = i as i16;
                write_signed!(self.buf, i16, I16_MIN_VALUE, i, stream)
            },
            #[cfg(target_pointer_width = "32")]
            Inner::ISize(i) => {
                let i = i as i32;
                write_signed!(self.buf, i32, I32_MIN_VALUE, i, stream)
            },
            #[cfg(target_pointer_width = "64")]
            Inner::ISize(i) => {
                let i = i as i64;
                write_signed!(self.buf, i64, I64_MIN_VALUE, i, stream)
            },
            #[cfg(target_pointer_width = "128")]
            Inner::ISize(i) => {
                let i = i as i128;
                write_signed!(self.buf, i128, I128_MIN_VALUE, i, stream)
            },

            Inner::U8(u) => write_unsigned!(self.buf, u, false, stream),
            Inner::U16(u) => write_unsigned!(self.buf, u, false, stream),
            Inner::U32(u) => write_unsigned!(self.buf, u, false, stream),
            Inner::U64(u) => write_unsigned!(self.buf, u, false, stream),
            Inner::U128(u) => write_unsigned!(self.buf, u, false, stream),
            Inner::USize(u) => write_unsigned!(self.buf, u, false, stream),

            Inner::F32(f) => stream.write_all(f.to_string().as_bytes()),
            Inner::F64(f) => stream.write_all(f.to_string().as_bytes()),
        }
    }

}

impl Debug for NumberWriter {

    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        write!(f, "NumberWriter {{ buf: {:?} }}", &self.buf[..])
    }

}

#[test]
fn tests() -> IoResult<()> {
    use alloc::vec::Vec;

    assert!(BUF_SIZE >= u128::max_value().to_string().len());
    assert!(BUF_SIZE >= i128::min_value().to_string().len());

    assert!(BUF_SIZE >= usize::max_value().to_string().len());
    assert!(BUF_SIZE >= isize::min_value().to_string().len());

    let mut buf = Vec::with_capacity(BUF_SIZE);
    let mut nw = NumberWriter::new();
    for i in 0..99 {
        buf.clear();
        let n = Number::from(u64::max_value() - i as u64);
        nw.write(&n, &mut buf)?;
        assert_eq!(n.to_string().as_bytes(), buf.as_slice());

        buf.clear();
        let n = Number::from(u128::max_value() - i as u128);
        nw.write(&n, &mut buf)?;
        assert_eq!(n.to_string().as_bytes(), buf.as_slice());

        buf.clear();
        let n = Number::from(i64::min_value() + i as i64);
        nw.write(&n, &mut buf)?;
        assert_eq!(n.to_string().as_bytes(), buf.as_slice());

        buf.clear();
        let n = Number::from(i128::min_value() + i as i128);
        nw.write(&n, &mut buf)?;
        assert_eq!(n.to_string().as_bytes(), buf.as_slice());
    }

    Ok(())
}