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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Config settings for lexical-core.

use lib::slice;
use super::algorithm::copy_to_dst;
use super::rounding::RoundingKind;

// HELPERS

/// Determine if the character is a control character for integers or floats.
/// Control characters include digits, `.`, `+`, and `-`.
#[inline]
#[allow(unknown_lints, ellipsis_inclusive_range_patterns)]
fn is_control_character(ch: u8, is_default: bool) -> bool {
    if is_default {
        // Default character handles radixes < 15 (where 'e'/'E' is a
        // a valid exponent character).
        match ch as char {
            '0' ... '9'     => true,
            'a' ... 'd'     => true,
            'A' ... 'D'     => true,
            '.' | '+' | '-' => true,
            _               => false,
        }
    } else {
        // Backup character handles radixes >= 15 (where no digit is
        // a valid exponent character).
        match ch as char {
            '0' ... '9'     => true,
            'a' ... 'z'     => true,
            'A' ... 'Z'     => true,
            '.' | '+' | '-' => true,
            _               => false,
        }
    }
}

// Check if byte array starts with case-insensitive N.
#[inline]
fn starts_with_n(bytes: &[u8]) -> bool {
    match bytes.get(0) {
        Some(&b'N') => true,
        Some(&b'n') => true,
        _           => false,
    }
}

// Check if byte array starts with case-insensitive I.
#[inline]
fn starts_with_i(bytes: &[u8]) -> bool {
    match bytes.get(0) {
        Some(&b'I') => true,
        Some(&b'i') => true,
        _           => false,
    }
}

/// Fixed-size string for float configurations.
///
/// These values are guaranteed less than or equal to the maximum
/// number of bytes after a sign byte has been written.
pub(crate) struct FloatConfigString {
    /// Storage data for the config string.
    data: [u8; F32_FORMATTED_SIZE - 1],
    /// Actual length of the data.
    length: usize,
}

impl FloatConfigString {
    /// Reset data from a byte string.
    pub(crate) fn load_bytes(&mut self, bytes: &[u8]) {
        assert!(bytes.len() <= self.data.len());
        copy_to_dst(&mut self.data, bytes);
        self.length = bytes.len();
    }

    /// Convert to byte slice.
    pub(crate) fn as_bytes(&self) -> &[u8] {
        // Always safe, since length can only be set from `load_bytes`.
        unsafe {
            self.data.get_unchecked(..self.length)
        }
    }
}

// GLOBALS

/// Default character for scientific notation, used when the `radix < 15`.
static mut EXPONENT_DEFAULT_CHAR: u8 = b'e';

/// Backup character for scientific notation, used when the `radix >= 15`.
#[cfg(feature ="radix")]
static mut EXPONENT_BACKUP_CHAR: u8 = b'^';

/// The rounding scheme for float conversions.
#[cfg(feature = "rounding")]
static mut FLOAT_ROUNDING: RoundingKind = RoundingKind::NearestTieEven;

cfg_if! {
if #[cfg(feature = "radix")] {
    /// Not a Number literal.
    static mut NAN_STRING: FloatConfigString = FloatConfigString {
        // b"NaN"
        data: [b'N', b'a', b'N', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
        length: 3
    };

    /// Short infinity literal.
    static mut INF_STRING: FloatConfigString = FloatConfigString {
        // b"inf"
        data: [b'i', b'n', b'f', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
        length: 3
    };

    /// Long infinity literal.
    static mut INFINITY_STRING: FloatConfigString = FloatConfigString {
        // b"infinity"
        data: [b'i', b'n', b'f', b'i', b'n', b'i', b't', b'y', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
        length: 8
    };
} else {
    /// Not a Number literal.
    static mut NAN_STRING: FloatConfigString = FloatConfigString {
        // b"NaN"
        data: [b'N', b'a', b'N', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
        length: 3
    };

    /// Short infinity literal.
    static mut INF_STRING: FloatConfigString = FloatConfigString {
        // b"inf"
        data: [b'i', b'n', b'f', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
        length: 3
    };

    /// Long infinity literal.
    static mut INFINITY_STRING: FloatConfigString = FloatConfigString {
        // b"infinity"
        data: [b'i', b'n', b'f', b'i', b'n', b'i', b't', b'y', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
        length: 8
    };
}}   // cfg_if

// GETTERS/SETTERS

/// Get default character for the exponent symbol.
///
/// Default character for scientific notation, used when the `radix < 15`.
#[inline]
pub fn get_exponent_default_char() -> u8
{
    unsafe {
        EXPONENT_DEFAULT_CHAR
    }
}

/// Set the default character for the exponent symbol.
///
/// Default character for scientific notation, used when the `radix < 15`.
///
/// To change the expected, default character for an exponent,
/// change this value before using lexical.
///
/// * `ch`      - Character for exponent symbol.
///
/// # Safety
///
/// Do not call this function in threaded-code, as it is not thread-safe.
/// Do not call this function after compiling any formats.
///
/// # Panics
///
/// Panics if the character is in the character set `[A-Da-d.+\-]`.
#[inline]
pub unsafe fn set_exponent_default_char(ch: u8)
{
    assert!(!is_control_character(ch, true));
    EXPONENT_DEFAULT_CHAR = ch
}

/// Get backup character for the exponent symbol.
///
/// For numerical strings of `radix >= 15`, 'e' or 'E' is a valid digit,
/// and therefore may no longer be used as a marker for the exponent.
#[inline]
#[cfg(feature ="radix")]
pub fn get_exponent_backup_char() -> u8
{
    unsafe {
        EXPONENT_BACKUP_CHAR
    }
}

/// Set the backup character for the exponent symbol.
///
/// For numerical strings of `radix >= 15`, 'e' or 'E' is a valid digit,
/// and therefore may no longer be used as a marker for the exponent.
///
/// To change the expected, backup character for an exponent,
/// change this value before using lexical.
///
/// * `ch`      - Character for exponent symbol.
///
/// # Safety
///
/// Do not call this function in threaded-code, as it is not thread-safe.
/// Do not call this function after compiling any formats.
///
/// # Panics
///
/// Panics if the character is in the character set `[A-Za-z.+\-]`.
#[inline]
#[cfg(feature ="radix")]
pub unsafe fn set_exponent_backup_char(ch: u8)
{
    assert!(!is_control_character(ch, false));
    EXPONENT_BACKUP_CHAR = ch
}

/// Get the default rounding scheme for float conversions.
///
/// This defines the global rounding-scheme for float parsing operations.
/// By default, this is set to `RoundingKind::NearestTieEven`. IEEE754
/// recommends this as the default for all for decimal and binary
/// operations.
#[inline]
#[cfg(feature = "rounding")]
pub fn get_float_rounding() -> RoundingKind {
    unsafe {
        FLOAT_ROUNDING
    }
}

/// Set the default rounding scheme for float conversions.
///
/// This defines the global rounding-scheme for float parsing operations.
/// By default, this is set to `RoundingKind::NearestTieEven`. IEEE754
/// recommends this as the default for all for decimal and binary
/// operations.
///
/// # Safety
///
/// Do not modify this value in threaded-code, as it is not thread-safe.
#[inline]
#[cfg(feature = "rounding")]
pub unsafe fn set_float_rounding(rounding: RoundingKind) {
    FLOAT_ROUNDING = rounding
}

/// Get string representation of Not a Number as a byte slice.
#[inline]
pub fn get_nan_string() -> &'static [u8]
{
    unsafe {
        NAN_STRING.as_bytes()
    }
}

/// Set representation of Not a Number from a byte slice.
///
/// * `bytes`    - Slice of bytes to assign as NaN string representation.
///
/// # Safety
///
/// Do not call this function in threaded-code, as it is not thread-safe.
///
/// # Panics
///
/// Panics if:
/// - `bytes.len() >= f32::FORMATTED_SIZE`
/// - `bytes` is empty
/// - `bytes` does not start with an `'N'` or `'n'`.
#[inline]
pub unsafe fn set_nan_string(bytes: &[u8])
{
    assert!(starts_with_n(bytes));
    NAN_STRING.load_bytes(bytes);
}

/// Get the short representation of an Infinity literal as a byte slice.
#[inline]
pub fn get_inf_string() -> &'static [u8]
{
    unsafe {
        INF_STRING.as_bytes()
    }
}

/// Set the short representation of Infinity from a byte slice.
///
/// * `bytes`    - Slice of bytes to assign as Infinity string representation.
///
/// # Safety
///
/// Do not call this function in threaded-code, as it is not thread-safe.
///
/// # Panics
///
/// Panics if:
/// - `bytes.len() >= f32::FORMATTED_SIZE`
/// - `bytes.len() >= get_infinity_string().len()`
/// - `bytes` is empty
/// - `bytes` does not start with an `'I'` or `'i'`.
#[inline]
pub unsafe fn set_inf_string(bytes: &[u8])
{
    assert!(starts_with_i(bytes) && bytes.len() <= INFINITY_STRING.length);
    INF_STRING.load_bytes(bytes);
}

/// Get the long representation of an Infinity literal as a byte slice.
#[inline]
pub fn get_infinity_string() -> &'static [u8]
{
    unsafe {
        INFINITY_STRING.as_bytes()
    }
}

/// Set the long representation of Infinity from a byte slice.
///
/// * `bytes`    - Slice of bytes to assign as Infinity string representation.
///
/// # Safety
///
/// Do not call this function in threaded-code, as it is not thread-safe.
///
/// # Panics
///
/// Panics if:
/// - `bytes.len() >= f32::FORMATTED_SIZE`
/// - `bytes.len() < get_inf_string().len()`
/// - `bytes` is empty
/// - `bytes` does not start with an `'I'` or `'i'`.
#[inline]
pub unsafe fn set_infinity_string(bytes: &[u8])
{
    assert!(starts_with_i(bytes) && bytes.len() >= INF_STRING.length);
    INFINITY_STRING.load_bytes(bytes);
}

// CONSTANTS

// The f64 buffer is actually a size of 60, but use 64 since it's a
// power of 2.
pub(crate) const I8_FORMATTED_SIZE_DECIMAL: usize = 4;
pub(crate) const I16_FORMATTED_SIZE_DECIMAL: usize = 6;
pub(crate) const I32_FORMATTED_SIZE_DECIMAL: usize = 11;
pub(crate) const I64_FORMATTED_SIZE_DECIMAL: usize = 20;
pub(crate) const U8_FORMATTED_SIZE_DECIMAL: usize = 3;
pub(crate) const U16_FORMATTED_SIZE_DECIMAL: usize = 5;
pub(crate) const U32_FORMATTED_SIZE_DECIMAL: usize = 10;
pub(crate) const U64_FORMATTED_SIZE_DECIMAL: usize = 20;
pub(crate) const F32_FORMATTED_SIZE_DECIMAL: usize = 64;
pub(crate) const F64_FORMATTED_SIZE_DECIMAL: usize = 64;
#[cfg(has_i128)] pub(crate) const I128_FORMATTED_SIZE_DECIMAL: usize = 40;
#[cfg(has_i128)] pub(crate) const U128_FORMATTED_SIZE_DECIMAL: usize = 39;

// Simple, fast optimization.
// Since we're declaring a variable on the stack, and our power-of-two
// alignment dramatically improved atoi performance, do it.
cfg_if! {
if #[cfg(feature = "radix")] {
    // Use 256, actually, since we seem to have memory issues with f64.
    // Clearly not sufficient memory allocated for non-decimal values.
    pub(crate) const I8_FORMATTED_SIZE: usize = 16;
    pub(crate) const I16_FORMATTED_SIZE: usize = 32;
    pub(crate) const I32_FORMATTED_SIZE: usize = 64;
    pub(crate) const I64_FORMATTED_SIZE: usize = 128;
    pub(crate) const U8_FORMATTED_SIZE: usize = 16;
    pub(crate) const U16_FORMATTED_SIZE: usize = 32;
    pub(crate) const U32_FORMATTED_SIZE: usize = 64;
    pub(crate) const U64_FORMATTED_SIZE: usize = 128;
    pub(crate) const F32_FORMATTED_SIZE: usize = 256;
    pub(crate) const F64_FORMATTED_SIZE: usize = 256;
    #[cfg(has_i128)] pub(crate) const I128_FORMATTED_SIZE: usize = 256;
    #[cfg(has_i128)] pub(crate) const U128_FORMATTED_SIZE: usize = 256;
} else {
    // The f64 buffer is actually a size of 60, but use 64 since it's a
    // power of 2.
    pub(crate) const I8_FORMATTED_SIZE: usize = I8_FORMATTED_SIZE_DECIMAL;
    pub(crate) const I16_FORMATTED_SIZE: usize = I16_FORMATTED_SIZE_DECIMAL;
    pub(crate) const I32_FORMATTED_SIZE: usize = I32_FORMATTED_SIZE_DECIMAL;
    pub(crate) const I64_FORMATTED_SIZE: usize = I64_FORMATTED_SIZE_DECIMAL;
    pub(crate) const U8_FORMATTED_SIZE: usize = U8_FORMATTED_SIZE_DECIMAL;
    pub(crate) const U16_FORMATTED_SIZE: usize = U16_FORMATTED_SIZE_DECIMAL;
    pub(crate) const U32_FORMATTED_SIZE: usize = U32_FORMATTED_SIZE_DECIMAL;
    pub(crate) const U64_FORMATTED_SIZE: usize = U64_FORMATTED_SIZE_DECIMAL;
    pub(crate) const F32_FORMATTED_SIZE: usize = F32_FORMATTED_SIZE_DECIMAL;
    pub(crate) const F64_FORMATTED_SIZE: usize = F64_FORMATTED_SIZE_DECIMAL;
    #[cfg(has_i128)] pub(crate) const I128_FORMATTED_SIZE: usize = I128_FORMATTED_SIZE_DECIMAL;
    #[cfg(has_i128)] pub(crate) const U128_FORMATTED_SIZE: usize = U128_FORMATTED_SIZE_DECIMAL;
}} // cfg_if

cfg_if! {
if #[cfg(target_pointer_width = "16")] {
    pub(crate) const ISIZE_FORMATTED_SIZE: usize = I16_FORMATTED_SIZE;
    pub(crate) const ISIZE_FORMATTED_SIZE_DECIMAL: usize = I16_FORMATTED_SIZE_DECIMAL;
    pub(crate) const USIZE_FORMATTED_SIZE: usize = U16_FORMATTED_SIZE;
    pub(crate) const USIZE_FORMATTED_SIZE_DECIMAL: usize = U16_FORMATTED_SIZE_DECIMAL;
} else if #[cfg(target_pointer_width = "32")] {
    pub(crate) const ISIZE_FORMATTED_SIZE: usize = I32_FORMATTED_SIZE;
    pub(crate) const ISIZE_FORMATTED_SIZE_DECIMAL: usize = I32_FORMATTED_SIZE_DECIMAL;
    pub(crate) const USIZE_FORMATTED_SIZE: usize = U32_FORMATTED_SIZE;
    pub(crate) const USIZE_FORMATTED_SIZE_DECIMAL: usize = U32_FORMATTED_SIZE_DECIMAL;
} else if #[cfg(target_pointer_width = "64")] {
    pub(crate) const ISIZE_FORMATTED_SIZE: usize = I64_FORMATTED_SIZE;
    pub(crate) const ISIZE_FORMATTED_SIZE_DECIMAL: usize = I64_FORMATTED_SIZE_DECIMAL;
    pub(crate) const USIZE_FORMATTED_SIZE: usize = U64_FORMATTED_SIZE;
    pub(crate) const USIZE_FORMATTED_SIZE_DECIMAL: usize = U64_FORMATTED_SIZE_DECIMAL;
}}  // cfg_if

/// Maximum number of bytes required to serialize any number to string.
pub const BUFFER_SIZE: usize = F64_FORMATTED_SIZE;

// FUNCTIONS

/// Get the exponent notation character.
#[inline]
#[allow(unused_variables)]
pub(crate) fn exponent_notation_char(radix: u32) -> u8 {
    #[cfg(not(feature ="radix"))] {
        get_exponent_default_char()
    }

    #[cfg(feature ="radix")] {
        if radix >= 15 {
            get_exponent_backup_char()
        } else {
            get_exponent_default_char()
        }
    }
}

// TEST
// ----

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

    #[cfg(feature ="radix")]
    #[test]
    fn exponent_notation_char_test() {
        let default = get_exponent_default_char();
        let backup = get_exponent_backup_char();
        assert_eq!(exponent_notation_char(2), default);
        assert_eq!(exponent_notation_char(8), default);
        assert_eq!(exponent_notation_char(10), default);
        assert_eq!(exponent_notation_char(15), backup);
        assert_eq!(exponent_notation_char(16), backup);
        assert_eq!(exponent_notation_char(32), backup);
    }

    // Only enable when no other threads touch NAN_STRING or INFINITY_STRING.
    #[test]
    #[ignore]
    fn special_bytes_test() {
        unsafe {
            let mut buffer = new_buffer();
            // Test serializing and deserializing special strings.
            assert!(f32::from_lexical(b"NaN").unwrap().is_nan());
            assert!(f32::from_lexical(b"nan").unwrap().is_nan());
            assert!(f32::from_lexical(b"NAN").unwrap().is_nan());
            assert!(f32::from_lexical(b"inf").unwrap().is_infinite());
            assert!(f32::from_lexical(b"INF").unwrap().is_infinite());
            assert!(f32::from_lexical(b"Infinity").unwrap().is_infinite());
            assert_eq!(f64::NAN.to_lexical(&mut buffer), b"NaN");
            assert_eq!(f64::INFINITY.to_lexical(&mut buffer), b"inf");

            set_nan_string(b"nan");
            set_inf_string(b"Infinity");

            assert!(f32::from_lexical(b"inf").err().unwrap().code == ErrorCode::InvalidDigit);
            assert!(f32::from_lexical(b"Infinity").unwrap().is_infinite());
            assert_eq!(f64::NAN.to_lexical(&mut buffer), b"nan");
            assert_eq!(f64::INFINITY.to_lexical(&mut buffer), b"Infinity");

            set_nan_string(b"NaN");
            set_inf_string(b"inf");
        }
    }

    // Only enable when no other threads touch FLOAT_ROUNDING.
    #[cfg(all(feature = "correct", feature = "rounding"))]
    #[test]
    #[ignore]
    fn special_rounding_test() {
        // Each one of these pairs is halfway, and we can detect the
        // rounding schemes from this.
        unsafe {
            // Nearest, tie-even
            set_float_rounding(RoundingKind::NearestTieEven);
            assert_eq!(f64::from_lexical(b"-9007199254740993").unwrap(), -9007199254740992.0);
            assert_eq!(f64::from_lexical(b"-9007199254740995").unwrap(), -9007199254740996.0);
            assert_eq!(f64::from_lexical(b"9007199254740993").unwrap(), 9007199254740992.0);
            assert_eq!(f64::from_lexical(b"9007199254740995").unwrap(), 9007199254740996.0);

            // Nearest, tie-away-zero
            set_float_rounding(RoundingKind::NearestTieAwayZero);
            assert_eq!(f64::from_lexical(b"-9007199254740993").unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical(b"-9007199254740995").unwrap(), -9007199254740996.0);
            assert_eq!(f64::from_lexical(b"9007199254740993").unwrap(), 9007199254740994.0);
            assert_eq!(f64::from_lexical(b"9007199254740995").unwrap(), 9007199254740996.0);

            // Toward positive infinity
            set_float_rounding(RoundingKind::TowardPositiveInfinity);
            assert_eq!(f64::from_lexical(b"-9007199254740993").unwrap(), -9007199254740992.0);
            assert_eq!(f64::from_lexical(b"-9007199254740995").unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical(b"9007199254740993").unwrap(), 9007199254740994.0);
            assert_eq!(f64::from_lexical(b"9007199254740995").unwrap(), 9007199254740996.0);

            // Toward negative infinity
            set_float_rounding(RoundingKind::TowardNegativeInfinity);
            assert_eq!(f64::from_lexical(b"-9007199254740993").unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical(b"-9007199254740995").unwrap(), -9007199254740996.0);
            assert_eq!(f64::from_lexical(b"9007199254740993").unwrap(), 9007199254740992.0);
            assert_eq!(f64::from_lexical(b"9007199254740995").unwrap(), 9007199254740994.0);

            // Toward zero
            set_float_rounding(RoundingKind::TowardZero);
            assert_eq!(f64::from_lexical(b"-9007199254740993").unwrap(), -9007199254740992.0);
            assert_eq!(f64::from_lexical(b"-9007199254740995").unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical(b"9007199254740993").unwrap(), 9007199254740992.0);
            assert_eq!(f64::from_lexical(b"9007199254740995").unwrap(), 9007199254740994.0);

            // Reset to default
            set_float_rounding(RoundingKind::NearestTieEven);
        }
    }

    // Only enable when no other threads touch FLOAT_ROUNDING.
    #[cfg(all(feature = "correct", feature = "radix", feature = "rounding"))]
    #[test]
    #[ignore]
    fn special_rounding_binary_test() {
        // Each one of these pairs is halfway, and we can detect the
        // rounding schemes from this.
        unsafe {
            // Nearest, tie-even
            set_float_rounding(RoundingKind::NearestTieEven);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000001", 2).unwrap(), -9007199254740992.0);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000011", 2).unwrap(), -9007199254740996.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000001", 2).unwrap(), 9007199254740992.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000011", 2).unwrap(), 9007199254740996.0);

            // Nearest, tie-away-zero
            set_float_rounding(RoundingKind::NearestTieAwayZero);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000001", 2).unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000011", 2).unwrap(), -9007199254740996.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000001", 2).unwrap(), 9007199254740994.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000011", 2).unwrap(), 9007199254740996.0);

            // Toward positive infinity
            set_float_rounding(RoundingKind::TowardPositiveInfinity);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000001", 2).unwrap(), -9007199254740992.0);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000011", 2).unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000001", 2).unwrap(), 9007199254740994.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000011", 2).unwrap(), 9007199254740996.0);

            // Toward negative infinity
            set_float_rounding(RoundingKind::TowardNegativeInfinity);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000001", 2).unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000011", 2).unwrap(), -9007199254740996.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000001", 2).unwrap(), 9007199254740992.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000011", 2).unwrap(), 9007199254740994.0);

            // Toward zero
            set_float_rounding(RoundingKind::TowardZero);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000001", 2).unwrap(), -9007199254740992.0);
            assert_eq!(f64::from_lexical_radix(b"-100000000000000000000000000000000000000000000000000011", 2).unwrap(), -9007199254740994.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000001", 2).unwrap(), 9007199254740992.0);
            assert_eq!(f64::from_lexical_radix(b"100000000000000000000000000000000000000000000000000011", 2).unwrap(), 9007199254740994.0);

            // Reset to default
            set_float_rounding(RoundingKind::NearestTieEven);
        }
    }

    #[should_panic]
    #[test]
    fn set_exponent_default_char_digit_test() {
        unsafe {
            set_exponent_default_char(b'0')
        }
    }

    #[should_panic]
    #[test]
    fn set_exponent_default_char_period_test() {
        unsafe {
            set_exponent_default_char(b'.')
        }
    }

    #[should_panic]
    #[test]
    fn set_exponent_default_char_add_test() {
        unsafe {
            set_exponent_default_char(b'+')
        }
    }

    #[should_panic]
    #[test]
    fn set_exponent_default_char_sub_test() {
        unsafe {
            set_exponent_default_char(b'-')
        }
    }

    #[cfg(all(feature = "radix"))]
    #[should_panic]
    #[test]
    fn set_exponent_backup_char_digit_test() {
        unsafe {
            set_exponent_backup_char(b'0')
        }
    }

    #[cfg(all(feature = "radix"))]
    #[should_panic]
    #[test]
    fn set_exponent_backup_char_period_test() {
        unsafe {
            set_exponent_backup_char(b'.')
        }
    }

    #[cfg(all(feature = "radix"))]
    #[should_panic]
    #[test]
    fn set_exponent_backup_char_add_test() {
        unsafe {
            set_exponent_backup_char(b'+')
        }
    }

    #[cfg(all(feature = "radix"))]
    #[should_panic]
    #[test]
    fn set_exponent_backup_char_sub_test() {
        unsafe {
            set_exponent_backup_char(b'-')
        }
    }

    #[should_panic]
    #[test]
    fn set_nan_string_empty_test() {
        unsafe {
            set_nan_string(b"")
        }
    }

    #[should_panic]
    #[test]
    fn set_nan_string_invalid_test() {
        unsafe {
            set_nan_string(b"i")
        }
    }

    #[should_panic]
    #[test]
    fn set_inf_string_empty_test() {
        unsafe {
            set_inf_string(b"")
        }
    }

    #[should_panic]
    #[test]
    fn set_inf_string_invalid_test() {
        unsafe {
            set_inf_string(b"n")
        }
    }

    #[should_panic]
    #[test]
    fn set_inf_string_long_test() {
        unsafe {
            set_inf_string(b"infinityinfinf")
        }
    }

    #[should_panic]
    #[test]
    fn set_infinity_string_empty_test() {
        unsafe {
            set_infinity_string(b"")
        }
    }

    #[should_panic]
    #[test]
    fn set_infinity_string_invalid_test() {
        unsafe {
            set_infinity_string(b"n")
        }
    }

    #[should_panic]
    #[test]
    fn set_infinity_string_short_test() {
        unsafe {
            set_infinity_string(b"i")
        }
    }
}