Skip to content

Commit

Permalink
Add chr function
Browse files Browse the repository at this point in the history
  • Loading branch information
timfennis committed Oct 15, 2024
1 parent 9d56f2c commit c85f497
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/stdlib/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use andy_cpp_macros::export_module;
mod inner {
use crate::interpreter::sequence::Sequence;
use crate::interpreter::value::Value;
use anyhow::anyhow;

use anyhow::{anyhow, Context};
use itertools::Itertools;
use std::rc::Rc;

Expand All @@ -21,6 +22,12 @@ mod inner {
Ok(res)
}

pub fn chr(num: i64) -> anyhow::Result<String> {
let num = u32::try_from(num).context("this number is not a valid character")?;
let char = char::from_u32(num).context("this number is not a valid character")?;
Ok(String::from(char))
}

// TODO: remove this function once actual slicing is supported
pub fn slice(string: &str, i: usize, j: usize) -> anyhow::Result<String> {
let len = string.chars().count();
Expand Down
4 changes: 4 additions & 0 deletions tests/programs/602_stdlib_string/004_chr.ndct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--PROGRAM--
print(chr(3486));
--EXPECT--

0 comments on commit c85f497

Please sign in to comment.