Harmonize non-negative int arguments

This commit is contained in:
Martin Haug 2022-02-03 11:27:06 +01:00
parent 3f76aadb1a
commit bd0d0e10d8
3 changed files with 43 additions and 45 deletions

View File

@ -182,7 +182,13 @@ dynamic! {
castable! { castable! {
usize, usize,
Expected: "non-negative integer", Expected: "non-negative integer",
Value::Int(int) => int.try_into().map_err(|_| "must be at least zero")?, Value::Int(int) => int.try_into().map_err(|_| {
if int < 0 {
"must be at least zero"
} else {
"number too large"
}
})?,
} }
castable! { castable! {

View File

@ -6,31 +6,6 @@ use std::str::FromStr;
use super::prelude::*; use super::prelude::*;
use crate::eval::Array; use crate::eval::Array;
static ROMAN_PAIRS: &'static [(&'static str, u32)] = &[
("", 1000000),
("", 500000),
("", 100000),
("", 50000),
("", 10000),
("", 5000),
("I̅V̅", 4000),
("M", 1000),
("CM", 900),
("D", 500),
("CD", 400),
("C", 100),
("XC", 90),
("L", 50),
("XL", 40),
("X", 10),
("IX", 9),
("V", 5),
("IV", 4),
("I", 1),
];
static SYMBOLS: &'static [char] = &['*', '†', '‡', '§', '‖', '¶'];
/// Ensure that a condition is fulfilled. /// Ensure that a condition is fulfilled.
pub fn assert(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> { pub fn assert(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?; let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?;
@ -177,7 +152,7 @@ pub fn modulo(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let (a, b) = match (v1, v2) { let (a, b) = match (v1, v2) {
(Value::Int(a), Value::Int(b)) => match a.checked_rem(b) { (Value::Int(a), Value::Int(b)) => match a.checked_rem(b) {
Some(res) => return Ok(res.into()), Some(res) => return Ok(Value::Int(res)),
None => bail!(span2, "divisor must not be zero"), None => bail!(span2, "divisor must not be zero"),
}, },
(Value::Int(a), Value::Float(b)) => (a as f64, b), (Value::Int(a), Value::Float(b)) => (a as f64, b),
@ -197,7 +172,7 @@ pub fn modulo(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
bail!(span2, "divisor must not be zero"); bail!(span2, "divisor must not be zero");
} }
Ok((a % b).into()) Ok(Value::Float(a % b))
} }
/// Find the minimum or maximum of a sequence of values. /// Find the minimum or maximum of a sequence of values.
@ -262,14 +237,35 @@ pub fn upper(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
/// Adapted from Yann Villessuzanne's roman.rs under the Unlicense, at /// Adapted from Yann Villessuzanne's roman.rs under the Unlicense, at
/// https://github.com/linfir/roman.rs/ /// https://github.com/linfir/roman.rs/
pub fn roman(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> { pub fn roman(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let source: Spanned<i64> = args.expect("integer")?; static PAIRS: &'static [(&'static str, usize)] = &[
let mut n = source.v as u32; ("", 1000000),
("", 500000),
("", 100000),
("", 50000),
("", 10000),
("", 5000),
("I̅V̅", 4000),
("M", 1000),
("CM", 900),
("D", 500),
("CD", 400),
("C", 100),
("XC", 90),
("L", 50),
("XL", 40),
("X", 10),
("IX", 9),
("V", 5),
("IV", 4),
("I", 1),
];
match n { let Spanned { mut v, span } = args.expect("non-negative integer")?;
0 => return Ok("N".into()), match v {
i if i > 3_999_999 => { 0_usize => return Ok("N".into()),
3_999_999 .. => {
bail!( bail!(
source.span, span,
"cannot convert integers greater than 3,999,999 to roman numerals" "cannot convert integers greater than 3,999,999 to roman numerals"
) )
} }
@ -277,9 +273,9 @@ pub fn roman(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
} }
let mut roman = String::new(); let mut roman = String::new();
for &(name, value) in ROMAN_PAIRS.iter() { for &(name, value) in PAIRS {
while n >= value { while v >= value {
n -= value; v -= value;
roman.push_str(name); roman.push_str(name);
} }
} }
@ -289,18 +285,14 @@ pub fn roman(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
/// Convert a number into a roman numeral. /// Convert a number into a roman numeral.
pub fn symbol(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> { pub fn symbol(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let source: Spanned<i64> = args.expect("integer")?; static SYMBOLS: &'static [char] = &['*', '†', '‡', '§', '‖', '¶'];
let n: usize = match source.v.try_into() {
Ok(n) => n, let n = args.expect::<usize>("non-negative integer")?;
Err(_) if source.v < 0 => bail!(source.span, "number must not be negative"),
Err(_) => bail!(source.span, "number too large"),
};
let symbol = SYMBOLS[n % SYMBOLS.len()]; let symbol = SYMBOLS[n % SYMBOLS.len()];
let amount = (n / SYMBOLS.len()) + 1; let amount = (n / SYMBOLS.len()) + 1;
let symbols: String = std::iter::repeat(symbol).take(amount).collect(); let symbols: String = std::iter::repeat(symbol).take(amount).collect();
Ok(symbols.into()) Ok(symbols.into())
} }

View File

@ -19,5 +19,5 @@
#roman(8000000) #roman(8000000)
--- ---
// Error: 9-11 number must not be negative // Error: 9-11 must be at least zero
#symbol(-1) #symbol(-1)