Skip to content

Commit

Permalink
Set reasonable upper bound for roman numerals
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Mar 15, 2024
1 parent 5e04cb9 commit 55881c9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
12 changes: 11 additions & 1 deletion core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::{FendError, Interrupt};
use crate::eval::evaluate_to_value;
use crate::ident::Ident;
use crate::interrupt::test_int;
use crate::num::{Base, FormattingStyle, Number};
use crate::num::{Base, FormattingStyle, Number, Range, RangeBound};
use crate::result::FResult;
use crate::scope::Scope;
use crate::serialize::{Deserialize, Serialize};
Expand Down Expand Up @@ -628,6 +628,16 @@ fn evaluate_as<I: Interrupt>(
if a == 0 {
return Err(FendError::RomanNumeralZero);
}
let upper_limit = 100_000;
if a > upper_limit {
return Err(FendError::OutOfRange {
value: Box::new(a),
range: Range {
start: RangeBound::Closed(Box::new(1)),
end: RangeBound::Closed(Box::new(upper_limit)),
},
});
}
return Ok(Value::String(borrow::Cow::Owned(to_roman(a))));
}
_ => (),
Expand Down
4 changes: 2 additions & 2 deletions core/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl<T: fmt::Display + fmt::Debug + 'static> RangeBound<T> {

#[derive(Debug)]
pub(crate) struct Range<T> {
start: RangeBound<T>,
end: RangeBound<T>,
pub(crate) start: RangeBound<T>,
pub(crate) end: RangeBound<T>,
}

impl<T> Range<T> {
Expand Down
1 change: 1 addition & 0 deletions core/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5935,4 +5935,5 @@ fn test_roman() {
test_eval_simple("3456 to roman", "MMMCDLVI");
test_eval_simple("1452 to roman", "MCDLII");
test_eval_simple("20002 to roman", "MMMMMMMMMMMMMMMMMMMMII");
expect_error("100001 to roman", Some("100001 must lie in the interval [1, 100000]"));
}

0 comments on commit 55881c9

Please sign in to comment.