Add starts with operator (#5061)

* add starts_with operator

* added a test
This commit is contained in:
Darren Schroeder
2022-04-01 13:35:46 -05:00
committed by GitHub
parent a088081695
commit 2cb815b7b4
8 changed files with 68 additions and 0 deletions

View File

@ -34,6 +34,7 @@ impl Expression {
Operator::Plus | Operator::Minus => 90,
Operator::NotContains
| Operator::Contains
| Operator::StartsWith
| Operator::LessThan
| Operator::LessThanOrEqual
| Operator::GreaterThan

View File

@ -23,6 +23,7 @@ pub enum Operator {
And,
Or,
Pow,
StartsWith,
}
impl Display for Operator {
@ -46,6 +47,7 @@ impl Display for Operator {
Operator::Pow => write!(f, "**"),
Operator::LessThanOrEqual => write!(f, "<="),
Operator::GreaterThanOrEqual => write!(f, ">="),
Operator::StartsWith => write!(f, "=^"),
}
}
}

View File

@ -2031,6 +2031,27 @@ impl Value {
}
}
pub fn starts_with(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
let span = span(&[self.span()?, rhs.span()?]);
match (self, rhs) {
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
val: lhs.starts_with(rhs),
span,
}),
(Value::CustomValue { val: lhs, span }, rhs) => {
lhs.operation(*span, Operator::StartsWith, op, rhs)
}
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),
lhs_span: self.span()?,
rhs_ty: rhs.get_type(),
rhs_span: rhs.span()?,
}),
}
}
pub fn not_contains(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
let span = span(&[self.span()?, rhs.span()?]);