pub struct ParseDecimalError {
kind: DecimalErrorKind,
}Fields§
§kind: DecimalErrorKindImplementations§
Source§impl ParseDecimalError
impl ParseDecimalError
Sourcefn empty() -> Self
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 }Sourcefn invalid_digit() -> Self
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
impl Clone for ParseDecimalError
Source§fn clone(&self) -> ParseDecimalError
fn clone(&self) -> ParseDecimalError
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ParseDecimalError
impl Debug for ParseDecimalError
Source§impl Display for ParseDecimalError
impl Display for ParseDecimalError
Source§impl From<ParseDecimalError> for ParseDoubleDoubleError
impl From<ParseDecimalError> for ParseDoubleDoubleError
Source§fn from(e: ParseDecimalError) -> Self
fn from(e: ParseDecimalError) -> Self
Converts to this type from the input type.
Source§impl From<ParseDecimalError> for ParseDoubleDoubleError
impl From<ParseDecimalError> for ParseDoubleDoubleError
Source§fn from(e: ParseDecimalError) -> Self
fn from(e: ParseDecimalError) -> Self
Converts to this type from the input type.
Source§impl PartialEq for ParseDecimalError
impl PartialEq for ParseDecimalError
impl Eq for ParseDecimalError
impl StructuralPartialEq for ParseDecimalError
Auto Trait Implementations§
impl Freeze for ParseDecimalError
impl RefUnwindSafe for ParseDecimalError
impl Send for ParseDecimalError
impl Sync for ParseDecimalError
impl Unpin for ParseDecimalError
impl UnwindSafe for ParseDecimalError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more