forked from extern/nushell
Add starts with operator (#5061)
* add starts_with operator * added a test
This commit is contained in:
@ -34,6 +34,7 @@ impl Expression {
|
||||
Operator::Plus | Operator::Minus => 90,
|
||||
Operator::NotContains
|
||||
| Operator::Contains
|
||||
| Operator::StartsWith
|
||||
| Operator::LessThan
|
||||
| Operator::LessThanOrEqual
|
||||
| Operator::GreaterThan
|
||||
|
@ -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, "=^"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()?]);
|
||||
|
||||
|
Reference in New Issue
Block a user