grass/src/builtin/string.rs

371 lines
11 KiB
Rust
Raw Normal View History

2020-05-18 11:50:40 -04:00
use super::{Builtin, GlobalFunctionMap};
2020-02-02 21:09:29 -05:00
use num_bigint::BigInt;
2020-03-22 22:28:54 -04:00
use num_traits::{Signed, ToPrimitive, Zero};
2020-04-05 18:20:58 -04:00
#[cfg(feature = "random")]
use rand::{distributions::Alphanumeric, thread_rng, Rng};
2020-04-30 18:41:33 -04:00
use crate::args::CallArgs;
use crate::common::QuoteKind;
2020-04-30 18:41:33 -04:00
use crate::error::SassResult;
use crate::scope::Scope;
use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
fn to_upper_case(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(mut i, q) => {
i.make_ascii_uppercase();
2020-05-22 14:35:41 -04:00
Ok(Value::String(i, q))
}
v => Err((
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
fn to_lower_case(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(mut i, q) => {
i.make_ascii_lowercase();
2020-05-22 14:35:41 -04:00
Ok(Value::String(i, q))
}
v => Err((
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
fn str_length(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(i, _) => Ok(Value::Dimension(
Number::from(i.chars().count()),
Unit::None,
)),
v => Err((
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
fn quote(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(i, _) => Ok(Value::String(i, QuoteKind::Quoted)),
v => Err((
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
fn unquote(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
i @ Value::String(..) => Ok(i.unquote()),
v => Err((
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
fn str_slice(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(3)?;
let (string, quotes) = match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(s, q) => (s, q),
v => {
return Err((
2020-04-30 18:41:33 -04:00
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
};
let str_len = string.chars().count();
let start = match arg!(args, scope, super_selector, 1, "start-at") {
Value::Dimension(n, Unit::None) if n.is_decimal() => {
return Err((format!("{} is not an int.", n), args.span()).into())
}
Value::Dimension(n, Unit::None) if n.is_positive() => {
n.to_integer().to_usize().unwrap_or(str_len + 1)
}
Value::Dimension(n, Unit::None) if n.is_zero() => 1_usize,
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 1_usize,
2020-05-20 22:40:21 -04:00
Value::Dimension(n, Unit::None) => (n.to_integer() + BigInt::from(str_len + 1))
.to_usize()
.unwrap(),
v @ Value::Dimension(..) => {
return Err((
2020-04-30 18:41:33 -04:00
format!(
"$start: Expected {} to have no units.",
2020-04-30 18:41:33 -04:00
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
v => {
return Err((
2020-04-30 18:41:33 -04:00
format!(
"$start-at: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
}
};
let mut end = match arg!(args, scope, super_selector, 2, "end-at" = Value::Null) {
Value::Dimension(n, Unit::None) if n.is_decimal() => {
return Err((format!("{} is not an int.", n), args.span()).into())
}
Value::Dimension(n, Unit::None) if n.is_positive() => {
n.to_integer().to_usize().unwrap_or(str_len + 1)
}
Value::Dimension(n, Unit::None) if n.is_zero() => 0_usize,
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 0_usize,
2020-05-20 22:40:21 -04:00
Value::Dimension(n, Unit::None) => (n.to_integer() + BigInt::from(str_len + 1))
.to_usize()
.unwrap_or(str_len + 1),
v @ Value::Dimension(..) => {
return Err((
format!(
"$end: Expected {} to have no units.",
2020-04-30 18:41:33 -04:00
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
Value::Null => str_len,
v => {
return Err((
format!(
"$end-at: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
}
};
if end > str_len {
end = str_len;
2020-04-30 18:41:33 -04:00
}
if start > end || start > str_len {
2020-05-22 14:35:41 -04:00
Ok(Value::String(String::new(), quotes))
} else {
2020-05-22 14:35:41 -04:00
Ok(Value::String(
string
.chars()
.skip(start - 1)
.take(end - start + 1)
.collect(),
quotes,
))
}
}
fn str_index(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let s1 = match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(i, _) => i,
v => {
return Err((
2020-04-30 18:41:33 -04:00
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
};
2020-04-30 18:41:33 -04:00
let substr = match arg!(args, scope, super_selector, 1, "substring") {
2020-05-22 14:35:41 -04:00
Value::String(i, _) => i,
v => {
return Err((
2020-04-30 18:41:33 -04:00
format!(
"$substring: {} is not a string.",
2020-04-30 18:41:33 -04:00
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
};
2020-04-30 18:41:33 -04:00
Ok(match s1.find(&substr) {
Some(v) => Value::Dimension(Number::from(v + 1), Unit::None),
None => Value::Null,
})
}
fn str_insert(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(3)?;
let (s1, quotes) = match arg!(args, scope, super_selector, 0, "string") {
2020-05-22 14:35:41 -04:00
Value::String(i, q) => (i, q),
v => {
return Err((
format!(
"$string: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
};
let substr = match arg!(args, scope, super_selector, 1, "insert") {
2020-05-22 14:35:41 -04:00
Value::String(i, _) => i,
v => {
return Err((
format!(
"$insert: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 18:41:33 -04:00
}
};
2020-03-22 15:58:32 -04:00
let index = match arg!(args, scope, super_selector, 2, "index") {
Value::Dimension(n, Unit::None) if n.is_decimal() => {
return Err((format!("$index: {} is not an int.", n), args.span()).into())
}
Value::Dimension(n, Unit::None) => n,
v @ Value::Dimension(..) => {
return Err((
format!(
"$index: Expected {} to have no units.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
}
v => {
return Err((
format!("$index: {} is not a number.", v.to_css_string(args.span())?),
args.span(),
)
.into())
}
};
2020-03-22 15:58:32 -04:00
if s1.is_empty() {
2020-05-22 14:35:41 -04:00
return Ok(Value::String(substr, quotes));
2020-04-30 18:41:33 -04:00
}
2020-03-22 16:14:45 -04:00
let len = s1.chars().count();
// Insert substring at char position, rather than byte position
let insert = |idx, s1: String, s2| {
s1.chars()
.enumerate()
.map(|(i, c)| {
if i + 1 == idx {
c.to_string() + s2
} else if idx == 0 && i == 0 {
s2.to_string() + &c.to_string()
} else {
c.to_string()
}
})
.collect::<String>()
};
2020-04-30 18:41:33 -04:00
let string = if index.is_positive() {
insert(
index
.to_integer()
.to_usize()
.unwrap_or(len + 1)
.min(len + 1)
- 1,
s1,
&substr,
)
} else if index.is_zero() {
insert(0, s1, &substr)
} else {
let idx = index.abs().to_integer().to_usize().unwrap_or(len + 1);
if idx > len {
2020-04-30 18:41:33 -04:00
insert(0, s1, &substr)
} else {
insert(len - idx + 1, s1, &substr)
}
};
2020-04-30 18:41:33 -04:00
2020-05-22 14:35:41 -04:00
Ok(Value::String(string, quotes))
}
2020-04-30 18:41:33 -04:00
#[cfg(feature = "random")]
fn unique_id(args: CallArgs, _: &Scope, _: &Selector) -> SassResult<Value> {
args.max_args(0)?;
let mut rng = thread_rng();
let string = std::iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.take(7)
.collect();
2020-05-22 14:35:41 -04:00
Ok(Value::String(string, QuoteKind::None))
}
2020-03-22 16:14:45 -04:00
2020-05-16 18:01:06 -04:00
pub(crate) fn declare(f: &mut GlobalFunctionMap) {
2020-04-30 18:41:33 -04:00
f.insert("to-upper-case", Builtin::new(to_upper_case));
f.insert("to-lower-case", Builtin::new(to_lower_case));
f.insert("str-length", Builtin::new(str_length));
f.insert("quote", Builtin::new(quote));
f.insert("unquote", Builtin::new(unquote));
f.insert("str-slice", Builtin::new(str_slice));
f.insert("str-index", Builtin::new(str_index));
f.insert("str-insert", Builtin::new(str_insert));
2020-04-05 18:20:58 -04:00
#[cfg(feature = "random")]
2020-04-30 18:41:33 -04:00
f.insert("unique-id", Builtin::new(unique_id));
}