Commit 1504684e authored by AdminXVII's avatar AdminXVII
Browse files

Fix lone zero handling

parent 62378ccb
Pipeline #3736 passed with stage
in 3 minutes and 1 second
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.9.0" version = "0.9.0"
......
...@@ -373,11 +373,14 @@ pub fn tokenize_polish(input: &str) -> Result<Vec<Token>, CalcError> { ...@@ -373,11 +373,14 @@ pub fn tokenize_polish(input: &str) -> Result<Vec<Token>, CalcError> {
Ok(tokens) Ok(tokens)
} }
fn digits<I>(input: &mut Peekable<I>, radix: u32) -> String fn digits<I>(input: &mut Peekable<I>, radix: u32, has_zero: bool) -> String
where where
I: Iterator<Item = char>, I: Iterator<Item = char>,
{ {
let mut number = String::new(); let mut number = String::new();
if has_zero {
number.push('0')
}
while let Some(&c) = input.peek() { while let Some(&c) = input.peek() {
if c.is_digit(radix) { if c.is_digit(radix) {
number.push(c); number.push(c);
...@@ -393,27 +396,27 @@ fn consume_number<I>(input: &mut Peekable<I>) -> Result<Value, CalcError> ...@@ -393,27 +396,27 @@ fn consume_number<I>(input: &mut Peekable<I>) -> Result<Value, CalcError>
where where
I: Iterator<Item = char>, I: Iterator<Item = char>,
{ {
let mut has_zero = false;
match input.peek() { match input.peek() {
Some(&'0') => { Some(&'0') => {
input.next(); input.next();
match input.peek() { match input.peek() {
Some(&'x') | Some(&'X') => { Some(&'x') | Some(&'X') => {
input.next(); input.next();
let digits = digits(input, 16); let digits = digits(input, 16, false);
let num = Integral::from_str_radix(&digits, 16)?; let num = Integral::from_str_radix(&digits, 16)?;
return Ok(Value::hex(num)); return Ok(Value::hex(num));
} }
Some(&_) => (), _ => has_zero = true,
None => return Ok(Value::dec(0)),
} }
} }
Some(_) => (), Some(_) => (),
None => return Err(CalcError::UnexpectedEndOfInput), None => return Err(CalcError::UnexpectedEndOfInput),
} }
let whole = digits(input, 10); let whole = digits(input, 10, has_zero);
if let Some(&'.') = input.peek() { if let Some(&'.') = input.peek() {
input.next(); input.next();
let frac = digits(input, 10); let frac = digits(input, 10, false);
let num = [whole, ".".into(), frac] let num = [whole, ".".into(), frac]
.concat() .concat()
.parse::<d128>() .parse::<d128>()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment