2020-02-08 20:32:10 -05:00
|
|
|
use std::collections::BTreeMap;
|
2020-02-02 21:09:29 -05:00
|
|
|
|
2020-02-14 14:23:54 -05:00
|
|
|
use num_bigint::BigInt;
|
|
|
|
use num_traits::cast::ToPrimitive;
|
|
|
|
use num_traits::sign::Signed;
|
|
|
|
|
2020-02-08 20:32:10 -05:00
|
|
|
use super::Builtin;
|
2020-02-08 21:19:54 -05:00
|
|
|
use crate::common::QuoteKind;
|
2020-02-08 20:38:37 -05:00
|
|
|
use crate::units::Unit;
|
|
|
|
use crate::value::{Number, Value};
|
2020-02-08 20:32:10 -05:00
|
|
|
|
|
|
|
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|
|
|
decl!(f "to-upper-case", |args, _| {
|
|
|
|
let s: &Value = arg!(args, 0, "string");
|
|
|
|
match s.eval() {
|
|
|
|
Value::Ident(i, q) => Some(Value::Ident(i.to_ascii_uppercase(), q)),
|
|
|
|
_ => todo!("")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
decl!(f "to-lower-case", |args, _| {
|
|
|
|
let s: &Value = arg!(args, 0, "string");
|
|
|
|
match s.eval() {
|
|
|
|
Value::Ident(i, q) => Some(Value::Ident(i.to_ascii_lowercase(), q)),
|
|
|
|
_ => todo!("")
|
|
|
|
}
|
|
|
|
});
|
2020-02-08 20:38:37 -05:00
|
|
|
decl!(f "str-length", |args, _| {
|
|
|
|
let s: &Value = arg!(args, 0, "string");
|
|
|
|
match s.eval() {
|
|
|
|
Value::Ident(i, _) => Some(Value::Dimension(Number::from(i.len()), Unit::None)),
|
|
|
|
_ => todo!("")
|
|
|
|
}
|
|
|
|
});
|
2020-02-08 21:19:54 -05:00
|
|
|
decl!(f "quote", |args, _| {
|
|
|
|
let s = arg!(args, 0, "string").eval();
|
|
|
|
match s {
|
|
|
|
Value::Ident(i, _) => Some(Value::Ident(i, QuoteKind::Double)),
|
|
|
|
_ => todo!("")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
decl!(f "unquote", |args, _| {
|
|
|
|
Some(arg!(args, 0, "string").eval().unquote())
|
|
|
|
});
|
2020-02-14 14:23:54 -05:00
|
|
|
decl!(f "str-slice", |args, _| {
|
|
|
|
let (string, quotes) = match arg!(args, 0, "string").eval() {
|
|
|
|
Value::Ident(s, q) => (s, q),
|
|
|
|
_ => todo!("____ is not a string")
|
|
|
|
};
|
|
|
|
let str_len = string.len();
|
|
|
|
let start = match arg!(args, 1, "start-at").eval() {
|
|
|
|
Value::Dimension(n, Unit::None) if n.to_integer().is_positive() => n.to_integer().to_usize().unwrap(),
|
|
|
|
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer()).to_usize().unwrap(),
|
|
|
|
Value::Dimension(..) => todo!("$start: Expected ___ to have no units."),
|
|
|
|
_ => todo!("$start-at: ____ is not a number")
|
|
|
|
};
|
|
|
|
let mut end = match arg!(args, 2, "end-at"=Value::Null).eval() {
|
|
|
|
Value::Dimension(n, Unit::None) if n.to_integer().is_positive() => n.to_integer().to_usize().unwrap(),
|
|
|
|
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer()).to_usize().unwrap(),
|
|
|
|
Value::Dimension(..) => todo!("$end: Expected ___ to have no units."),
|
|
|
|
Value::Null => str_len,
|
|
|
|
_ => todo!("$end-at: ____ is not a number")
|
|
|
|
};
|
|
|
|
|
|
|
|
if end > str_len {
|
|
|
|
end = str_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if start > end || start > str_len {
|
|
|
|
match quotes {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => Some(Value::Ident(String::new(), QuoteKind::Double)),
|
|
|
|
QuoteKind::None => Some(Value::Null),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let s = string[start-1..end].to_string();
|
|
|
|
match quotes {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => Some(Value::Ident(s, QuoteKind::Double)),
|
|
|
|
QuoteKind::None => Some(Value::Ident(s, QuoteKind::None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2020-02-08 20:32:10 -05:00
|
|
|
}
|