Commit bff14f6e authored by AdminXVII's avatar AdminXVII
Browse files

Add test and don't push the zero

parent 1504684e
Pipeline #3741 passed with stage
in 2 minutes and 54 seconds
......@@ -373,14 +373,11 @@ pub fn tokenize_polish(input: &str) -> Result<Vec<Token>, CalcError> {
Ok(tokens)
}
fn digits<I>(input: &mut Peekable<I>, radix: u32, has_zero: bool) -> String
fn digits<I>(input: &mut Peekable<I>, radix: u32) -> String
where
I: Iterator<Item = char>,
{
let mut number = String::new();
if has_zero {
number.push('0')
}
while let Some(&c) = input.peek() {
if c.is_digit(radix) {
number.push(c);
......@@ -396,27 +393,30 @@ fn consume_number<I>(input: &mut Peekable<I>) -> Result<Value, CalcError>
where
I: Iterator<Item = char>,
{
let mut has_zero = false;
match input.peek() {
Some(&'0') => {
input.next();
match input.peek() {
Some(&'x') | Some(&'X') => {
match input.peek().cloned() {
Some('x') | Some('X') => {
input.next();
let digits = digits(input, 16, false);
let digits = digits(input, 16);
let num = Integral::from_str_radix(&digits, 16)?;
return Ok(Value::hex(num));
}
_ => has_zero = true,
Some(c) if c.is_digit(16) || c == '.' => (),
_ => return Ok(Value::dec(0)),
}
}
Some(_) => (),
None => return Err(CalcError::UnexpectedEndOfInput),
}
let whole = digits(input, 10, has_zero);
let mut whole = digits(input, 10);
if let Some(&'.') = input.peek() {
input.next();
let frac = digits(input, 10, false);
let frac = digits(input, 10);
if whole.is_empty() && frac.is_empty() {
whole = "0".to_string();
}
let num = [whole, ".".into(), frac]
.concat()
.parse::<d128>()
......@@ -477,6 +477,19 @@ mod tests {
assert_eq!(tokenize(line), Ok(expected));
}
#[test]
fn zero() {
let line = "0 + 0. + 0";
let expected = vec![
Token::Number(Value::dec(0)),
Token::Plus,
Token::Number(Value::Float(d128::from(0))),
Token::Plus,
Token::Number(Value::dec(0)),
];
assert_eq!(tokenize(line), Ok(expected));
}
#[test]
fn dice() {
let line = "3d6";
......
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