[go: up one dir, main page]

lexical-core 0.2.0

Lexical, to- and from-string conversion routines.
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Low-level API generator.
//!
//! Uses either the internal "Grisu2", or the external "Grisu3" or "Ryu"
//! algorithms provided by `https://github.com/dtolnay`.

use lib::ptr;
use util::*;

#[cfg(feature = "radix")]
use super::radix::{double_radix, float_radix};

// Select the back-end
cfg_if! {
if #[cfg(feature = "grisu3")] {
    use super::grisu3::{double_decimal, float_decimal};
} else if #[cfg(feature = "ryu")] {
    use super::ryu::{double_decimal, float_decimal};
} else {
    use super::grisu2::{double_decimal, float_decimal};
}}  //cfg_if

// TRAITS

/// Trait to define serialization of a float to string.
pub(crate) trait FloatToString: Float {
    /// Export float to decimal string with optimized algorithm.
    unsafe extern "C" fn decimal(self, first: *mut u8) -> *mut u8;

    /// Export float to radix string with slow algorithm.
    #[cfg(feature = "radix")]
    unsafe extern "C" fn radix(self, radix: u32, first: *mut u8) -> *mut u8;
}

impl FloatToString for f32 {
    #[inline(always)]
    unsafe extern "C" fn decimal(self, first: *mut u8) -> *mut u8 {
        float_decimal(self, first)
    }

    #[inline(always)]
    #[cfg(feature = "radix")]
    unsafe extern "C" fn radix(self, radix: u32, first: *mut u8) -> *mut u8 {
        float_radix(self, radix, first)
    }
}

impl FloatToString for f64 {
    #[inline(always)]
    unsafe extern "C" fn decimal(self, first: *mut u8) -> *mut u8 {
        double_decimal(self, first)
    }

    #[inline(always)]
    #[cfg(feature = "radix")]
    unsafe extern "C" fn radix(self, radix: u32, first: *mut u8) -> *mut u8 {
        double_radix(self, radix, first)
    }
}

// FTOA

/// Forward the correct arguments the ideal encoder.
#[inline]
unsafe fn forward<F: FloatToString>(value: F, radix: u32, first: *mut u8)
    -> *mut u8
{
    debug_assert_radix!(radix);

    #[cfg(not(feature = "radix"))] {
        value.decimal(first)
    }

    #[cfg(feature = "radix")] {
        match radix {
            10 => value.decimal(first),
            _  => value.radix(radix, first),
        }
    }
}

/// Convert float-to-string and handle special (positive) floats.
#[inline]
unsafe fn filter_special<F: FloatToString>(value: F, radix: u32, first: *mut u8)
    -> *mut u8
{
    // Logic errors, disable in release builds.
    debug_assert!(value.is_sign_positive(), "Value cannot be negative.");
    debug_assert_radix!(radix);

    // We already check for 0 in `filter_sign` if value.is_zero().
    #[cfg(not(feature = "trim_floats"))] {
        if value.is_zero() {
            ptr::copy_nonoverlapping(b"0.0".as_ptr(), first, 3);
            return first.add(3);
        }
    }

    if value.is_nan() {
        ptr::copy_nonoverlapping(NAN_STRING.as_ptr(), first, NAN_STRING.len());
        first.add(NAN_STRING.len())
    } else if value.is_special() {
        // Must be positive infinity, we've already handled sign
        ptr::copy_nonoverlapping(INF_STRING.as_ptr(), first, INF_STRING.len());
        first.add(INF_STRING.len())
    } else {
        forward(value, radix, first)
    }
}

/// Handle +/- values.
#[inline]
unsafe fn filter_sign<F: FloatToString>(mut value: F, radix: u32, mut first: *mut u8)
    -> *mut u8
{
    debug_assert_radix!(radix);

    // Export "-0.0" and "0.0" as "0" with trimmed floats.
    #[cfg(feature = "trim_floats")] {
        if value.is_zero() {
            ptr::copy_nonoverlapping(b"0".as_ptr(), first, 1);
            return first.add(1);
        }
    }

    // If the sign bit is set, invert it and just set the first
    // value to "-".
    if value.is_sign_negative() {
        *first= b'-';
        value = -value;
        first = first.add(1);
    }

    filter_special(value, radix, first)
}

/// Handle insufficient buffer sizes.
#[inline]
unsafe fn filter_buffer<F: FloatToString>(value: F, radix: u32, first: *mut u8, last: *mut u8)
    -> *mut u8
{
    // Logic errors, disable in release builds.
    debug_assert!(first <= last, "First must be <= last");
    debug_assert_radix!(radix);

    // Current buffer has sufficient capacity, use it.
    filter_sign(value, radix, first)
}

// UNSAFE API

/// Generate the unsafe API wrappers.
///
/// * `name`        Function name.
/// * `f`           Float type.
macro_rules! generate_unsafe_api {
    ($name:ident, $t:ty, $size:expr) => (
        /// Unsafe, C-like exporter for float numbers.
        ///
        /// # Warning
        ///
        /// Do not call this function directly, unless you **know**
        /// you have a buffer of sufficient size. No size checking is
        /// done in release mode, this function is **highly** dangerous.
        /// Sufficient buffer sizes is denoted by `BUFFER_SIZE`.
        #[inline]
        unsafe fn $name(value: $t, base: u8, first: *mut u8, last: *mut u8) -> *mut u8
        {
            // check to use a temporary buffer
            assert!(distance(first, last) >= $size);
            let p = filter_buffer(value, base.into(), first, last);

            // Trim a trailing ".0" from a float.
            #[cfg(feature = "trim_floats")] {
                let dist = distance(first, p);
                if ends_with_range(first, dist, ".0".as_ptr(), 2) {
                    p.sub(2)
                } else {
                    p
                }
            }

            #[cfg(not(feature = "trim_floats"))] {
                p
            }
        }
    )
}

generate_unsafe_api!(f32toa_unsafe, f32, MAX_F32_SIZE);
generate_unsafe_api!(f64toa_unsafe, f64, MAX_F64_SIZE);

// LOW-LEVEL API
// -------------

// WRAP UNSAFE LOCAL

generate_to_bytes_local!(f32toa_local, f32, f32toa_unsafe);
generate_to_bytes_local!(f64toa_local, f64, f64toa_unsafe);

// RANGE API (FFI)
generate_to_range_api!(f32toa_range, f32, f32toa_local);
generate_to_range_api!(f64toa_range, f64, f64toa_local);

// SLICE API
generate_to_slice_api!(f32toa_slice, f32, f32toa_local);
generate_to_slice_api!(f64toa_slice, f64, f64toa_local);

// TESTS
// -----

#[cfg(test)]
mod tests {
    use util::test::*;
    use super::*;
    use atof::*;

    // Test data for roundtrips.
    const F32_DATA : [f32; 31] = [0., 0.1, 1., 1.1, 12., 12.1, 123., 123.1, 1234., 1234.1, 12345., 12345.1, 123456., 123456.1, 1234567., 1234567.1, 12345678., 12345678.1, 123456789., 123456789.1, 123456789.12, 123456789.123, 123456789.1234, 123456789.12345, 1.2345678912345e8, 1.2345e+8, 1.2345e+11, 1.2345e+38, 1.2345e-8, 1.2345e-11, 1.2345e-38];
    const F64_DATA: [f64; 33] = [0., 0.1, 1., 1.1, 12., 12.1, 123., 123.1, 1234., 1234.1, 12345., 12345.1, 123456., 123456.1, 1234567., 1234567.1, 12345678., 12345678.1, 123456789., 123456789.1, 123456789.12, 123456789.123, 123456789.1234, 123456789.12345, 1.2345678912345e8, 1.2345e+8, 1.2345e+11, 1.2345e+38, 1.2345e+308, 1.2345e-8, 1.2345e-11, 1.2345e-38, 1.2345e-299];

    #[cfg(feature = "radix")]
    #[test]
    fn f32toa_base2_test() {
        let mut buffer = new_buffer();
        // positive
        #[cfg(feature = "trim_floats")] {
            assert_eq!(as_slice(b"0"), f32toa_slice(0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"0"), f32toa_slice(-0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"1"), f32toa_slice(1.0, 2, &mut buffer));
            assert_eq!(as_slice(b"10"), f32toa_slice(2.0, 2, &mut buffer));
        }

        #[cfg(not(feature = "trim_floats"))] {
            assert_eq!(as_slice(b"0.0"), f32toa_slice(0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"-0.0"), f32toa_slice(-0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"1.0"), f32toa_slice(1.0, 2, &mut buffer));
            assert_eq!(as_slice(b"10.0"), f32toa_slice(2.0, 2, &mut buffer));
        }

        assert_eq!(as_slice(b"1.1"), f32toa_slice(1.5, 2, &mut buffer));
        assert_eq!(as_slice(b"1.01"), f32toa_slice(1.25, 2, &mut buffer));
        assert_eq!(b"1.001111000000110010", &f32toa_slice(1.2345678901234567890e0, 2, &mut buffer)[..20]);
        assert_eq!(b"1100.010110000111111", &f32toa_slice(1.2345678901234567890e1, 2, &mut buffer)[..20]);
        assert_eq!(b"1111011.011101001111", &f32toa_slice(1.2345678901234567890e2, 2, &mut buffer)[..20]);
        assert_eq!(b"10011010010.10010001", &f32toa_slice(1.2345678901234567890e3, 2, &mut buffer)[..20]);

        // negative
        assert_eq!(b"-1.001111000000110010", &f32toa_slice(-1.2345678901234567890e0, 2, &mut buffer)[..21]);
        assert_eq!(b"-1100.010110000111111", &f32toa_slice(-1.2345678901234567890e1, 2, &mut buffer)[..21]);
        assert_eq!(b"-1111011.011101001111", &f32toa_slice(-1.2345678901234567890e2, 2, &mut buffer)[..21]);
        assert_eq!(b"-10011010010.10010001", &f32toa_slice(-1.2345678901234567890e3, 2, &mut buffer)[..21]);

        // special
        assert_eq!(as_slice(b"NaN"), f32toa_slice(f32::NAN, 2, &mut buffer));
        assert_eq!(as_slice(b"inf"), f32toa_slice(f32::INFINITY, 2, &mut buffer));

        // bugfixes
        assert_eq!(as_slice(b"1.101010000010101111000e-11011"), f32toa_slice(0.000000012345, 2, &mut buffer));
    }

    #[test]
    fn f32toa_base10_test() {
        let mut buffer = new_buffer();
        // positive
        #[cfg(feature = "trim_floats")] {
            assert_eq!(as_slice(b"0"), f32toa_slice(0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"0"), f32toa_slice(-0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"1"), f32toa_slice(1.0, 10, &mut buffer));
            assert_eq!(as_slice(b"10"), f32toa_slice(10.0, 10, &mut buffer));
        }

        #[cfg(not(feature = "trim_floats"))] {
            assert_eq!(as_slice(b"0.0"), f32toa_slice(0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"-0.0"), f32toa_slice(-0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"1.0"), f32toa_slice(1.0, 10, &mut buffer));
            assert_eq!(as_slice(b"10.0"), f32toa_slice(10.0, 10, &mut buffer));
        }

        assert_eq!(as_slice(b"1.234567"), &f32toa_slice(1.2345678901234567890e0, 10, &mut buffer)[..8]);
        assert_eq!(as_slice(b"12.34567"), &f32toa_slice(1.2345678901234567890e1, 10, &mut buffer)[..8]);
        assert_eq!(as_slice(b"123.4567"), &f32toa_slice(1.2345678901234567890e2, 10, &mut buffer)[..8]);
        assert_eq!(as_slice(b"1234.567"), &f32toa_slice(1.2345678901234567890e3, 10, &mut buffer)[..8]);

        // negative
        assert_eq!(as_slice(b"-1.234567"), &f32toa_slice(-1.2345678901234567890e0, 10, &mut buffer)[..9]);
        assert_eq!(as_slice(b"-12.34567"), &f32toa_slice(-1.2345678901234567890e1, 10, &mut buffer)[..9]);
        assert_eq!(as_slice(b"-123.4567"), &f32toa_slice(-1.2345678901234567890e2, 10, &mut buffer)[..9]);
        assert_eq!(as_slice(b"-1234.567"), &f32toa_slice(-1.2345678901234567890e3, 10, &mut buffer)[..9]);

        // special
        assert_eq!(as_slice(b"NaN"), f32toa_slice(f32::NAN, 10, &mut buffer));
        assert_eq!(as_slice(b"inf"), f32toa_slice(f32::INFINITY, 10, &mut buffer));
    }

    #[test]
    fn f32toa_base10_roundtrip_test() {
        let mut buffer = new_buffer();
        for f in F32_DATA.iter() {
            let s = f32toa_slice(*f, 10, &mut buffer);
            assert_relative_eq!(atof32_slice(10, s), *f, epsilon=1e-6, max_relative=1e-6);
        }
    }

    #[cfg(feature = "radix")]
    #[test]
    fn f32toa_basen_roundtrip_test() {
        let mut buffer = new_buffer();
        for f in F32_DATA.iter() {
            for radix in 2..37 {
                // The lower accuracy is due to slight rounding errors of
                // ftoa for the Grisu method with non-10 bases.
                let s = f32toa_slice(*f, radix, &mut buffer);
                assert_relative_eq!(atof32_slice(radix, s), *f, max_relative=2e-5);
            }
        }
    }

    #[cfg(feature = "radix")]
    #[test]
    fn f64toa_base2_test() {
        let mut buffer = new_buffer();
        // positive
        #[cfg(feature = "trim_floats")] {
            assert_eq!(as_slice(b"0"), f64toa_slice(0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"0"), f64toa_slice(-0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"1"), f64toa_slice(1.0, 2, &mut buffer));
            assert_eq!(as_slice(b"10"), f64toa_slice(2.0, 2, &mut buffer));
        }

        #[cfg(not(feature = "trim_floats"))] {
            assert_eq!(as_slice(b"0.0"), f64toa_slice(0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"-0.0"), f64toa_slice(-0.0, 2, &mut buffer));
            assert_eq!(as_slice(b"1.0"), f64toa_slice(1.0, 2, &mut buffer));
            assert_eq!(as_slice(b"10.0"), f64toa_slice(2.0, 2, &mut buffer));
        }

        assert_eq!(as_slice(b"1.00111100000011001010010000101000110001"), &f64toa_slice(1.2345678901234567890e0, 2, &mut buffer)[..40]);
        assert_eq!(as_slice(b"1100.01011000011111100110100110010111101"), &f64toa_slice(1.2345678901234567890e1, 2, &mut buffer)[..40]);
        assert_eq!(as_slice(b"1111011.01110100111100000001111111101101"), &f64toa_slice(1.2345678901234567890e2, 2, &mut buffer)[..40]);
        assert_eq!(as_slice(b"10011010010.1001000101100001001111110100"), &f64toa_slice(1.2345678901234567890e3, 2, &mut buffer)[..40]);

        // negative
        assert_eq!(as_slice(b"-1.00111100000011001010010000101000110001"), &f64toa_slice(-1.2345678901234567890e0, 2, &mut buffer)[..41]);
        assert_eq!(as_slice(b"-1100.01011000011111100110100110010111101"), &f64toa_slice(-1.2345678901234567890e1, 2, &mut buffer)[..41]);
        assert_eq!(as_slice(b"-1111011.01110100111100000001111111101101"), &f64toa_slice(-1.2345678901234567890e2, 2, &mut buffer)[..41]);
        assert_eq!(as_slice(b"-10011010010.1001000101100001001111110100"), &f64toa_slice(-1.2345678901234567890e3, 2, &mut buffer)[..41]);

        // special
        assert_eq!(as_slice(b"NaN"), f64toa_slice(f64::NAN, 2, &mut buffer));
        assert_eq!(as_slice(b"inf"), f64toa_slice(f64::INFINITY, 2, &mut buffer));
    }

    #[test]
    fn f64toa_base10_test() {
        let mut buffer = new_buffer();
        // positive
        #[cfg(feature = "trim_floats")] {
            assert_eq!(as_slice(b"0"), f64toa_slice(0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"0"), f64toa_slice(-0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"1"), f64toa_slice(1.0, 10, &mut buffer));
            assert_eq!(as_slice(b"10"), f64toa_slice(10.0, 10, &mut buffer));
        }

        #[cfg(not(feature = "trim_floats"))] {
            assert_eq!(as_slice(b"0.0"), f64toa_slice(0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"-0.0"), f64toa_slice(-0.0, 10, &mut buffer));
            assert_eq!(as_slice(b"1.0"), f64toa_slice(1.0, 10, &mut buffer));
            assert_eq!(as_slice(b"10.0"), f64toa_slice(10.0, 10, &mut buffer));
        }

        assert_eq!(as_slice(b"1.234567"), &f64toa_slice(1.2345678901234567890e0, 10, &mut buffer)[..8]);
        assert_eq!(as_slice(b"12.34567"), &f64toa_slice(1.2345678901234567890e1, 10, &mut buffer)[..8]);
        assert_eq!(as_slice(b"123.4567"), &f64toa_slice(1.2345678901234567890e2, 10, &mut buffer)[..8]);
        assert_eq!(as_slice(b"1234.567"), &f64toa_slice(1.2345678901234567890e3, 10, &mut buffer)[..8]);

        // negative
        assert_eq!(as_slice(b"-1.234567"), &f64toa_slice(-1.2345678901234567890e0, 10, &mut buffer)[..9]);
        assert_eq!(as_slice(b"-12.34567"), &f64toa_slice(-1.2345678901234567890e1, 10, &mut buffer)[..9]);
        assert_eq!(as_slice(b"-123.4567"), &f64toa_slice(-1.2345678901234567890e2, 10, &mut buffer)[..9]);
        assert_eq!(as_slice(b"-1234.567"), &f64toa_slice(-1.2345678901234567890e3, 10, &mut buffer)[..9]);

        // special
        assert_eq!(b"NaN".to_vec(), f64toa_slice(f64::NAN, 10, &mut buffer));
        assert_eq!(b"inf".to_vec(), f64toa_slice(f64::INFINITY, 10, &mut buffer));
    }

    #[test]
    fn f64toa_base10_roundtrip_test() {
        let mut buffer = new_buffer();
        for f in F64_DATA.iter() {
            let s = f64toa_slice(*f, 10, &mut buffer);
            assert_relative_eq!(atof64_slice(10, s), *f, epsilon=1e-12, max_relative=1e-12);
        }
    }

    #[cfg(feature = "radix")]
    #[test]
    fn f64toa_basen_roundtrip_test() {
        let mut buffer = new_buffer();
        for f in F64_DATA.iter() {
            for radix in 2..37 {
                // The lower accuracy is due to slight rounding errors of
                // ftoa for the Grisu method with non-10 bases.
                let s = f64toa_slice(*f, radix, &mut buffer);
                assert_relative_eq!(atof64_slice(radix, s), *f, max_relative=3e-5);
            }
        }
    }

    #[cfg(feature = "correct")]
    quickcheck! {
        fn f32_quickcheck(f: f32) -> bool {
            let mut buffer = new_buffer();
            f == atof32_slice(10, f32toa_slice(f, 10, &mut buffer))
        }

        fn f64_quickcheck(f: f64) -> bool {
            let mut buffer = new_buffer();
            f == atof64_slice(10, f64toa_slice(f, 10, &mut buffer))
        }
    }

    #[cfg(feature = "correct")]
    proptest! {
        #[test]
        fn f332_proptest(i in f32::MIN..f32::MAX) {
            let mut buffer = new_buffer();
            i == atof32_slice(10, f32toa_slice(i, 10, &mut buffer))
        }

        #[test]
        fn f64_proptest(i in f64::MIN..f64::MAX) {
            let mut buffer = new_buffer();
            i == atof64_slice(10, f64toa_slice(i, 10, &mut buffer))
        }
    }

    #[test]
    #[should_panic]
    fn f32toa_buffer_test() {
        let mut buffer = [b'0'; MAX_F32_SIZE-1];
        f64toa_slice(1.2345, 10, &mut buffer);
    }

    #[test]
    #[should_panic]
    fn f64toa_buffer_test() {
        let mut buffer = [b'0'; MAX_F64_SIZE-1];
        f64toa_slice(1.2345, 10, &mut buffer);
    }
}