grass/src/builtin/string.rs

363 lines
10 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;
2020-06-16 19:38:30 -04:00
use crate::parse::Parser;
use crate::unit::Unit;
use crate::value::{Number, Value};
2020-06-16 19:38:30 -04:00
fn to_upper_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 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()),
}
}
2020-06-16 19:38:30 -04:00
fn to_lower_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 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()),
}
}
2020-06-16 19:38:30 -04:00
fn str_length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 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()),
}
}
2020-06-16 19:38:30 -04:00
fn quote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 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()),
}
}
2020-06-16 19:38:30 -04:00
fn unquote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 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()),
}
}
2020-06-16 19:38:30 -04:00
fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(3)?;
2020-06-16 19:38:30 -04:00
let (string, quotes) = match parser.arg(&mut args, 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();
2020-06-16 19:38:30 -04:00
let start = match parser.arg(&mut args, 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())
}
};
2020-06-16 19:38:30 -04:00
let mut end = match parser.default_arg(&mut args, 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,
))
}
}
2020-06-16 19:38:30 -04:00
fn str_index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
2020-06-16 19:38:30 -04:00
let s1 = match parser.arg(&mut args, 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
2020-06-16 19:38:30 -04:00
let substr = match parser.arg(&mut args, 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,
})
}
2020-06-16 19:38:30 -04:00
fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(3)?;
2020-06-16 19:38:30 -04:00
let (s1, quotes) = match parser.arg(&mut args, 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
}
};
2020-06-16 19:38:30 -04:00
let substr = match parser.arg(&mut args, 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
2020-06-16 19:38:30 -04:00
let index = match parser.arg(&mut args, 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")]
2020-05-31 05:32:19 -04:00
#[allow(clippy::needless_pass_by_value)]
2020-06-16 19:38:30 -04:00
fn unique_id(args: CallArgs, _: &mut Parser<'_>) -> 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));
}