Files
adler32
aho_corasick
alloc_no_stdlib
alloc_stdlib
ansi_term
assert_matches
atty
backtrace
backtrace_sys
bincode
binjs
binjs_convert_from_json
binjs_decode
binjs_dump
binjs_encode
binjs_es6
binjs_generate_prediction_tables
binjs_generic
binjs_io
binjs_meta
binjs_shared
bitflags
brotli
brotli_decompressor
byteorder
c2_chacha
cfg_if
clap
crc32fast
derive_more
downcast_rs
either
env_logger
failure
flate2
getrandom
humantime
inflector
cases
camelcase
case
classcase
kebabcase
pascalcase
screamingsnakecase
sentencecase
snakecase
tablecase
titlecase
traincase
numbers
deordinalize
ordinalize
suffix
foreignkey
itertools
itoa
lazy_static
libc
log
lzw
memchr
miniz_oxide
nom
ppv_lite86
proc_macro2
quick_error
quote
rand
rand_chacha
rand_core
rand_pcg
range_encoding
regex
regex_syntax
rustc_demangle
ryu
serde
serde_derive
serde_json
smallvec
strsim
syn
termcolor
textwrap
thread_local
unicode_width
unicode_xid
vec_map
weedle
which
xml
  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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use bytes::varnum::*;

use binjs_shared::F64;

use std;
use std::io::Write;

/// The representation of "no float", used for `float | null`.
const NONE_FLOAT_REPR: u64 = 0x7FF0000000000001;
const VARNUM_PREFIX_FLOAT: [u8; 2] = VARNUM_INVALID_ZERO_1;
const VARNUM_NULL: [u8; 3] = VARNUM_INVALID_ZERO_2;

pub fn varbytes_of_float(value: Option<f64>) -> Box<[u8]> {
    let mut buf = Vec::with_capacity(4);
    buf.write_maybe_varfloat(value).unwrap(); // The write cannot fail on a Vec<>
    buf.into()
}

/// Encode a f64 | null, little-endian
pub fn bytes_of_float(value: Option<f64>) -> [u8; 8] {
    let mut as_u64: u64 = match value {
        None => NONE_FLOAT_REPR,
        Some(value) => unsafe { std::mem::transmute::<f64, u64>(value) },
    };
    let mut buf: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
    for i in 0..8 {
        buf[i] = (as_u64 % 256) as u8;
        as_u64 >>= 8;
    }
    buf
}

/// Utility for manipulating of `varfloats`, a somewhat optimized representation of floats.
///
/// This format is designed to help the most common floating point numbers (fairly short
/// integers) take fewer bytes.
///
/// Instead of always fitting in 64 bits, varfloats are represented as follows:
/// - null is represented as VARNUM_NULL (24 bits);
/// - floats with an i32 value are transmuted to u32s and represented as signed varnums
///    (8 to 40 bits, where numbers in [-63, 63] fit in 8 bits);
/// - other float values are prefixed with VARNUM_PREFIX_FLOAT (16 bits), then represented
///     with the usual 64 bits.
pub trait WriteVarFloat {
    fn write_maybe_varfloat(&mut self, value: Option<f64>) -> Result<usize, std::io::Error>;
    fn write_varfloat(&mut self, num: f64) -> Result<usize, std::io::Error>;

    /// Utility: as `write_maybe_varfloat` but with a `F64` instead of a `f64`.
    fn write_maybe_varfloat2(&mut self, value: Option<F64>) -> Result<usize, std::io::Error> {
        self.write_maybe_varfloat(value.map(Into::<f64>::into))
    }

    /// Utility: as `write_varfloat` but with a `F64` instead of a `f64`.
    fn write_varfloat2(&mut self, num: F64) -> Result<usize, std::io::Error> {
        self.write_varfloat(num.into())
    }
}

impl<T> WriteVarFloat for T
where
    T: Write,
{
    fn write_maybe_varfloat(&mut self, value: Option<f64>) -> Result<usize, std::io::Error> {
        match value {
            None => {
                // Magic constant NULL.
                self.write_all(&VARNUM_NULL)?;
                Ok(VARNUM_NULL.len())
            }
            Some(v) => self.write_varfloat(v),
        }
    }

    fn write_varfloat(&mut self, value: f64) -> Result<usize, std::io::Error> {
        {
            // Let's see if we can represent this as an integer.
            // We can represent it as an integer if:
            // - it has the same value as its projection to i32;
            // - it's not -0.0
            let as_signed_integer = value as i32;
            if as_signed_integer as f64 == value
                && (as_signed_integer != 0 || value.is_sign_positive())
            {
                return self.write_signed_varnum(as_signed_integer);
            }
        }
        // Encode as a float prefixed by 0b00000001 0b00000000 (which is an invalid integer).
        let bytes = bytes_of_float(Some(value));
        self.write_all(&VARNUM_PREFIX_FLOAT)?;
        self.write_all(&bytes)?;
        Ok(bytes.len() + VARNUM_PREFIX_FLOAT.len())
    }
}

/// Utility for manipulating of `varfloats`, a somewhat optimized representation of floats.
///
/// This format is designed to help the most common floating point numbers (fairly short
/// integers) take fewer bytes.
///
/// Instead of always fitting in 64 bits, varfloats are represented as follows:
/// - null is represented as VARNUM_NULL (24 bits);
/// - floats with an i32 value are transmuted to u32s and represented as signed varnums
///    (8 to 40 bits, where numbers in [-63, 63] fit in 8 bits);
/// - other float values are prefixed with VARNUM_PREFIX_FLOAT (16 bits), then represented
///     with the usual 64 bits.
pub trait ReadVarFloat {
    fn read_maybe_varfloat(&mut self) -> Result<Option<f64>, std::io::Error>;
}
impl<T> ReadVarFloat for T
where
    T: std::io::Read,
{
    fn read_maybe_varfloat(&mut self) -> Result<Option<f64>, std::io::Error> {
        let mut as_i32 = 0;
        let bytes_read = self.read_extended_signed_varnum_to(&mut as_i32)?;
        if as_i32 == 0 {
            match bytes_read {
                1 => {
                    // 0 as one byte, that's the regular 0.
                    return Ok(Some(0.));
                }
                2 => {
                    // 0 as two bytes, that's VARNUM_PREFIX_FLOAT.
                    // The next 8 bytes are a IEEE f64.
                    let mut buf: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
                    self.read_exact(&mut buf)?;
                    return Ok(float_of_bytes(&buf));
                }
                3 => {
                    // 0 as three bytes, that's VARNUM_NULL
                    return Ok(None);
                }
                _ => {
                    // That's an error.
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidData,
                        "Invalid varfloat",
                    ));
                }
            }
        }
        // Otherwise, it's an i32.
        Ok(Some(as_i32 as f64))
    }
}

/// Decode a f64 | null, little-endian
pub fn float_of_bytes(buf: &[u8; 8]) -> Option<f64> {
    let as_u64 = ((buf[0] as u64) << 0)
        | ((buf[1] as u64) << 8)
        | ((buf[2] as u64) << 16)
        | ((buf[3] as u64) << 24)
        | ((buf[4] as u64) << 32)
        | ((buf[5] as u64) << 40)
        | ((buf[6] as u64) << 48)
        | ((buf[7] as u64) << 56);
    if as_u64 == NONE_FLOAT_REPR {
        None
    } else {
        let as_f64 = unsafe { std::mem::transmute::<_, f64>(as_u64) };
        Some(as_f64)
    }
}

#[test]
fn test_floats() {
    use std::f64::*;
    for x in &[0., 100., 10., 1000., INFINITY, MIN, MAX, NEG_INFINITY] {
        let value = Some(*x);
        let encoded = bytes_of_float(value);
        let decoded = float_of_bytes(&encoded);
        println!("Encoded {:?} as {:?}, decoded as {:?}", x, encoded, decoded);
        assert_eq!(decoded, value);
    }

    assert_eq!(float_of_bytes(&bytes_of_float(None)), None);
}

#[test]
fn test_var_floats() {
    fn single_value(value: Option<f64>) -> usize {
        let mut buf = Vec::new();
        buf.write_maybe_varfloat(value).unwrap();

        let size = buf.len();

        let mut input = std::io::Cursor::new(buf);
        let decoded = input.read_maybe_varfloat().unwrap();
        assert_eq!(decoded, value);

        size
    }
    use std::f64::*;

    // Testing values that should fit in one byte.
    for i in -63..63 {
        let bytes = single_value(Some(i as f64));
        assert_eq!(
            bytes, 1,
            "Integer values between -63 and 63 should fit in one byte: {}",
            i
        );
    }

    // Testing a sampling of other values.
    for x in &[
        -256.,
        1000.,
        0.5,
        1.7,
        3.8,
        11.1,
        INFINITY,
        MIN,
        MAX,
        NEG_INFINITY,
    ] {
        single_value(Some(*x));
    }

    // Finally, `None`.
    single_value(None);
}