Skip to main content

ParseDecimalError

Struct ParseDecimalError 

Source
pub struct ParseDecimalError {
    kind: DecimalErrorKind,
}

Fields§

§kind: DecimalErrorKind

Implementations§

Source§

impl ParseDecimalError

Source

fn empty() -> Self

Examples found in repository?
crates/competitive/src/num/decimal/convert.rs (line 45)
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        if s.is_empty() {
45            return Err(ParseDecimalError::empty());
46        }
47
48        let (s, sign) = if let Some(s) = s.strip_prefix('+') {
49            (s, Sign::Plus)
50        } else if let Some(s) = s.strip_prefix('-') {
51            (s, Sign::Minus)
52        } else {
53            (s, Sign::Plus)
54        };
55
56        let (integer_str, decimal_str) = if let Some((integer_str, decimal_str)) = s.split_once('.')
57        {
58            (integer_str, decimal_str)
59        } else {
60            (s, "")
61        };
62
63        if !integer_str.is_ascii() || !decimal_str.is_ascii() {
64            return Err(ParseDecimalError::invalid_digit());
65        }
66
67        let integer_bytes = integer_str.trim_start_matches('0').as_bytes();
68        let decimal_bytes = decimal_str.trim_end_matches('0').as_bytes();
69
70        let mut integer = Vec::with_capacity(integer_bytes.len().div_ceil(RADIX_LEN));
71        for chunk in integer_bytes.rchunks(18) {
72            let chunk = unsafe { std::str::from_utf8_unchecked(chunk) };
73            match chunk.parse::<u64>() {
74                Ok(val) => integer.push(val),
75                Err(_) => return Err(ParseDecimalError::invalid_digit()),
76            }
77        }
78
79        let mut decimal = Vec::with_capacity(decimal_bytes.len().div_ceil(RADIX_LEN));
80        for chunk in decimal_bytes.chunks(18) {
81            let chunk = unsafe { std::str::from_utf8_unchecked(chunk) };
82            match chunk.parse::<u64>() {
83                Ok(val) => decimal.push(val * POW10[RADIX_LEN - chunk.len()]),
84                Err(_) => return Err(ParseDecimalError::invalid_digit()),
85            }
86        }
87
88        let sign = if integer.is_empty() && decimal.is_empty() {
89            Sign::Zero
90        } else {
91            sign
92        };
93
94        Ok(Decimal {
95            sign,
96            integer,
97            decimal,
98        })
99    }
Source

fn invalid_digit() -> Self

Examples found in repository?
crates/competitive/src/num/decimal/convert.rs (line 64)
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        if s.is_empty() {
45            return Err(ParseDecimalError::empty());
46        }
47
48        let (s, sign) = if let Some(s) = s.strip_prefix('+') {
49            (s, Sign::Plus)
50        } else if let Some(s) = s.strip_prefix('-') {
51            (s, Sign::Minus)
52        } else {
53            (s, Sign::Plus)
54        };
55
56        let (integer_str, decimal_str) = if let Some((integer_str, decimal_str)) = s.split_once('.')
57        {
58            (integer_str, decimal_str)
59        } else {
60            (s, "")
61        };
62
63        if !integer_str.is_ascii() || !decimal_str.is_ascii() {
64            return Err(ParseDecimalError::invalid_digit());
65        }
66
67        let integer_bytes = integer_str.trim_start_matches('0').as_bytes();
68        let decimal_bytes = decimal_str.trim_end_matches('0').as_bytes();
69
70        let mut integer = Vec::with_capacity(integer_bytes.len().div_ceil(RADIX_LEN));
71        for chunk in integer_bytes.rchunks(18) {
72            let chunk = unsafe { std::str::from_utf8_unchecked(chunk) };
73            match chunk.parse::<u64>() {
74                Ok(val) => integer.push(val),
75                Err(_) => return Err(ParseDecimalError::invalid_digit()),
76            }
77        }
78
79        let mut decimal = Vec::with_capacity(decimal_bytes.len().div_ceil(RADIX_LEN));
80        for chunk in decimal_bytes.chunks(18) {
81            let chunk = unsafe { std::str::from_utf8_unchecked(chunk) };
82            match chunk.parse::<u64>() {
83                Ok(val) => decimal.push(val * POW10[RADIX_LEN - chunk.len()]),
84                Err(_) => return Err(ParseDecimalError::invalid_digit()),
85            }
86        }
87
88        let sign = if integer.is_empty() && decimal.is_empty() {
89            Sign::Zero
90        } else {
91            sign
92        };
93
94        Ok(Decimal {
95            sign,
96            integer,
97            decimal,
98        })
99    }

Trait Implementations§

Source§

impl Clone for ParseDecimalError

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ParseDecimalError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ParseDecimalError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ParseDecimalError

Source§

impl From<ParseDecimalError> for ParseDoubleDoubleError

Source§

fn from(e: ParseDecimalError) -> Self

Converts to this type from the input type.
Source§

impl From<ParseDecimalError> for ParseDoubleDoubleError

Source§

fn from(e: ParseDecimalError) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ParseDecimalError

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ParseDecimalError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToArrayVecScalar for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.