Skip to content

Commit

Permalink
Merge pull request #262 from mat-1/main
Browse files Browse the repository at this point in the history
Add converting codepoints to characters
  • Loading branch information
printfn authored Jan 15, 2024
2 parents bb43194 + 5c48b97 commit 76bddc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,20 @@ fn evaluate_as<I: Interrupt>(
}
return Err(FendError::ExpectedAString);
}
"char" | "character" => {
let a = evaluate(a, scope, attrs, context, int)?;
if let Value::Num(v) = a {
let n = v.try_as_usize(int)?;
let ch = n
.try_into()
.ok()
.and_then(std::char::from_u32)
.ok_or(FendError::InvalidCodepoint(n))?;

return Ok(Value::String(ch.to_string().into()));
}
return Err(FendError::ExpectedANumber);
}
_ => (),
}
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(crate) enum FendError {
ExpectedACharacter,
StringCannotBeLonger,
StringCannotBeEmpty,
InvalidCodepoint(usize),
ExpectedADigit(char),
ExpectedChar(char, char),
ExpectedDigitSeparator(char),
Expand Down Expand Up @@ -160,6 +161,9 @@ impl fmt::Display for FendError {
Self::ExpectedARealNumber => write!(f, "expected a real number"),
Self::StringCannotBeLonger => write!(f, "string cannot be longer than one codepoint"),
Self::StringCannotBeEmpty => write!(f, "string cannot be empty"),
Self::InvalidCodepoint(codepoint) => {
write!(f, "invalid codepoint: U+{codepoint:04x}")
}
Self::UnableToGetCurrentDate => write!(f, "unable to get the current date"),
Self::NegativeNumbersNotAllowed => write!(f, "negative numbers are not allowed"),
Self::ProbabilityDistributionsNotAllowed => {
Expand Down

0 comments on commit 76bddc3

Please sign in to comment.