2013-02-14 20:29:36 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-05-12 19:34:15 -05:00
|
|
|
use container::Container;
|
2013-02-14 20:29:36 -06:00
|
|
|
use core::cmp::{Ord, Eq};
|
2013-05-01 00:40:05 -05:00
|
|
|
use ops::{Add, Sub, Mul, Div, Rem, Neg};
|
2013-02-14 20:29:36 -06:00
|
|
|
use option::{None, Option, Some};
|
|
|
|
use char;
|
|
|
|
use str;
|
2013-06-28 16:05:10 -05:00
|
|
|
use str::StrSlice;
|
2013-02-14 20:29:36 -06:00
|
|
|
use kinds::Copy;
|
2013-06-28 11:54:03 -05:00
|
|
|
use vec::{CopyableVector, ImmutableVector, MutableVector};
|
2013-05-12 19:34:15 -05:00
|
|
|
use vec::OwnedVector;
|
2013-06-28 16:05:10 -05:00
|
|
|
use num::{NumCast, Zero, One, cast, pow_with_uint, Integer};
|
|
|
|
use num::{Round, Float, FPNaN, FPInfinite};
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
pub enum ExponentFormat {
|
|
|
|
ExpNone,
|
|
|
|
ExpDec,
|
|
|
|
ExpBin
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SignificantDigits {
|
|
|
|
DigAll,
|
|
|
|
DigMax(uint),
|
|
|
|
DigExact(uint)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SignFormat {
|
|
|
|
SignNone,
|
|
|
|
SignNeg,
|
|
|
|
SignAll
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:39:51 -06:00
|
|
|
pub trait NumStrConv {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn NaN() -> Option<Self>;
|
|
|
|
fn inf() -> Option<Self>;
|
|
|
|
fn neg_inf() -> Option<Self>;
|
|
|
|
fn neg_zero() -> Option<Self>;
|
2013-02-14 21:40:32 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn round_to_zero(&self) -> Self;
|
|
|
|
fn fractional_part(&self) -> Self;
|
2013-02-14 21:40:32 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 22:40:01 -06:00
|
|
|
macro_rules! impl_NumStrConv_Floating (($t:ty) => (
|
|
|
|
impl NumStrConv for $t {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn NaN() -> Option<$t> { Some( 0.0 / 0.0) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn inf() -> Option<$t> { Some( 1.0 / 0.0) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn neg_inf() -> Option<$t> { Some(-1.0 / 0.0) }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn neg_zero() -> Option<$t> { Some(-0.0 ) }
|
2013-02-14 22:40:01 -06:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-28 16:05:10 -05:00
|
|
|
fn round_to_zero(&self) -> $t { self.trunc() }
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-28 16:05:10 -05:00
|
|
|
fn fractional_part(&self) -> $t { self.fract() }
|
2013-02-14 22:40:01 -06:00
|
|
|
}
|
|
|
|
))
|
|
|
|
|
|
|
|
macro_rules! impl_NumStrConv_Integer (($t:ty) => (
|
|
|
|
impl NumStrConv for $t {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline] fn NaN() -> Option<$t> { None }
|
|
|
|
#[inline] fn inf() -> Option<$t> { None }
|
|
|
|
#[inline] fn neg_inf() -> Option<$t> { None }
|
|
|
|
#[inline] fn neg_zero() -> Option<$t> { None }
|
2013-02-14 22:40:01 -06:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline] fn round_to_zero(&self) -> $t { *self }
|
|
|
|
#[inline] fn fractional_part(&self) -> $t { 0 }
|
2013-02-14 22:40:01 -06:00
|
|
|
}
|
|
|
|
))
|
2013-02-14 21:40:32 -06:00
|
|
|
|
2013-02-14 22:40:01 -06:00
|
|
|
// FIXME: #4955
|
|
|
|
// Replace by two generic impls for traits 'Integral' and 'Floating'
|
2013-02-14 21:40:32 -06:00
|
|
|
impl_NumStrConv_Floating!(float)
|
|
|
|
impl_NumStrConv_Floating!(f32)
|
|
|
|
impl_NumStrConv_Floating!(f64)
|
|
|
|
|
|
|
|
impl_NumStrConv_Integer!(int)
|
|
|
|
impl_NumStrConv_Integer!(i8)
|
|
|
|
impl_NumStrConv_Integer!(i16)
|
|
|
|
impl_NumStrConv_Integer!(i32)
|
|
|
|
impl_NumStrConv_Integer!(i64)
|
|
|
|
|
|
|
|
impl_NumStrConv_Integer!(uint)
|
|
|
|
impl_NumStrConv_Integer!(u8)
|
|
|
|
impl_NumStrConv_Integer!(u16)
|
|
|
|
impl_NumStrConv_Integer!(u32)
|
|
|
|
impl_NumStrConv_Integer!(u64)
|
|
|
|
|
2013-03-21 20:04:13 -05:00
|
|
|
|
|
|
|
// Special value strings as [u8] consts.
|
2013-03-22 20:52:04 -05:00
|
|
|
static inf_buf: [u8, ..3] = ['i' as u8, 'n' as u8, 'f' as u8];
|
|
|
|
static positive_inf_buf: [u8, ..4] = ['+' as u8, 'i' as u8, 'n' as u8,
|
|
|
|
'f' as u8];
|
|
|
|
static negative_inf_buf: [u8, ..4] = ['-' as u8, 'i' as u8, 'n' as u8,
|
|
|
|
'f' as u8];
|
|
|
|
static nan_buf: [u8, ..3] = ['N' as u8, 'a' as u8, 'N' as u8];
|
2013-03-21 20:04:13 -05:00
|
|
|
|
2013-06-28 16:05:10 -05:00
|
|
|
/**
|
|
|
|
* Converts an integral number to its string representation as a byte vector.
|
|
|
|
* This is meant to be a common base implementation for all integral string
|
|
|
|
* conversion functions like `to_str()` or `to_str_radix()`.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
* - `num` - The number to convert. Accepts any number that
|
|
|
|
* implements the numeric traits.
|
|
|
|
* - `radix` - Base to use. Accepts only the values 2-36.
|
|
|
|
* - `sign` - How to emit the sign. Options are:
|
|
|
|
* - `SignNone`: No sign at all. Basically emits `abs(num)`.
|
|
|
|
* - `SignNeg`: Only `-` on negative values.
|
|
|
|
* - `SignAll`: Both `+` on positive, and `-` on negative numbers.
|
|
|
|
* - `f` - a callback which will be invoked for each ascii character
|
|
|
|
* which composes the string representation of this integer
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
* A tuple containing the byte vector, and a boolean flag indicating
|
|
|
|
* whether it represents a special value like `inf`, `-inf`, `NaN` or not.
|
|
|
|
* It returns a tuple because there can be ambiguity between a special value
|
|
|
|
* and a number representation at higher bases.
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
* - Fails if `radix` < 2 or `radix` > 36.
|
|
|
|
*/
|
|
|
|
pub fn int_to_str_bytes_common<T:NumCast+Zero+Eq+Ord+Integer+
|
|
|
|
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
|
|
|
|
num: T, radix: uint, sign: SignFormat, f: &fn(u8)) {
|
|
|
|
assert!(2 <= radix && radix <= 36);
|
|
|
|
|
|
|
|
let _0: T = Zero::zero();
|
|
|
|
|
|
|
|
let neg = num < _0;
|
|
|
|
let radix_gen: T = cast(radix);
|
|
|
|
|
|
|
|
let mut deccum = num;
|
|
|
|
// This is just for integral types, the largest of which is a u64. The
|
|
|
|
// smallest base that we can have is 2, so the most number of digits we're
|
|
|
|
// ever going to have is 64
|
|
|
|
let mut buf = [0u8, ..64];
|
|
|
|
let mut cur = 0;
|
|
|
|
|
|
|
|
// Loop at least once to make sure at least a `0` gets emitted.
|
|
|
|
loop {
|
|
|
|
// Calculate the absolute value of each digit instead of only
|
|
|
|
// doing it once for the whole number because a
|
|
|
|
// representable negative number doesn't necessary have an
|
|
|
|
// representable additive inverse of the same type
|
|
|
|
// (See twos complement). But we assume that for the
|
|
|
|
// numbers [-35 .. 0] we always have [0 .. 35].
|
|
|
|
let current_digit_signed = deccum % radix_gen;
|
|
|
|
let current_digit = if current_digit_signed < _0 {
|
|
|
|
-current_digit_signed
|
|
|
|
} else {
|
|
|
|
current_digit_signed
|
|
|
|
};
|
|
|
|
buf[cur] = match current_digit.to_u8() {
|
|
|
|
i @ 0..9 => '0' as u8 + i,
|
|
|
|
i => 'a' as u8 + (i - 10),
|
|
|
|
};
|
|
|
|
cur += 1;
|
|
|
|
|
|
|
|
deccum = deccum / radix_gen;
|
|
|
|
// No more digits to calculate for the non-fractional part -> break
|
|
|
|
if deccum == _0 { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decide what sign to put in front
|
|
|
|
match sign {
|
|
|
|
SignNeg | SignAll if neg => { f('-' as u8); }
|
|
|
|
SignAll => { f('+' as u8); }
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
// We built the number in reverse order, so un-reverse it here
|
|
|
|
while cur > 0 {
|
|
|
|
cur -= 1;
|
|
|
|
f(buf[cur]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 20:29:36 -06:00
|
|
|
/**
|
|
|
|
* Converts a number to its string representation as a byte vector.
|
|
|
|
* This is meant to be a common base implementation for all numeric string
|
|
|
|
* conversion functions like `to_str()` or `to_str_radix()`.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
* - `num` - The number to convert. Accepts any number that
|
|
|
|
* implements the numeric traits.
|
|
|
|
* - `radix` - Base to use. Accepts only the values 2-36.
|
|
|
|
* - `negative_zero` - Whether to treat the special value `-0` as
|
|
|
|
* `-0` or as `+0`.
|
|
|
|
* - `sign` - How to emit the sign. Options are:
|
|
|
|
* - `SignNone`: No sign at all. Basically emits `abs(num)`.
|
|
|
|
* - `SignNeg`: Only `-` on negative values.
|
|
|
|
* - `SignAll`: Both `+` on positive, and `-` on negative numbers.
|
|
|
|
* - `digits` - The amount of digits to use for emitting the
|
|
|
|
* fractional part, if any. Options are:
|
|
|
|
* - `DigAll`: All calculatable digits. Beware of bignums or
|
|
|
|
* fractions!
|
|
|
|
* - `DigMax(uint)`: Maximum N digits, truncating any trailing zeros.
|
|
|
|
* - `DigExact(uint)`: Exactly N digits.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
* A tuple containing the byte vector, and a boolean flag indicating
|
|
|
|
* whether it represents a special value like `inf`, `-inf`, `NaN` or not.
|
|
|
|
* It returns a tuple because there can be ambiguity between a special value
|
|
|
|
* and a number representation at higher bases.
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
* - Fails if `radix` < 2 or `radix` > 36.
|
|
|
|
*/
|
2013-06-28 16:05:10 -05:00
|
|
|
pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
|
2013-05-01 00:40:05 -05:00
|
|
|
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
|
2013-06-28 16:05:10 -05:00
|
|
|
num: T, radix: uint, negative_zero: bool,
|
2013-02-14 20:29:36 -06:00
|
|
|
sign: SignFormat, digits: SignificantDigits) -> (~[u8], bool) {
|
2013-06-28 16:05:10 -05:00
|
|
|
assert!(2 <= radix && radix <= 36);
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
let _0: T = Zero::zero();
|
|
|
|
let _1: T = One::one();
|
|
|
|
|
2013-06-28 16:05:10 -05:00
|
|
|
match num.classify() {
|
|
|
|
FPNaN => { return ("NaN".as_bytes().to_owned(), true); }
|
|
|
|
FPInfinite if num > _0 => {
|
|
|
|
return match sign {
|
|
|
|
SignAll => ("+inf".as_bytes().to_owned(), true),
|
|
|
|
_ => ("inf".as_bytes().to_owned(), true)
|
|
|
|
};
|
2013-02-14 22:20:36 -06:00
|
|
|
}
|
2013-06-28 16:05:10 -05:00
|
|
|
FPInfinite if num < _0 => {
|
|
|
|
return match sign {
|
|
|
|
SignNone => ("inf".as_bytes().to_owned(), true),
|
|
|
|
_ => ("-inf".as_bytes().to_owned(), true),
|
|
|
|
};
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
2013-06-28 16:05:10 -05:00
|
|
|
_ => {}
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
|
2013-06-28 16:05:10 -05:00
|
|
|
let neg = num < _0 || (negative_zero && _1 / num == Float::neg_infinity());
|
2013-02-14 20:29:36 -06:00
|
|
|
let mut buf: ~[u8] = ~[];
|
|
|
|
let radix_gen: T = cast(radix as int);
|
|
|
|
|
|
|
|
// First emit the non-fractional part, looping at least once to make
|
|
|
|
// sure at least a `0` gets emitted.
|
2013-06-28 16:05:10 -05:00
|
|
|
let mut deccum = num.trunc();
|
2013-02-14 20:29:36 -06:00
|
|
|
loop {
|
|
|
|
// Calculate the absolute value of each digit instead of only
|
|
|
|
// doing it once for the whole number because a
|
|
|
|
// representable negative number doesn't necessary have an
|
|
|
|
// representable additive inverse of the same type
|
|
|
|
// (See twos complement). But we assume that for the
|
|
|
|
// numbers [-35 .. 0] we always have [0 .. 35].
|
2013-06-28 16:05:10 -05:00
|
|
|
let current_digit = (deccum % radix_gen).abs();
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
// Decrease the deccumulator one digit at a time
|
2013-06-15 19:26:59 -05:00
|
|
|
deccum = deccum / radix_gen;
|
2013-06-28 16:05:10 -05:00
|
|
|
deccum = deccum.trunc();
|
2013-02-14 20:29:36 -06:00
|
|
|
|
2013-04-08 15:50:34 -05:00
|
|
|
buf.push(char::from_digit(current_digit.to_int() as uint, radix)
|
|
|
|
.unwrap() as u8);
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
// No more digits to calculate for the non-fractional part -> break
|
|
|
|
if deccum == _0 { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// If limited digits, calculate one digit more for rounding.
|
|
|
|
let (limit_digits, digit_count, exact) = match digits {
|
|
|
|
DigAll => (false, 0u, false),
|
|
|
|
DigMax(count) => (true, count+1, false),
|
|
|
|
DigExact(count) => (true, count+1, true)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Decide what sign to put in front
|
|
|
|
match sign {
|
|
|
|
SignNeg | SignAll if neg => {
|
2013-04-08 15:50:34 -05:00
|
|
|
buf.push('-' as u8);
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
SignAll => {
|
2013-04-08 15:50:34 -05:00
|
|
|
buf.push('+' as u8);
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2013-06-28 11:54:03 -05:00
|
|
|
buf.reverse();
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
// Remember start of the fractional digits.
|
|
|
|
// Points one beyond end of buf if none get generated,
|
|
|
|
// or at the '.' otherwise.
|
|
|
|
let start_fractional_digits = buf.len();
|
|
|
|
|
|
|
|
// Now emit the fractional part, if any
|
2013-06-28 16:05:10 -05:00
|
|
|
deccum = num.fract();
|
2013-02-14 20:29:36 -06:00
|
|
|
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
|
2013-04-08 15:50:34 -05:00
|
|
|
buf.push('.' as u8);
|
2013-02-14 20:29:36 -06:00
|
|
|
let mut dig = 0u;
|
|
|
|
|
|
|
|
// calculate new digits while
|
|
|
|
// - there is no limit and there are digits left
|
|
|
|
// - or there is a limit, it's not reached yet and
|
|
|
|
// - it's exact
|
|
|
|
// - or it's a maximum, and there are still digits left
|
|
|
|
while (!limit_digits && deccum != _0)
|
|
|
|
|| (limit_digits && dig < digit_count && (
|
|
|
|
exact
|
|
|
|
|| (!exact && deccum != _0)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Shift first fractional digit into the integer part
|
2013-06-15 19:26:59 -05:00
|
|
|
deccum = deccum * radix_gen;
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
// Calculate the absolute value of each digit.
|
|
|
|
// See note in first loop.
|
2013-06-28 16:05:10 -05:00
|
|
|
let current_digit = deccum.trunc().abs();
|
2013-02-14 20:29:36 -06:00
|
|
|
|
2013-04-08 15:50:34 -05:00
|
|
|
buf.push(char::from_digit(
|
|
|
|
current_digit.to_int() as uint, radix).unwrap() as u8);
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
// Decrease the deccumulator one fractional digit at a time
|
2013-06-28 16:05:10 -05:00
|
|
|
deccum = deccum.fract();
|
2013-02-14 20:29:36 -06:00
|
|
|
dig += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If digits are limited, and that limit has been reached,
|
|
|
|
// cut off the one extra digit, and depending on its value
|
|
|
|
// round the remaining ones.
|
|
|
|
if limit_digits && dig == digit_count {
|
|
|
|
let ascii2value = |chr: u8| {
|
|
|
|
char::to_digit(chr as char, radix).unwrap() as uint
|
|
|
|
};
|
|
|
|
let value2ascii = |val: uint| {
|
|
|
|
char::from_digit(val, radix).unwrap() as u8
|
|
|
|
};
|
|
|
|
|
2013-04-08 15:50:34 -05:00
|
|
|
let extra_digit = ascii2value(buf.pop());
|
|
|
|
if extra_digit >= radix / 2 { // -> need to round
|
|
|
|
let mut i: int = buf.len() as int - 1;
|
|
|
|
loop {
|
|
|
|
// If reached left end of number, have to
|
|
|
|
// insert additional digit:
|
|
|
|
if i < 0
|
|
|
|
|| buf[i] == '-' as u8
|
|
|
|
|| buf[i] == '+' as u8 {
|
|
|
|
buf.insert((i + 1) as uint, value2ascii(1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the '.'
|
|
|
|
if buf[i] == '.' as u8 { i -= 1; loop; }
|
|
|
|
|
|
|
|
// Either increment the digit,
|
|
|
|
// or set to 0 if max and carry the 1.
|
|
|
|
let current_digit = ascii2value(buf[i]);
|
|
|
|
if current_digit < (radix - 1) {
|
|
|
|
buf[i] = value2ascii(current_digit+1);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
buf[i] = value2ascii(0);
|
|
|
|
i -= 1;
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if number of digits is not exact, remove all trailing '0's up to
|
|
|
|
// and including the '.'
|
|
|
|
if !exact {
|
|
|
|
let buf_max_i = buf.len() - 1;
|
|
|
|
|
|
|
|
// index to truncate from
|
|
|
|
let mut i = buf_max_i;
|
|
|
|
|
|
|
|
// discover trailing zeros of fractional part
|
|
|
|
while i > start_fractional_digits && buf[i] == '0' as u8 {
|
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only attempt to truncate digits if buf has fractional digits
|
|
|
|
if i >= start_fractional_digits {
|
|
|
|
// If buf ends with '.', cut that too.
|
|
|
|
if buf[i] == '.' as u8 { i -= 1 }
|
|
|
|
|
|
|
|
// only resize buf if we actually remove digits
|
|
|
|
if i < buf_max_i {
|
2013-03-21 06:36:21 -05:00
|
|
|
buf = buf.slice(0, i + 1).to_owned();
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // If exact and trailing '.', just cut that
|
|
|
|
else {
|
|
|
|
let max_i = buf.len() - 1;
|
|
|
|
if buf[max_i] == '.' as u8 {
|
2013-03-21 06:36:21 -05:00
|
|
|
buf = buf.slice(0, max_i).to_owned();
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(buf, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a number to its string representation. This is a wrapper for
|
|
|
|
* `to_str_bytes_common()`, for details see there.
|
|
|
|
*/
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-28 16:05:10 -05:00
|
|
|
pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
|
|
|
|
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
|
|
|
|
num: T, radix: uint, negative_zero: bool,
|
2013-02-14 20:29:36 -06:00
|
|
|
sign: SignFormat, digits: SignificantDigits) -> (~str, bool) {
|
2013-06-28 16:05:10 -05:00
|
|
|
let (bytes, special) = float_to_str_bytes_common(num, radix,
|
2013-02-14 20:29:36 -06:00
|
|
|
negative_zero, sign, digits);
|
|
|
|
(str::from_bytes(bytes), special)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some constants for from_str_bytes_common's input validation,
|
|
|
|
// they define minimum radix values for which the character is a valid digit.
|
2013-03-22 16:00:15 -05:00
|
|
|
priv static DIGIT_P_RADIX: uint = ('p' as uint) - ('a' as uint) + 11u;
|
|
|
|
priv static DIGIT_I_RADIX: uint = ('i' as uint) - ('a' as uint) + 11u;
|
|
|
|
priv static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a byte slice as a number. This is meant to
|
|
|
|
* be a common base implementation for all numeric string conversion
|
|
|
|
* functions like `from_str()` or `from_str_radix()`.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
* - `buf` - The byte slice to parse.
|
|
|
|
* - `radix` - Which base to parse the number as. Accepts 2-36.
|
|
|
|
* - `negative` - Whether to accept negative numbers.
|
|
|
|
* - `fractional` - Whether to accept numbers with fractional parts.
|
|
|
|
* - `special` - Whether to accept special values like `inf`
|
|
|
|
* and `NaN`. Can conflict with `radix`, see Failure.
|
|
|
|
* - `exponent` - Which exponent format to accept. Options are:
|
|
|
|
* - `ExpNone`: No Exponent, accepts just plain numbers like `42` or
|
|
|
|
* `-8.2`.
|
|
|
|
* - `ExpDec`: Accepts numbers with a decimal exponent like `42e5` or
|
|
|
|
* `8.2E-2`. The exponent string itself is always base 10.
|
|
|
|
* Can conflict with `radix`, see Failure.
|
|
|
|
* - `ExpBin`: Accepts numbers with a binary exponent like `42P-8` or
|
|
|
|
* `FFp128`. The exponent string itself is always base 10.
|
|
|
|
* Can conflict with `radix`, see Failure.
|
|
|
|
* - `empty_zero` - Whether to accept a empty `buf` as a 0 or not.
|
2013-04-07 09:23:42 -05:00
|
|
|
* - `ignore_underscores` - Whether all underscores within the string should
|
|
|
|
* be ignored.
|
2013-02-14 20:29:36 -06:00
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
* Returns `Some(n)` if `buf` parses to a number n without overflowing, and
|
|
|
|
* `None` otherwise, depending on the constraints set by the remaining
|
|
|
|
* arguments.
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
* - Fails if `radix` < 2 or `radix` > 36.
|
|
|
|
* - Fails if `radix` > 14 and `exponent` is `ExpDec` due to conflict
|
|
|
|
* between digit and exponent sign `'e'`.
|
|
|
|
* - Fails if `radix` > 25 and `exponent` is `ExpBin` due to conflict
|
|
|
|
* between digit and exponent sign `'p'`.
|
|
|
|
* - Fails if `radix` > 18 and `special == true` due to conflict
|
|
|
|
* between digit and lowest first character in `inf` and `NaN`, the `'i'`.
|
|
|
|
*/
|
2013-05-01 00:40:05 -05:00
|
|
|
pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+
|
2013-02-14 22:40:01 -06:00
|
|
|
Mul<T,T>+Sub<T,T>+Neg<T>+Add<T,T>+
|
|
|
|
NumStrConv>(
|
2013-02-14 20:29:36 -06:00
|
|
|
buf: &[u8], radix: uint, negative: bool, fractional: bool,
|
2013-04-07 09:23:42 -05:00
|
|
|
special: bool, exponent: ExponentFormat, empty_zero: bool,
|
|
|
|
ignore_underscores: bool
|
2013-02-14 20:29:36 -06:00
|
|
|
) -> Option<T> {
|
|
|
|
match exponent {
|
|
|
|
ExpDec if radix >= DIGIT_E_RADIX // decimal exponent 'e'
|
2013-05-05 17:18:51 -05:00
|
|
|
=> fail!("from_str_bytes_common: radix %? incompatible with \
|
2013-05-09 06:52:07 -05:00
|
|
|
use of 'e' as decimal exponent", radix),
|
2013-02-14 20:29:36 -06:00
|
|
|
ExpBin if radix >= DIGIT_P_RADIX // binary exponent 'p'
|
2013-05-05 17:18:51 -05:00
|
|
|
=> fail!("from_str_bytes_common: radix %? incompatible with \
|
2013-05-09 06:52:07 -05:00
|
|
|
use of 'p' as binary exponent", radix),
|
2013-02-14 20:29:36 -06:00
|
|
|
_ if special && radix >= DIGIT_I_RADIX // first digit of 'inf'
|
2013-05-05 17:18:51 -05:00
|
|
|
=> fail!("from_str_bytes_common: radix %? incompatible with \
|
2013-05-09 06:52:07 -05:00
|
|
|
special values 'inf' and 'NaN'", radix),
|
2013-03-20 15:24:09 -05:00
|
|
|
_ if (radix as int) < 2
|
2013-05-05 17:18:51 -05:00
|
|
|
=> fail!("from_str_bytes_common: radix %? to low, \
|
2013-05-09 06:52:07 -05:00
|
|
|
must lie in the range [2, 36]", radix),
|
2013-03-20 15:24:09 -05:00
|
|
|
_ if (radix as int) > 36
|
2013-05-05 17:18:51 -05:00
|
|
|
=> fail!("from_str_bytes_common: radix %? to high, \
|
2013-05-09 06:52:07 -05:00
|
|
|
must lie in the range [2, 36]", radix),
|
2013-02-14 20:29:36 -06:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
let _0: T = Zero::zero();
|
|
|
|
let _1: T = One::one();
|
|
|
|
let radix_gen: T = cast(radix as int);
|
|
|
|
|
|
|
|
let len = buf.len();
|
|
|
|
|
|
|
|
if len == 0 {
|
|
|
|
if empty_zero {
|
|
|
|
return Some(_0);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if special {
|
2013-03-21 20:04:13 -05:00
|
|
|
if buf == inf_buf || buf == positive_inf_buf {
|
2013-02-14 22:20:36 -06:00
|
|
|
return NumStrConv::inf();
|
2013-03-21 20:04:13 -05:00
|
|
|
} else if buf == negative_inf_buf {
|
2013-02-14 20:29:36 -06:00
|
|
|
if negative {
|
2013-02-14 22:20:36 -06:00
|
|
|
return NumStrConv::neg_inf();
|
2013-02-14 20:29:36 -06:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2013-03-21 20:04:13 -05:00
|
|
|
} else if buf == nan_buf {
|
2013-02-14 22:20:36 -06:00
|
|
|
return NumStrConv::NaN();
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 20:19:58 -05:00
|
|
|
let (start, accum_positive) = match buf[0] as char {
|
|
|
|
'-' if !negative => return None,
|
|
|
|
'-' => (1u, false),
|
|
|
|
'+' => (1u, true),
|
|
|
|
_ => (0u, true)
|
2013-02-14 20:29:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize accumulator with signed zero for floating point parsing to
|
|
|
|
// work
|
2013-06-15 19:26:59 -05:00
|
|
|
let mut accum = if accum_positive { copy _0 } else { -_1 * _0};
|
|
|
|
let mut last_accum = copy accum; // Necessary to detect overflow
|
2013-02-14 20:29:36 -06:00
|
|
|
let mut i = start;
|
|
|
|
let mut exp_found = false;
|
|
|
|
|
|
|
|
// Parse integer part of number
|
|
|
|
while i < len {
|
|
|
|
let c = buf[i] as char;
|
|
|
|
|
|
|
|
match char::to_digit(c, radix) {
|
|
|
|
Some(digit) => {
|
|
|
|
// shift accum one digit left
|
2013-06-15 19:26:59 -05:00
|
|
|
accum = accum * copy radix_gen;
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
// add/subtract current digit depending on sign
|
|
|
|
if accum_positive {
|
2013-06-15 19:26:59 -05:00
|
|
|
accum = accum + cast(digit as int);
|
2013-02-14 20:29:36 -06:00
|
|
|
} else {
|
2013-06-15 19:26:59 -05:00
|
|
|
accum = accum - cast(digit as int);
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
|
2013-04-07 10:26:51 -05:00
|
|
|
// Detect overflow by comparing to last value, except
|
|
|
|
// if we've not seen any non-zero digits.
|
|
|
|
if last_accum != _0 {
|
|
|
|
if accum_positive && accum <= last_accum { return None; }
|
|
|
|
if !accum_positive && accum >= last_accum { return None; }
|
|
|
|
}
|
2013-06-15 19:26:59 -05:00
|
|
|
last_accum = copy accum;
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
None => match c {
|
2013-04-07 09:23:42 -05:00
|
|
|
'_' if ignore_underscores => {}
|
2013-02-14 20:29:36 -06:00
|
|
|
'e' | 'E' | 'p' | 'P' => {
|
|
|
|
exp_found = true;
|
|
|
|
break; // start of exponent
|
|
|
|
}
|
|
|
|
'.' if fractional => {
|
|
|
|
i += 1u; // skip the '.'
|
|
|
|
break; // start of fractional part
|
|
|
|
}
|
|
|
|
_ => return None // invalid number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse fractional part of number
|
|
|
|
// Skip if already reached start of exponent
|
|
|
|
if !exp_found {
|
2013-06-15 19:26:59 -05:00
|
|
|
let mut power = copy _1;
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
while i < len {
|
|
|
|
let c = buf[i] as char;
|
|
|
|
|
|
|
|
match char::to_digit(c, radix) {
|
|
|
|
Some(digit) => {
|
|
|
|
// Decrease power one order of magnitude
|
2013-06-15 19:26:59 -05:00
|
|
|
power = power / radix_gen;
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
let digit_t: T = cast(digit);
|
|
|
|
|
|
|
|
// add/subtract current digit depending on sign
|
|
|
|
if accum_positive {
|
2013-06-15 19:26:59 -05:00
|
|
|
accum = accum + digit_t * power;
|
2013-02-14 20:29:36 -06:00
|
|
|
} else {
|
2013-06-15 19:26:59 -05:00
|
|
|
accum = accum - digit_t * power;
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect overflow by comparing to last value
|
|
|
|
if accum_positive && accum < last_accum { return None; }
|
|
|
|
if !accum_positive && accum > last_accum { return None; }
|
2013-06-15 19:26:59 -05:00
|
|
|
last_accum = copy accum;
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
|
|
|
None => match c {
|
2013-04-07 09:23:42 -05:00
|
|
|
'_' if ignore_underscores => {}
|
2013-02-14 20:29:36 -06:00
|
|
|
'e' | 'E' | 'p' | 'P' => {
|
|
|
|
exp_found = true;
|
|
|
|
break; // start of exponent
|
|
|
|
}
|
|
|
|
_ => return None // invalid number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case: buf not empty, but does not contain any digit in front
|
|
|
|
// of the exponent sign -> number is empty string
|
|
|
|
if i == start {
|
|
|
|
if empty_zero {
|
|
|
|
return Some(_0);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 19:26:59 -05:00
|
|
|
let mut multiplier = copy _1;
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
if exp_found {
|
|
|
|
let c = buf[i] as char;
|
|
|
|
let base = match (c, exponent) {
|
2013-04-07 09:23:42 -05:00
|
|
|
// c is never _ so don't need to handle specially
|
2013-02-14 20:29:36 -06:00
|
|
|
('e', ExpDec) | ('E', ExpDec) => 10u,
|
|
|
|
('p', ExpBin) | ('P', ExpBin) => 2u,
|
|
|
|
_ => return None // char doesn't fit given exponent format
|
|
|
|
};
|
|
|
|
|
|
|
|
// parse remaining bytes as decimal integer,
|
|
|
|
// skipping the exponent char
|
|
|
|
let exp: Option<int> = from_str_bytes_common(
|
2013-04-07 09:23:42 -05:00
|
|
|
buf.slice(i+1, len), 10, true, false, false, ExpNone, false,
|
|
|
|
ignore_underscores);
|
2013-02-14 20:29:36 -06:00
|
|
|
|
|
|
|
match exp {
|
|
|
|
Some(exp_pow) => {
|
|
|
|
multiplier = if exp_pow < 0 {
|
|
|
|
_1 / pow_with_uint::<T>(base, (-exp_pow.to_int()) as uint)
|
|
|
|
} else {
|
|
|
|
pow_with_uint::<T>(base, exp_pow.to_int() as uint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => return None // invalid exponent -> invalid number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(accum * multiplier)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a string as a number. This is a wrapper for
|
|
|
|
* `from_str_bytes_common()`, for details see there.
|
|
|
|
*/
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-01 00:40:05 -05:00
|
|
|
pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+Mul<T,T>+
|
2013-02-14 22:20:36 -06:00
|
|
|
Sub<T,T>+Neg<T>+Add<T,T>+NumStrConv>(
|
2013-02-14 20:29:36 -06:00
|
|
|
buf: &str, radix: uint, negative: bool, fractional: bool,
|
2013-04-07 09:23:42 -05:00
|
|
|
special: bool, exponent: ExponentFormat, empty_zero: bool,
|
|
|
|
ignore_underscores: bool
|
2013-02-14 20:29:36 -06:00
|
|
|
) -> Option<T> {
|
2013-06-10 22:10:37 -05:00
|
|
|
from_str_bytes_common(buf.as_bytes(), radix, negative,
|
2013-04-07 09:23:42 -05:00
|
|
|
fractional, special, exponent, empty_zero,
|
|
|
|
ignore_underscores)
|
2013-02-14 20:29:36 -06:00
|
|
|
}
|
2013-04-07 10:26:51 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use option::*;
|
|
|
|
|
2013-04-07 09:23:42 -05:00
|
|
|
#[test]
|
|
|
|
fn from_str_ignore_underscores() {
|
|
|
|
let s : Option<u8> = from_str_common("__1__", 2, false, false, false,
|
|
|
|
ExpNone, false, true);
|
|
|
|
assert_eq!(s, Some(1u8));
|
|
|
|
|
|
|
|
let n : Option<u8> = from_str_common("__1__", 2, false, false, false,
|
|
|
|
ExpNone, false, false);
|
|
|
|
assert_eq!(n, None);
|
|
|
|
|
2013-04-23 04:10:37 -05:00
|
|
|
let f : Option<f32> = from_str_common("_1_._5_e_1_", 10, false, true, false,
|
2013-04-07 09:23:42 -05:00
|
|
|
ExpDec, false, true);
|
2013-04-23 04:10:37 -05:00
|
|
|
assert_eq!(f, Some(1.5e1f32));
|
2013-04-07 09:23:42 -05:00
|
|
|
}
|
|
|
|
|
2013-04-07 10:26:51 -05:00
|
|
|
#[test]
|
|
|
|
fn from_str_issue5770() {
|
|
|
|
// try to parse 0b1_1111_1111 = 511 as a u8. Caused problems
|
|
|
|
// since 255*2+1 == 255 (mod 256) so the overflow wasn't
|
|
|
|
// detected.
|
|
|
|
let n : Option<u8> = from_str_common("111111111", 2, false, false, false,
|
2013-04-07 09:23:42 -05:00
|
|
|
ExpNone, false, false);
|
2013-04-07 10:26:51 -05:00
|
|
|
assert_eq!(n, None);
|
|
|
|
}
|
|
|
|
}
|