use facet::Facet;
use xur_runtime::{BuiltinType, Registry, XurStr};
#[derive(Clone, Copy, Facet)]
#[repr(C)]
pub enum Error {
InvalidCodepoint,
NotSingleChar,
Parse,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Error::InvalidCodepoint => "not a valid Unicode codepoint",
Error::NotSingleChar => "expected a single character",
Error::Parse => "not a valid number",
})
}
}
pub(crate) fn register(registry: &Registry) {
let mut m = registry.module("std::str");
m.ty::<Error>("Error");
m.named_function("chr", &["codepoint"], |n: u32| -> Result<String, Error> {
char::from_u32(n)
.map(|c| c.to_string())
.ok_or(Error::InvalidCodepoint)
});
registry
.builtin(BuiltinType::Str)
.method("len", |s: &str| s.chars().count() as i64)
.method("is_empty", |s: &str| s.is_empty())
.method("ord", |s: &str| -> Result<u32, Error> {
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => Ok(c as u32),
_ => Err(Error::NotSingleChar),
}
})
.method("to_upper", |s: &str| s.to_uppercase())
.method("to_lower", |s: &str| s.to_lowercase())
.method("trim", |s: &str| s.trim().to_owned())
.method("trim_start", |s: &str| s.trim_start().to_owned())
.method("trim_end", |s: &str| s.trim_end().to_owned())
.named_method("contains", &["part"], |s: &str, part: XurStr| {
s.contains(&*part)
})
.named_method("starts_with", &["prefix"], |s: &str, prefix: XurStr| {
s.starts_with(&*prefix)
})
.named_method("ends_with", &["suffix"], |s: &str, suffix: XurStr| {
s.ends_with(&*suffix)
})
.named_method(
"replace",
&["from", "to"],
|s: &str, from: XurStr, to: XurStr| s.replace(&*from, &to),
)
.named_method("split", &["separator"], |s: &str, separator: XurStr| {
s.split(&*separator).map(str::to_owned).collect::<Vec<_>>()
})
.method("lines", |s: &str| {
s.lines().map(str::to_owned).collect::<Vec<_>>()
})
.named_method("repeat", &["n"], |s: &str, n: u32| s.repeat(n as usize))
.named_method(
"pad_left",
&["width", "fill"],
|s: &str, width: usize, fill: XurStr| pad_left(s, width, &fill),
)
.named_method(
"pad_right",
&["width", "fill"],
|s: &str, width: usize, fill: XurStr| pad_right(s, width, &fill),
)
.named_method(
"slice",
&["start", "end"],
|s: &str, start: i64, end: i64| {
let chars: Vec<char> = s.chars().collect();
let len = chars.len();
let start = resolve_index(start, len);
let end = resolve_index(end, len);
if start >= end {
String::new()
} else {
chars[start..end].iter().collect()
}
},
)
.method("chars", |s: &str| {
s.chars().map(|c| c.to_string()).collect::<Vec<_>>()
})
.method("bytes", |s: &str| {
s.bytes().map(|b| b as i64).collect::<Vec<i64>>()
})
.named_method(
"insert",
&["index", "substr"],
|s: &str, index: i64, substr: XurStr| {
let chars: Vec<char> = s.chars().collect();
let len = chars.len();
let pos = resolve_index(index, len);
let byte_pos = chars[..pos].iter().map(|c| c.len_utf8()).sum::<usize>();
let mut result = s.to_owned();
result.insert_str(byte_pos, &substr);
result
},
)
.named_method(
"remove",
&["start", "end"],
|s: &str, start: i64, end: i64| {
let mut chars: Vec<char> = s.chars().collect();
let len = chars.len();
let start = resolve_index(start, len);
let end = resolve_index(end, len);
if start < end {
chars.drain(start..end);
}
chars.into_iter().collect::<String>()
},
)
.method("parse_int", |s: &str| {
s.trim().parse::<i64>().map_err(|_| Error::Parse)
})
.method("parse_hex", |s: &str| {
i64::from_str_radix(
s.trim().trim_start_matches("0x").trim_start_matches("0X"),
16,
)
.map_err(|_| Error::Parse)
})
.method("parse_float", |s: &str| {
s.trim().parse::<f64>().map_err(|_| Error::Parse)
})
.named_method(
"find",
&["needle"],
|s: &str, needle: XurStr| -> Option<i64> {
s.find(&*needle)
.map(|byte_offset| s[..byte_offset].chars().count() as i64)
},
)
.named_method(
"rfind",
&["needle"],
|s: &str, needle: XurStr| -> Option<i64> {
s.rfind(&*needle)
.map(|byte_offset| s[..byte_offset].chars().count() as i64)
},
);
}
fn resolve_index(index: i64, len: usize) -> usize {
if index < 0 {
len.saturating_sub((-index) as usize)
} else {
(index as usize).min(len)
}
}
fn pad_left(s: &str, width: usize, fill: &str) -> String {
let missing = width.saturating_sub(s.chars().count());
let padding: String = fill.chars().cycle().take(missing).collect();
padding + s
}
fn pad_right(s: &str, width: usize, fill: &str) -> String {
let missing = width.saturating_sub(s.chars().count());
let padding: String = fill.chars().cycle().take(missing).collect();
let mut result = s.to_owned();
result.push_str(&padding);
result
}
#[cfg(test)]
mod tests {
use xur_runtime::testing::TestCx;
use xur_runtime::{BuiltinType, Value};
use crate::registry;
fn call(name: &str, args: &[Value]) -> Value {
let registry = registry();
let mut cx = TestCx::new(®istry, crate::PRELUDE);
let id = registry
.builtin_method(BuiltinType::Str, name)
.expect("method exists");
cx.call(id, args).unwrap()
}
#[test]
fn string_methods() {
assert_eq!(call("to_upper", &[Value::str("abc")]), Value::str("ABC"));
assert_eq!(call("len", &[Value::str("αβ")]), Value::int(2));
assert_eq!(
call("split", &[Value::str("a,b"), Value::str(",")]),
Value::list(vec![Value::str("a"), Value::str("b")])
);
}
#[test]
fn ord_returns_codepoint() {
let registry = registry();
let lang = TestCx::new(®istry, crate::PRELUDE).lang;
assert_eq!(
call("ord", &[Value::str("A")]),
Value::adt(lang.ok, vec![Value::int(65)])
);
}
#[test]
fn ord_rejects_multi_char() {
let result = call("ord", &[Value::str("AB")]);
assert!(matches!(result, Value::Adt(_)));
}
#[test]
fn parsing_returns_results() {
let registry = registry();
let lang = TestCx::new(®istry, crate::PRELUDE).lang;
assert_eq!(
call("parse_int", &[Value::str(" 42 ")]),
Value::adt(lang.ok, vec![Value::int(42)])
);
assert!(matches!(
call("parse_int", &[Value::str("x")]),
Value::Adt(adt) if adt.ctor == lang.err
));
}
#[test]
fn parse_hex_returns_results() {
let registry = registry();
let lang = TestCx::new(®istry, crate::PRELUDE).lang;
assert_eq!(
call("parse_hex", &[Value::str("ff")]),
Value::adt(lang.ok, vec![Value::int(255)])
);
assert_eq!(
call("parse_hex", &[Value::str("0xFF")]),
Value::adt(lang.ok, vec![Value::int(255)])
);
assert_eq!(
call("parse_hex", &[Value::str(" 7ee45f400000 ")]),
Value::adt(lang.ok, vec![Value::int(0x7ee45f400000)])
);
assert!(matches!(
call("parse_hex", &[Value::str("zz")]),
Value::Adt(adt) if adt.ctor == lang.err
));
}
#[test]
fn trim_start_and_end() {
assert_eq!(
call("trim_start", &[Value::str(" hello ")]),
Value::str("hello ")
);
assert_eq!(
call("trim_end", &[Value::str(" hello ")]),
Value::str(" hello")
);
}
#[test]
fn pad_right_pads_to_width() {
assert_eq!(
call(
"pad_right",
&[Value::str("hi"), Value::int(5), Value::str(".")]
),
Value::str("hi...")
);
assert_eq!(
call(
"pad_right",
&[Value::str("hello"), Value::int(3), Value::str(".")]
),
Value::str("hello")
);
}
#[test]
fn find_returns_char_index() {
let registry = registry();
let lang = TestCx::new(®istry, crate::PRELUDE).lang;
assert_eq!(
call("find", &[Value::str("hello world"), Value::str("world")]),
Value::adt(lang.some, vec![Value::int(6)])
);
assert_eq!(
call("find", &[Value::str("hello"), Value::str("xyz")]),
Value::adt(lang.none, vec![])
);
}
#[test]
fn rfind_returns_last_char_index() {
let registry = registry();
let lang = TestCx::new(®istry, crate::PRELUDE).lang;
assert_eq!(
call("rfind", &[Value::str("abcabc"), Value::str("abc")]),
Value::adt(lang.some, vec![Value::int(3)])
);
}
#[test]
fn find_with_multibyte_chars() {
let registry = registry();
let lang = TestCx::new(®istry, crate::PRELUDE).lang;
assert_eq!(
call("find", &[Value::str("αβγδ"), Value::str("γ")]),
Value::adt(lang.some, vec![Value::int(2)])
);
}
#[test]
fn slice_with_positive_and_negative_indices() {
assert_eq!(
call(
"slice",
&[Value::str("hello"), Value::int(1), Value::int(4)]
),
Value::str("ell")
);
assert_eq!(
call(
"slice",
&[Value::str("hello"), Value::int(0), Value::int(-1)]
),
Value::str("hell")
);
assert_eq!(
call(
"slice",
&[Value::str("hello"), Value::int(-3), Value::int(-1)]
),
Value::str("ll")
);
}
#[test]
fn chars_returns_list_of_single_chars() {
assert_eq!(
call("chars", &[Value::str("abc")]),
Value::list(vec![Value::str("a"), Value::str("b"), Value::str("c")])
);
}
#[test]
fn bytes_returns_list_of_byte_values() {
assert_eq!(
call("bytes", &[Value::str("AB")]),
Value::list(vec![Value::int(65), Value::int(66)])
);
}
#[test]
fn insert_at_char_position() {
assert_eq!(
call(
"insert",
&[Value::str("helo"), Value::int(3), Value::str("l")]
),
Value::str("hello")
);
}
#[test]
fn remove_char_range() {
assert_eq!(
call(
"remove",
&[Value::str("hello"), Value::int(1), Value::int(4)]
),
Value::str("ho")
);
}
}