Fix off-by-one error message on indexing.

This commit is contained in:
Stephen Chung 2020-03-06 09:55:00 +08:00
parent 3d3b939ba6
commit c7801e1d7e

View File

@ -134,9 +134,10 @@ impl std::fmt::Display for EvalAltResult {
Self::ErrorArrayBounds(max, _, pos) if *max == 0 => write!(f, "{} ({})", desc, pos), Self::ErrorArrayBounds(max, _, pos) if *max == 0 => write!(f, "{} ({})", desc, pos),
Self::ErrorArrayBounds(max, index, pos) => write!( Self::ErrorArrayBounds(max, index, pos) => write!(
f, f,
"Array index {} is out of bounds: max {} elements ({})", "Array index {} is out of bounds: only {} element{} in the array ({})",
index, index,
max - 1, max,
if *max > 1 { "s" } else { "" },
pos pos
), ),
Self::ErrorStringBounds(_, index, pos) if *index < 0 => { Self::ErrorStringBounds(_, index, pos) if *index < 0 => {
@ -145,9 +146,10 @@ impl std::fmt::Display for EvalAltResult {
Self::ErrorStringBounds(max, _, pos) if *max == 0 => write!(f, "{} ({})", desc, pos), Self::ErrorStringBounds(max, _, pos) if *max == 0 => write!(f, "{} ({})", desc, pos),
Self::ErrorStringBounds(max, index, pos) => write!( Self::ErrorStringBounds(max, index, pos) => write!(
f, f,
"String index {} is out of bounds: max {} characters ({})", "String index {} is out of bounds: only {} character{} in the string ({})",
index, index,
max - 1, max,
if *max > 1 { "s" } else { "" },
pos pos
), ),
} }