grass/src/builtin/string.rs

94 lines
4.1 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2020-02-02 21:09:29 -05:00
use num_bigint::BigInt;
use num_traits::cast::ToPrimitive;
use num_traits::sign::Signed;
use super::Builtin;
use crate::common::QuoteKind;
use crate::units::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
decl!(f "to-upper-case", |args, _| {
2020-02-16 18:18:57 -05:00
max_args!(args, 1);
match arg!(args, 0, "string") {
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_uppercase(), q)),
2020-02-16 18:32:13 -05:00
v => Err(format!("$string: {} is not a string.", v).into()),
}
});
decl!(f "to-lower-case", |args, _| {
2020-02-16 18:18:57 -05:00
max_args!(args, 1);
match arg!(args, 0, "string") {
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_lowercase(), q)),
2020-02-16 18:32:13 -05:00
v => Err(format!("$string: {} is not a string.", v).into()),
}
});
decl!(f "str-length", |args, _| {
2020-02-16 18:18:57 -05:00
max_args!(args, 1);
match arg!(args, 0, "string") {
Value::Ident(i, _) => Ok(Value::Dimension(Number::from(i.len()), Unit::None)),
2020-02-16 18:32:13 -05:00
v => Err(format!("$string: {} is not a string.", v).into()),
}
});
decl!(f "quote", |args, _| {
2020-02-16 18:18:57 -05:00
max_args!(args, 1);
match arg!(args, 0, "string") {
Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Double)),
2020-02-16 18:32:13 -05:00
v => Err(format!("$string: {} is not a string.", v).into()),
}
});
decl!(f "unquote", |args, _| {
2020-02-16 18:18:57 -05:00
max_args!(args, 1);
match arg!(args, 0, "string") {
2020-02-16 18:45:14 -05:00
Value::Ident(i, _) if i.is_empty() => Ok(Value::Null),
2020-02-16 18:32:13 -05:00
i @ Value::Ident(..) => Ok(i.unquote()),
v => Err(format!("$string: {} is not a string.", v).into()),
}
});
decl!(f "str-slice", |args, _| {
2020-02-16 18:18:57 -05:00
max_args!(args, 3);
let (string, quotes) = match arg!(args, 0, "string") {
Value::Ident(s, q) => (s, q),
2020-02-16 18:32:13 -05:00
v => return Err(format!("$string: {} is not a string.", v).into()),
};
let str_len = string.len();
let start = match arg!(args, 1, "start-at") {
Value::Dimension(n, Unit::None) if n.is_decimal() => return Err(format!("{} is not an int.", n).into()),
Value::Dimension(n, Unit::None) if n.to_integer().is_positive() => n.to_integer().to_usize().unwrap(),
Value::Dimension(n, Unit::None) if n == Number::from(0) => 1_usize,
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 1_usize,
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer()).to_usize().unwrap(),
2020-02-16 18:42:35 -05:00
v @ Value::Dimension(..) => return Err(format!("$start: Expected {} to have no units.", v).into()),
v => return Err(format!("$start-at: {} is not a number.", v).into()),
};
let mut end = match arg!(args, 2, "end-at"=Value::Null) {
Value::Dimension(n, Unit::None) if n.is_decimal() => return Err(format!("{} is not an int.", n).into()),
Value::Dimension(n, Unit::None) if n.to_integer().is_positive() => n.to_integer().to_usize().unwrap(),
2020-02-15 08:54:10 -05:00
Value::Dimension(n, Unit::None) if n == Number::from(0) => 0_usize,
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 0_usize,
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer()).to_usize().unwrap(),
2020-02-16 18:42:35 -05:00
v @ Value::Dimension(..) => return Err(format!("$end: Expected {} to have no units.", v).into()),
Value::Null => str_len,
2020-02-16 18:42:35 -05:00
v => return Err(format!("$end-at: {} is not a number.", v).into()),
};
if end > str_len {
end = str_len;
}
if start > end || start > str_len {
match quotes {
QuoteKind::Double | QuoteKind::Single => Ok(Value::Ident(String::new(), QuoteKind::Double)),
QuoteKind::None => Ok(Value::Null),
}
} else {
let s = string[start-1..end].to_string();
match quotes {
QuoteKind::Double | QuoteKind::Single => Ok(Value::Ident(s, QuoteKind::Double)),
QuoteKind::None => Ok(Value::Ident(s, QuoteKind::None)),
}
}
});
}